diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/MemberInDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/MemberInDTO.java index 740de97..3ac953a 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/MemberInDTO.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/MemberInDTO.java @@ -20,7 +20,7 @@ public class MemberInDTO { private BigDecimal amount; private Integer orderId; // 使用的优惠券 - private List userCouponIds = new ArrayList<>(); + private Integer userCouponId; // 是否使用积分抵扣 private Integer pointsNum ; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index d97bfbe..d9d8c0d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -1369,19 +1369,7 @@ public class CartService { private void returnCoupon(TbOrderInfo orderInfo) { // 返还优惠券 - if (StrUtil.isNotBlank(orderInfo.getCouponInfoList())) { - OrderCouponInfoDTO couponInfoDTO = JSONObject.parseObject(orderInfo.getCouponInfoList(), OrderCouponInfoDTO.class); - // 券返还 - if (!couponInfoDTO.getOutRecordList().isEmpty()) { - couponInfoDTO.getOutRecordList().forEach(item -> { - item.setRefNum(item.getUseNum()); - }); - shopCouponService.refund(couponInfoDTO.getOutRecordList()); - couponInfoDTO.setOutRecordList(new ArrayList<>()); - orderInfo.setCouponInfoList(JSONObject.toJSONString(couponInfoDTO)); - } - } - + PayService.returnCoupon(orderInfo, shopCouponService); } private void consumeCoupon(List outRecordList, Integer memberId, TbOrderInfo orderInfo) { diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java index 15a5ce2..de45ae3 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -236,6 +236,7 @@ public class PayService { /** * 校验订单支付状态是否正常 + * * @param orderInfo 订单信息 */ private void checkOrderPayState(TbOrderInfo orderInfo) { @@ -255,8 +256,9 @@ public class PayService { /** * 保存订单支付详情 + * * @param orderInfo 订单信息 - * @param payType 支付类型 + * @param payType 支付类型 * @return 订单支付 */ private TbOrderPayment createOrderPayment(TbOrderInfo orderInfo, String payType) { @@ -338,7 +340,7 @@ public class PayService { String smallAppid = thirdApply.getSmallAppid(); String appId = thirdApply.getAppId(); String appToken = thirdApply.getAppToken(); - if ("aliPay".equals(orderInfo.getPayType())){ + if ("aliPay".equals(orderInfo.getPayType())) { smallAppid = thirdApply.getAlipaySmallAppid(); } String convertPayType = "aliPay".equals(orderInfo.getPayType()) ? "ALIPAY" : "WECHAT"; @@ -402,7 +404,7 @@ public class PayService { } // 校验优惠券积分同享 - if (!payDTO.getUserCouponIds().isEmpty() && freeDineConfig.getWithCoupon() == 0) { + if (payDTO.getUserCouponId() != null && freeDineConfig.getWithCoupon() == 0) { throw new MsgException("当前店铺未开启与优惠券同享"); } @@ -414,6 +416,10 @@ public class PayService { if (!freeDineConfig.getUseType().contains(orderInfo.getEatModel())) { throw new MsgException("当前店铺未开启此就餐模式霸王餐"); } + + // 返还优惠券 + returnCoupon(orderInfo, shopCouponService); + BigDecimal shouldPayAmount = orderInfo.getOriginAmount().multiply(BigDecimal.valueOf(freeDineConfig.getRechargeTimes())); @@ -442,36 +448,32 @@ public class PayService { record.setState(TableConstant.FreeDineRecord.State.WAIT_PAY.getValue()); record.setOrderId(orderInfo.getId()); record.setOrderAmount(orderInfo.getOrderAmount()); - if (!payDTO.getUserCouponIds().isEmpty()) { - List userCouponList = shopCouponService.getActivateCouponByAmount(Integer.valueOf(orderInfo.getShopId()), String.valueOf(payDTO.getUserId()), shouldPayAmount); - if (userCouponList.isEmpty()) { - throw new MsgException("存在不可用优惠券"); - } + if (payDTO.getUserCouponId() != null) { + List userCouponList = shopCouponService.getActivateCouponByAmount(Integer.valueOf(orderInfo.getShopId()), String.valueOf(payDTO.getUserId()), shouldPayAmount); + if (userCouponList.isEmpty()) { + throw new MsgException("存在不可用优惠券"); + } - ArrayList activateInInfoVOS = new ArrayList<>(); - for (Integer userCouponId : payDTO.getUserCouponIds()) { - TbUserCouponVo userCouponVo = userCouponList.stream().filter(item -> item.getId().equals(userCouponId)).findFirst().orElse(null); - if (userCouponVo == null) { - throw new MsgException("存在不可用优惠券"); - } + TbUserCouponVo userCouponVo = userCouponList.stream().filter(item -> item.getId().equals(payDTO.getUserCouponId())).findFirst().orElse(null); + if (userCouponVo == null) { + throw new MsgException("存在不可用优惠券"); + } - shouldPayAmount = shouldPayAmount.subtract(userCouponVo.getDiscountAmount()); - ActivateInInfoVO activateInInfoVO = new ActivateInInfoVO() - .setId(userCouponVo.getId()) - .setCouponId(userCouponVo.getCouponId()) - .setNum(1); - activateInInfoVOS.add(activateInInfoVO); - } + shouldPayAmount = shouldPayAmount.subtract(userCouponVo.getDiscountAmount()); - List tbShopCoupons = mpShopCouponMapper.selectBatchIds(activateInInfoVOS.stream().map(ActivateInInfoVO::getCouponId).collect(Collectors.toList())); - if (tbShopCoupons.size() != payDTO.getUserCouponIds().size()) { - throw new MsgException("存在不可用优惠券"); - } + TbShopCoupon tbShopCoupons = mpShopCouponMapper.selectById(userCouponVo.getCouponId()); + if (tbShopCoupons == null) { + throw new MsgException("存在不可用优惠券"); + } - // 设置优惠券信息 -// orderInfo.seta(JSONObject.toJSONString(activateInInfoVOS)); - orderInfo.setUserCouponAmount(BigDecimal.valueOf(tbShopCoupons.stream().map(TbShopCoupon::getDiscountAmount).reduce(0, Integer::sum))); - record.setCouponInfo(JSONObject.toJSONString(userCouponList)); + // 设置优惠券信息 + OrderCouponInfoDTO couponInfoDTO = new OrderCouponInfoDTO(); + couponInfoDTO.setFullReductionCouponMap(new HashMap(){{ + put(userCouponVo.getId(), userCouponVo); + }}); + orderInfo.setCouponInfoList(JSONObject.toJSONString(couponInfoDTO)); + orderInfo.setUserCouponAmount(userCouponVo.getDiscountAmount()); + record.setCouponInfo(JSONObject.toJSONString(userCouponList)); } record.setRechargeAmount(shouldPayAmount); @@ -485,6 +487,21 @@ public class PayService { return shouldPayAmount; } + static void returnCoupon(TbOrderInfo orderInfo, TbShopCouponService shopCouponService) { + if (StrUtil.isNotBlank(orderInfo.getCouponInfoList())) { + OrderCouponInfoDTO couponInfoDTO = JSONObject.parseObject(orderInfo.getCouponInfoList(), OrderCouponInfoDTO.class); + // 券返还 + if (!couponInfoDTO.getOutRecordList().isEmpty()) { + couponInfoDTO.getOutRecordList().forEach(item -> { + item.setRefNum(item.getUseNum()); + }); + shopCouponService.refund(couponInfoDTO.getOutRecordList()); + couponInfoDTO.setOutRecordList(new ArrayList<>()); + orderInfo.setCouponInfoList(JSONObject.toJSONString(couponInfoDTO)); + } + } + } + @Transactional(rollbackFor = Exception.class) public Result payOrder(String openId, OrderPayDTO payDTO, String ip) throws Exception { @@ -660,7 +677,7 @@ public class PayService { TbShopTable shopTable = mpShopTableService.selectByQrcode(orderInfo.getTableId()); if (shopTable.getAutoClear() != null && shopTable.getAutoClear() == 1) { mpShopTableService.updateStateByQrcode(orderInfo.getTableId(), TableConstant.ShopTable.State.IDLE); - }else { + } else { mpShopTableService.updateStateByQrcode(orderInfo.getTableId(), TableConstant.ShopTable.State.CLEANING); } } @@ -834,7 +851,7 @@ public class PayService { String smallAppid = thirdApply.getSmallAppid(); String appId = thirdApply.getAppId(); String appToken = thirdApply.getAppToken(); - if ("aliPay".equals(payType)){ + if ("aliPay".equals(payType)) { smallAppid = thirdApply.getAlipaySmallAppid(); } String convertPayType = "aliPay".equals(payType) ? "ALIPAY" : "WECHAT"; @@ -1045,7 +1062,7 @@ public class PayService { @Transactional(rollbackFor = Exception.class) - public Result memberIn(MemberInDTO memberInDTO, String ip) throws JsonProcessingException { + public Result memberIn(MemberInDTO memberInDTO, String ip) throws JsonProcessingException { TbUserInfo userInfo = tbUserInfoMapper.selectByPrimaryKey(memberInDTO.getUserId()); if (ObjectUtil.isEmpty(userInfo)) { @@ -1074,7 +1091,7 @@ public class PayService { payAmount = getFreeDineOrderInfo(memberInDTO, userInfo); memberIn.setType(TableConstant.MemberIn.Type.FREE_DINE.getValue()); // 会员充值 - }else { + } else { payAmount = memberInDTO.getAmount().setScale(2, RoundingMode.DOWN); memberIn.setType(TableConstant.MemberIn.Type.NORMAL.getValue()); } @@ -1217,8 +1234,9 @@ public class PayService { /** * 订单支付成功修改订单和子单状态 - * @param orderInfo 订单信息 - * @param payType 支付平台 + * + * @param orderInfo 订单信息 + * @param payType 支付平台 * @param payOrderNO 三方支付订单号 */ private void orderSuccessPay(TbOrderInfo orderInfo, String payType, String payOrderNO) { @@ -1237,9 +1255,9 @@ public class PayService { //修改主单状态 orderInfo.setStatus("closed"); - if("alipay".equalsIgnoreCase(payType)){ + if ("alipay".equalsIgnoreCase(payType)) { orderInfo.setPayType("ali_lite"); - }else if("wechat".equalsIgnoreCase(payType)){ + } else if ("wechat".equalsIgnoreCase(payType)) { orderInfo.setPayType("wx_lite"); } orderInfo.setPayOrderNo(payOrderNO); @@ -1289,14 +1307,14 @@ public class PayService { TbShopTable shopTable = mpShopTableService.selectByQrcode(orderInfo.getTableId()); if (shopTable.getAutoClear() != null && shopTable.getAutoClear() == 1) { mpShopTableService.updateStateByQrcode(orderInfo.getTableId(), TableConstant.ShopTable.State.IDLE); - }else { + } else { mpShopTableService.updateStateByQrcode(orderInfo.getTableId(), TableConstant.ShopTable.State.CLEANING); } } } @Transactional(rollbackFor = Exception.class) - public String callBackPayFST(String payOrderNO,String payType) { + public String callBackPayFST(String payOrderNO, String payType) { TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPayOrderNo(payOrderNO); if (ObjectUtil.isEmpty(orderInfo)) { return "订单信息不存在"; @@ -1346,14 +1364,14 @@ public class PayService { for (TbActivate tbActivate : list) { if (tbActivate.getIsUseCoupon() == 1) { TbShopCoupon coupon = couponMapper.queryById(tbActivate.getCouponId()); - if(coupon!=null){ - if(coupon.getType()==1){ + if (coupon != null) { + if (coupon.getType() == 1) { //满减 - tbActivate.setCouponDesc("满"+coupon.getFullAmount() + "减" + coupon.getDiscountAmount()+" * "+tbActivate.getNum()+"张"); + tbActivate.setCouponDesc("满" + coupon.getFullAmount() + "减" + coupon.getDiscountAmount() + " * " + tbActivate.getNum() + "张"); } else if (coupon.getType() == 2) { //商品 tbActivate.setCouponDesc("商品券"); - tbActivate.setGives(couProductMapper.queryProsByActivateId(coupon.getId(),tbActivate.getNum())); + tbActivate.setGives(couProductMapper.queryProsByActivateId(coupon.getId(), tbActivate.getNum())); } } } @@ -1476,7 +1494,7 @@ public class PayService { Date end = new Date(); if ("fixed".equals(tbShopCoupon.getValidityType())) { //固定时间 - end = DateUtil.offsetDay(new Date(),tbShopCoupon.getValidDays()); + end = DateUtil.offsetDay(new Date(), tbShopCoupon.getValidDays()); } else if ("custom".equals(tbShopCoupon.getValidityType())) { //自定义时间 start = tbShopCoupon.getValidStartTime(); @@ -1484,7 +1502,7 @@ public class PayService { } if (tbShopCoupon != null) { List actGiveRecords = new ArrayList<>(); - if(tbShopCoupon.getType() == 1) { + if (tbShopCoupon.getType() == 1) { //满减 TbActivateInRecord record = new TbActivateInRecord(); record.setVipUserId(Integer.valueOf(tbShopUser.getId())); @@ -1500,7 +1518,7 @@ public class PayService { record.setSourceFlowId(flowId); record.setUseStartTime(start); record.setUseEndTime(end); - record.setCouponJson(activateInRecordService.getCouponJson(activate,tbShopCoupon,null)); + record.setCouponJson(activateInRecordService.getCouponJson(activate, tbShopCoupon, null)); actGiveRecords.add(record); } else if (tbShopCoupon.getType() == 2) { //商品卷 @@ -1512,19 +1530,19 @@ public class PayService { record.setName("商品卷"); record.setType(2); record.setProId(actPro.getProductId()); - record.setNum(actPro.getNum()*tbShopCoupon.getNumber()); - record.setOverNum(actPro.getNum()*tbShopCoupon.getNumber()); + record.setNum(actPro.getNum() * tbShopCoupon.getNumber()); + record.setOverNum(actPro.getNum() * tbShopCoupon.getNumber()); record.setShopId(Integer.valueOf(tbShopUser.getShopId())); record.setSourceActId(activate.getId()); record.setSourceFlowId(flowId); record.setUseStartTime(start); record.setUseEndTime(end); - record.setCouponJson(activateInRecordService.getCouponJson(activate,tbShopCoupon,actPro)); + record.setCouponJson(activateInRecordService.getCouponJson(activate, tbShopCoupon, actPro)); actGiveRecords.add(record); } } activateInRecordMapper.insertBatch(actGiveRecords); - tbShopCoupon.setLeftNumber(tbShopCoupon.getLeftNumber()-activate.getNum()); + tbShopCoupon.setLeftNumber(tbShopCoupon.getLeftNumber() - activate.getNum()); couponMapper.update(tbShopCoupon); } } @@ -1555,8 +1573,9 @@ public class PayService { /** * 检查充值回调是否是霸王餐活动 - * @param memberIn 充值记录 - * @param payType 支付平台 + * + * @param memberIn 充值记录 + * @param payType 支付平台 * @param payOrderNo 三方交易订单号 */ private void checkFreeDineForMemberInCallBack(TbMemberIn memberIn, String payType, String payOrderNo) { @@ -1573,23 +1592,22 @@ public class PayService { Integer shopId = Integer.valueOf(orderInfo.getShopId()); // 销毁使用的优惠券 -// if (StrUtil.isNotBlank(orderInfo.getActivateInInfoList())) { -// ArrayList activateOutRecords = new ArrayList<>(); -// JSONArray activateInfoList = JSONObject.parseArray(orderInfo.getActivateInInfoList()); -// activateInfoList.forEach(item -> { -// ActivateInInfoVO infoVO = ((JSONObject)item).toJavaObject(ActivateInInfoVO.class); -// TbActivateOutRecord tbActivateOutRecord = new TbActivateOutRecord(); -// tbActivateOutRecord.setShopId(shopId); -// tbActivateOutRecord.setGiveId(infoVO.getId()); -// tbActivateOutRecord.setVipUserId(Integer.valueOf(orderInfo.getMemberId())); -// tbActivateOutRecord.setType(TableConstant.ActivateOutRecord.Type.FULL_REDUCTION.getValue()); -// tbActivateOutRecord.setUseNum(infoVO.getNum()); -// tbActivateOutRecord.setStatus(TableConstant.ActivateOutRecord.Status.CLOSED.getValue()); -// tbActivateOutRecord.setCreateTime(DateUtil.date()); -// activateOutRecords.add(tbActivateOutRecord); -// }); -// shopCouponService.use(shopId, orderInfo.getId(), Integer.valueOf(orderInfo.getUserId()), activateOutRecords); -// } + if (StrUtil.isNotBlank(orderInfo.getCouponInfoList())) { + ArrayList activateOutRecords = new ArrayList<>(); + OrderCouponInfoDTO couponInfoDTO = JSONObject.parseObject(orderInfo.getCouponInfoList(), OrderCouponInfoDTO.class); + couponInfoDTO.getFullReductionCouponMap().values().forEach(item -> { + TbActivateOutRecord tbActivateOutRecord = new TbActivateOutRecord(); + tbActivateOutRecord.setShopId(shopId); + tbActivateOutRecord.setGiveId(item.getId()); + tbActivateOutRecord.setVipUserId(Integer.valueOf(orderInfo.getMemberId())); + tbActivateOutRecord.setType(TableConstant.ActivateOutRecord.Type.FULL_REDUCTION.getValue()); + tbActivateOutRecord.setUseNum(item.getCurrentUseNum()); + tbActivateOutRecord.setStatus(TableConstant.ActivateOutRecord.Status.CLOSED.getValue()); + tbActivateOutRecord.setCreateTime(DateUtil.date()); + activateOutRecords.add(tbActivateOutRecord); + }); + shopCouponService.use(shopId, orderInfo.getId(), Integer.valueOf(orderInfo.getUserId()), activateOutRecords); + } // 更改订单状态 orderSuccessPay(orderInfo, payType, payOrderNo);