jyy
2021-03-08 27e4ff33760c5ff07cf844311505344b06343396
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.matrix.system.shopXcx.mqTask;
 
 
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.service.BizUserService;
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.*;
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.shopXcx.bean.ShopOrder;
import com.matrix.system.shopXcx.bean.ShopOrderDetails;
import com.matrix.system.shopXcx.bean.ShopSku;
import com.matrix.system.shopXcx.dao.ShopOrderDao;
import com.matrix.system.shopXcx.dao.ShopOrderDetailsDao;
import com.matrix.system.shopXcx.dao.ShopSkuDao;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
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;
 
/**
 * 微商城订单同步到erp系统
 */
@Component
public class OrderTask implements DeliverCallback {
 
 
    @Autowired
    ShopOrderDao shopOrderDao;
    @Autowired
    ShopOrderDetailsDao shopOrderDetailsDao;
 
    @Autowired
    BizUserService bizUserService;
 
 
    @Autowired
    private SysVipInfoDao vipDap;
 
    @Autowired
    SysVipInfoDao vipDao;
 
    @Autowired
    CodeService codeService;
 
    @Autowired
    SysOrderDao sysOrderDao;
 
    @Autowired
    ShoppingGoodsService shoppingGoodsService;
 
    @Autowired
    SysOrderItemDao orderItemDao;
 
    @Autowired
    SysOrderService orderService;
 
    @Autowired
    ShopSkuDao shopSkuDao;
 
    @Autowired
    BusParameterSettingsDao parameterSettingsDao;
 
    @Autowired
    private SysOrderFlowDao sysOrderFlowDao;
 
    @Autowired
    private ShoppingGoodsDao shoppingGoodsDao;
 
    @Transactional(rollbackFor = Exception.class)
    public void createOrder(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.setCompanyId(vipInfo.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);
        // 创建订单明细,并计算总价与折扣总价
        // 总价
        double total = 0;
        double zkTotal = 0;
        int count = 0;
        // 设置订单条目
        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());
 
            // 设置对应产品的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);
 
    }
 
 
 
 
    @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);
 
    }
}