Hentua
2023-06-15 7f55c18d7db36c9cad2ac96e481680bc1107e51c
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -3,6 +3,7 @@
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.exception.GeneratorException;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.MallUtils;
@@ -18,12 +19,16 @@
import cc.mrbird.febs.pay.model.AgreementPayDto;
import cc.mrbird.febs.pay.model.AgreementSignDto;
import cc.mrbird.febs.pay.model.UnipayDto;
import cc.mrbird.febs.pay.service.IAliPayService;
import cc.mrbird.febs.pay.service.IPayService;
import cc.mrbird.febs.pay.service.UnipayService;
import cc.mrbird.febs.rabbit.producter.AgentProducer;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
@@ -63,14 +68,14 @@
    private final MallOrderRefundMapper mallOrderRefundMapper;
    private final MallOrderRefundOperationMapper mallOrderRefundOperationMapper;
    private final MallShoppingCartMapper mallShoppingCartMapper;
    private final IApiMallMemberService memberService;
    private final IMallMoneyFlowService mallMoneyFlowService;
    private final RedisUtils redisUtils;
    private final AgentProducer agentProducer;
    private final IPayService payService;
    private final IAliPayService payService;
    private final IMallAchieveService mallAchieveService;
    private final UnipayService unipayService;
    private final IPayService wechatPayService;
    private final MallMemberBankSignMapper mallMemberBankSignMapper;
    private final MallMemberBankMapper mallMemberBankMapper;
@@ -78,6 +83,11 @@
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallAchieveRecordMapper mallAchieveRecordMapper;
    private final MallMemberWalletMapper mallMemberWalletMapper;
    private final MallMqRecordMapper mallMqRecordMapper;
    private final MallShopMapper mallShopMapper;
    private final ICommonService commonService;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -113,11 +123,8 @@
                throw new FebsException("请选择门店");
            }
            Long shopId = addOrderDto.getShopId();
            MallShopApply mallShopApply = mallShopApplyMapper.selectById(shopId);
            MallShop mallShopApply = mallShopMapper.selectById(shopId);
            if(ObjectUtil.isEmpty(mallShopApply)){
                throw new FebsException("请选择门店");
            }
            if(MallShopApply.APPLY_AGREE != mallShopApply.getStatus()){
                throw new FebsException("请选择门店");
            }
            orderInfo.setShopId(shopId);
@@ -138,6 +145,14 @@
                if (MallGoods.ISSALE_STATUS_DISABLED.equals(mallGoods.getIsSale())) {
                    throw new FebsException(mallGoods.getGoodsName() + "已下架");
                }
                if (mallGoods.getRulesTimes() != null && mallGoods.getRulesCnt() != null) {
                    DateTime startDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -mallGoods.getRulesTimes());
                    List<MallOrderInfo> orderInfos = this.baseMapper.selectScoreOrderListInDate(DateUtil.beginOfDay(startDate), DateUtil.endOfDay(new Date()), mallGoods.getId(), member.getId());
                    if (CollUtil.isNotEmpty(orderInfos) && orderInfos.size() >= mallGoods.getRulesCnt()) {
                        throw new FebsException("超出领取最大限制");
                    }
                }
                BigDecimal amount = mallGoods.getScore().multiply(BigDecimal.valueOf(item.getCnt()));
@@ -189,6 +204,11 @@
                orderItem.setSkuImage(sku.getSkuImage());
                orderItem.setIsNormal(mallGoods.getIsNormal());
                orderItem.setCostPrice(sku.getCostPrice());
                orderItem.setStaticMulti(mallGoods.getStaticMulti());
                orderItem.setGoodsLevel(mallGoods.getGoodsLevel());
                orderItem.setNormalBonus(mallGoods.getNormalBonus());
                orderItem.setNormalPer(mallGoods.getNormalPer());
                total = total.add(amount);
                carriage = carriage.add(mallGoods.getCarriage());
