王佳豪
2021-06-19 72ca7383704a906f654731a68a06e9d284eb51ee
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
package com.zy.common.service.erp.task;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.MatCode;
import com.zy.asrs.entity.WaitPakin;
import com.zy.asrs.service.MatCodeService;
import com.zy.asrs.service.OutStockMainService;
import com.zy.asrs.service.OutStockService;
import com.zy.asrs.service.WaitPakinService;
import com.zy.asrs.utils.VersionUtils;
import com.zy.common.service.erp.ErpService;
import com.zy.common.service.erp.dto.InStockDto;
import com.zy.common.service.erp.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * erp任务控制器
 * Created by vincent on 2020/11/27
 */
@Slf4j
@Component
public class ErpScheduler {
 
    @Autowired
    private ErpService erpService;
    @Autowired
    private MatCodeService matCodeService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private OutStockMainService outStockMainService;
    @Autowired
    private OutStockService outStockService;
 
    private Map<Integer, M_item> itemMap = new HashMap<>();
 
    /**
     * 原材料
     */
    @Scheduled(cron = "0/5 * * * * ? ")
//    @PostConstruct
    public void inStockExecute(){
//        List<M_item> item = erpService.getItem();
//        for (M_item item1 : item) {
//            itemMap.put(item1.getMitemid(), item1);
//        }
 
        List<InStockDto> readyInStock = erpService.getReadyInStock();
        for (InStockDto dto : readyInStock) {
            for (InStockBillEntry entry : dto.getInStockBillEntries()) {
//                if (entry.getFNumber() == null && itemMap.containsKey(entry.getFItemID())) {
//                    entry.setFNumber(itemMap.get(entry.getFItemID()).getMnumber());
//                    boolean b = erpService.updateFnumber(entry.getFItemID(), entry.getFNumber());
//                    if (b) {
//                        System.err.println(entry.getFItemID() + ": " + entry.getFNumber());
//                    }
//                }
 
                MatCode matCode = matCodeService.selectById(entry.getFnumber());
                if (null != matCode) {
                    // 生成入库通知单
                    WaitPakin waitPakin = new WaitPakin();
                    waitPakin.setMemo("原材料");
                    waitPakin.setStatus("Y");    // 状态
                    waitPakin.setIoStatus("N");     // 入出状态
                    waitPakin.setAppeTime(new Date());
                    waitPakin.setModiTime(new Date());
                    VersionUtils.setWaitPakIn(waitPakin, matCode);
                    waitPakin.setMnemonic(entry.getFOrderBillNo()); // todo:luxiaotao 生产单号
                    waitPakin.setSupplier(dto.getInStockBill().getFBillNo()); // todo:luxiaotao 通知单号
                    Wrapper<WaitPakin> wrapper = new EntityWrapper<WaitPakin>()
                            .eq("supplier", waitPakin.getSupplier())
                            .eq("matnr", waitPakin.getMatnr())
                            .isNull("zpallet");
                    WaitPakin one = waitPakinService.selectOne(wrapper);
                    try {
                        if (one == null) {
                            waitPakin.setAnfme(entry.getFAuxQty());
                            if (!waitPakinService.insert(waitPakin)) {
                                log.error("{}原材料入库通知单的{}物料数据处理失败", waitPakin.getSupplier(), waitPakin.getMatnr());
                            }
                        }
//                        else {
//                            waitPakin.setAnfme(one.getAnfme() + entry.getFAuxQty());
//                            waitPakinService.update(waitPakin, wrapper);
//                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println(JSON.toJSON(waitPakin));
                    }
                }
            }
            if (!erpService.haveReadInStock(dto.getInStockBill().getFBillNo())) {
                log.error("{}原材料入库通知单已读失败", dto.getInStockBill().getFBillNo());
            }
        }
 
    }
 
 
    /**
     * 成品
     */
    @Scheduled(cron = "0/5 * * * * ? ")
//    @PostConstruct
    public void inCPICMOExecute(){
//        List<M_item> item = erpService.getItem();
//        for (M_item item1 : item) {
//            itemMap.put(item1.getMitemid(), item1);
//        }
 
        List<CPICMO> cpicmos = erpService.getReadyCPICMO();
        for (CPICMO cpicmo : cpicmos) {
//                if (Cools.isEmpty(cpicmo.getFnumber()) && itemMap.containsKey(cpicmo.getFItemID())) {
//                    cpicmo.setFnumber(itemMap.get(cpicmo.getFItemID()).getMnumber());
//                    boolean b = erpService.updateCFnumber(cpicmo.getFItemID(), cpicmo.getFnumber());
//                    if (b) {
//                        System.err.println(cpicmo.getFItemID() + ": " + cpicmo.getFnumber());
//                    }
//                }
            MatCode matCode = matCodeService.selectById(cpicmo.getFnumber());
            if (null != matCode) {
                // 生成入库通知单
                WaitPakin waitPakin = new WaitPakin();
                waitPakin.setMemo("成品");
                waitPakin.setStatus("Y");    // 状态
                waitPakin.setIoStatus("N");     // 入出状态
                waitPakin.setAppeTime(new Date());
                waitPakin.setModiTime(new Date());
                VersionUtils.setWaitPakIn(waitPakin, matCode);
                waitPakin.setMnemonic(cpicmo.getFSourceBillNo()); // todo:luxiaotao 生产单号
                waitPakin.setSupplier(cpicmo.getFBillNo()); // todo:luxiaotao 通知单号
                Wrapper<WaitPakin> wrapper = new EntityWrapper<WaitPakin>()
                        .eq("supplier", waitPakin.getSupplier())
                        .eq("matnr", waitPakin.getMatnr())
                        .isNull("zpallet");
                WaitPakin one = waitPakinService.selectOne(wrapper);
                if (one == null) {
                    waitPakin.setAnfme(cpicmo.getFAuxQty());
                    if (!waitPakinService.insert(waitPakin)) {
                        log.error("{}成品入库通知单的{}物料数据处理失败", waitPakin.getSupplier(), waitPakin.getMatnr());
                    }
                }
                if (!erpService.haveReadCPICMO(cpicmo.getFBillNo())) {
                    log.error("{}成品入库通知单已读失败", cpicmo.getFBillNo());
                }
            }
        }
    }
 
