| | |
| | | package com.oying.modules.sh.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.oying.exception.BadRequestException; |
| | | import com.oying.modules.hwc.service.SwiftPassService; |
| | | import com.oying.modules.pc.product.domain.Product; |
| | | import com.oying.modules.pc.product.domain.enums.ProductStatusEnum; |
| | | import com.oying.modules.pc.product.service.ProductService; |
| | | import com.oying.modules.sh.domain.Order; |
| | | import com.oying.modules.sh.domain.OrderProductSnapshot; |
| | | import com.oying.modules.sh.domain.request.GeneratorOrder; |
| | | import com.oying.modules.sh.domain.request.ProductOrder; |
| | | import com.oying.modules.sh.domain.request.SubmitOrder; |
| | | import com.oying.modules.sh.domain.vo.OrderInfo; |
| | | import com.oying.modules.sh.domain.vo.ProductInfo; |
| | | import com.oying.modules.sh.service.OrderProductSnapshotService; |
| | | import com.oying.modules.sh.service.UserAddressService; |
| | | import com.oying.utils.FileUtil; |
| | | import com.oying.utils.*; |
| | | import com.oying.utils.enums.GenerateEnum; |
| | | import com.oying.utils.enums.PayStateEnum; |
| | | import com.oying.utils.enums.PayTypeEnum; |
| | | import lombok.RequiredArgsConstructor; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | |
| | | import com.oying.modules.sh.mapper.OrderMapper; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.oying.utils.PageUtil; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import com.oying.utils.PageResult; |
| | | |
| | | /** |
| | | * @author lixin |
| | |
| | | private final OrderMapper orderMapper; |
| | | private final UserAddressService userAddressService; |
| | | private final ProductService productService; |
| | | private final SwiftPassService swiftPassService; |
| | | private final OrderProductSnapshotService productSnapshotService; |
| | | private final RedisUtils redisUtils; |
| | | |
| | | @Override |
| | | public PageResult<Order> queryAll(OrderQueryCriteria criteria, Page<Object> page) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Order submitOrder(SubmitOrder submit) { |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public synchronized Order submitOrder(SubmitOrder submit, HttpServletRequest request) { |
| | | if (!submit.getPayType().equals(PayTypeEnum.HWC)) { |
| | | throw new BadRequestException("支付类型暂未开放"); |
| | | } |
| | | String orderNum = redisUtils.generateOrderSn(GenerateEnum.ORDER.getKey()); |
| | | // 总金额 |
| | | BigDecimal amount = BigDecimal.ZERO; |
| | | GeneratorOrder generator = new GeneratorOrder(); |
| | | generator.setProducts(submit.getProducts()); |
| | | generator.setStoreId(submit.getStoreId()); |
| | | OrderInfo info = generatorOrder(generator); |
| | | // 商品快照 |
| | | List<OrderProductSnapshot> snapshots = new ArrayList<>(); |
| | | |
| | | for (ProductInfo productInfo : info.getProducts()) { |
| | | Product product = productInfo.getProduct(); |
| | | OrderProductSnapshot snapshot = new OrderProductSnapshot(); |
| | | snapshot.setOrderNum(orderNum); |
| | | snapshot.setStoreId(submit.getStoreId()); |
| | | snapshot.setProductId(product.getProductId()); |
| | | snapshot.setProductBarcode(product.getBarcode()); |
| | | snapshot.setProductName(product.getName()); |
| | | snapshot.setProductTitle(product.getTitle()); |
| | | snapshot.setProductMainImage(product.getMainImageUrl()); |
| | | snapshot.setParamData(JSONObject.toJSONString(productInfo.getProductLabels())); |
| | | snapshot.setUnitPrice(product.getPrice()); |
| | | snapshot.setDetailCount(productInfo.getCount()); |
| | | snapshot.setOriginalPrice(product.getPrice()); |
| | | snapshot.setPaidPrice(product.getPrice()); |
| | | BigDecimal decimal = BigDecimalUtils.multiply(product.getPrice(), productInfo.getCount()); |
| | | amount = BigDecimalUtils.add(amount, decimal); |
| | | snapshot.setActuallyPayPrice(decimal); |
| | | snapshot.setPayState(PayStateEnum.NOTPAY.getKey()); |
| | | snapshots.add(snapshot); |
| | | } |
| | | // 下单后30分钟失效 |
| | | Timestamp time = DateUtil.addMinute(DateUtil.getTimeStamp(), 30); |
| | | String expire = DateUtil.localDateTimeFormat(time.toLocalDateTime(), DateUtil.SDF_YMDHMS); |
| | | swiftPassService.pay(StringUtils.getIp(request), BigDecimalUtils.yuanToCents(amount).toString(), expire,SecurityUtils.getCurrentUserId()); |
| | | productSnapshotService.saveBatch(snapshots); |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public OrderInfo generatorOrder(GeneratorOrder criteria) { |
| | | public synchronized OrderInfo generatorOrder(GeneratorOrder criteria) { |
| | | List<ProductInfo> products = new ArrayList<>(); |
| | | BigDecimal amount = BigDecimal.ZERO; |
| | | for (ProductOrder productOrder : criteria.getProducts()) { |
| | |
| | | ProductStatusEnum statusEnum = ProductStatusEnum.get(product.getStatus()); |
| | | throw new BadRequestException(product.getName() + ":" + (statusEnum != null ? statusEnum.getReasonPhrase() : "状态")); |
| | | } |
| | | if (productOrder.getProductCount() > product.getStockQuantity()) { |
| | | if (productOrder.getCount() > product.getStockQuantity()) { |
| | | throw new BadRequestException("商品库存不足"); |
| | | } |
| | | if (productOrder.getProductCount() < product.getMinPurchaseQuantity()) { |
| | | if (productOrder.getCount() < product.getMinPurchaseQuantity()) { |
| | | throw new BadRequestException("起售数量不足"); |
| | | } |
| | | ProductInfo info = new ProductInfo(); |
| | | info.setProduct(product); |
| | | info.setProductLabels(product.getLabels()); |
| | | info.setCount(productOrder.getProductCount()); |
| | | info.setCount(productOrder.getCount()); |
| | | products.add(info); |
| | | amount = amount.add(product.getPrice().multiply(BigDecimal.valueOf(productOrder.getProductCount()))); |
| | | amount = BigDecimalUtils.add(amount, BigDecimalUtils.multiply(product.getPrice(), productOrder.getCount())); |
| | | } |
| | | OrderInfo info = new OrderInfo(); |
| | | info.setProducts(products); |