935090232@qq.com
2022-02-20 c253b555c7905c5136d47cd615ef545fa50cc6ad
zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java
@@ -1,23 +1,20 @@
package com.matrix.system.shopXcx.mqTask;
import com.matrix.biz.bean.BizUser;
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.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.hive.bean.ShoppingGoods;
import com.matrix.system.hive.bean.SysOrder;
import com.matrix.system.hive.bean.SysOrderItem;
import com.matrix.system.hive.bean.SysVipInfo;
import com.matrix.system.hive.dao.SysOrderDao;
import com.matrix.system.hive.dao.SysOrderItemDao;
import com.matrix.system.hive.dao.SysVipInfoDao;
import com.matrix.system.fenxiao.dao.ShopSalesmanGradeDao;
import com.matrix.system.fenxiao.dao.ShopSalesmanOrderDao;
import com.matrix.system.hive.bean.*;
import com.matrix.system.hive.dao.*;
import com.matrix.system.hive.service.CodeService;
import com.matrix.system.hive.service.ShoppingGoodsService;
import com.matrix.system.hive.service.SysOrderService;
import com.matrix.system.hive.service.SysVipInfoService;
import com.matrix.system.shopXcx.bean.ShopOrder;
import com.matrix.system.shopXcx.bean.ShopOrderDetails;
import com.matrix.system.shopXcx.bean.ShopSku;
@@ -26,17 +23,21 @@
import com.matrix.system.shopXcx.dao.ShopSkuDao;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import org.apache.logging.log4j.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 微商城订单同步到erp系统
 */
public class OrderTask implements DeliverCallback {
@Component
public class OrderTask implements MessageHandler {
    @Autowired
@@ -45,7 +46,7 @@
    ShopOrderDetailsDao shopOrderDetailsDao;
    @Autowired
    BizUserService bizUserService;
    SysVipInfoService sysVipInfoService;
    @Autowired
@@ -75,12 +76,33 @@
    @Autowired
    BusParameterSettingsDao parameterSettingsDao;
    @Transactional(rollbackFor = Exception.class)
    public void createOrder(ShopOrder orderDto) {
    @Autowired
    private SysOrderFlowDao sysOrderFlowDao;
        BizUser bizUser = bizUserService.findByOpenId(orderDto.getUserId());
    @Autowired
    private ShoppingGoodsDao shoppingGoodsDao;
    @Autowired
    private ShopSalesmanOrderDao shopSalesmanOrderDao;
    @Autowired
    private ShopSalesmanGradeDao shopSalesmanGradeDao;
    @Transactional(rollbackFor = Exception.class)
    public void synchronizationOrderToErp(ShopOrder orderDto) {
        //判断是否存在需要同步的产品,只有绑定了erp中产品的才同步
        boolean needTb=false;
        for (ShopOrderDetails orderItemDto : orderDto.getDetails()) {
            ShopSku shopSku = shopSkuDao.selectById(orderItemDto.getsId());
            if (StringUtils.isNotBlank(shopSku.getStockCode())) {
                needTb=true;
            }
        }
        if(needTb){
            SysVipInfo vipInfo = sysVipInfoService.findById(orderDto.getUserId());
        SysOrder order = new SysOrder();
        SysVipInfo vipInfo = vipDao.selectByPhone(bizUser.getPhoneNumber());
        if(SysVipInfo.UNDEAL_VIP==vipInfo.getIsDeal()){
            //非成交客户下单更新客户为成交客户
            vipInfo.setIsDeal(SysVipInfo.DEAL_VIP);
@@ -88,6 +110,7 @@
        }
        //获取订单归属门店
            order.setCompanyId(orderDto.getCompanyId());
        order.setShopId(Long.parseLong(orderDto.getStoreId()+""));
        //同步的订单订单编号保持一致
        order.setOrderNo(orderDto.getOrderNo());
@@ -101,12 +124,10 @@
        order.setCashPay(orderDto.getOrderMoney().doubleValue());
        order.setTotal(orderDto.getOrderMoney().doubleValue());
        order.setZkTotal(orderDto.getOrderMoney().doubleValue());
        order.setArrears(0);
            order.setPayTime(new Date());
            order.setArrears(0D);
        int i = sysOrderDao.insert(order);
        // 创建订单明细,并计算总价与折扣总价
        // 总价
        double total = 0;
        double zkTotal = 0;
        int count = 0;
        // 设置订单条目
        for (ShopOrderDetails orderItemDto : orderDto.getDetails()) {
@@ -133,11 +154,6 @@
            orderItem.setPrice(orderItemDto.getPrice().doubleValue());
            orderItem.setZkPrice(orderItemDto.getPrice().doubleValue());
            orderItem.setGoodsId(shoppingGoods.getId());
            if (shoppingGoods.getGoodType().equals(Dictionary.SHOPPING_GOODS_TYPE_JJCP)) {
                orderItem.setAssembleId(shoppingGoods.getAssembleGoods().get(0).getId());
            } else if (shoppingGoods.getGoodType().equals(Dictionary.SHOPPING_GOODS_TYPE_XM)) {
                orderItem.setAssembleId(shoppingGoods.getAssembleProj().get(0).getId());
            }
            // 设置对应产品的id
            switch (shoppingGoods.getGoodType()) {
@@ -193,22 +209,53 @@
        // 设置销量
        orderService.setShopSelCount(sourceOrder);
            SysOrderFlow flow = new SysOrderFlow();
            flow.setFlowNo(codeService.getFlowCode() + "-" + i);
            Long goodsId = sourceOrder.getItems().get(0).getGoodsId();
            ShoppingGoods goods = shoppingGoodsDao.selectById(goodsId);
            flow.setFlowContent(goods.getName() + "等" + sourceOrder.getItems().size() + "件产品");
            flow.setOrderId(sourceOrder.getId());
            flow.setVipId(sourceOrder.getVipId());
            flow.setFlowType(SysOrderFlow.FLOW_TYPE_BUY);
            flow.setAmount(orderDto.getOrderMoney());
            flow.setPayMethod("微信");
            flow.setShopId(sourceOrder.getShopId());
            flow.setCompanyId(sourceOrder.getCompanyId());
            sysOrderFlowDao.insert(flow);
        }else{
            LogUtil.info("不存在需要同步的产品");
    }
    }
    @Override
    public void handle(String consumerTag, Delivery message) throws IOException {
    public String getName() {
        return "微商城订单同步到erp系统";
    }
            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));
            //获取订单详情
            List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId));
            order.setDetails(orderDetails);
            createOrder(order);
        //同步订单到erp
        synchronizationOrderToErp(order);
    }
}