Helius
2021-12-29 1975ada3e42a06d4e3ff1cc36b3613020208ce1b
zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/ScoreOrderTask.java
@@ -1,8 +1,15 @@
package com.matrix.system.shopXcx.mqTask;
import com.matrix.biz.service.BizUserService;
import com.matrix.component.asyncmessage.MessageHandler;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.hive.bean.SysVipInfo;
import com.matrix.system.hive.dao.SysVipInfoDao;
import com.matrix.system.hive.service.SysVipInfoService;
import com.matrix.system.score.constant.ScoreSettingConstant;
import com.matrix.system.score.entity.ScoreVipDetail;
import com.matrix.system.score.service.ScoreVipDetailService;
import com.matrix.system.shopXcx.bean.ShopOrder;
@@ -15,13 +22,15 @@
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
 * 订单创建积分事件处理
 */
@Component
public class ScoreOrderTask implements DeliverCallback {
public class ScoreOrderTask implements MessageHandler {
    @Autowired
@@ -30,17 +39,32 @@
    ShopOrderDetailsDao shopOrderDetailsDao;
    @Autowired
    BizUserService bizUserService;
    SysVipInfoService sysVipInfoService;
    @Autowired
    ScoreVipDetailService scoreVipDetailService;
    @Autowired
    BusParameterSettingsDao busParameterSettingsDao;
    @Autowired
    SysVipInfoDao sysVipInfoDao;
    @Override
    public void handle(String consumerTag, Delivery message) throws IOException {
    public String getName() {
        return "订单创建积分事件";
    }
        String orderId = new String(message.getBody(), "UTF-8");
    @Override
    public String getRouteKey() {
        return AsyncMessageRouting.CREATE_ORDER;
    }
    @Override
    public void handle(Map<String,Object> param)  {
        String orderId = (String) param.get("orderId");
        LogUtil.debug("收到订单积分任务orderId={}", orderId);
        //获取订单信息
        ShopOrder order = shopOrderDao.selectById(Integer.valueOf(orderId));
@@ -48,30 +72,81 @@
        List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId));
        order.setDetails(orderDetails);
        //扣除积分
        if(order.getScorePay()!=null){
            scoreVipDetailService.deductionScore(order.getUserId(),null,Long.parseLong(order.getStoreId()+""),order.getScorePay(),Long.parseLong(order.getId()+""), ScoreVipDetail.SCORE_VIP_TYPE_CASH);
        }
        //消费获得积分
        int addScore=0;
        for(ShopOrderDetails shopOrderDetail : orderDetails) {
            if(shopOrderDetail.getPayType()==ShopOrderDetails.PAYTYPE_MICRO){
              Integer xfkdScore=  shopOrderDetail.getShopSku().getScore();
              if(xfkdScore!=null && xfkdScore>0){
                  addScore+=xfkdScore;
              }
        if (order.getScorePay() != null && order.getScorePay() > 0) {
            scoreVipDetailService.deductionScore(order.getUserId(), null,
                    Long.parseLong(order.getStoreId() + ""), order.getScorePay(),
                    Long.parseLong(order.getId() + ""), ScoreVipDetail.SCORE_VIP_TYPE_CASH, "商城积分抵扣");
        } else {
            //消费获得积分
            int addScore = 0;
            BusParameterSettings cashConsumptionShop = busParameterSettingsDao.selectCompanyParamByCode(ScoreSettingConstant.CASH_CONSUMPTION_SHOP, order.getCompanyId());
            for (ShopOrderDetails shopOrderDetail : orderDetails) {
                if (shopOrderDetail.getPayType() == ShopOrderDetails.PAYTYPE_MICRO) {
                    Integer xfkdScore = shopOrderDetail.getShopSku().getScore();
                    if (xfkdScore != null && xfkdScore > 0) {
                        addScore += xfkdScore;
                    } else {
                        if (StringUtils.isNotBlank(cashConsumptionShop.getParamValue())) {
                            addScore += shopOrderDetail.getTotalPrice().divide(new BigDecimal(cashConsumptionShop.getParamValue())).intValue();
                        }
                    }
                }
            }
            if (addScore > 0) {
                //插入新的积分
                scoreVipDetailService.addScore(
                        order.getUserId(),
                        null,
                        Long.parseLong(order.getStoreId() + ""),
                        addScore,
                        Long.parseLong(orderId + ""),
                        ScoreVipDetail.SCORE_VIP_TYPE_CASH,
                        "微商城消费");
                //插入上级积分
                SysVipInfo vipInfo = sysVipInfoService.findById(order.getUserId());
                if (vipInfo.getRecommendId() != null) {
                    //推荐注册老带新积分奖励
                    SysVipInfo referrerVip = sysVipInfoDao.selectById(vipInfo.getRecommendId());
                    if (StringUtils.isNotBlank(cashConsumptionShop.getParamValue1())) {
                        int parentScore = order.getOrderMoney().divide(new BigDecimal(cashConsumptionShop.getParamValue1())).intValue();
                        if (parentScore > 0) {
                            scoreVipDetailService.addScore(
                                    referrerVip.getId(),
                                    null,
                                    Long.parseLong(order.getStoreId() + ""),
                                    parentScore,
                                    Long.parseLong(order.getId() + ""),
                                    ScoreVipDetail.SCORE_VIP_TYPE_CASH,
                                    "推荐消费奖励"
                            );
                        }
                    }
                    //推荐注册二级带新积分奖励
                    if (referrerVip.getRecommendId() != null && StringUtils.isNotBlank(cashConsumptionShop.getParamValue2())) {
                        SysVipInfo topVipInfo = sysVipInfoDao.selectById(referrerVip.getRecommendId());
                        int topParentScore = order.getOrderMoney().divide(new BigDecimal(cashConsumptionShop.getParamValue2())).intValue();
                        if (topParentScore > 0) {
                            scoreVipDetailService.addScore(
                                    topVipInfo.getId(),
                                    null,
                                    Long.parseLong(order.getStoreId() + ""),
                                    topParentScore,
                                    Long.parseLong(order.getId() + ""),
                                    ScoreVipDetail.SCORE_VIP_TYPE_CASH,
                                    "推荐消费奖励"
                            );
                        }
                    }
                }
            }
        }
        if(addScore>0){
            //插入新的积分
            scoreVipDetailService.addScore(
                    order.getUserId(),null,
                    null,
                    Long.parseLong(order.getStoreId()+""),
                    addScore,
                    Long.parseLong(orderId+""),
                    ScoreVipDetail.SCORE_VIP_TYPE_CASH,
                    "微商城消费");
        }
    }