xiaoyong931011
2022-08-01 969759b44baf414778aa68f1f3af5cd79d9f7e9b
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -2,10 +2,8 @@
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.MallUtils;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.common.utils.*;
import cc.mrbird.febs.mall.conversion.MallGoodsCommentConversion;
import cc.mrbird.febs.mall.conversion.MallOrderInfoConversion;
import cc.mrbird.febs.mall.conversion.MallOrderRefundConversion;
import cc.mrbird.febs.mall.dto.*;
@@ -15,13 +13,16 @@
import cc.mrbird.febs.mall.vo.OrderDetailVo;
import cc.mrbird.febs.mall.vo.OrderListVo;
import cc.mrbird.febs.mall.vo.OrderRefundVo;
import cc.mrbird.febs.pay.model.BrandWCPayRequestData;
import cc.mrbird.febs.pay.service.IPayService;
import cc.mrbird.febs.pay.service.IXcxPayService;
import cc.mrbird.febs.rabbit.producter.AgentProducer;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -31,7 +32,6 @@
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@@ -48,6 +48,7 @@
    private final MallAddressInfoMapper mallAddressInfoMapper;
    private final MallOrderItemMapper mallOrderItemMapper;
    private final MallMemberMapper memberMapper;
    private final MallGoodsCommentMapper mallGoodsCommentMapper;
    private final IApiMallMemberWalletService memberWalletService;
    private final MallExpressInfoMapper expressInfoMapper;
    private final MallOrderRefundMapper mallOrderRefundMapper;
@@ -59,12 +60,17 @@
    private final AgentProducer agentProducer;
    private final IPayService payService;
    private final IXcxPayService iXcxPayService;
    private final IMallAchieveService mallAchieveService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOrder(AddOrderDto addOrderDto) {
        MallMember member = LoginUserUtil.getLoginUser();
        String takeUniqueCode = addOrderDto.getTakeUniqueCode();
        if(StrUtil.isEmpty(takeUniqueCode)){
            throw new FebsException("请选择自提点");
        }
        MallAddressInfo address = mallAddressInfoMapper.selectAddressInfoByMemberIdAndId(member.getId(), addOrderDto.getAddressId());
        if (address == null) {
            throw new FebsException("地址不存在");
@@ -83,6 +89,9 @@
        orderInfo.setLongitude(address.getLongitude());
        orderInfo.setRemark(addOrderDto.getRemark());
        orderInfo.setOrderType(addOrderDto.getOrderType());
        orderInfo.setTakeUniqueCode(addOrderDto.getTakeUniqueCode());
        orderInfo.setDeliveryType(1);
        if (CollUtil.isEmpty(addOrderDto.getItems())) {
            throw new FebsException("参数错误");
@@ -131,7 +140,7 @@
                // 零撸专区购买
                if (new BigDecimal(mallGoods.getPresentPrice()).compareTo(BigDecimal.ZERO) == 0) {
                    List<MallOrderItem> items = mallOrderItemMapper.selectItemByGoodsIdUnCancel(mallGoods.getId());
                    List<MallOrderItem> items = mallOrderItemMapper.selectItemByGoodsIdUnCancel(mallGoods.getId(), member.getId());
                    if (CollUtil.isNotEmpty(items)) {
                        throw new FebsException("无法重复领取同一个商品");
                    }
@@ -210,10 +219,19 @@
        }
        String payResultStr = "";
        String wxResultStr = "";
        switch (payOrderDto.getType()) {
            case "1":
//                orderInfo.setPayOrderNo(payOrderDto.getPayOrderNo());
//                orderInfo.setPayImage(payOrderDto.getPayImage());
                BrandWCPayRequestData brandWCPayRequestData = null;
                try {
                    brandWCPayRequestData = iXcxPayService.startPayment(orderInfo);
                } catch (Exception e) {
                    throw new FebsException("支付失败");
                }
                wxResultStr = JSONUtil.toJsonStr(brandWCPayRequestData);
                payResultStr = brandWCPayRequestData.getPrepay_id();
                orderInfo.setPayMethod("微信支付");
                agentProducer.sendOrderReturn(orderInfo.getId());
                break;
@@ -243,14 +261,21 @@
                if (CollUtil.isNotEmpty(orderItems)) {
                    for (MallOrderItem orderItem : orderItems) {
                        MallGoods mallGoods = mallGoodsMapper.selectById(orderItem.getGoodsId());
                        BigDecimal score = BigDecimal.ZERO;
                        MallGoodsSku sku = mallGoodsSkuMapper.selectById(orderItem.getSkuId());
                        if (mallGoods.getIsNormal() == 2) {
                            hasTc = true;
                            MallGoodsSku sku = mallGoodsSkuMapper.selectById(orderItem.getSkuId());
                            BigDecimal score = sku.getOriginalPrice().multiply(mallGoods.getStaticMulti());
                            score = sku.getPresentPrice().multiply(mallGoods.getStaticMulti()).multiply(new BigDecimal(orderItem.getCnt()));
//                            BigDecimal staticMulti = mallGoods.getStaticMulti() == null ? BigDecimal.ZERO : mallGoods.getStaticMulti();
//                            score = sku.getPresentPrice().multiply(staticMulti);
                        }
                        if (score.compareTo(BigDecimal.ZERO) > 0) {
                            memberWalletService.add(score, member.getId(), "score");
                            mallMoneyFlowService.addMoneyFlow(member.getId(), score, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
                            // 添加业绩
                            mallAchieveService.add(orderItem.getId());
                        }
                    }
                }
@@ -265,7 +290,7 @@
                }
                mallMoneyFlowService.addMoneyFlow(member.getId(), orderInfo.getAmount().negate(), MoneyFlowTypeEnum.PAY.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.BALANCE.getValue());
//                agentProducer.sendAutoLevelUpMsg(member.getId());
                agentProducer.sendAutoLevelUpMsg(member.getId());
                agentProducer.sendOrderReturn(orderInfo.getId());
                break;
            case "4":
@@ -285,12 +310,16 @@
            default:
        }
        //订单支付成功产生一个提货码
        String takeCode = ShareCodeUtil.toSerialCode(orderInfo.getId());
        orderInfo.setTakeCode(takeCode);
        this.baseMapper.updateById(orderInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("orderInfo", payResultStr);
        map.put("orderNo", orderInfo.getOrderNo());
        map.put("orderId", orderInfo.getId());
        map.put("wxResultStr", wxResultStr);
        return map;
    }
@@ -309,7 +338,10 @@
            throw new FebsException("支付密码错误");
        }
        memberWalletService.reduce(orderInfo.getAmount().add(orderInfo.getCarriage()), mallMember.getId(), field);
        int reduce = memberWalletService.reduce(orderInfo.getAmount().add(orderInfo.getCarriage()), mallMember.getId(), field);
        if (reduce == 2) {
            throw new FebsException("余额不足");
        }
        return orderInfo.getOrderNo();
    }
@@ -416,6 +448,13 @@
            orderRefund.setRefundTime(new Date());
            orderRefund.setBeforeStatus(beforeStatus);
            orderRefund.setStatus(OrderRefundStatusEnum.REFUND_APPLY.getValue());
            // 未发货则退运费,发货了则不退
            if (beforeStatus == 2) {
                orderRefund.setAmount(orderInfo.getAmount().add(orderInfo.getCarriage()));
            } else {
                orderRefund.setAmount(orderInfo.getAmount());
            }
            mallOrderRefundMapper.insert(orderRefund);
        } else {
            orderRefund.setDesp(addRefundDto.getDesp());
@@ -424,6 +463,12 @@
            orderRefund.setRefundTime(new Date());
            orderRefund.setBeforeStatus(beforeStatus);
            orderRefund.setStatus(OrderRefundStatusEnum.REFUND_APPLY.getValue());
            // 未发货则退运费,发货了则不退
            if (beforeStatus == 2) {
                orderRefund.setAmount(orderInfo.getAmount().add(orderInfo.getCarriage()));
            } else {
                orderRefund.setAmount(orderInfo.getAmount());
            }
            mallOrderRefundMapper.updateById(orderRefund);
        }
