Cui Zhi Feng
2024-09-08 bc9bd69850c95dacb22c7da4777a1d0470c33722
src/main/java/com/mzl/flower/service/payment/OrderService.java
@@ -40,9 +40,11 @@
import com.mzl.flower.mapper.system.UserWechatMapper;
import com.mzl.flower.mapper.warehouse.WarehouseLocationMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.coupon.CouponRecordService;
import com.mzl.flower.service.flower.FlowerService;
import com.mzl.flower.service.menber.MemberGrowthRecordService;
import com.mzl.flower.service.point.CustomerPointService;
import com.mzl.flower.service.point.PointGoodsService;
import com.mzl.flower.service.system.CodeService;
import com.mzl.flower.service.system.WeChatService;
import com.mzl.flower.service.transport.TransportService;
@@ -147,47 +149,77 @@
    @Autowired
    private CustomerPointService customerPointService;
    @Autowired
    private PointGoodsService pointGoodsService;
    @Autowired
    private RedisLockService lockService;
    @Autowired
    private CouponRecordService couponRecordService;
    public void changeFlower2Cart(CartSaveDTO dto) {
        String userId = SecurityUtils.getUserId();
        Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
                .eq("create_by", userId).eq("flower_id", dto.getId()));
        if (c == null) {
            if (dto.getNum() <= 0) {
                throw new ValidationException("数量不能小于等于0,请刷新页面重试");
            }
            c = new Cart();
            c.setFlowerId(dto.getId());
            c.setNum(dto.getNum());
            c.create(userId);
        String key = userId + "_" + dto.getId();
        boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key);
        if(!lock){
            return;
        }
            cartMapper.insert(c);
        } else {
            c.setNum(c.getNum() + dto.getNum());
            c.update(userId);
            if (c.getNum() <= 0) {
                cartMapper.deleteById(c.getId());
        try {
            Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
                    .eq("create_by", userId).eq("flower_id", dto.getId()));
            if (c == null) {
                if (dto.getNum() <= 0) {
                    throw new ValidationException("数量不能小于等于0,请刷新页面重试");
                }
                c = new Cart();
                c.setFlowerId(dto.getId());
                c.setNum(dto.getNum());
                c.create(userId);
                cartMapper.insert(c);
            } else {
                cartMapper.updateById(c);
                c.setNum(c.getNum() + dto.getNum());
                c.update(userId);
                if (c.getNum() <= 0) {
                    cartMapper.deleteById(c.getId());
                } else {
                    cartMapper.updateById(c);
                }
            }
        } finally {
            lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key);
        }
    }
    public void saveFlower2Cart(CartSaveDTO dto) {
        String userId = SecurityUtils.getUserId();
        Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
                .eq("create_by", userId).eq("flower_id", dto.getId()));
        if (c == null) {
            c = new Cart();
            c.setFlowerId(dto.getId());
            c.setNum(dto.getNum());
            c.create(userId);
            cartMapper.insert(c);
        } else {
            c.setNum(dto.getNum());
            c.update(userId);
        String key = userId + "_" + dto.getId();
        boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key);
        if(!lock){
            return;
        }
            cartMapper.updateById(c);
        try {
            Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
                    .eq("create_by", userId).eq("flower_id", dto.getId()));
            if (c == null) {
                c = new Cart();
                c.setFlowerId(dto.getId());
                c.setNum(dto.getNum());
                c.create(userId);
                cartMapper.insert(c);
            } else {
                c.setNum(dto.getNum());
                c.update(userId);
                cartMapper.updateById(c);
            }
        } finally {
            lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key);
        }
    }
