自动化立体仓库 - WMS系统
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
package com.zy.asrs.task.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.SnowflakeIdWorker;
import com.core.exception.CoolException;
import com.zy.asrs.entity.DiaphragmInfo;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.DocTypeService;
import com.zy.asrs.service.OrderDetlService;
import com.zy.asrs.service.OrderService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.asrs.utils.MesSyncUtil;
import com.zy.common.constant.MesConstant;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.Date;
import java.util.List;
 
@Slf4j
@Service
public class MesPakinHandler extends AbstractHandler<String> {
    @Value("${mes.pakin.url}")
    private String url;
 
    @Value("${mes.pakin.path}")
    private String path;
 
    @Autowired
    private DocTypeService docTypeService;
 
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private ApiLogService apiLogService;
 
    @Autowired
    private OrderDetlService orderDetlService;
 
    @Transactional
    public ReturnT<String> start() {
        boolean success = true;
        log.info("url:" + url + path);
        JSONObject jsonObject = null;
        try {
            jsonObject = doGetJsonData();
        } catch (IOException e) {
            success = false;
            e.printStackTrace();
        }
 
        int code = jsonObject.getInteger("code");
 
        if(code == 200){
            JSONObject jsonData = jsonObject.getJSONObject("data");
            String orderNo = jsonData.getString("orderNo");
            Order o = insertOrder(jsonData,orderNo);
            insertOrUpdateOrderDetl(jsonData, o);
 
        }else {
            success = false;
            log.error("请求mes接口响应错误,响应码为: " + code);
        }
        saveApiLog(jsonObject.toJSONString(),success);
        return SUCCESS;
    }
 
    /*
    保存日志
     */
    private void saveApiLog(String response, boolean success){
        apiLogService.save(
                "生成入库单据",
                url + path,
                null,
                "127.0.0.1",
                null,
                response,
                success
        );
    }
 
    /*
    发起一个doGet请求
     */
    private JSONObject doGetJsonData() throws IOException {
        return JSON.parseObject( new HttpHandler.Builder()
                .setUri(url)
                .setPath(path)
                .setParams(MesSyncUtil.getRequestParamMap())
                .build()
                .doGet());
    }
 
    /*
    新增order信息
     */
    private Order insertOrder(JSONObject jsonData, String orderNo){
        Order o = orderService.selectByNo(orderNo);
        if (!Cools.isEmpty(o)) {
            throw new CoolException(jsonData.getString("orderNo") + "单据已存在,请勿重复提交");
        }
 
        o = orderMapping(jsonData);
        if (!orderService.insert(o)) {
            throw new CoolException("生成单据主档失败,请联系管理员");
        }
        return o;
    }
 
    /*
    根据orderNo、matnr、batch,新增或者更改orderDetl信息
     */
    private void insertOrUpdateOrderDetl(JSONObject jsonData, Order o){
        List<DiaphragmInfo> diaphragmInfoList =  JSONObject.parseArray(jsonData.getString("orderDetails"),DiaphragmInfo.class);
 
        diaphragmInfoList.forEach(d -> {
            EntityWrapper<OrderDetl> wrapper = new EntityWrapper<>();
            wrapper.eq("order_no",o.getOrderNo()).eq("matnr",d.getItemCode()).eq("batch",d.getBatchNo());
            OrderDetl od = orderDetlService.selectOne(wrapper);
            if(od == null){
                od = new OrderDetl();
                od.setOrderNo(o.getOrderNo());
                od.setOrderId(o.getId());
                orderDetlMapping(d,od);
                orderDetlService.insert(od);
            }else {
                od.setQty(od.getQty() + d.getQuantity().doubleValue());
                od.setAnfme(od.getAnfme() + d.getAssQuantity().doubleValue());
                od.setUpdateTime(new Date());
                orderDetlService.updateById(od);
            }
        });
    }
 
    /*
    映射Order类和jsonData的字段
     */
    private Order orderMapping(JSONObject jsonData){
        Order o = new Order();
        o.setStatus(1);
        //设置订单状态 --- 初始化状态
        o.setSettle(0L);
        //uuid
        o.setUuid(String.valueOf(snowflakeIdWorker.nextId()));
        //单据编号
        o.setOrderNo(jsonData.getString("orderNo"));
        //单据时间
        o.setOrderTime(jsonData.getString("orderTime"));
        //单据类型
        DocType docType = docTypeService.selectOrAdd(jsonData.getString("orderType"), Boolean.TRUE);
        o.setDocType(docType.getDocId());
        o.setCreateTime(new Date());
        o.setUpdateTime(new Date());
        return o;
    }
 
    /*
    映射OrderDetl与隔膜信息类的字段关系
     */
    private void orderDetlMapping(DiaphragmInfo d, OrderDetl od){
        //批号 -- 生产批号
        od.setBatch(d.getBatchNo());
        //商品编码 -- 物料编码
        od.setMatnr(d.getItemCode());
        //商品名称 -- 物料名称
        od.setMaktx(d.getItemName());
        //数量 -- 数量
        od.setAnfme(d.getQuantity() == null ? null : d.getQuantity().doubleValue());
        //完成数量 -- 辅数量
        od.setQty(d.getAssQuantity() == null ? null : d.getAssQuantity().doubleValue());
        //规格 -- 规格/型号
        od.setSpecs(d.getStd());
        //备注 -- 备注
        od.setMemo(d.getNote());
        //条形码 -- 条形码
        od.setQrCode(d.getBarCode());
        //颜色 -- 大卷位置
        od.setColor(d.getPosition());
        //型号 -- 小卷位置
        od.setModel(d.getPosition2());
        //品牌 -- 等级
        od.setBrand(d.getGrade());
        //收卷开始日期
        od.setStartDate(d.getStartDate());
        //收卷结束日期
        od.setEndDate(d.getEndDate());
 
        od.setCreateTime(new Date());
        od.setUpdateTime(new Date());
    }
}