@@ -470,4 +515,44 @@
            this.baseMapper.updateById(orderInfo);
        }
    }
    @Override
    @Transactional
    public void goodsComment(ApiAddCommentDtos addCommentDtos) {
        Long orderId = addCommentDtos.getOrderId();
        MallMember member = LoginUserUtil.getLoginUser();
        MallOrderInfo orderInfo = this.baseMapper.selectOrderDetailsById(orderId);
        if (orderInfo == null || AppContants.DEL_FLAG_Y == orderInfo.getDelFlag()) {
            throw new FebsException("订单不存在");
        }
        if (OrderStatusEnum.FINISH.getValue() != orderInfo.getStatus()) {
            throw new FebsException("该状态不能评价");
        }
        if (MallOrderInfo.COMMENT_STATE_YES == orderInfo.getCommentState()) {
            throw new FebsException("该状态不能评价");
        }
        orderInfo.setCommentState(MallOrderInfo.COMMENT_STATE_YES);
        this.baseMapper.updateById(orderInfo);
        List<ApiAddCommentDto> apiAddCommentDtos = addCommentDtos.getApiAddCommentDtos();
        if(CollUtil.isNotEmpty(apiAddCommentDtos)){
            for(ApiAddCommentDto apiAddCommentDto : apiAddCommentDtos){
                Long skuId = apiAddCommentDto.getSkuId();
                MallGoodsSku mallGoodsSku = mallGoodsSkuMapper.selectById(skuId);
                Long goodsId = apiAddCommentDto.getGoodsId();
                MallGoods mallGoods = mallGoodsMapper.selectById(goodsId);
                MallGoodsComment mallGoodsComment = MallGoodsCommentConversion.INSTANCE.dtoToEntity(apiAddCommentDto);
                mallGoodsComment.setMemberId(member.getId());
                mallGoodsComment.setOrderId(orderId);
                mallGoodsComment.setGoodsName(mallGoods.getGoodsName());
                mallGoodsComment.setSkuName(mallGoodsSku.getSkuName());
                mallGoodsComment.setStyleId(mallGoodsSku.getStyleId());
                mallGoodsComment.setStyleName(mallGoodsSku.getStyleName());
                mallGoodsComment.setShowState(MallGoodsComment.SHOW_STATE_ENABLE);
                mallGoodsCommentMapper.insert(mallGoodsComment);
            }
        }
    }
}