自动化立体仓库 - WMS系统
LSH
2023-01-04 a740e5dfd94871f23b9ef859d0b552ade6790fff
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
package com.zy.ints.task.scheduler;
 
import com.core.common.Cools;
import com.zy.asrs.entity.Mat;
import com.zy.asrs.entity.Tag;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.MatService;
import com.zy.asrs.service.TagService;
import com.zy.ints.entity.Prdt;
import com.zy.ints.entity.PrdtBak;
import com.zy.ints.erp.ErpSqlServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
 
/**
 * erp任务控制器
 * Created by vincent on 2020/11/27
 */
@Slf4j
@Component
public class ErpPrdtScheduler {
 
    /**
     * ERP接口是否启用
     */
    @Value("${erp.enabled}")
    private Boolean erpEnabled;
    @Value("${erp.useName.ip}")
    private String ip;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private TagService tagService;
    @Autowired
    private ErpSqlServer erpSqlServer;
    @Autowired
    private MatService matService;
 
    /**
     * 获取商品信息表资料
     */
    @Scheduled(cron = "${erp.refreshtime}")
    public void obtainPedt() {
        if (!erpEnabled) return;
        String sqlSelectPrdt = "select prd_no as prdNo,name,ut,spc,type,status from erp_prdt where 1=1";
        String sqlUpDatePrdtOne = "update erp_prdt set status=1 where prd_no=";
        String sqlUpDatePrdtTwo = "update erp_prdt set status=2 where prd_no=";
        try {
            Tag tag = tagService.selectByName("全部", 1);
 
            List<Prdt> prdts = erpSqlServer.select(sqlSelectPrdt, Prdt.class);
            if (prdts.size() > 0) {
                for (Prdt prdt : prdts) {
                    Date date = new Date();
                    if (prdt.getStatus() == 0) {
                        Integer type = prdt.getType();
                        if (type == 1) {
                            Mat mat = matService.selectByMatnr(prdt.getPrdNo());
                            if (Cools.isEmpty(mat)) {
                                Mat matNew = new Mat();
                                matNew.setTagId(tag.getId());
                                matNew.setMatnr(prdt.getPrdNo());
                                matNew.setMaktx(prdt.getName());
                                matNew.setUnit(prdt.getUt());
                                matNew.setSpecs(prdt.getSpc());
                                matNew.setCreateBy(9999L);//9999表示erp下发
                                matNew.setCreateTime(date);
                                matNew.setUpdateBy(9999L);//9999表示erp下发
                                matNew.setUpdateTime(date);
                                if (matService.insert(matNew)) {
                                    erpSqlServer.update(sqlUpDatePrdtOne + "'" + prdt.getPrdNo() + "'");
                                    callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息成功!添加商品信息成功!", true);
                                } else {
                                    erpSqlServer.update(sqlUpDatePrdtTwo + "'" + prdt.getPrdNo() + "'");
                                    callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息失败!!!添加商品信息失败!!!", false);
                                }
                            } else {
                                erpSqlServer.update(sqlUpDatePrdtTwo + "'" + prdt.getPrdNo() + "'");
                                callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息失败!!!商品已存在!!!", false);
                            }
                        } else if (type == 2) {
                            Mat mat = matService.selectByMatnr(prdt.getPrdNo());
                            if (!Cools.isEmpty(mat)) {
                                mat.setMaktx(prdt.getName());
                                mat.setUnit(prdt.getUt());
                                mat.setSpecs(prdt.getSpc());
                                if (matService.updateById(mat)) {
                                    erpSqlServer.update(sqlUpDatePrdtOne + "'" + prdt.getPrdNo() + "'");
                                    callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息成功!修改商品信息成功!", true);
                                } else {
                                    erpSqlServer.update(sqlUpDatePrdtTwo + "'" + prdt.getPrdNo() + "'");
                                    callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息失败!!!修改商品信息失败!!!", false);
                                }
                            }
                        } else if (type == 3) {
                            Mat mat = matService.selectByMatnr(prdt.getPrdNo());
                            if (!Cools.isEmpty(mat)) {
                                if (matService.deleteById(mat.getId())) {
                                    erpSqlServer.update(sqlUpDatePrdtOne + "'" + prdt.getPrdNo() + "'");
                                    callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息成功!删除商品信息成功!", true);
                                } else {
                                    erpSqlServer.update(sqlUpDatePrdtTwo + "'" + prdt.getPrdNo() + "'");
                                    callApiLogSave(prdt, "erp_prdt", "接收ERP下发商品信息成功!删除商品信息失败!", false);
                                }
                            }
                        } else {
                            callApiLogSave(prdt, "erp_prdt", "商品信息出现异常资料未处理!", false);
                        }
                    } else if (prdt.getStatus() == 1) {
                        HashMap<String, Object> condition = new HashMap<>();
                        condition.put("prd_no", "'" + prdt.getPrdNo() + "'");
                        condition.put("name", "'" + prdt.getName() + "'");
                        condition.put("ut", "'" + prdt.getUt() + "'");
                        condition.put("spc", "'" + prdt.getSpc() + "'");
                        condition.put("type", "'" + prdt.getType() + "'");
                        condition.put("status", "'" + prdt.getStatus() + "'");
                        erpSqlServer.insert(PrdtBak.class, condition);
//                        callApiLogSave(prdt, "erp_prdt_bak", "ERP下发商品信息备份成功!", true);
 
                        HashMap<String, String> condition2 = new HashMap<>();
                        condition2.put("prd_no", "'" + prdt.getPrdNo() + "'");
                        erpSqlServer.delete(Prdt.class, condition2);
//                        callApiLogSave(prdt, "erp_prdt_bak", "ERP下发商品信息备份后删除成功!", true);
                    } else {
                        callApiLogSave(prdt, "erp_prdt", "商品信息出现异常资料未处理!", false);
                    }
                }
            }
        } catch (Exception e) {
            Date date = new Date();
            log.error(date + ": 表名:erp_prdt :" + e);
        }
    }
 
    public void callApiLogSave(Prdt prdt, String tableName, String response, Boolean bool) {
        apiLogService.save("ERP下发商品信息", tableName, "null", ip,
                "品号:" + prdt.getPrdNo() + "、品名:" + prdt.getName() + "、类型:" + prdt.getType() + "、状态:" + prdt.getStatus(),
                response, bool);
    }
}