#
luxiaotao1123
2021-03-12 6a1173f27140d908513aef29f6d7240a452edb17
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package zy.cloud.wms.common.service.erp;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import zy.cloud.wms.common.service.erp.entity.GetBasisResult;
import zy.cloud.wms.common.service.erp.entity.GetOrderResult;
import zy.cloud.wms.manager.entity.Cstmr;
import zy.cloud.wms.manager.entity.CustOrder;
import zy.cloud.wms.manager.entity.Mat;
import zy.cloud.wms.manager.service.CstmrService;
import zy.cloud.wms.manager.service.CustOrderService;
import zy.cloud.wms.manager.service.LocDetlService;
import zy.cloud.wms.manager.service.MatService;
import zy.cloud.wms.manager.utils.HttpHandler;
 
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * erp任务控制器
 * Created by vincent on 2020/11/27
 */
@Slf4j
@Component
public class ErpScheduler {
 
    public static final String URI = "http://8.133.182.21:8080/api";
    public static final String GET_ORDERS = "cM/basis/getOrders";
    public static final String GET_BASIS = "cM/basis/getBasis";
    public static final String UPLOAD_STOCK = "cM/basis/uploadStock";
 
    @Autowired
    private CustOrderService custOrderService;
    @Autowired
    private MatService matService;
    @Autowired
    private CstmrService cstmrService;
    @Autowired
    private LocDetlService locDetlService;
 
