MesApiController.java
13.4 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.myproject.webapp.controller.webService;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.myproject.bean.update.*;
import com.myproject.bean.utils.BoxStatusBean;
import com.myproject.bean.utils.StatusBean;
import com.myproject.dao.mongo.IDataLogDao;
import com.myproject.exception.ValidateException;
import com.myproject.manager.IBarcodeManager;
import com.myproject.manager.IComponentManager;
import com.myproject.manager.IStoragePosManager;
import com.myproject.util.DateUtil;
import com.myproject.util.StorageConstants;
import com.myproject.webapp.controller.storage.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.util.Integers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/rest/api/v2/mes")
public class MesApiController extends BaseController {
@Autowired
protected ITaskService taskService;
@Autowired
protected IComponentManager componentManager;
@Autowired
private IBarcodeManager barcodeManager;
@Autowired
private IStoragePosManager storagePosManager;
@Autowired
private DataCache dataCache;
@Autowired
private IDataLogDao dataLogDao;
protected final static Logger log = LogManager.getLogger(MesApiController.class);
/**
* 基础数据同步
*/
@RequestMapping(value = "/dataUpdate")
@ResponseBody
public String dataUpdate(HttpServletRequest request) {
try {
String Partnumber = checkParameter(request,"PN");
String w = checkParameter(request,"PACKAGE_WIDTH");
String h = checkParameter(request,"PACKAGE_HEIGHT");
log.info("收到 MES 的 数据同步信息"+Partnumber+"【"+w+"x"+h+"】");
Component c = componentManager.findOneByPn(Partnumber);
if(c == null){
c = new Component();
}
c.setPartNumber(Partnumber);
c.setPlateSize(Integer.valueOf(w));
c.setHeight(Integer.valueOf(h));
componentManager.save(c);
} catch (ValidateException e) {
return "Error:" + e.getMessage();
}
return "OK";
}
private String checkParameter(HttpServletRequest request, String paramName) throws ValidateException {
String value = request.getParameter(paramName);
if(Strings.isNullOrEmpty(value)){
log.info(paramName + " is required.");
throw new ValidateException(paramName + " is required.");
}
return value;
}
@RequestMapping(value = "/codeUpdate")
@ResponseBody
public String codeUpdate(HttpServletRequest request) {
try {
String REEL_ID = checkParameter(request,"RI");
String PARTNUMBER = checkParameter(request,"PN");
String QTY = checkParameter(request,"QTY");
log.info("收到条码同步信息:RI="+REEL_ID + " PN=" + PARTNUMBER + " QTY="+QTY);
Component c = componentManager.findOneByPn(PARTNUMBER);
if(c == null){
return "Error: 物料编号[" + PARTNUMBER + "]的档案不存在";
}
Barcode barcode = barcodeManager.findByBarcode(REEL_ID);
if(barcode == null){
barcode = new Barcode();
}
barcode.setBarcode(REEL_ID);
barcode.setPartNumber(PARTNUMBER);
barcode.setInitialAmount(Integer.valueOf(QTY));
barcode.setAmount(Integer.valueOf(QTY));
barcode.setPlateSize(c.getPlateSize());
barcode.setHeight(c.getHeight());
barcodeManager.save(barcode);
}catch (ValidateException ve) {
return "Error:" + ve.getMessage();
}catch (Exception e){
return "Error:" + e.getMessage();
}
return "OK";
}
@RequestMapping(value = "/stackOut")
@ResponseBody
public String stackOut(HttpServletRequest request) {
try {
String[] REEL_IDS = request.getParameterValues("RIS");
if(REEL_IDS == null || REEL_IDS.length == 0){
return "Error: RI 为必须项";
}
ArrayList<StoragePos> poses = Lists.newArrayList();
for (String REEL_ID : REEL_IDS) {
StoragePos pos = storagePosManager.getByBarcode(REEL_ID);
if(pos == null){
return "Error: RI["+REEL_ID+"]在BOX中不存在";
}
poses.add(pos);
}
for (StoragePos pos : poses) {
log.info("出库位置仓位【"+pos.getPosName()+"】");
taskService.checkout(pos,null,false);
}
}catch (Exception e){
return "Error:" + e.getMessage();
}
return "OK";
}
@RequestMapping(value = "/inventory")
@ResponseBody
public List<Map<String,Object>> getInventory(HttpServletRequest request){
String storageId = request.getParameter("LOC");
// try {
// String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
// log.info("body:" + body);
// } catch (IOException e) {
// e.printStackTrace();
// }
List<Map<String,Object>> ReelBarCodes = Lists.newArrayList();
List<StoragePos> poses;
if(!Strings.isNullOrEmpty(storageId)){
poses = storagePosManager.findNotEmptyByStorageId(storageId);
}else{
poses = storagePosManager.findNotEmpty();
}
for (StoragePos pos : poses){
Barcode barcode = pos.getBarcode();
Map<String,Object> item = Maps.newHashMap();
item.put("RI",barcode.getBarcode());
item.put("PN",barcode.getPartNumber());
item.put("QTY",barcode.getAmount());
item.put("LOC", pos.getStorageId());
ReelBarCodes.add(item);
}
return ReelBarCodes /*+ dataCache.getSettings().getNotifyApiUrl()*/;
}
@RequestMapping(value = "/unlock")
@ResponseBody
public String unlock(HttpServletRequest request) {
String orderName = request.getParameter("orderName");//锁定标签
log.info("收到unlock 请求:orderName=" + orderName);
Storage defaultStorage = dataCache.defaultStorage();
List<StoragePos> lockPoses = storagePosManager.findLockPos(orderName);
for (StoragePos lockPos : lockPoses) {
try {
Barcode code = lockPos.getBarcode();
code.setLockId(null);
code.setLockName(null);
lockPos.setBarcode(code);
storagePosManager.save(lockPos);
dataCache.unLockOneReel(defaultStorage.getCid(),code.getPartNumber());
} catch (ValidateException e) {
e.printStackTrace();
}
}
return "OK";
}
@RequestMapping(value = "/lock")
@ResponseBody
public String lock(HttpServletRequest request){
String orderName = request.getParameter("orderName");//锁定标签
String pn = request.getParameter("pn");//Partnumber
int num =Integers.parseInt(request.getParameter("num"),-1);//锁定数量
log.info("lock 请求:orderName="+orderName + ";pn=" + pn + ";num=" + num);
if(Strings.isNullOrEmpty(orderName) || Strings.isNullOrEmpty(pn) || num == -1){
return "Error:参数错误";
}
//验证库存
Collection<String> excludePosIds = Sets.newHashSet();
Collection<StoragePos> posToLock = Sets.newHashSet();
int lockNum = 0;
while (lockNum < num){
StoragePos pos = storagePosManager.findPartNumberPos(null, pn, excludePosIds, StorageConstants.CHECKOUT_TYPE.EXPIRE_FIRST);
if(pos == null){
return "Error:库存不足";
}
Barcode barcode = pos.getBarcode();
if(barcode != null){
lockNum = lockNum + barcode.getAmount();
log.info("lockOrderName="+orderName+"查找到物料【 "+barcode.getBarcode()+" 】,PN=["+ barcode.getPartNumber()+"],数量="+barcode.getAmount()+"已找到数量:"+lockNum);
posToLock.add(pos);
}
excludePosIds.add(pos.getId());
}
for (StoragePos storagePos : posToLock) {
try {
Barcode code = storagePos.getBarcode();
code.setLockId(orderName);
code.setLockName("API");
storagePos.setBarcode(code);
storagePosManager.save(storagePos);
Storage storage = dataCache.getStorageById(storagePos.getStorageId());
dataCache.lockOneReel(storage.getCid(),pn);
} catch (ValidateException e) {
e.printStackTrace();
}
}
return "OK";
}
@RequestMapping(value = "/pnInventory")
@ResponseBody
public Map<String,Map<String,Integer>> getParnumberInventory(HttpServletRequest request){
log.info("收到 getParnumberInventory 请求");
Map<String,Map<String,Integer>> partnumberInventory = Maps.newHashMap();
List<StoragePos> poses = storagePosManager.findNotEmpty();
for (StoragePos pos : poses){
Barcode barcode = pos.getBarcode();
String parnumber = barcode.getPartNumber();
Map<String,Integer> itemInventory = partnumberInventory.get(parnumber);
int totalNum = 0;
int lockNum = 0;
if(itemInventory == null){
itemInventory = Maps.newHashMap();
}else{
totalNum = itemInventory.get("totalNum");
lockNum = itemInventory.get("lockNum");
}
totalNum = totalNum + barcode.getAmount();
if(!Strings.isNullOrEmpty(barcode.getLockId())){
lockNum = lockNum + barcode.getAmount();
}
itemInventory.put("totalNum", totalNum);
itemInventory.put("lockNum", lockNum);
partnumberInventory.put(parnumber,itemInventory);
}
return partnumberInventory;
}
@RequestMapping(value = "/history")
@ResponseBody
public List<Map<String,Object>> history(HttpServletRequest request) {
String bid = request.getParameter("bid");
// String start = request.getParameter("start");
// String end = request.getParameter("end");
// Date startDate = toDate(start);
// Date endDate = toDate(end);
String numStr = request.getParameter("num");
int num = 20;
if(!Strings.isNullOrEmpty(numStr)){
try{
num = Integer.valueOf(numStr);
}catch (Exception e){
}
}
List<Map<String,Object>> results = Lists.newArrayList();
List<DataLog> datas = dataLogDao.findHistory(bid,num);
datas.sort(new Comparator<DataLog>() {
@Override
public int compare(DataLog o1, DataLog o2) {
return o1.getCreateDate().compareTo(o2.getCreateDate());
}
});
for (DataLog dataItem : datas){
Map<String,Object> item = Maps.newHashMap();
item.put("barcode",dataItem.getBarcode());
item.put("partnumber",dataItem.getPartNumber());
item.put("qty",dataItem.getNum());
item.put("type", dataItem.getType());
item.put("opor", dataItem.getOperator());
item.put("boxName",dataItem.getStorageName());
item.put("posName",dataItem.getPosName());
item.put("source",dataItem.getSourceId());
item.put("status", dataItem.getStatus());
item.put("date", DateUtil.toDateTimeString(dataItem.getCreateDate()));
results.add(item);
}
return results;
}
@RequestMapping(value = "/status")
@ResponseBody
public Map<String,Object> status(HttpServletRequest request) {
Map<String,Object> results = Maps.newHashMap();
for (Storage storage : dataCache.getAllStorage().values()){
StatusBean statusBean = taskService.getStatus(storage.getCid());
Map<Integer, BoxStatusBean> boxStatusMap = statusBean.getBoxStatus();
int status = -1;
if(boxStatusMap != null){
BoxStatusBean boxStatusBean = boxStatusMap.get(1);
if(boxStatusBean != null){
status = boxStatusBean.getStatus();
}
}
int storageStatus = status;
if(status == StorageConstants.STATUS.OFFLINE || statusBean.timeOut()){
//离线
storageStatus = -1;
}
results.put(storage.getName(), storageStatus);
}
return results;
}
private Date toDate(String dateStr){
try {
return DateUtil.toDate(dateStr,"yyyyMMddHHmmss");
} catch (Exception e) {
}
return null;
}
}