diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/constant/TableConstant.java b/src/main/java/com/chaozhanggui/system/cashierservice/constant/TableConstant.java index 46ebaf9..a9a70a7 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/constant/TableConstant.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/constant/TableConstant.java @@ -16,4 +16,19 @@ public interface TableConstant { } } } + + class OrderInfo { + @Getter + public enum Status { + REFUNDING("refunding"), REFUND("refund"), CLOSED("closed"), CREATE("create"), + UNPAID("unpaid"), PAYING("paying"), RETURN("return"); + private final String value; + + Status(String value) { + this.value = value; + } + } + } + + } 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 bd9662c..8792695 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/PayController.java @@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.controller; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.chaozhanggui.system.cashierservice.annotation.LimitSubmit; +import com.chaozhanggui.system.cashierservice.entity.dto.MemberInDTO; import com.chaozhanggui.system.cashierservice.entity.dto.OrderPayDTO; import com.chaozhanggui.system.cashierservice.service.PayService; import com.chaozhanggui.system.cashierservice.sign.Result; @@ -11,10 +12,12 @@ import com.chaozhanggui.system.cashierservice.util.TokenUtil; import com.fasterxml.jackson.core.JsonProcessingException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.math.BigDecimal; import java.util.Map; import java.util.Objects; @@ -49,7 +52,6 @@ public class PayController { } - /** * 储值卡支付 * @@ -64,7 +66,7 @@ public class PayController { @RequestParam("memberId") String memberId, @RequestParam("pwd") String pwd ) { - return payService.accountPay(orderId, memberId, token,pwd); + return payService.accountPay(orderId, memberId, token, pwd); } @RequestMapping("groupOrderPay") @@ -78,7 +80,7 @@ public class PayController { String userId = ""; if (environment.equals("wx") && payType.equals("wechatPay")) { userId = TokenUtil.parseParamFromToken(token).getString("openId"); - } else if("aliPay".equals(payType)){ + } else if ("aliPay".equals(payType)) { userId = TokenUtil.parseParamFromToken(token).getString("openId"); } else { userId = TokenUtil.parseParamFromToken(token).getString("userId"); @@ -139,16 +141,22 @@ public class PayController { * * @param request * @param openId - * @param map - * @return */ @RequestMapping("memeberIn") @LimitSubmit(key = "memeberIn:%s") - public Result memeberIn(HttpServletRequest request, @RequestHeader("openId") String openId, @RequestHeader("id") String id, - @RequestBody Map map - ) { + public Result memeberIn(HttpServletRequest request, + @RequestHeader("openId") String openId, @RequestHeader("id") String id, + @RequestBody @Validated MemberInDTO memberInDTO) { try { - return payService.memberIn(openId, id, map.get("amount").toString(), map.get("shopId").toString(), IpUtil.getIpAddr(request)); + if (memberInDTO.getOrderId() == null && memberInDTO.getAmount() == null) { + return Result.fail("金额和订单id不能同时为空"); + } + + if(memberInDTO.getAmount() != null && memberInDTO.getAmount().compareTo(BigDecimal.ZERO) <= 0) { + return Result.fail("充值金额不能小于等于0"); + } + + return payService.memberIn(memberInDTO, IpUtil.getIpAddr(request)); } catch (JsonProcessingException e) { throw new RuntimeException(e); } @@ -162,7 +170,7 @@ public class PayController { @RequestParam("userId") String userId, @RequestParam("shopId") String shopId ) { - return payService.getShopByMember(userId,shopId,page,pageSize); + return payService.getShopByMember(userId, shopId, page, pageSize); } @RequestMapping("queryMemberAccount") diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberIn.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberIn.java index aa6b8d4..59bfa55 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberIn.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberIn.java @@ -1,9 +1,12 @@ package com.chaozhanggui.system.cashierservice.entity; +import lombok.Data; + import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; +@Data public class TbMemberIn implements Serializable { private Integer id; @@ -27,93 +30,9 @@ public class TbMemberIn implements Serializable { private Integer shopId; + private Integer orderId; + private static final long serialVersionUID = 1L; - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - - public Integer getUserId() { - return userId; - } - - public void setUserId(Integer userId) { - this.userId = userId; - } - - public Integer getMerchantId() { - return merchantId; - } - - public void setMerchantId(Integer merchantId) { - this.merchantId = merchantId; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code == null ? null : code.trim(); - } - - public BigDecimal getAmount() { - return amount; - } - - public void setAmount(BigDecimal amount) { - this.amount = amount; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status == null ? null : status.trim(); - } - - public String getOrderNo() { - return orderNo; - } - - public void setOrderNo(String orderNo) { - this.orderNo = orderNo == null ? null : orderNo.trim(); - } - - public String getTradeNo() { - return tradeNo; - } - - public void setTradeNo(String tradeNo) { - this.tradeNo = tradeNo == null ? null : tradeNo.trim(); - } - - public Date getCreateTime() { - return createTime; - } - - public void setCreateTime(Date createTime) { - this.createTime = createTime; - } - - public Date getUpdateTime() { - return updateTime; - } - - public void setUpdateTime(Date updateTime) { - this.updateTime = updateTime; - } - - public Integer getShopId() { - return shopId; - } - - public void setShopId(Integer shopId) { - this.shopId = shopId; - } -} \ No newline at end of file +} 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 186b8e6..a695431 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbOrderInfo.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbOrderInfo.java @@ -123,6 +123,10 @@ public class TbOrderInfo implements Serializable { private Integer seatCount; private BigDecimal seatAmount; + private BigDecimal pointsDiscountAmount; + private Integer pointsNum; + private String couponIdList; + private BigDecimal couponDiscountAmount; //根据状态返回 需付款 已付款 未付款 已退 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 new file mode 100644 index 0000000..aaad450 --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/MemberInDTO.java @@ -0,0 +1,26 @@ +package com.chaozhanggui.system.cashierservice.entity.dto; + +import com.chaozhanggui.system.cashierservice.entity.TbOrderInfo; +import lombok.Data; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.math.BigDecimal; +import java.util.List; + +@Data +public class MemberInDTO { + @NotNull + private Integer shopId; + @NotNull(message = "用户信息不允许为空") + private Integer userId; + @NotBlank(message = "用户信息不允许为空") + private String openId; + private BigDecimal amount; + private Integer orderId; + // 使用的优惠券 + private List couponIds; + // 是否使用积分抵扣 + private Boolean usePoints; +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/OrderPayDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/OrderPayDTO.java index fe92413..6ce9276 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/OrderPayDTO.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/OrderPayDTO.java @@ -14,10 +14,5 @@ public class OrderPayDTO { private Integer orderId; @NotBlank(message = "支付类型不允许为空") private String payType; - @NotNull - private Boolean isFreeDine; - // 使用的优惠券 - private Integer couponId; - // 是否使用积分抵扣 - private Boolean usePoints; + } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/mapper/MpShopCouponMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/mapper/MpShopCouponMapper.java new file mode 100644 index 0000000..5a7425d --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/mapper/MpShopCouponMapper.java @@ -0,0 +1,19 @@ +package com.chaozhanggui.system.cashierservice.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.chaozhanggui.system.cashierservice.entity.TbCashierCart; +import com.chaozhanggui.system.cashierservice.entity.TbShopCoupon; + +/** +* @author Administrator +* @description 针对表【tb_call_queue】的数据库操作Mapper +* @createDate 2024-09-13 13:44:26 +* @Entity com.chaozhanggui.system.cashierservice.entity.TbCallQueue +*/ +public interface MpShopCouponMapper extends BaseMapper { + +} + + + + 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 dab3fce..7cdbf0a 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -12,6 +12,7 @@ import com.chaozhanggui.system.cashierservice.constant.TableConstant; import com.chaozhanggui.system.cashierservice.dao.*; import com.chaozhanggui.system.cashierservice.entity.Enum.OrderUseTypeEnum; import com.chaozhanggui.system.cashierservice.entity.*; +import com.chaozhanggui.system.cashierservice.entity.dto.MemberInDTO; import com.chaozhanggui.system.cashierservice.entity.dto.OrderPayDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ReturnGroupOrderDto; import com.chaozhanggui.system.cashierservice.entity.dto.ShopEatTypeInfoDTO; @@ -21,6 +22,7 @@ import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.mapper.MpCashierCartMapper; import com.chaozhanggui.system.cashierservice.mapper.MpOrderDetailMapper; import com.chaozhanggui.system.cashierservice.mapper.MpOrderInfoMapper; +import com.chaozhanggui.system.cashierservice.mapper.MpShopCouponMapper; import com.chaozhanggui.system.cashierservice.model.PayReq; import com.chaozhanggui.system.cashierservice.model.TradeQueryReq; import com.chaozhanggui.system.cashierservice.mpservice.MpShopTableService; @@ -190,6 +192,13 @@ public class PayService { private final MpShopTableService mpShopTableService; private final TbFreeDineConfigService freeDineConfigService; private final TbShopCouponService shopCouponService; + @Qualifier("tbShopCouponService") + @Autowired + private TbShopCouponService tbShopCouponService; + @Autowired + private TbShopCouponMapper tbShopCouponMapper; + @Autowired + private MpShopCouponMapper mpShopCouponMapper; public PayService(@Qualifier("tbShopSongOrderServiceImpl") TbShopSongOrderService shopSongOrderService, MpShopTableService mpShopTableService, TbFreeDineConfigService freeDineConfigService, TbShopCouponService shopCouponService) { this.shopSongOrderService = shopSongOrderService; @@ -367,7 +376,14 @@ public class PayService { throw new MsgException("支付失败"); } - private void freeDinePay(OrderPayDTO payDTO, String productName, TbOrderInfo orderInfo, TbMerchantThirdApply thirdApply, String userId) { + private BigDecimal getFreeDineOrderInfo(MemberInDTO payDTO) { + TbOrderInfo orderInfo = mpOrderInfoMapper.selectOne(new LambdaQueryWrapper() + .eq(TbOrderInfo::getId, payDTO.getOrderId()) + .in(TbOrderInfo::getStatus, TableConstant.OrderInfo.Status.UNPAID.getValue(), TableConstant.OrderInfo.Status.PAYING.getValue())); + if (orderInfo == null) { + throw new MsgException("订单不存在或不处于待支付状态"); + } + // 获取店铺霸王餐配置 TbFreeDineConfig freeDineConfig = freeDineConfigService.getById(orderInfo.getShopId()); if (freeDineConfig == null || freeDineConfig.getEnable() == null || freeDineConfig.getEnable() == 0) { @@ -380,7 +396,7 @@ public class PayService { } // 校验优惠券积分同享 - if (payDTO.getCouponId() != null && freeDineConfig.getWithCoupon() == 0) { + if (!payDTO.getCouponIds().isEmpty() && freeDineConfig.getWithCoupon() == 0) { throw new MsgException("当前店铺未开启与优惠券同享"); } @@ -389,18 +405,40 @@ public class PayService { } BigDecimal shouldPayAmount = orderInfo.getOriginAmount().multiply(BigDecimal.valueOf(freeDineConfig.getRechargeTimes())); - if (payDTO.getCouponId() != null) { - List couponList = shopCouponService.getActivateCouponByAmount(Integer.valueOf(orderInfo.getShopId()), userId, shouldPayAmount); - if (couponList.stream().noneMatch(item -> item.getCouponId().equals(payDTO.getCouponId()))) { - throw new MsgException("此优惠券不可用"); + if (!payDTO.getCouponIds().isEmpty()) { + List userCouponList = shopCouponService.getActivateCouponByAmount(Integer.valueOf(orderInfo.getShopId()), String.valueOf(payDTO.getUserId()), shouldPayAmount); + if (userCouponList.isEmpty()) { + throw new MsgException("存在不可用优惠券"); } + + ArrayList couponIdList = new ArrayList<>(); + for (Integer couponId : payDTO.getCouponIds()) { + TbUserCouponVo userCouponVo = userCouponList.stream().filter(item -> item.getCouponId().equals(couponId)).findFirst().orElse(null); + if (userCouponVo == null) { + throw new MsgException("存在不可用优惠券"); + } + couponIdList.add(userCouponVo.getCouponId()); + } + + List tbShopCoupons = mpShopCouponMapper.selectBatchIds(couponIdList); + if (tbShopCoupons.size() != payDTO.getCouponIds().size()) { + throw new MsgException("存在不可用优惠券"); + } + + // 设置优惠券信息 + orderInfo.setCouponIdList(JSONObject.toJSONString(couponIdList)); + orderInfo.setUserCouponAmount(BigDecimal.valueOf(tbShopCoupons.stream().map(TbShopCoupon::getDiscountAmount).reduce(0, Integer::sum))); } // TODO 霸王餐积分 if (payDTO.getUsePoints()) { } - TbShopInfo shopInfo = mpsh.selectById(orderInfo.getShopId()); + mpOrderInfoMapper.updateById(orderInfo); + return shouldPayAmount; + } + + private void createFreeDineOrder(TbOrderInfo orderInfo) { } @Transactional(rollbackFor = Exception.class) @@ -961,34 +999,37 @@ public class PayService { @Transactional(rollbackFor = Exception.class) - public Result memberIn(String openId, String userId, String amount, String shopId, String ip) throws JsonProcessingException { - if (ObjectUtil.isEmpty(openId) || ObjectUtil.isEmpty(userId)) { - return Result.fail("用户信息允许为空"); - } + public Result memberIn(MemberInDTO memberInDTO, String ip) throws JsonProcessingException { - TbUserInfo userInfo = tbUserInfoMapper.selectByPrimaryKey(Integer.valueOf(userId)); + TbUserInfo userInfo = tbUserInfoMapper.selectByPrimaryKey(memberInDTO.getUserId()); if (ObjectUtil.isEmpty(userInfo)) { return Result.fail("用户基本信息不存在"); } - TbShopUser tbShopUser = tbShopUserMapper.selectByUserIdAndShopId(userId, shopId); + TbShopUser tbShopUser = tbShopUserMapper.selectByUserIdAndShopId(String.valueOf(memberInDTO.getUserId()), String.valueOf(memberInDTO.getShopId())); if (ObjectUtil.isEmpty(tbShopUser)) { return Result.fail("对应的用户信息不存在"); } - - TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(shopId)); + TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(memberInDTO.getShopId()); if (ObjectUtil.isEmpty(shopInfo)) { return Result.fail("对应的店铺信息不存在"); } - TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(shopInfo.getMerchantId())); if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) { return Result.fail("支付通道不存在"); } - BigDecimal payAmount = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_DOWN); + // 霸王餐活动充值 + BigDecimal payAmount; + if (memberInDTO.getOrderId() != null) { + payAmount = memberInDTO.getAmount().setScale(2, RoundingMode.DOWN); + // 会员充值 + }else { + payAmount = getFreeDineOrderInfo(memberInDTO); + } + TbMemberIn memberIn = new TbMemberIn(); memberIn.setAmount(payAmount); @@ -998,6 +1039,7 @@ public class PayService { memberIn.setStatus("7"); memberIn.setMerchantId(Integer.valueOf(shopInfo.getMerchantId())); memberIn.setCreateTime(new Date()); + memberIn.setOrderId(memberInDTO.getOrderId()); tbMemberInMapper.insert(memberIn); @@ -1009,11 +1051,11 @@ public class PayService { req.setIp(ip); req.setMercOrderNo(SnowFlakeUtil.generateOrderNo()); req.setNotifyUrl(callBackIn); - req.setPayAmt(amount); + req.setPayAmt(payAmount.toPlainString()); req.setPayType("03"); req.setPayWay("WXZF"); req.setSubject("充值"); - req.setUserId(openId); + req.setUserId(String.valueOf(memberInDTO.getUserId())); Map map = BeanUtil.transBeanMap(req); @@ -1036,7 +1078,10 @@ public class PayService { String orderNo = DateUtils.getsdfTimesSS(); PublicResp publicResp = thirdPayService .scanpay(thirdUrl, thirdApply.getAppId(), - "会员充值", "会员充值", new BigDecimal(amount).setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(), "WECHAT", thirdApply.getSmallAppid(), openId, ip, orderNo, thirdApply.getStoreId(), callInBack, null, thirdApply.getAppToken()); + "会员充值", "会员充值", new BigDecimal(amount).setScale(2, BigDecimal.ROUND_DOWN) + .multiply(new BigDecimal(100)).longValue(), "WECHAT", + thirdApply.getSmallAppid(), memberInDTO.getOpenId(), ip, orderNo, thirdApply.getStoreId(), + callInBack, null, thirdApply.getAppToken()); if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { if ("000000".equals(publicResp.getCode())) { WxScanPayResp wxScanPayResp = publicResp.getObjData(); @@ -1045,6 +1090,7 @@ public class PayService { memberIn.setOrderNo(orderNo); memberIn.setTradeNo(wxScanPayResp.getPayOrderId()); memberIn.setUpdateTime(new Date()); + memberIn.setOrderId(memberIn.getOrderId()); tbMemberInMapper.updateByPrimaryKeySelective(memberIn); ObjectMapper mapper = new ObjectMapper(); return Result.success(CodeEnum.SUCCESS, mapper.readTree(wxScanPayResp.getPayInfo()));