@@ -276,31 +296,25 @@
        String rcResult = "";
        switch (payOrderDto.getType()) {
            case "1":
                UnipayDto unipayDtoWx = new UnipayDto();
//                unipayDto.setAmount(new BigDecimal("0.01"));
                unipayDtoWx.setAmount(orderInfo.getAmount());
                unipayDtoWx.setFrpCode("WEIXIN_APP3");
                unipayDtoWx.setTradeMerchantNo("777180800385820");
                unipayDtoWx.setOrderNo(orderInfo.getOrderNo());
                List<MallOrderItem> itemsWx = orderInfo.getItems();
                if(CollUtil.isEmpty(itemsWx)){
                    unipayDtoWx.setProductName("商品");
                    orderInfo.setName("商品");
                }else{
                    unipayDtoWx.setProductName(itemsWx.get(0).getGoodsName());
                    orderInfo.setName(itemsWx.get(0).getGoodsName());
                }
                String unipayStrWx = unipayService.unipay(unipayDtoWx);
                if("fail" == unipayStrWx){
                    throw new FebsException("支付失败");
                }else{
//                    JSONUtil.parseObj(unipayStr);
                    JSONObject jsonObject = JSONUtil.parseObj(unipayStrWx);
                    payResultStr = (String) jsonObject.get("r7_TrxNo");
                    rcResult = (String) jsonObject.get("rc_Result");
                if (orderInfo.getScoreCnt().compareTo(BigDecimal.ZERO) > 0) {
                    int reduce = memberWalletService.reduce(orderInfo.getScoreCnt(), member.getId(), "prizeScore");
                    if (reduce == 2) {
                        throw new FebsException("积分不足");
                    }
                    mallMoneyFlowService.addMoneyFlow(member.getId(), orderInfo.getScoreCnt().negate(), MoneyFlowTypeEnum.PAY.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.PRIZE_SCORE.getValue());
                }
                payResultStr = wechatPayService.pay(orderInfo);
                orderInfo.setPayOrderNo(payResultStr);
//                orderInfo.setPayOrderNo(payOrderDto.getPayOrderNo());
//                orderInfo.setPayImage(payOrderDto.getPayImage());
                orderInfo.setPayMethod("微信支付");
                orderInfo.setPayResult("2");
                break;
            case "2":
//                if (StrUtil.isNotBlank(orderInfo.getPayOrderNo())) {
@@ -341,12 +355,52 @@
                orderInfo.setStatus(OrderStatusEnum.WAIT_SHIPPING.getValue());
                orderInfo.setPayTime(new Date());
                orderInfo.setPayResult("1");
                this.baseMapper.updateById(orderInfo);
                changeWallet(orderInfo.getId());
                //发送补贴消息
                agentProducer.sendPerkMoneyMsg(orderInfo.getId());
                //发送代理自动升级消息
                agentProducer.sendAutoLevelUpMsg(member.getId());
                // 商品级别 会员/代理/股东
                int goodsLevel = 0;
                BigDecimal score = BigDecimal.ZERO;
                for (MallOrderItem item : orderInfo.getItems()) {
                    // 计算静态倍数
                    if(item.getIsNormal() == 2) {
                        score = score.add(item.getPrice().multiply(item.getStaticMulti()));
                    }
                    // 判断会员层级
                    if (item.getIsNormal() == 2 && goodsLevel < item.getGoodsLevel()) {
                        goodsLevel = item.getGoodsLevel();
                    }
                    mallAchieveService.add(item.getId());
                }
                member.setAccountLevel(goodsLevel);
                String level = GoodsToLevelEnums.getLevelByAccountLevel(goodsLevel);
                if (MemberLevelEnum.getLevelCode(member.getLevel()) < MemberLevelEnum.getLevelCode(level)) {
                    member.setLevel(level);
                }
                this.memberMapper.updateById(member);
                memberWalletService.add(score, member.getId(), "score");
                if (orderInfo.getScoreCnt().compareTo(BigDecimal.ZERO) > 0) {
                    int reduce = memberWalletService.reduce(orderInfo.getScoreCnt(), member.getId(), "prizeScore");
                    if (reduce == 2) {
                        throw new FebsException("积分不足");
                    }
                    mallMoneyFlowService.addMoneyFlow(member.getId(), orderInfo.getScoreCnt().negate(), MoneyFlowTypeEnum.PAY.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.PRIZE_SCORE.getValue());
                }
                mallMoneyFlowService.addMoneyFlow(member.getId(), score, MoneyFlowTypeEnum.BUY_TC.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
                mallMoneyFlowService.addMoneyFlow(member.getId(), orderInfo.getAmount().subtract(orderInfo.getScoreAmount()).negate(), MoneyFlowTypeEnum.PAY.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.BALANCE.getValue());
                ThreadUtil.execute(() -> {
                    ThreadUtil.sleep(5000);
                    //发送代理自动升级消息
                    agentProducer.sendAutoLevelUpMsg(member.getId());
                    //发送直推返利消息
                    agentProducer.sendOrderReturn(orderInfo.getId());
                    // 发送分红消息
                    agentProducer.sendOrderBonus(orderInfo.getId());
                });
                break;
            case "4":
                if (orderInfo.getOrderType() != 2) {
@@ -412,155 +466,7 @@
        map.put("orderNo", orderInfo.getOrderNo());
        map.put("orderId", orderInfo.getId());
        map.put("rcResult", rcResult);
        return map;
    }
    /**
     * 支付金额后的操作
     *      减少积分,增加流水
     *      购买套餐后,升级会员等级
     *      增加支付金额流水
     *      增加补贴额度
     *      增加贡献值,增加流水
     *      产生一条业绩记录
     * @param orderId
     */
    @Override
    public void changeWallet(Long orderId){
        MallOrderInfo orderInfo = this.baseMapper.selectById(orderId);
        Long memberId = orderInfo.getMemberId();
        boolean hasTc = false;
        //总贡献值
        BigDecimal starSum = BigDecimal.ZERO;
        /**
         * 总补贴额度
         * 所有合伙人补贴至消费礼包的三倍额度需要复购一次。
         */
        BigDecimal scoreSum = BigDecimal.ZERO;
        List<MallOrderItem> orderItems = this.baseMapper.getMallOrderItemByOrderId(orderInfo.getId());
        if (CollUtil.isNotEmpty(orderItems)) {
            for (MallOrderItem orderItem : orderItems) {
                MallGoods mallGoods = mallGoodsMapper.selectById(orderItem.getGoodsId());
                /**
                 * 购买套餐赠送贡献值
                 * 补贴3倍额度
                 */
                if (mallGoods.getIsNormal() == 2) {
                    hasTc = true;
                    BigDecimal star = mallGoods.getStar()==null ? BigDecimal.ZERO : mallGoods.getStar();
                    starSum = starSum.add(star);
                    scoreSum = scoreSum.add(
                            new BigDecimal(mallGoods.getPresentPrice())
                                    .multiply(new BigDecimal(3))
                                    .abs()
                                    .setScale(2,BigDecimal.ROUND_DOWN));
                }
            }
            /**
             * 增加贡献值,增加流水
             */
            if (starSum.compareTo(BigDecimal.ZERO) > 0) {
                //系统设置的个人贡献点最大值
                DataDictionaryCustom starGetDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                        DataDictionaryEnum.STAR_GET.getType(),
                        DataDictionaryEnum.STAR_GET.getCode());
                if(ObjectUtil.isNotEmpty(starGetDic)){
                    String starGetStr = StrUtil.isEmpty(starGetDic.getValue()) ? "0" : starGetDic.getValue();
                    BigDecimal starGet = new BigDecimal(starGetStr);
                    MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
                    BigDecimal star = new BigDecimal(mallMemberWallet.getStar());
                    if(starGet.compareTo(star.add(starSum)) < 0){
                        if(starGet.compareTo(BigDecimal.ZERO) == 0){
                            mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
                            mallMoneyFlowService.addMoneyFlow(
                                    memberId,
                                    starSum,
                                    MoneyFlowTypeEnum.STAR.getValue(),
                                    orderInfo.getOrderNo(),
                                    FlowTypeEnum.STAR.getValue());
                        }
                        if(starGet.compareTo(star) > 0){
                            BigDecimal subtract = starGet.subtract(star);
                            if(subtract.compareTo(starSum) < 0){
                                starSum = subtract;
                            }
                            mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
                            mallMoneyFlowService.addMoneyFlow(
                                    memberId,
                                    starSum,
                                    MoneyFlowTypeEnum.STAR.getValue(),
                                    orderInfo.getOrderNo(),
                                    FlowTypeEnum.STAR.getValue());
                        }
                    }else{
                        mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
                        mallMoneyFlowService.addMoneyFlow(
                                memberId,
                                starSum,
                                MoneyFlowTypeEnum.STAR.getValue(),
                                orderInfo.getOrderNo(),
                                FlowTypeEnum.STAR.getValue());
                    }
                }
            }
            /**
             * 增加补贴额度
             */
            if (scoreSum.compareTo(BigDecimal.ZERO) > 0) {
                memberWalletService.add(scoreSum, memberId, "totalScore");
                mallMoneyFlowService.addMoneyFlow(
                        memberId,
                        scoreSum,
                        MoneyFlowTypeEnum.TOTAL_SCORE.getValue(),
                        orderInfo.getOrderNo(),
                        FlowTypeEnum.TOTAL_SCORE.getValue());
            }
        }
        /**
         * 购买套餐后,升级会员等级
          */
        if (hasTc) {
            MallMember mallMember = memberMapper.selectById(memberId);
            if (AgentLevelEnum.ZERO_LEVEL.name().equals(mallMember.getLevel())) {
                mallMember.setLevel(AgentLevelEnum.SECOND_LEVEL.name());
                memberMapper.updateById(mallMember);
            }
        }
        /**
         * 增加支付金额流水
         */
        mallMoneyFlowService.addMoneyFlow(
                memberId,
                orderInfo.getAmount().subtract(orderInfo.getScoreAmount()).negate(),
                MoneyFlowTypeEnum.PAY.getValue(),
                orderInfo.getOrderNo(),
                FlowTypeEnum.BALANCE.getValue());
        /**
         * 减少积分,增加流水
         */
        int reduce = memberWalletService.reduce(orderInfo.getScoreCnt(), memberId, "prizeScore");
        if (reduce == 2) {
            throw new FebsException("积分不足");
        }
        mallMoneyFlowService.addMoneyFlow(
                memberId,
                orderInfo.getScoreCnt().negate(),
                MoneyFlowTypeEnum.PAY.getValue(),
                orderInfo.getOrderNo(),
                FlowTypeEnum.PRIZE_SCORE.getValue());
        /**
         * 产生一条业绩记录
         */
        MallAchieveRecord mallAchieveRecord = new MallAchieveRecord();
        mallAchieveRecord.setMemberId(memberId);
        mallAchieveRecord.setAchieveTime(new Date());
        mallAchieveRecord.setAmount(orderInfo.getAmount().subtract(orderInfo.getScoreAmount()));
        mallAchieveRecord.setOrderId(orderId);
        mallAchieveRecord.setPayTime(orderInfo.getPayTime());
        mallAchieveRecordMapper.insert(mallAchieveRecord);
    }
    public static void main(String[] args) {
@@ -596,6 +502,8 @@
        if (reduce == 2) {
            throw new FebsException("余额不足");
        }
        return orderInfo.getOrderNo();
    }
@@ -639,16 +547,15 @@
            orderDetailVo.setRemainTime(remainTime);
        }
        if(2 == deliverType){
            MallShopApply mallShopApply = mallShopApplyMapper.selectById(orderInfo.getShopId());
            MallShop mallShopApply = mallShopMapper.selectById(orderInfo.getShopId());
            if(ObjectUtil.isNotEmpty(mallShopApply)){
                ApiShopApplyVo apiShopApplyVo = new ApiShopApplyVo();
                apiShopApplyVo.setName(mallShopApply.getName());
                apiShopApplyVo.setName(mallShopApply.getShopName());
                apiShopApplyVo.setPhone(mallShopApply.getPhone());
                apiShopApplyVo.setSaleArea(mallShopApply.getSaleArea());
                apiShopApplyVo.setLatitude(mallShopApply.getLatitude());
                apiShopApplyVo.setLongitude(mallShopApply.getLongitude());
                apiShopApplyVo.setLatitude(mallShopApply.getLatitude().toPlainString());
                apiShopApplyVo.setLongitude(mallShopApply.getLongitude().toPlainString());
                apiShopApplyVo.setShopName(mallShopApply.getShopName());
                apiShopApplyVo.setAddress(mallShopApply.getAddress());
                apiShopApplyVo.setAddress(mallShopApply.getShopAddress());
                orderDetailVo.setApiShopApplyVo(apiShopApplyVo);
            }
        }
@@ -849,43 +756,31 @@
        BigDecimal total = BigDecimal.ZERO;
        for (AddOrderItemDto item : addOrderDto.getItems()) {
            MallGoodsSku sku = mallGoodsSkuMapper.selectSkuInfoById(item.getSkuId());
            MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
            /**
             * 贡献点判断
             */
            if (mallGoods.getIsNormal() == 2) {
                hasTc = true;
            if(ObjectUtil.isEmpty(sku)){
                throw new FebsException("商品已更新,请重新下单");
            }
                /*
                获取单个商品的折扣积分数量
                    = 现价 * 数量 * 折扣现金比例
                 */
            BigDecimal scorePercent = new BigDecimal(mallGoods.getScorePercent()).multiply(new BigDecimal(0.01));
            MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
            if(ObjectUtil.isEmpty(mallGoods)){
                throw new FebsException("商品已更新,请重新下单");
            }
            /*
            获取单个商品的折扣积分数量
                = 现价 * 数量 * 折扣现金比例
             */
            BigDecimal scorePercent = new BigDecimal(mallGoods.getScorePercent()).multiply(BigDecimal.valueOf(0.01));
            BigDecimal amount = sku.getPresentPrice().multiply(BigDecimal.valueOf(item.getCnt())).multiply(scorePercent);
            total = total.add(amount);
        }
        BigDecimal score = ObjectUtil.isEmpty(addOrderDto.getScore()) ? BigDecimal.ZERO : addOrderDto.getScore();
        if(hasTc){
            //系统设置的个人贡献点最大值
            DataDictionaryCustom starGetDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    DataDictionaryEnum.STAR_GET.getType(),
                    DataDictionaryEnum.STAR_GET.getCode());
            if(ObjectUtil.isNotEmpty(starGetDic)){
                String starGetStr = StrUtil.isEmpty(starGetDic.getValue()) ? "0" : starGetDic.getValue();
                BigDecimal starGet = new BigDecimal(starGetStr);
                MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
                BigDecimal star = new BigDecimal(mallMemberWallet.getStar());
                if(starGet.compareTo(BigDecimal.ZERO) != 0 && starGet.compareTo(star) <= 0){
                    throw new FebsException("无法购买套餐");
                }
                BigDecimal prizeScore = mallMemberWallet.getPrizeScore();
                if(prizeScore.compareTo(score) < 0){
                    throw new FebsException("积分不足");
                }
            }
        MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        BigDecimal prizeScore = mallMemberWallet.getPrizeScore();
        if(prizeScore.compareTo(score) < 0){
            throw new FebsException("积分不足");
        }
        /**
         * 使用积分折扣现金
         */