jyy
2021-03-22 1d629038767eb756a4823b4e8b11764ac949b63d
zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java
@@ -3,15 +3,18 @@
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.service.BizUserService;
import com.matrix.core.constance.MatrixConstance;
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.fenxiao.dao.ShopSalesmanGradeDao;
import com.matrix.system.fenxiao.dao.ShopSalesmanOrderDao;
import com.matrix.system.fenxiao.entity.ShopSalesmanGrade;
import com.matrix.system.fenxiao.entity.ShopSalesmanOrder;
import com.matrix.system.hive.bean.*;
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.hive.dao.*;
import com.matrix.system.hive.service.CodeService;
import com.matrix.system.hive.service.ShoppingGoodsService;
import com.matrix.system.hive.service.SysOrderService;
@@ -23,17 +26,19 @@
import com.matrix.system.shopXcx.dao.ShopSkuDao;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import org.springframework.beans.BeanUtils;
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.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 微商城订单同步到erp系统
 */
@Component
public class OrderTask implements DeliverCallback {
@@ -73,132 +78,161 @@
    @Autowired
    BusParameterSettingsDao parameterSettingsDao;
    @Autowired
    private SysOrderFlowDao sysOrderFlowDao;
    @Autowired
    private ShoppingGoodsDao shoppingGoodsDao;
    @Autowired
    private ShopSalesmanOrderDao shopSalesmanOrderDao;
    @Autowired
    private ShopSalesmanGradeDao shopSalesmanGradeDao;
    @Transactional(rollbackFor = Exception.class)
    public void createOrder(ShopOrder orderDto) {
    public void synchronizationOrderToErp(ShopOrder orderDto) {
        BizUser bizUser = bizUserService.findByOpenId(orderDto.getUserId());
        SysOrder order = new SysOrder();
        SysVipInfo vipInfo = vipDao.selectByPhone(bizUser.getPhoneNumber());
        if(SysVipInfo.UNDEAL_VIP==vipInfo.getIsDeal()){
            //非成交客户下单更新客户为成交客户
            vipInfo.setIsDeal(SysVipInfo.DEAL_VIP);
            vipDao.update(vipInfo);
        }
        //获取订单归属门店
        order.setShopId(Long.parseLong(orderDto.getStoreId()+""));
        //同步的订单订单编号保持一致
        order.setOrderNo(orderDto.getOrderNo());
        order.setVipId(vipInfo.getId());
        order.setOrderTime(new Date());
        order.setRemark(AppConstance.WX_ORDER_FLAG);
        order.setStaffId(vipInfo.getStaffId());
        order.setIsCross(2 + "");
        order.setStatu(Dictionary.ORDER_STATU_YFK);
        order.setCardPay(0.00);
        order.setCashPay(orderDto.getOrderMoney().doubleValue());
        order.setTotal(orderDto.getOrderMoney().doubleValue());
        order.setZkTotal(orderDto.getOrderMoney().doubleValue());
        order.setArrears(0);
        int i = sysOrderDao.insert(order);
        // 创建订单明细,并计算总价与折扣总价
        // 总价
        double total = 0;
        double zkTotal = 0;
        int count = 0;
        // 设置订单条目
        //判断是否存在需要同步的产品,只有绑定了erp中产品的才同步
        boolean needTb=false;
        for (ShopOrderDetails orderItemDto : orderDto.getDetails()) {
            ShopSku shopSku = shopSkuDao.selectById(orderItemDto.getsId());
            if (StringUtils.isBlank(shopSku.getStockCode())) {
                LogUtil.warn("销售产品未绑定erp中的产品,无法进行同步");
            }
            String goodsCode =shopSku.getStockCode();
            ShoppingGoods shoppingGoods = shoppingGoodsService.findById(Long.parseLong(goodsCode));
            if (shoppingGoods == null) {
                LogUtil.warn("无效的商品id{}", shopSku.getAtrid());
            }
            SysOrderItem orderItem = new SysOrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setCount(orderItemDto.getCount());
            orderItem.setIsFree(orderItemDto.getPrice().doubleValue() > 0 ? "否" : "是");
            orderItem.setType(shoppingGoods.getGoodType());
            orderItem.setStatus(Dictionary.ORDER_STATU_YFK);
            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()) {
                case Dictionary.SHOPPING_GOODS_TYPE_JJCP:
                    orderItemDao.insert(orderItem);
                    break;
                // 购买的是单个项目
                case Dictionary.SHOPPING_GOODS_TYPE_XM:
                    orderItemDao.insert(orderItem);
                    break;
                case Dictionary.SHOPPING_GOODS_TYPE_TC:
                    // 每一个套餐都看成一个单独的订单条目
                    count = orderItem.getCount();
                    for (int j = 0; j < count; j++) {
                        orderItem.setCount(1);
                        orderItem.setId(null);
                        orderItemDao.insert(orderItem);
                    }
                    break;
                case Dictionary.SHOPPING_GOODS_TYPE_CZK:
                    // 新增明细
                    orderItem.setGoodsId(shoppingGoods.getId());
                    // 每一个充值卡都看成一个单独的订单条目
                    count = orderItem.getCount();
                    for (int j = 0; j < count; j++) {
                        orderItem.setCount(1);
                        orderItem.setId(null);
                        orderItemDao.insert(orderItem);
                    }
                    break;
                case Dictionary.SHOPPING_GOODS_TYPE_ZHK:
                    // 新增明细
                    orderItem.setGoodsId(shoppingGoods.getId());
                    // 每一个充值卡都看成一个单独的订单条目
                    count = orderItem.getCount();
                    for (int j = 0; j < count; j++) {
                        orderItem.setCount(1);
                        orderItem.setId(null);
                        orderItemDao.insert(orderItem);
                    }
                    break;
            if (StringUtils.isNotBlank(shopSku.getStockCode())) {
                needTb=true;
            }
        }
        // 处理收款逻辑
        SysOrder sourceOrder = sysOrderDao.selectById(order.getId());
        sourceOrder.setItems(orderItemDao.selectByOrderId(order.getId()));
        // 设置会员充值卡使用情况
        orderService.addMoneyCardUse(sourceOrder);
        if(needTb){
        // 改变客户项目套餐使用情况
        orderService.addTaocanProj(sourceOrder);
            BizUser bizUser = bizUserService.findByOpenId(orderDto.getUserId());
            SysOrder order = new SysOrder();
            //TODO 根据公司id区分
            SysVipInfo vipInfo = vipDao.selectByPhone(bizUser.getPhoneNumber());
            if(SysVipInfo.UNDEAL_VIP==vipInfo.getIsDeal()){
                //非成交客户下单更新客户为成交客户
                vipInfo.setIsDeal(SysVipInfo.DEAL_VIP);
                vipDao.update(vipInfo);
            }
        // 设置销量
        orderService.setShopSelCount(sourceOrder);
            //获取订单归属门店
            order.setCompanyId(orderDto.getCompanyId());
            order.setShopId(Long.parseLong(orderDto.getStoreId()+""));
            //同步的订单订单编号保持一致
            order.setOrderNo(orderDto.getOrderNo());
            order.setVipId(vipInfo.getId());
            order.setOrderTime(new Date());
            order.setRemark(AppConstance.WX_ORDER_FLAG);
            order.setStaffId(vipInfo.getStaffId());
            order.setIsCross(2 + "");
            order.setStatu(Dictionary.ORDER_STATU_YFK);
            order.setCardPay(0.00);
            order.setCashPay(orderDto.getOrderMoney().doubleValue());
            order.setTotal(orderDto.getOrderMoney().doubleValue());
            order.setZkTotal(orderDto.getOrderMoney().doubleValue());
            order.setPayTime(new Date());
            order.setArrears(0D);
            int i = sysOrderDao.insert(order);
            // 创建订单明细,并计算总价与折扣总价
            int count = 0;
            // 设置订单条目
            for (ShopOrderDetails orderItemDto : orderDto.getDetails()) {
        SysOrderFlow flow = new SysOrderFlow();
        flow.setAmount(orderDto.getOrderMoney());
        flow.setPayMethod("微信");
        List<SysOrderFlow> flows = new ArrayList<>();
        flows.add(flow);
        sourceOrder.setFlows(flows);
        orderService.updateAfterMoney(sourceOrder);
                ShopSku shopSku = shopSkuDao.selectById(orderItemDto.getsId());
                if (StringUtils.isBlank(shopSku.getStockCode())) {
                    LogUtil.warn("销售产品未绑定erp中的产品,无法进行同步");
                }
                String goodsCode =shopSku.getStockCode();
                ShoppingGoods shoppingGoods = shoppingGoodsService.findById(Long.parseLong(goodsCode));
                if (shoppingGoods == null) {
                    LogUtil.warn("无效的商品id{}", shopSku.getAtrid());
                }
                SysOrderItem orderItem = new SysOrderItem();
                orderItem.setOrderId(order.getId());
                orderItem.setCount(orderItemDto.getCount());
                orderItem.setIsFree(orderItemDto.getPrice().doubleValue() > 0 ? "否" : "是");
                orderItem.setType(shoppingGoods.getGoodType());
                orderItem.setStatus(Dictionary.ORDER_STATU_YFK);
                orderItem.setPrice(orderItemDto.getPrice().doubleValue());
                orderItem.setZkPrice(orderItemDto.getPrice().doubleValue());
                orderItem.setGoodsId(shoppingGoods.getId());
                // 设置对应产品的id
                switch (shoppingGoods.getGoodType()) {
                    case Dictionary.SHOPPING_GOODS_TYPE_JJCP:
                        orderItemDao.insert(orderItem);
                        break;
                    // 购买的是单个项目
                    case Dictionary.SHOPPING_GOODS_TYPE_XM:
                        orderItemDao.insert(orderItem);
                        break;
                    case Dictionary.SHOPPING_GOODS_TYPE_TC:
                        // 每一个套餐都看成一个单独的订单条目
                        count = orderItem.getCount();
                        for (int j = 0; j < count; j++) {
                            orderItem.setCount(1);
                            orderItem.setId(null);
                            orderItemDao.insert(orderItem);
                        }
                        break;
                    case Dictionary.SHOPPING_GOODS_TYPE_CZK:
                        // 新增明细
                        orderItem.setGoodsId(shoppingGoods.getId());
                        // 每一个充值卡都看成一个单独的订单条目
                        count = orderItem.getCount();
                        for (int j = 0; j < count; j++) {
                            orderItem.setCount(1);
                            orderItem.setId(null);
                            orderItemDao.insert(orderItem);
                        }
                        break;
                    case Dictionary.SHOPPING_GOODS_TYPE_ZHK:
                        // 新增明细
                        orderItem.setGoodsId(shoppingGoods.getId());
                        // 每一个充值卡都看成一个单独的订单条目
                        count = orderItem.getCount();
                        for (int j = 0; j < count; j++) {
                            orderItem.setCount(1);
                            orderItem.setId(null);
                            orderItemDao.insert(orderItem);
                        }
                        break;
                }
            }
            // 处理收款逻辑
            SysOrder sourceOrder = sysOrderDao.selectById(order.getId());
            sourceOrder.setItems(orderItemDao.selectByOrderId(order.getId()));
            // 设置会员充值卡使用情况
            orderService.addMoneyCardUse(sourceOrder);
            // 改变客户项目套餐使用情况
            orderService.addTaocanProj(sourceOrder);
            // 设置销量
            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("不存在需要同步的产品");
        }
    }
@@ -208,14 +242,16 @@
    @Override
    public void handle(String consumerTag, Delivery message) throws IOException {
            String orderId = new String(message.getBody(), "UTF-8");
            LogUtil.debug("收到创建订单任务orderId={}", orderId);
            //获取订单信息
            ShopOrder order = shopOrderDao.selectById(Integer.valueOf(orderId));
            //获取订单详情
            List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId));
            order.setDetails(orderDetails);
            createOrder(order);
        String orderId = new String(message.getBody(), "UTF-8");
        LogUtil.debug("收到创建订单任务orderId={}", orderId);
        //获取订单信息
        ShopOrder order = shopOrderDao.selectById(Integer.valueOf(orderId));
        //获取订单详情
        List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId));
        order.setDetails(orderDetails);
        //同步订单到erp
        synchronizationOrderToErp(order);
    }
}