自动化立体仓库 - 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
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
package com.zy.ints.task.scheduler;
 
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SnowflakeIdWorker;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.Mat;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.service.*;
import com.zy.ints.entity.ErpDetTb;
import com.zy.ints.entity.ErpDetTbBak;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
 
/**
 * erp任务控制器
 * Created by vincent on 2020/11/27
 */
@Slf4j
@Component
public class ErpDetTbScheduler {
    /**
     * ERP接口是否启用
     */
    @Value("${erp.enabled}")
    private Boolean erpEnabled;
    @Value("${erp.useName.ip}")
    private String ip;
    @Autowired
    private ErpSqlServer erpSqlServer;
    @Autowired
    private MatService matService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private ApiLogService apiLogService;
 
    /**
     * 获取入出库订单信息
     */
    @Scheduled(cron = "${erp.refreshtime}")
    public void InOrOutOrder() {
        if (!erpEnabled) return;
        String sqlSelectErpDetTb = "select bill_no as billNo,prd_no as prdNo,iokindid,add_id as addId,qty,prd_mark as prdMark,wh,billdate,status,temp1,temp2,temp3 from erp_det_tb where 1=1";
        String sqlUpDateErpDetTbOne = "update erp_det_tb set status=1 where 1=1 and prd_no=";
        String sqlUpDateErpDetTbTwo = "update erp_det_tb set status=2 ";
 
        try {
            List<ErpDetTb> erpDetTbs = erpSqlServer.select(sqlSelectErpDetTb, ErpDetTb.class);
            if (erpDetTbs.size() > 0) {
                for (ErpDetTb erpDetTb : erpDetTbs) {
                    Date now = new Date();
                    Integer status = erpDetTb.getStatus();
                    if (status == 0) {
                        Mat mat = matService.selectByMatnr(erpDetTb.getPrdNo());
                        if (Cools.isEmpty(mat)) {
                            erpSqlServer.update(sqlUpDateErpDetTbTwo + ",temp1='品号信息在立库不存在,请先添加商品!!!'" + "where 1=1 and prd_no=" + "'" + erpDetTb.getPrdNo() + "'");
                            callApiLogSave(erpDetTb, "erp_det_tb", "品号信息在立库不存在,请先添加商品!!!", false);
                        } else {
                            Order order = orderService.selectByNo(erpDetTb.getBillNo());
                            if (Cools.isEmpty(order)) {
                                order = new Order(
                                        String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                                        erpDetTb.getBillNo(),    // 订单编号
                                        DateUtils.convert(now),    // 单据日期
                                        null,    // 单据类型
                                        null,    // 项目编号
                                        null,    //
                                        null,    // 调拨项目编号
                                        null,    // 初始票据号
                                        null,    // 票据号
                                        null,    // 客户编号
                                        null,    // 客户
                                        null,    // 联系方式
                                        null,    // 操作人员
                                        null,    // 合计金额
                                        null,    // 优惠率
                                        null,    // 优惠金额
                                        null,    // 销售或采购费用合计
                                        null,    // 实付金额
                                        null,    // 付款类型
                                        null,    // 业务员
                                        null,    // 结算天数
                                        null,    // 邮费支付类型
                                        null,    // 邮费
                                        null,    // 付款时间
                                        null,    // 发货时间
                                        null,    // 物流名称
                                        null,    // 物流单号
                                        1L,    // 订单状态
                                        1,    // 状态
                                        9999L,    // 添加人员
                                        now,    // 添加时间
                                        9999L,    // 修改人员
                                        now,    // 修改时间
                                        null    // 备注
                                );
                                String InAndOut = erpDetTb.getAddId().equals("2") ? "(出库)" : "(入库)";
                                DocType docType = docTypeService.selectOrAdd(erpDetTb.getIokindid$() + InAndOut, erpDetTb.getAddId().equals("1"));
                                if (!Cools.isEmpty(docType)) {
                                    order.setDocType(docType.getDocId());
                                    if (!orderService.insert(order)) {
                                        callApiLogSave(erpDetTb, "erp_det_tb", "订单主档添加失败!!", false);
                                        continue;
                                    } else {
                                        callApiLogSave(erpDetTb, "erp_det_tb", "订单主档添加成功!!", true);
                                    }
                                }
                            }
                            Order order1 = orderService.selectByNo(erpDetTb.getBillNo());
                            if (!Cools.isEmpty(order1)) {
                                OrderDetl orderDetl = orderDetlService.selectItem(order1.getId(), erpDetTb.getBillNo(), erpDetTb.getPrdMark());
                                if (Cools.isEmpty(orderDetl)) {
                                    orderDetl = new OrderDetl();
                                    orderDetl.setOrderId(order1.getId());
                                    orderDetl.setOrderNo(erpDetTb.getBillNo());
                                    orderDetl.setAnfme(erpDetTb.getQty());
                                    orderDetl.setQty(0.0D);
                                    orderDetl.setMatnr(erpDetTb.getPrdNo());
                                    orderDetl.setMaktx(mat.getMaktx());
                                    orderDetl.setBatch(erpDetTb.getPrdMark());
                                    orderDetl.setSpecs(mat.getSpecs());
                                    orderDetl.setUnit(mat.getUnit());
 
                                    orderDetl.setStatus(1);
                                    orderDetl.setCreateBy(9999L);
                                    orderDetl.setCreateTime(now);
                                    orderDetl.setUpdateBy(9999L);
                                    orderDetl.setUpdateTime(now);
                                    orderDetl.setMemo(erpDetTb.getWh());
                                    if (orderDetlService.insert(orderDetl)) {
                                        erpSqlServer.update(sqlUpDateErpDetTbOne + "'" + erpDetTb.getPrdNo() + "'");
                                        callApiLogSave(erpDetTb, "erp_det_tb", "接收ERP下发订单接收成功!!", true);
                                    } else {
                                        erpSqlServer.update(sqlUpDateErpDetTbTwo + ",temp1='添加订单明细档失败!!!'" + "where 1=1 and prd_no=" + "'" + erpDetTb.getPrdNo() + "'");
                                        callApiLogSave(erpDetTb, "erp_det_tb", "ERP下发订单接收失败!!", false);
                                    }
                                }
                            }
                        }
                    } else if (status == 1) {
                        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
                        HashMap<String, Object> condition = new HashMap<>();
                        condition.put("bill_no", "'" + erpDetTb.getBillNo() + "'");
                        condition.put("prd_no", "'" + erpDetTb.getPrdNo() + "'");
                        condition.put("iokindid", "'" + erpDetTb.getIokindid() + "'");
                        condition.put("add_id", "'" + erpDetTb.getAddId() + "'");
                        condition.put("qty", "'" + erpDetTb.getQty() + "'");
                        condition.put("prd_mark", "'" + erpDetTb.getPrdMark() + "'");
                        condition.put("wh", "'" + erpDetTb.getWh() + "'");
                        condition.put("billdate", "'" + format + "'");
                        condition.put("status", "'" + erpDetTb.getStatus() + "'");
                        condition.put("temp1", "'" + erpDetTb.getTemp1() + "'");
                        condition.put("temp2", "'" + erpDetTb.getTemp2() + "'");
                        condition.put("temp3", "'" + erpDetTb.getTemp3() + "'");
                        erpSqlServer.insert(ErpDetTbBak.class, condition);
 
//                        callApiLogSave(erpDetTb, "erp_det_tb_bak", "ERP下发订单备份成功!!", true);
 
                        HashMap<String, String> condition2 = new HashMap<>();
                        condition2.put("bill_no", "'" + erpDetTb.getBillNo() + "'");
                        condition2.put("prd_no", "'" + erpDetTb.getPrdNo() + "'");
                        condition2.put("prd_mark", "'" + erpDetTb.getPrdMark() + "'");
                        erpSqlServer.delete(ErpDetTb.class, condition2);
 
//                        callApiLogSave(erpDetTb, "erp_det_tb", "ERP下发订单备份后删除成功!!", true);
 
                    } else {
                        callApiLogSave(erpDetTb, "erp_det_tb", "接收ERP下发订单失败!!!状态异常!!!", false);
                    }
                }
            }
        } catch (Exception e) {
            Date date = new Date();
            log.error(date + ": 表名:erp_det_tb :" + e);
        }
    }
 
    public void callApiLogSave(ErpDetTb erpDetTb, String tableName, String response, Boolean bool) {
        apiLogService.save("ERP下发订单信息", tableName, "null", ip,
                "订单号:" + erpDetTb.getBillNo() + "、品号:" + erpDetTb.getPrdNo() + "、货品特征:" + erpDetTb.getPrdMark() + "、状态:" + erpDetTb.getStatus(),
                response, bool);
    }
}