@@ -503,7 +535,12 @@
    }
    private void usePointGoods(String orderId, List<Long> goodsRecordIdList){
        //TODO 使用积分商品兑换券
        //使用积分商品兑换券
        if(goodsRecordIdList != null && goodsRecordIdList.size() > 0){
            for(Long grId : goodsRecordIdList){
                pointGoodsService.useExchangeGoods(grId, orderId);
            }
        }
    }
    /**
@@ -527,7 +564,9 @@
    private CouponRecordDO useCouponRecord(String orderId, BigDecimal amount, String couponRecordId){
        if(StringUtils.isNotEmpty(couponRecordId)){
            //TODO 优惠券使用和验证,如果不符合使用条件需要抛出异常。需调用优惠券使用方法
            //优惠券使用和验证,如果不符合使用条件需要抛出异常。需调用优惠券使用方法
            couponRecordService.useCoupon(couponRecordId, orderId, amount);
            return couponRecordService.getCouponRecordById(couponRecordId);
        }
        return null;
@@ -839,6 +878,74 @@
        return ls;
    }
    public List<OrderCheckLocationListDTO> selectOrderCheckLocationList(OrderQueryDTO dto) {
        dto.setIdList(splitParam(dto.getIds()));
        dto.setStartDate(parseLocalDateTime(dto.getStartDateStr(), true));
        dto.setEndDate(parseLocalDateTime(dto.getEndDateStr(), false));
        dto.setCreateStartDate(parseLocalDateTime(dto.getCreateStartDateStr(), 17, 0, 0, -1));
        dto.setCreateEndDate(parseLocalDateTime(dto.getCreateEndDateStr(), 17, 0, 0, 0));
        List<OrderCheckListDTO> ls = orderMapper.selectOrderCheckList(dto);
        List<OrderCheckLocationListDTO> result = new ArrayList<>();
        if (ls != null && ls.size() > 0) {
            List<String> orderIds = new ArrayList<>();
            Map<Long, OrderCheckLocationListDTO> rMap = new HashMap<>();
            Map<String, OrderCheckListDTO> orderMap = new HashMap<>();
            for (OrderCheckListDTO c : ls) {
                orderIds.add(c.getId());
                orderMap.put(c.getId(), c);
                Long locationId = c.getWarehouseLocationId();
                OrderCheckLocationListDTO llc = rMap.get(locationId);
                if(llc == null){
                    llc = new OrderCheckLocationListDTO();
                    BeanUtils.copyProperties(c, llc);
                    rMap.put(locationId, llc);
                    result.add(llc);
                }
            }
            Map<Long, String> stationMap = prepareStationMap();
            List<OrderItem> itemList = orderItemMapper.selectList(new QueryWrapper<OrderItem>()
                    .in("order_id", orderIds));
            Map<String, List<OrderItemLocationListDTO>> map = new HashMap<>();
            for (OrderItem oi : itemList) {
                String orderId = oi.getOrderId();
                List<OrderItemLocationListDTO> ll = map.computeIfAbsent(orderId, k -> new ArrayList<>());
                OrderItemLocationListDTO d = new OrderItemLocationListDTO();
                BeanUtils.copyProperties(oi, d);
                d.setStationName(stationMap.get(oi.getStationId()));
                OrderCheckListDTO c = orderMap.get(oi.getOrderId());
                d.setOrderNo(c.getOrderNo());
                d.setOrderId(c.getId());
                ll.add(d);
            }
            for (OrderCheckListDTO c : ls) {
                String orderId = c.getId();
                Long locationId = c.getWarehouseLocationId();
                OrderCheckLocationListDTO r = rMap.get(locationId);
                List<OrderItemLocationListDTO> items = r.getItems();
                if(items == null){
                    items = new ArrayList<>();
                    r.setItems(items);
                }
                items.addAll(map.get(orderId));
            }
        }
        return result;
    }
    public Page<OrderListDTO> selectPartnerOrderList(Page page, OrderQueryDTO dto) {
        Partner p = getCurrentPartner();
        dto.setPartnerId(p.getId());
@@ -949,6 +1056,9 @@
        if(checkList != null && checkList.size() > 0){
            for(OrderItemCheck c : checkList){
                if(Constants.CHECK_TYPE.replace.name().equals(c.getType())){
                    continue;
                }
                totalAmount = totalAmount.subtract(getAmount(c.getDeductAmount()));
            }
        }