    @Scheduled(cron = "0/5 * * * * ? ")
    public void syncMat(){
        List<M_item> items = erpService.getItem();
        for (M_item item : items) {
            MatCode matCode = matCodeService.selectById(item.getMnumber());
            if (matCode == null) {
                matCode = new MatCode();
                VersionUtils.setMatCode(matCode, item);
                matCode.setAppeTime(new Date());
                matCode.setModiTime(new Date());
                boolean insert = matCodeService.insert(matCode);
                if (insert) {
                    log.info("=====>> 新增物料成功 : {}", JSON.toJSONString(item));
                } else {
                    log.error("=====>> 新增物料失败!!! : {}", JSON.toJSONString(item));
                }
            } else {
                VersionUtils.setMatCode(matCode, item);
                matCode.setModiTime(new Date());
                boolean b = matCodeService.updateById(matCode);
                if (b) {
                    log.info("=====>> 更新物料成功 : {}", JSON.toJSONString(item));
                } else {
                    log.error("=====>> 更新物料失败!!! : {}", JSON.toJSONString(item));
                }
            }
            // 更新完之后删除
            erpService.deleteItem(item.getMnumber());
        }
    }
 
    /**
     * 成品出库-发货通知单表头
     */
    @Scheduled(cron = "0/15 * * * * ? ")
    public void syncOutStock(){
        List<OutStockBill> result = erpService.syncOutStock();
        for (Integer i = 0; i < result.size(); i++) {
            OutStockBill main = outStockMainService.selectOne(new EntityWrapper<OutStockBill>().eq("FBrNo", result.get(i).getFBrNo()).and().eq("FInterID", result.get(i).getFInterID()));
            if (null == main) {
                if (!outStockMainService.insert(result.get(i))) {
                    log.info("出库通知主表OutStockBill同步失败");
                };
            } else {
 
            }
        }
    }
 
    /**
     * 成品出库-发货通知单表体
     */
    @Scheduled(cron = "0/15 * * * * ? ")
    public void syncOutStockDetail(){
        List<OutStockBillEntry> result = erpService.syncOutStockDetail();
        for (Integer i = 0; i < result.size(); i++) {
            OutStockBillEntry main = outStockService.selectOne(new EntityWrapper<OutStockBillEntry>().eq("FInterID", result.get(i).getFInterID()).and().eq("FEntryID", result.get(i).getFEntryID()));
            if (null == main) {
                if (!outStockService.insert(result.get(i))) {
                    log.info("出库通知明细表OutStockBillEntry同步失败");
                };
            }
        }
    }
 
    /**
     * 原材料出库
     */
//    @Scheduled(cron = "0/3 * * * * ? ")
    public void outStockExecute(){
 
    }
 
}