    /**
     * 商品下载
     */
    //    @Scheduled(cron = "0/5 * * * * ? ")
//    @PostConstruct
    public void getBasis1Execute(){
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("rec", 0);
            param.put("Flag", 1);
            String response = new HttpHandler.Builder()
                    .setUri(URI)
                    .setPath(GET_BASIS)
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                log.info(response);
                Date now = new Date();
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return;
                }
                List<GetBasisResult> list = JSON.parseArray(result.getData(), GetBasisResult.class);
                for (GetBasisResult data : list) {
                    Mat mat = matService.selectByMatnr(data.getUserCode());
                    String modifyDate = data.getModifyDate();
                    Date updateTime = null;
                    if (!Cools.isEmpty(modifyDate)) {
                        updateTime = DateUtils.convert(data.getModifyDate());
                    }
                    if (null == mat) {
                        mat = new Mat(
                                String.valueOf(data.getRec()),    // 编号
                                null,    // 所属区域
                                null,    // 所属归类
                                data.getUserCode(),    // 商品编号
                                data.getFullName(),    // 商品名称
                                null,    // 名称
                                null,    // 规格
                                null,    // 型号
                                null,    // 批号
                                null,    // 单位
                                null,    // 条码
                                null,    // 单据类型
                                null,    // 单据编号
                                null,    // 客户名称
                                null,    // 品项数
                                null,    // 库存余量
                                null,    // 重量
                                1,    //
                                null,    // 添加人员
                                now,    // 添加时间
                                null,    // 修改人员
                                updateTime,    // 修改时间
                                null    // 备注
                        );
                        matService.insert(mat);
                    } else {
                        mat.setUuid(String.valueOf(data.getRec()));
                        mat.setMatnr(data.getUserCode());
                        mat.setMaktx(data.getFullName());
                        mat.setUpdateTime(updateTime);
                        matService.updateById(mat);
                    }
                    log.info("====>> 更新物料:{}", data.getUserCode());
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 客户下载
     */
    //    @Scheduled(cron = "0/5 * * * * ? ")
//    @PostConstruct
    public void getBasis2Execute(){
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("rec", 0);
            param.put("Flag", 2);
            String response = new HttpHandler.Builder()
                    .setUri(URI)
                    .setPath(GET_BASIS)
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                log.info(response);
                Date now = new Date();
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return;
                }
                List<GetBasisResult> list = JSON.parseArray(result.getData(), GetBasisResult.class);
                for (GetBasisResult data : list) {
                    Cstmr cstmr = cstmrService.selectOne(new EntityWrapper<Cstmr>().eq("uuid", data.getUserCode()));
                    String modifyDate = data.getModifyDate();
                    Date updateTime = null;
                    if (!Cools.isEmpty(modifyDate)) {
                        updateTime = DateUtils.convert(data.getModifyDate());
                    }
                    if (null == cstmr) {
                        cstmr = new Cstmr(
                                data.getUserCode(),    // 客户编号[非空]
                                data.getFullName(),    // 客户名称
                                null,    // 联系人
                                null,    // 联系电话
                                null,    // 联系地址
                                1,    // 状态
                                null,    // 添加人员
                                now,    // 添加时间
                                null,    // 修改人员
                                updateTime,    // 修改时间
                                String.valueOf(data.getRec())    // 备注
                        );
                        cstmrService.insert(cstmr);
                    } else {
                        cstmr.setUuid(data.getUserCode());
                        cstmr.setName(data.getFullName());
                        cstmr.setUpdateTime(updateTime);
                        cstmr.setMemo(String.valueOf(data.getRec()));
                        cstmrService.updateById(cstmr);
                    }
                    log.info("====>> 更新客户资料:{}", data.getUserCode());
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 持久化销售订单
     */
//    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void getOrdersExecute(){
        try {
            Map<String, Object> json = new HashMap<>();
            json.put("vchType", 41);
            String response = new HttpHandler.Builder()
                    .setUri(URI)
                    .setPath(GET_ORDERS)
                    .setJson(JSON.toJSONString(json))
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                log.info(response);
                Date now = new Date();
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return;
                }
                List<GetOrderResult> list = JSON.parseArray(result.getData(), GetOrderResult.class);
                if (!Cools.isEmpty(list)) {
                    boolean complete = true;
                    for (GetOrderResult data : list) {
                        CustOrder custOrder = new CustOrder(
                                data.getNumber(),    // 销售单号
                                data.getBillDate(),    // 单据日期[非空]
                                data.getBTypeID(),    // 客户编号
                                data.getETypeID(),    // 经手人编号[非空]
                                data.getUserCode(),    // 商品编号
                                data.getQty(),    // 商品数量
                                data.getPrice(),    // 商品单价
                                data.getComment(),    // 商品备注[非空]
                                0,    // 状态
                                now,    // 添加时间
                                now,    // 修改时间
                                null    // 备注
                        );
                        boolean insert = custOrderService.insert(custOrder);
                        if (!insert) {
                            complete = false;
                            log.error("保存销售订单失败!");
                        }
                    }
                    if (complete) {
                        List<String> collect = list.stream().map(GetOrderResult::getNumber).distinct().collect(Collectors.toList());
                        for (String number : collect) {
                            custOrderService.updateStatus(number, 1);
                        }
                    }
 
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
 
    /**
     * 更新库存
     */
//    @Scheduled(cron = "0/3 * * * * ? ")
    public void uploadStockExecute(){
        try {
 
//            locDetlService
 
            Map<String, Object> param = new HashMap<>();
            param.put("rec", 0);
            param.put("Flag", 1);
            String response = new HttpHandler.Builder()
                    .setUri(URI)
                    .setPath(GET_BASIS)
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                log.info(response);
                Date now = new Date();
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return;
                }
                List<GetBasisResult> list = JSON.parseArray(result.getData(), GetBasisResult.class);
                for (GetBasisResult data : list) {
                    Mat mat = matService.selectByMatnr(data.getUserCode());
                    String modifyDate = data.getModifyDate();
                    Date updateTime = null;
                    if (!Cools.isEmpty(modifyDate)) {
                        updateTime = DateUtils.convert(data.getModifyDate());
                    }
                    if (null == mat) {
                        mat = new Mat(
                                String.valueOf(data.getRec()),    // 编号
                                null,    // 所属区域
                                null,    // 所属归类
                                data.getUserCode(),    // 商品编号
                                data.getFullName(),    // 商品名称
                                null,    // 名称
                                null,    // 规格
                                null,    // 型号
                                null,    // 批号
                                null,    // 单位
                                null,    // 条码
                                null,    // 单据类型
                                null,    // 单据编号
                                null,    // 客户名称
                                null,    // 品项数
                                null,    // 库存余量
                                null,    // 重量
                                1,    //
                                null,    // 添加人员
                                now,    // 添加时间
                                null,    // 修改人员
                                updateTime,    // 修改时间
                                null    // 备注
                        );
                        matService.insert(mat);
                    } else {
                        mat.setUuid(String.valueOf(data.getRec()));
                        mat.setMatnr(data.getUserCode());
                        mat.setMaktx(data.getFullName());
                        mat.setUpdateTime(updateTime);
                        matService.updateById(mat);
                    }
                    log.info("====>> 更新物料:{}", data.getUserCode());
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
    }
 
 
}