diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java index 87c7fa0..dbd9c04 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java @@ -15,6 +15,7 @@ import com.chaozhanggui.system.cashierservice.dao.TbShopInfoMapper; import com.chaozhanggui.system.cashierservice.entity.TbOrderDetail; import com.chaozhanggui.system.cashierservice.entity.TbOrderInfo; import com.chaozhanggui.system.cashierservice.entity.TbShopInfo; +import com.chaozhanggui.system.cashierservice.entity.dto.CreditDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ReturnGroupOrderDto; import com.chaozhanggui.system.cashierservice.entity.dto.ReturnOrderDTO; import com.chaozhanggui.system.cashierservice.entity.dto.VipPayDTO; @@ -25,6 +26,7 @@ import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.util.IpUtil; import com.chaozhanggui.system.cashierservice.util.JSONUtil; +import com.chaozhanggui.system.cashierservice.util.Utils; import com.chaozhanggui.system.cashierservice.util.WechatUtil; import com.fasterxml.jackson.core.JsonProcessingException; import lombok.extern.slf4j.Slf4j; @@ -214,12 +216,20 @@ public class PayController { } if ((vipPayDTO.getPayAmount() != null && vipPayDTO.getPayAmount().compareTo(BigDecimal.ZERO) <= 0) || - (vipPayDTO.getDiscountAmount() != null && vipPayDTO.getDiscountAmount().compareTo(BigDecimal.ZERO) <= 0)) { + (vipPayDTO.getDiscountAmount() != null && vipPayDTO.getDiscountAmount().compareTo(BigDecimal.ZERO) <= 0)) { return Result.fail("折扣金额必须大于0"); } return payService.vipPay(vipPayDTO.getOrderId(), token, vipPayDTO.getVipUserId(), vipPayDTO.getPayAmount(), vipPayDTO.getDiscountAmount()); } + /** + * 挂账支付 + */ + @PostMapping("creditPay") + public Result creditPay(@RequestHeader("token") String token, @RequestBody CreditDTO creditDTO) { + return payService.creditPay(creditDTO, token); + } + /** * 银行卡支付 * @@ -315,11 +325,11 @@ public class PayController { @GetMapping("/noToken/queryOrderInfo") public Result noTokenQueryOrderInfo(String orderId) { - if(StrUtil.isBlank(orderId)){ + if (StrUtil.isBlank(orderId)) { return Result.fail("订单id不能为空"); } TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(Integer.valueOf(orderId)); - if(orderInfo == null){ + if (orderInfo == null) { return Result.fail("订单不存在"); } Map data = new HashMap<>(4); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbOrderInfo.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbOrderInfo.java index ffbf016..5a5bfa6 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbOrderInfo.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbOrderInfo.java @@ -122,6 +122,7 @@ public class TbOrderInfo implements Serializable { private BigDecimal pointsDiscountAmount; private String refundRemark; private Integer pointsNum; + private String creditBuyerId; public TbOrderInfo(){ super(); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/CreditDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/CreditDTO.java new file mode 100644 index 0000000..4c479e8 --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/CreditDTO.java @@ -0,0 +1,22 @@ +package com.chaozhanggui.system.cashierservice.entity.dto; + +import lombok.Data; + +import javax.validation.constraints.DecimalMin; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.math.BigDecimal; + +@Data +public class CreditDTO { + @NotNull + private Integer orderId; + @NotEmpty + private String creditBuyerId; + @DecimalMin("0.00") + private BigDecimal payAmount; + @DecimalMin("0.00") + private BigDecimal discountAmount; +} + 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 a6ec140..de6c638 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -15,6 +15,7 @@ import com.chaozhanggui.system.cashierservice.bean.TableStateEnum; import com.chaozhanggui.system.cashierservice.bean.constant.TableConstant; import com.chaozhanggui.system.cashierservice.dao.*; import com.chaozhanggui.system.cashierservice.entity.*; +import com.chaozhanggui.system.cashierservice.entity.dto.CreditDTO; import com.chaozhanggui.system.cashierservice.entity.dto.OrderInfoCouponInfoDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ReturnGroupOrderDto; import com.chaozhanggui.system.cashierservice.entity.dto.ReturnOrderDTO; @@ -146,14 +147,14 @@ public class PayService { private final MpProductStockDetailMapper mpProductStockDetailMapper; private final MpOrderInfoService mpOrderInfoService; private final TbMemberPointsService memberPointsService; - + private final TbCreditBuyerOrderService creditBuyerOrderService; private final Utils utils; @Autowired private MPOrderInfoMapper mPOrderInfoMapper; - public PayService(RedisTemplate redisTemplate, MpCashierCartService mpCashierCartService, TbShopCouponService shopCouponService, MpOrderDetailService mpOrderDetailService, MpShopUnitMapper mpShopUnitMapper, MpProductStockDetailMapper mpProductStockDetailMapper, MpOrderInfoService mpOrderInfoService, Utils utils) { + public PayService(RedisTemplate redisTemplate, MpCashierCartService mpCashierCartService, TbShopCouponService shopCouponService, MpOrderDetailService mpOrderDetailService, MpShopUnitMapper mpShopUnitMapper, MpProductStockDetailMapper mpProductStockDetailMapper, MpOrderInfoService mpOrderInfoService, TbMemberPointsService memberPointsService, TbCreditBuyerOrderService creditBuyerOrderService, Utils utils) { this.redisTemplate = redisTemplate; this.mpCashierCartService = mpCashierCartService; this.shopCouponService = shopCouponService; @@ -161,8 +162,9 @@ public class PayService { this.mpShopUnitMapper = mpShopUnitMapper; this.mpProductStockDetailMapper = mpProductStockDetailMapper; this.mpOrderInfoService = mpOrderInfoService; + this.memberPointsService = memberPointsService; + this.creditBuyerOrderService = creditBuyerOrderService; this.utils = utils; - memberPointsService = null; } public static void main(String[] args) { @@ -286,7 +288,7 @@ public class PayService { return Result.fail(CodeEnum.PAYTYPENOEXIST); } - utils.runFunAndTransactional(() -> { + utils.runFunAndTransactional(() -> { TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderId); if (ObjectUtil.isEmpty(payment) || payment == null) { payment = new TbOrderPayment(); @@ -875,6 +877,90 @@ public class PayService { return Result.success(SUCCESS); } + public Result creditPay(CreditDTO creditDTO, String token) { + + TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(creditDTO.getOrderId()); + + if (ObjectUtil.isEmpty(orderInfo)) { + return Result.fail(CodeEnum.ORDERNOEXIST); + } + + if (!"unpaid".equals(orderInfo.getStatus())) { + return Result.fail(CodeEnum.ORDERSTATUSERROR); + } + + + int count = tbShopPayTypeMapper.countSelectByShopIdAndPayType(orderInfo.getShopId(), "cash"); + if (count < 1) { + return Result.fail(CodeEnum.PAYTYPENOEXIST); + } + + + if (creditDTO.getPayAmount().add(creditDTO.getDiscountAmount()).compareTo(orderInfo.getOrderAmount()) != 0) { + return Result.fail("优惠金额 + 支付金额不等于订单金额"); + } + + creditBuyerOrderService.save(creditDTO.getCreditBuyerId(), Long.valueOf(orderInfo.getId())); + + utils.runFunAndTransactional(() -> { + + + if (creditDTO.getPayAmount() != null && creditDTO.getDiscountAmount() != null) { + orderInfo.setPayAmount(creditDTO.getPayAmount()); + orderInfo.setDiscountAmount(creditDTO.getDiscountAmount()); + orderInfo.setDiscountRatio(ObjectUtil.isNotEmpty(creditDTO.getPayAmount()) ? creditDTO.getPayAmount() + .divide(orderInfo.getOrderAmount(), 2, RoundingMode.HALF_UP).setScale(2, RoundingMode.HALF_DOWN) : null); + } else { + orderInfo.setPayAmount(orderInfo.getOrderAmount()); + } + orderInfo.setCreditBuyerId(creditDTO.getCreditBuyerId()); + orderInfo.setPayType("creditBuyer"); + orderInfo.setStatus("closed"); + orderInfo.setPayOrderNo("deposit".concat(SnowFlakeUtil.generateOrderNo())); + tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo); + //更新购物车状态 + mpCashierCartService.updateStateByOrderId(TableConstant.OrderInfo.Status.CLOSED, orderInfo.getId()); + + + if (ObjectUtil.isNotNull(orderInfo.getDiscountRatio()) && ObjectUtil.isNotEmpty(orderInfo.getDiscountRatio())) { + tbOrderDetailMapper.updateStatusByOrderId(creditDTO.getOrderId(), "closed", orderInfo.getDiscountRatio()); + } else { + tbOrderDetailMapper.updateStatusByOrderId(creditDTO.getOrderId(), "closed", null); + } + return null; + }); + + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("token", token); + jsonObject.put("type", "create"); + jsonObject.put("orderId", creditDTO.getOrderId()); + + producer.putOrderCollect(jsonObject.toJSONString()); + + // 打印消息 + if (!OrderUseTypeEnum.DINE_IN_AFTER.getValue().equals(orderInfo.getUseType())) { + List detailList = mPOrderDetailMapper.selectList(new LambdaQueryWrapper() + .eq(TbOrderDetail::getOrderId, orderInfo.getId()) + .eq(TbOrderDetail::getStatus, "closed")); + rabbitMsgUtils.printDishesTicket(orderInfo.getId(), false, detailList.toArray(new TbOrderDetail[0])); + } + rabbitMsgUtils.printPlaceTicket(orderInfo.getId(), false); + + // 发送库存记录mq消息 + JSONObject mqData = new JSONObject(); + mqData.put("orderId", creditDTO.getOrderId()); + mqData.put("type", "pc"); + producer.sendStockSaleMsg(mqData); + + String tableCartKey = RedisCst.getCurrentOrderKey(orderInfo.getTableId(), + orderInfo.getShopId()); + redisUtil.del(tableCartKey); + clearTableInfoCache(orderInfo); + + return Result.success(SUCCESS); + } + public Result vipPay(Integer orderId, String token, Integer vipUserId, BigDecimal payAmount, BigDecimal discountAmount) { if (ObjectUtil.isEmpty(orderId)) { return Result.fail(CodeEnum.PARAM); @@ -1307,24 +1393,24 @@ public class PayService { // TbOrderInfo returnOrder = mpOrderInfoService.selectReturnOrderByOrderId(returnOrderDTO.getOrderId()); TbOrderInfo returnOrder = new TbOrderInfo(); - String orderNo = generateOrderNumber(); - cn.hutool.core.bean.BeanUtil.copyProperties(oldOrderInfo, returnOrder); - returnOrder.setId(null); - returnOrder.setOrderNo(orderNo); - returnOrder.setRefundAmount(returnAmount); - returnOrder.setOrderType("return"); - returnOrder.setStatus(isOnline ? "refunding" : "refund"); - returnOrder.setUpdatedAt(null); - returnOrder.setSystemTime(DateUtil.date().getTime()); - returnOrder.setCreatedAt(DateUtil.date().getTime()); - returnOrder.setPayOrderNo(null); - returnOrder.setSource(oldOrderInfo.getId()); - returnOrder.setRefundRemark(returnOrderDTO.getNote()); - returnOrder.setOrderAmount(returnAmount); - returnOrder.setAmount(returnAmount); - returnOrder.setSettlementAmount(returnAmount); - returnOrder.setPayAmount(returnAmount); - mPOrderInfoMapper.insert(returnOrder); + String orderNo = generateOrderNumber(); + cn.hutool.core.bean.BeanUtil.copyProperties(oldOrderInfo, returnOrder); + returnOrder.setId(null); + returnOrder.setOrderNo(orderNo); + returnOrder.setRefundAmount(returnAmount); + returnOrder.setOrderType("return"); + returnOrder.setStatus(isOnline ? "refunding" : "refund"); + returnOrder.setUpdatedAt(null); + returnOrder.setSystemTime(DateUtil.date().getTime()); + returnOrder.setCreatedAt(DateUtil.date().getTime()); + returnOrder.setPayOrderNo(null); + returnOrder.setSource(oldOrderInfo.getId()); + returnOrder.setRefundRemark(returnOrderDTO.getNote()); + returnOrder.setOrderAmount(returnAmount); + returnOrder.setAmount(returnAmount); + returnOrder.setSettlementAmount(returnAmount); + returnOrder.setPayAmount(returnAmount); + mPOrderInfoMapper.insert(returnOrder); for (TbOrderDetail orderDetail : detailList) { orderDetail.setOrderId(returnOrder.getId()); @@ -1434,11 +1520,11 @@ public class PayService { orderInfo.setRefundAmount(orderInfo.getRefundAmount().add(returnOrderInfo.getRefundAmount())); orderInfo.setRefundRemark(returnOrderDTO.getNote()); if (hasNormalReturn && ("scanCode".equals(payType) || "wx_lite".equals(payType))) { - if (returnOrderDTO.getIsOnline() != null && !returnOrderDTO.getIsOnline()) { + if (returnOrderDTO.getIsOnline() != null && !returnOrderDTO.getIsOnline()) { mpOrderDetailService.updateStatusByOrderIdAndIds(TableConstant.OrderInfo.Status.REFUNDING, TableConstant.OrderInfo.Status.REFUND, returnOrderDTO.getOrderId(), returnOrderDTO.getOrderDetails().stream().map(ReturnOrderDTO.OrderDetail::getId).collect(Collectors.toList())); - }else { + } else { TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getMerchantId())); MsgException.checkNull(thirdApply, "支付参数配置错误"); @@ -2489,4 +2575,5 @@ public class PayService { } + }