#
luxiaotao1123
2021-03-02 a2613d9aaaafecf790db9a3a94f965dc5d1ba41e
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
package zy.cloud.wms.common.service.erp;
 
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
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.GetDataResult;
import zy.cloud.wms.manager.entity.CustOrder;
import zy.cloud.wms.manager.service.CustOrderService;
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";
 
    @Autowired
    private CustOrderService custOrderService;
 
    /**
     * 持久化销售订单
     */
//    @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.error("请求:{}\nError,响应结果为空!", URI + GET_ORDERS);
            } else {
                log.info(response);
                Date now = new Date();
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return;
                }
                List<GetDataResult> list = JSON.parseArray(result.getData(), GetDataResult.class);
                if (!Cools.isEmpty(list)) {
                    boolean complete = true;
                    for (GetDataResult 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(GetDataResult::getNumber).distinct().collect(Collectors.toList());
                        for (String number : collect) {
                            custOrderService.updateStatus(number, 1);
                        }
                    }
 
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
 
 
}