Blame view

src/main/java/com/huaheng/api/mes/controller/MesReceiptController.java 31.8 KB
hh authored
1
2
package com.huaheng.api.mes.controller;
hh authored
3
4
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
hh authored
5
6
7
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.mes.dto.Detail;
hh authored
8
import com.huaheng.api.mes.dto.GetOrderHistoryDTO;
hh authored
9
import com.huaheng.api.mes.dto.Header;
hh authored
10
11
12
import com.huaheng.api.mes.dto.ReceiptDTO;
import com.huaheng.api.mes.utils.CallaMOM;
import com.huaheng.api.mes.utils.SqlServer;
hh authored
13
14
15
16
17
18
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.RetCode;
hh authored
19
import com.huaheng.pc.config.material.domain.Material;
hh authored
20
21
22
23
24
25
26
27
28
29
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
hh authored
30
31
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
hh authored
32
33
34
35
36
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
hh authored
37
import java.io.UnsupportedEncodingException;
hh authored
38
import java.math.BigDecimal;
hh authored
39
import java.net.URL;
hh authored
40
41
42
43
44
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
hh authored
45
import java.util.*;
hh authored
46
hh authored
47
hh authored
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@RestController
@RequestMapping("/API/WMS/v2")
public class MesReceiptController extends BaseController {

    @Resource
    private ReceiptHeaderService receiptHeaderService;

    @Resource
    private ReceiptDetailService receiptDetailService;

    @Resource
    private ShipmentHeaderService shipmentHeaderService;

    @Resource
    private ShipmentDetailService shipmentDetailService;

    @Resource
    private InventoryDetailService inventoryDetailService;
hh authored
67
68
69
    @Resource
    private TaskDetailService taskDetailService;
hh authored
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    @PostMapping("/receipt")
    @ApiLogger(apiName = "添加入库单及其明细", from="ROBOT")
    @ApiOperation("添加入库单及其明细")
    public AjaxResult receipt(@RequestBody ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        List<Detail> details = receiptDTO.getShipemtnDetails();

        AjaxResult ajaxResult = new AjaxResult();
        boolean isNull = enterIsNull(ajaxResult, header, details);
        if (isNull){
            return ajaxResult;
        }

        ReceiptHeader receiptHeader = new ReceiptHeader();
hh authored
84
85
        if(StringUtils.isNotEmpty(header.getMOMID()))
            receiptHeader.setMOMID(header.getMOMID());
hh authored
86
        receiptHeader.setCode(header.getReferCode());
hh authored
87
88
89
        // 入库类型--需要保持一致
        // receiptHeader.setReceiptType(header.getRefeCodeType());
        receiptHeader.setReceiptType("receipt");
hh authored
90
        // receiptHeader.setReceiptType("CS");
hh authored
91
92
        receiptHeader.setTotalQty(header.getTotalQty());
        receiptHeader.setTotalLines(details.size());
hh authored
93
94
95
        // TAID MOM目标类型 // FAID MOM来源类型
        receiptHeader.setTAID(header.getTAID());
        receiptHeader.setFAID(header.getFAID());
hh authored
96
97
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
hh authored
98
        receiptHeader.setWarehouseCode("CS0001");
hh authored
99
        receiptHeader.setCompanyCode("BHF");
hh authored
100
101
        receiptHeader.setCreatedBy("MOM");
hh authored
102
103
104
105
106
        receiptHeaderService.save(receiptHeader);

        List<ReceiptDetail> shipemtnDetailList = new ArrayList<>();
        details.forEach(detail->{
            ReceiptDetail receiptDetail = new ReceiptDetail();
hh authored
107
108
            if(StringUtils.isNotEmpty(detail.getDetailID()))
                receiptDetail.setMOMID(detail.getDetailID());
hh authored
109
            receiptDetail.setIsVirtualBom(detail.getIsVirtual());
hh authored
110
111
            receiptDetail.setMaterialCode(detail.getMaterialCode());
            receiptDetail.setMaterialName(detail.getMaterialName());
hh authored
112
            receiptDetail.setQty(detail.getQty());
hh authored
113
114
115
            receiptDetail.setIsUrgent(detail.getIsUrgent());
            receiptDetail.setNoticeNo(detail.getNoticeNo());
            receiptDetail.setSNNO(detail.getSNNO());
hh authored
116
            receiptDetail.setReceiptBarcode(detail.getReceiptBarcode());
hh authored
117
118
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setReceiptCode(receiptHeader.getCode());
hh authored
119
            receiptDetail.setStatusFlowCode("2");
hh authored
120
            receiptDetail.setWarehouseCode("CS0001");
hh authored
121
            receiptDetail.setCompanyCode("BHF");
hh authored
122
            receiptDetail.setInventorySts("good");
hh authored
123
            receiptDetail.setCreatedBy("MOM");
hh authored
124
125
126
            shipemtnDetailList.add(receiptDetail);
        });
        receiptDetailService.saveBatch(shipemtnDetailList);
hh authored
127
hh authored
128
        return AjaxResult.success(receiptDTO);
hh authored
129
130
    }
hh authored
131
hh authored
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    /**
     * 添加出库单及其明细
     * @param receiptDTO
     * @return
     */
    @PostMapping("/shipment")
    @ApiLogger(apiName = "添加出库单及其明细", from="ROBOT")
    @ApiOperation("添加出库单及其明细")
    public AjaxResult shipment(@RequestBody ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        List<Detail> details = receiptDTO.getShipemtnDetails();

        AjaxResult ajaxResult = new AjaxResult();
        boolean inNull = enterIsNull(ajaxResult, header, details);
        if (inNull)  return ajaxResult;

        ShipmentHeader shipmentHeader = new ShipmentHeader();
hh authored
149
        // shipmentHeader.setId(header.getId());
hh authored
150
151
        if(StringUtils.isNotEmpty(header.getMOMID()))
            shipmentHeader.setMOMID(header.getMOMID());
hh authored
152
        shipmentHeader.setCode(header.getReferCode());
hh authored
153
154
155
        // 出库类型--需要保持一致
        // shipmentHeader.setShipmentType(header.getRefeCodeType());OTF
        shipmentHeader.setShipmentType("OTF");
hh authored
156
        shipmentHeader.setTotalQty(header.getTotalQty());
hh authored
157
158
159
        // TAID MOM目标类型 // FAID MOM来源类型
        shipmentHeader.setTAID(header.getTAID());
        shipmentHeader.setFAID(header.getFAID());
160
        shipmentHeader.setNoticeNo(details.get(0).getNoticeNo());
hh authored
161
162
163
164
        shipmentHeader.setWorkshop(header.getWorkshop());
        shipmentHeader.setWarehouse(header.getWarehouse());
        shipmentHeader.setSuperiorCode(header.getSuperiorCode());
        shipmentHeader.setSuperiorName(header.getSuperiorName());
hh authored
165
166
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
hh authored
167
        shipmentHeader.setTotalLines(details.size());
hh authored
168
        shipmentHeader.setWarehouseCode("CS0001");
hh authored
169
        shipmentHeader.setCompanyCode("BHF");
hh authored
170
        shipmentHeader.setCreatedBy("MOM");
hh authored
171
172
173
174
175
        shipmentHeaderService.save(shipmentHeader);

        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
        details.forEach(detail->{
            ShipmentDetail shipmentDetail = new ShipmentDetail();
hh authored
176
177
            if(StringUtils.isNotEmpty(detail.getDetailID()))
                shipmentDetail.setMOMID(detail.getDetailID());
hh authored
178
179
180
            shipmentDetail.setMaterialCode(detail.getMaterialCode());
            shipmentDetail.setMaterialName(detail.getMaterialName());
            shipmentDetail.setPort(detail.getEndSation());
hh authored
181
            shipmentDetail.setQty(detail.getQty());
hh authored
182
183
184
185
            shipmentDetail.setIsUrgent(detail.getIsUrgent());
            shipmentDetail.setNoticeNo(detail.getNoticeNo());
            shipmentDetail.setSNNO(detail.getSNNO());
hh authored
186
187
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
hh authored
188
            shipmentDetail.setCompanyCode("BHF");
hh authored
189
190
            shipmentDetail.setWarehouseCode("CS0001");
            shipmentDetail.setInventorySts("good");
hh authored
191
            shipmentDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
hh authored
192
            shipmentDetail.setCreatedBy("MOM");
hh authored
193
194
195
            shipmentDetailList.add(shipmentDetail);
        });
        shipmentDetailService.saveBatch(shipmentDetailList);
hh authored
196
197

        return AjaxResult.success(receiptDTO);
hh authored
198
199
    }
hh authored
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    private JSONObject getJsonObject(Header header, List<Detail> details) {
        JSONObject data = new JSONObject();
        JSONObject Rd_In_M = new JSONObject();
        Rd_In_M.put("MGPK", header.getId());
        Rd_In_M.put("cCode", header.getReferCode());
        JSONArray Rd_In_S = new JSONArray();
        details.forEach(detail -> {
            JSONObject json = new JSONObject();
            json.put("MGPK", header.getId());
            json.put("SGPK", detail.getId());
            json.put("cInvCode", detail.getMaterialCode());
            Rd_In_S.add(json);
        });
        data.put("Requester", "WMS");
        data.put("Rd_In_M", Rd_In_M);
        data.put("Rd_In_S", Rd_In_S);
        return data;
    }
hh authored
219
hh authored
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    /**
     * 入库单参数_判空处理
     * @param ajaxResult
     * @param shipmentHeader
     * @param shipemtnDetails
     */
    private boolean enterIsNull(AjaxResult ajaxResult, Header shipmentHeader, List<Detail> shipemtnDetails){

        if (isNullData(ajaxResult, shipmentHeader, shipemtnDetails)) return true;

        if (StringUtils.isEmpty(shipmentHeader.getRefeCodeType())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("单据类型不能为空!!!");
            return true;
        } else if (StringUtils.isEmpty(shipmentHeader.getReferCode())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("单据单号不能为空!!!");
            return true;
hh authored
236
237
        }else if (shipmentHeader.getTotalQty() == null || shipmentHeader.getTotalQty().intValue() <= 0) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("数量不能为空!!!");
hh authored
238
239
240
241
242
        }

        shipemtnDetails.forEach(shipemtnDetail->{
            if (StringUtils.isEmpty(shipemtnDetail.getMaterialCode())) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("物料编码不能为空!!!");
hh authored
243
244
            } else if (shipemtnDetail.getTotalQty() == null || shipemtnDetail.getQty().intValue() <= 0) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("数量不能为空!!!");
hh authored
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
            }
        });
        return StringUtils.isNotEmpty(ajaxResult.getMsg());
    }

    /**
     * 出库单参数_判空处理
     * @param ajaxResult
     * @param shipmentHeader
     * @param shipemtnDetails
     */
    private boolean outIsNull (AjaxResult ajaxResult, Header shipmentHeader, List<Detail> shipemtnDetails){
        if (isNullData(ajaxResult, shipmentHeader, shipemtnDetails)) return true;

        if (StringUtils.isEmpty(shipmentHeader.getReferCode())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("单据单号不能为空!!!");
            return true;
        }

        shipemtnDetails.forEach(shipemtnDetail->{
            if (StringUtils.isEmpty(shipemtnDetail.getMaterialCode())) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("物料编码不能为空!!!");
            }
        });
        return StringUtils.isNotEmpty(ajaxResult.getMsg());
    }

    /**
     * 判空处理
     */
    private boolean isNullData(AjaxResult ajaxResult, Header shipmentHeader, List<Detail> shipemtnDetails) {
        if (shipmentHeader == null) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("出库单头表不能为空!!!");
            return true;
        } else if (shipemtnDetails == null || shipemtnDetails.size() < 1) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("出库单明细表集不能为空!!!");
            return true;
        }
        return false;
    }

    /**
hh authored
287
     * 入库单单据取消
hh authored
288
     */
hh authored
289
290
291
    @PostMapping("/cancelEnterWarehouse")
    @ApiOperation("入库单单据取消")
    public AjaxResult cancelEnterWarehouse(@RequestBody ReceiptDTO receiptDTO) {
hh authored
292
293
294
        Header header = receiptDTO.getShipmentHeader();
        List<Detail> details = receiptDTO.getShipemtnDetails();
hh authored
295
296
297
        AjaxResult ajaxResult = isNull(header, details);
        if (StringUtils.isNotEmpty(ajaxResult.getMsg())) return ajaxResult;
hh authored
298
        // 查询入库单据表头
hh authored
299
300
301
302
303
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderWrapper = Wrappers.lambdaQuery();
        receiptHeaderWrapper.eq(ReceiptHeader::getId, header.getId());
        ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptHeaderWrapper);
        if (receiptHeader == null)
            return ajaxResult.setCode(RetCode.FAIL).setMsg("没有找到对应单据!!!");
hh authored
304
        // 查询入库单据明细
hh authored
305
306
307
308
309
310
311
312
313
314
315
        LambdaQueryWrapper<ReceiptDetail> receiptDetailWrapper = Wrappers.lambdaQuery();
        receiptDetailWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
        List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailWrapper);
        if (receiptDetailList == null)
            return ajaxResult.setCode(RetCode.FAIL).setMsg("没有找到对应单据明细!!!");
        // 获取删除明细
        List<ReceiptDetail> removeReceiptDetails = new ArrayList<>();
        receiptDetailList.forEach(receiptDetail-> details.forEach(detail->{
            if (detail.getMaterialCode().equals(receiptDetail.getMaterialCode()) &&
                    detail.getTotalQty().intValue() == receiptDetail.getTaskQty().intValue()){
                removeReceiptDetails.add(receiptDetail);
hh authored
316
            }
hh authored
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
        }));
        if (removeReceiptDetails.size() > 0){
            ArrayList<Integer> ids = new ArrayList<>();
            removeReceiptDetails.forEach(receiptDetail->{
                ids.add(receiptDetail.getId());
            });
            receiptDetailService.removeByIds(ids);
        }else {
            return ajaxResult.setCode(RetCode.FAIL).setMsg("没有匹配到对应要取消的入库单单据明细!!!");
        }
        // 出库单明细数量和要删除的明细数量一致,则删除主表
        boolean isRemove = removeReceiptDetails.size() == receiptDetailList.size();
        if (isRemove){
            shipmentHeaderService.removeById(receiptHeader);
        }
        return new AjaxResult<T>().setCode(RetCode.SUCCESS).setMsg("取消成功");
    }


    /**
     * 出库单单据取消
     */
    @PostMapping("/cancelOutWarehouse")
    @ApiOperation("出库单单据取消")
    public AjaxResult cancelOutWarehouse(@RequestBody ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        List<Detail> details = receiptDTO.getShipemtnDetails();

        AjaxResult ajaxResult = isNull(header, details);
        if (StringUtils.isNotEmpty(ajaxResult.getMsg())) return ajaxResult;
hh authored
347
348
349

        // 查询出库单据表头
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderWrapper = Wrappers.lambdaQuery();
hh authored
350
351
352
353
        shipmentHeaderWrapper.eq(ShipmentHeader::getId, header.getId());
        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentHeaderWrapper);
        if (shipmentHeader == null)
            return ajaxResult.setCode(RetCode.FAIL).setMsg("没有找到对应单据!!!");
hh authored
354
355
        // 查询出库单据明细
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailWrapper = Wrappers.lambdaQuery();
hh authored
356
        shipmentDetailWrapper.eq(ShipmentDetail::getShipmentId, header.getId());
hh authored
357
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailWrapper);
hh authored
358
359
        if (shipmentDetailList == null)
            return ajaxResult.setCode(RetCode.FAIL).setMsg("没有找到对应单据明细!!!");
hh authored
360
361
        // 获取删除明细
        List<ShipmentDetail> removeShipmentDetails = new ArrayList<>();
hh authored
362
363
364
365
366
        shipmentDetailList.forEach(shipmentDetai-> details.forEach(detail->{
            if (detail.getMaterialCode().equals(shipmentDetai.getMaterialCode()) && detail.getTotalQty().intValue() == (shipmentDetai.getTaskQty().intValue())){
                removeShipmentDetails.add(shipmentDetai);
            }
        }));
hh authored
367
368
        if (removeShipmentDetails.size() > 0){
            ArrayList<Integer> ids = new ArrayList<>();
hh authored
369
            removeShipmentDetails.forEach(shipmentDetail-> ids.add(shipmentDetail.getId()));
hh authored
370
            shipmentDetailService.removeByIds(ids);
hh authored
371
372
        }else {
            return ajaxResult.setCode(RetCode.FAIL).setMsg("没有匹配到对应要取消的出库单单据明细!!!");
hh authored
373
374
375
376
377
378
        }
        // 出库单明细数量和要删除的明细数量一致,则删除主表
        boolean isRemove = removeShipmentDetails.size() == shipmentDetailList.size();
        if (isRemove){
            shipmentHeaderService.removeById(shipmentHeader);
        }
hh authored
379
380
381
382
383
384
385
386
387
388
389
390
391
392
        return new AjaxResult<T>().setCode(RetCode.SUCCESS).setMsg("取消成功");

    }

    private AjaxResult isNull(Header header, List<Detail> details) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean isNull = outIsNull(ajaxResult, header, details);
        details.forEach(detail -> {
            if (detail.getTotalQty() == null || detail.getTotalQty().intValue() <= 0) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("总数量不能为空!!!");
            }
        });
        if (isNull || StringUtils.isNotEmpty(ajaxResult.getMsg())) return ajaxResult;
        return ajaxResult;
hh authored
393
394
395
396
397
398
399
400
401
402
403
404
    }

    /**
     * 查询库存
     */
    @PostMapping("/searchInventory")
    @ApiLogger(apiName = "查询库存", from="ROBOT")
    @ApiOperation("查询库存")
    public AjaxResult searchInventory(@RequestBody Detail detailed) {

        String materialName =  detailed.getMaterialName();
        String materialCode = detailed.getMaterialCode();
hh authored
405
406
407
        String containerCode = detailed.getContainerCode();
        if(StringUtils.isEmpty(materialName) && StringUtils.isEmpty(materialCode) && StringUtils.isEmpty(containerCode)){
            return AjaxResult.error("物料名称、物料编码、托盘号不能同时为空!!!");
hh authored
408
409
410
411
412
413
        }
        LambdaQueryWrapper<InventoryDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        // 物料编码
        if (StringUtils.isNotEmpty(materialName)){
            lambdaQueryWrapper.eq(InventoryDetail::getMaterialName, materialName);
        }
hh authored
414
        // 物料名称
hh authored
415
416
417
        if (StringUtils.isNotEmpty(materialCode)){
            lambdaQueryWrapper.eq(InventoryDetail::getMaterialCode, materialCode);
        }
hh authored
418
419
420
421
        // 托盘号
        if (StringUtils.isNotEmpty(containerCode)){
            lambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode);
        }
hh authored
422
423
        List<InventoryDetail> list = inventoryDetailService.list(lambdaQueryWrapper);
hh authored
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
        return AjaxResult.success(list);

    }

    /**
     * 获取入库单明细
     * @return AjaxResult
     */
    @PostMapping("/getReceiptDetail")
    @ApiLogger(apiName = "获取入库单明细", from="ROBOT")
    @ApiOperation("获取入库单明细")
    public AjaxResult getReceiptDetail(@RequestBody GetOrderHistoryDTO getOrderHistoryDTO){

        if(StringUtils.isEmpty(getOrderHistoryDTO.getEndTime()) || StringUtils.isEmpty(getOrderHistoryDTO.getStartTime())){
            return AjaxResult.error("开始时间和结束时间不能为空!!!");
        }
        LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .ge(ReceiptDetail::getCreated, getOrderHistoryDTO.getStartTime())
                .le(ReceiptDetail::getCreated, getOrderHistoryDTO.getEndTime())
                .ge(ReceiptDetail::getProcessStamp, 300);
        List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);

        return AjaxResult.success(list);
    }

    /**
     * 获取出库单明细记录
     * @return AjaxResult
     */
    @PostMapping("/getShipmentDetail")
    @ApiLogger(apiName = "获取出库单明细", from="ROBOT")
    @ApiOperation("获取出库单明细")
    public AjaxResult getShipmentDetail(@RequestBody GetOrderHistoryDTO getOrderHistoryDTO) {

        if(StringUtils.isEmpty(getOrderHistoryDTO.getEndTime()) || StringUtils.isEmpty(getOrderHistoryDTO.getStartTime())){
            return AjaxResult.error("开始时间和结束时间不能为空!!!");
        }
        LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .ge(ShipmentDetail::getCreated, getOrderHistoryDTO.getStartTime())
                .le(ShipmentDetail::getCreated, getOrderHistoryDTO.getEndTime())
                .ge(ShipmentDetail::getStatus, 300);
        List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);

        return AjaxResult.success(list);
    }

    /**
     * 获取sqlServer数据库中物料信息, 同步到本地数据库中
     */
    @PostMapping("/getMaterial")
    @ApiOperation("获取外部物料信息")
    public List<Material> getMaterial(){
        List<Material> materialList = new ArrayList<>();
        try {
hh authored
480
hh authored
481
            // String sql = "SELECT * FROM [dbo].[sys_user] WHERE createTime > (select DATEADD(HOUR,-3,getdate())) AND createTime <= (select GETDATE())";
hh authored
482
483
484
485
            // String sql = "SELECT * FROM [dbo].[base_material]  WHERE MaterialName = 'SMLS Pipe'";
            // 获取新增一天以内和修改两个小时以内的物料数据
            String sql = "select a.cInvCode,a.cInvName,a.cComUnitCode,b.cidefine6,b.cidefine7,a.dSDate,a.dModifyDate from [dbo].[Inventory] a left join [dbo].[Inventory_extradefine] b on a.cInvCode = b.cInvCode WHERE (a.cDefWareHouse='035' or a.cDefWareHouse='003') and datediff(day,a.dSDate,getdate())=0 OR a.dModifyDate >=  DATEADD(HOUR, -3, GETDATE()) ";
hh authored
486
            ResultSet resultSet = SqlServer.find(sql);
hh authored
487
488
489
490
            if (resultSet == null){
                return materialList;
            }
            while(resultSet.next()){
hh authored
491
492
493
494
495
496
497
                // 字段要与海王表一致
                String code = resultSet.getString("cInvCode");
                String name = resultSet.getString("cInvName");
                String unit = resultSet.getString("cComUnitCode");
                String spec = resultSet.getString("cidefine6");
                String high = resultSet.getString("cidefine7");
                int isHigh = "高托".equals(high) ? 1 : 0;
hh authored
498
499

                Material material = new Material();
hh authored
500
                material.setCode(code);
hh authored
501
502
                material.setName(name);
                material.setUnit(unit);
hh authored
503
504
                material.setSpec(spec);
                material.setIsHigh(isHigh);
hh authored
505
506
507
508
509
510
511
512
513
514
                material.setWarehouseCode("CS0001");
                material.setCompanyCode("BHF");

                materialList.add(material);
            }
            // materialService.saveBatch(materialList);
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return materialList;
hh authored
515
    }
hh authored
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532

    /** 当前时间向推几小时 */
    public String dateRoll(int ihour) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        LocalDateTime date = LocalDateTime.now();
        // 获取当前时间的前几小时时间
        LocalDateTime localDateTime = date.minusHours(ihour);

        return dateTimeFormatter.format(localDateTime);
    }

    /**
     * 查询站台任务明细列表
     */
    @ApiLogger(apiName = "查询站台任务明细列表", from="ROBOT")
    @PostMapping("/kanbanInfo")
hh authored
533
    @CrossOrigin
hh authored
534
535
536
537
538
539
540
541
542
543
544
    public AjaxResult kanbanInfo(@RequestBody TaskDetail taskDetail){
        String fromLocation = taskDetail.getFromLocation();
        if (StringUtils.isEmpty(fromLocation))
            return AjaxResult.error("站台不能为空!!!");
        LambdaQueryWrapper<TaskDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TaskDetail::getFromLocation, fromLocation)
                .lt(TaskDetail::getStatus,"100");
        List<TaskDetail> taskDetailList = taskDetailService.list(wrapper);
        return AjaxResult.success(taskDetailList);
    }
hh authored
545
    /**
546
     * post调用E_Rd_InMOM)入库成功接口
hh authored
547
548
549
     */
    @ApiLogger(apiName = "post调用E_Rd_In(MOM)入库接口", from="ROBOT")
    @PostMapping("/postE_Rd_In")
550
551
    public Boolean postE_Rd_In(@RequestBody ReceiptDetail receiptDetail){
        // ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptDetail.getId()); //59
hh authored
552
        // 查询入库单据表头
553
        ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptDetail.getReceiptId()); //59
hh authored
554
        JSONObject Rd_In_M = new JSONObject();
hh authored
555
556
        Rd_In_M.put("mno", receiptHeader.getCode());
        Rd_In_M.put("MGPK", receiptHeader.getMOMID());
hh authored
557
558
559
        Rd_In_M.put("cBusType", receiptHeader.getReceiptType());
        Rd_In_M.put("TAID", receiptHeader.getTAID());
        Rd_In_M.put("FAID", receiptHeader.getFAID());
hh authored
560
        Rd_In_M.put("UserNo",  receiptHeader.getLastUpdatedBy());
hh authored
561
hh authored
562
        // 查询入库单据明细
hh authored
563
564
565
//        LambdaQueryWrapper<ReceiptDetail> receiptDetailWrapper = Wrappers.lambdaQuery();
//        receiptDetailWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
//        List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailWrapper);
hh authored
566
567
        JSONArray jsonArray = new JSONArray();
            JSONObject Rd_In_S = new JSONObject();
hh authored
568
569
            Rd_In_S.put("MGPK", receiptHeader.getMOMID());
            Rd_In_S.put("SGPK", receiptDetail.getMOMID());
hh authored
570
571
572
573
574
575
576
            Rd_In_S.put("SNNO", receiptDetail.getSNNO());
            Rd_In_S.put("cInvCode", receiptDetail.getMaterialCode());
            Rd_In_S.put("Iquantity", receiptDetail.getTaskQty());
            Rd_In_S.put("iFQuantity", receiptDetail.getQty());
            Rd_In_S.put("cwhcode", receiptDetail.getWarehouseCode());
            Rd_In_S.put("MOCode", receiptDetail.getNoticeNo());
            Rd_In_S.put("ISUrgent", receiptDetail.getIsUrgent());
hh authored
577
            Rd_In_S.put("receiptBarcode", receiptDetail.getReceiptBarcode());
hh authored
578
579
580
581
582
583
            jsonArray.add(Rd_In_S);
        JSONObject data = new JSONObject();
        data.put("Rd_In_M", Rd_In_M);
        data.put("Rd_In_S", jsonArray);

        String action = "E_Rd_In";
hh authored
584
        JSONObject json = new JSONObject();
hh authored
585
        json.put("data", data);
hh authored
586
587
588
589
590

        return postMOM(action, json);
    }

    /**
591
     * post调用E_Rd_InMOM)出库成功接口
hh authored
592
593
594
     */
    @ApiLogger(apiName = "post调用E_Rd_Out(MOM)出库接口", from="ROBOT")
    @PostMapping("/postE_Rd_Out")
595
    public Boolean postE_Rd_Out(@RequestBody ShipmentHeader shipmentHeader){
hh authored
596
        // ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentHeaderID.getId());
hh authored
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
        JSONArray dataArray = new JSONArray();
        // 如果订单为合并类型
        if ("merge".equals(shipmentHeader.getShipmentType())){
            // 查询所有子表
            LambdaQueryWrapper<ShipmentDetail> shipmentDetailWrapper = Wrappers.lambdaQuery();
            shipmentDetailWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
            List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailWrapper);
            // 查询所有合并前的头表
            String ids = shipmentHeader.getRecordID();
            String [] headerIdArr = ids.split(",");
            LambdaQueryWrapper<ShipmentHeader> headerWrapper = Wrappers.lambdaQuery();
            headerWrapper.in(ShipmentHeader::getId, headerIdArr);
            List<ShipmentHeader> shipmentHeaderList = shipmentHeaderService.list(headerWrapper);
            // 根据合并前的头表id分组
            Map<Integer,List<ShipmentDetail>> map = new HashMap<>();
            for (ShipmentDetail detail : shipmentDetailList){
                    Integer key = detail.getRecordID();
                    if(map.containsKey(key)) {
                        map.get(key).add(detail);
                    }else {
                        List<ShipmentDetail> list = new ArrayList<>();
                        list.add(detail);
                        map.put(key, list);
                    }
            }
hh authored
622
hh authored
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
            for (Integer key : map.keySet()) {
                JSONObject data = new JSONObject();
                // 合并前的头表
                ShipmentHeader header = new ShipmentHeader();
                // 合并前头表对应的子表
                List<ShipmentDetail> detailList = new ArrayList<>();
                for(ShipmentHeader SH : shipmentHeaderList){
                    if (key.equals(SH.getId())){
                        header = SH;
                        detailList = map.get(key);
                    }
                }
                // 查询出库单据表头
                JSONObject Rd_Out_M = getRd_Out_M(header);
                JSONArray jsonArray = getRd_Out_S(header, detailList);
                data.put("Rd_Out_M", Rd_Out_M);
                data.put("Rd_Out_S", jsonArray);
                dataArray.add(data);
            }
        }else {
            // 查询出库单据表头
            JSONObject Rd_Out_M = getRd_Out_M(shipmentHeader);
            // 查询出库单据明细
            JSONArray jsonArray = getJsonArray(shipmentHeader);
            JSONObject data = new JSONObject();
            data.put("Rd_Out_M", Rd_Out_M);
            data.put("Rd_Out_S", jsonArray);
            dataArray.add(data);
        }

        String action = "E_Rd_Out";
        JSONObject json = new JSONObject();
        json.put("data", dataArray);

        return postMOM(action, json);
    }

    private JSONArray getJsonArray(ShipmentHeader shipmentHeader) {
hh authored
661
662
663
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailWrapper = Wrappers.lambdaQuery();
        shipmentDetailWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailWrapper);
hh authored
664
665
666
667
        return getRd_Out_S(shipmentHeader,shipmentDetailList);
    }

    private JSONArray getRd_Out_S(ShipmentHeader shipmentHeader, List<ShipmentDetail> shipmentDetailList) {
hh authored
668
        JSONArray jsonArray = new JSONArray();
hh authored
669
        shipmentDetailList.forEach(shipmentDetail->{
hh authored
670
            JSONObject Rd_Out_S = new JSONObject();
hh authored
671
672
            Rd_Out_S.put("MGPK", shipmentHeader.getMOMID());
            Rd_Out_S.put("SGPK",  shipmentDetail.getMOMID());
hh authored
673
674
675
676
677
678
679
680
681
            Rd_Out_S.put("SNNO",  shipmentDetail.getSNNO());
            Rd_Out_S.put("cInvCode",  shipmentDetail.getMaterialCode());
            Rd_Out_S.put("Iquantity",  shipmentDetail.getTaskQty());
            Rd_Out_S.put("iFQuantity",  shipmentDetail.getQty());
            Rd_Out_S.put("cwhcode",  shipmentDetail.getWarehouseCode());
            Rd_Out_S.put("MOCode",  shipmentDetail.getNoticeNo());
            Rd_Out_S.put("ISUrgent",  shipmentDetail.getIsUrgent());
            jsonArray.add(Rd_Out_S);
        });
hh authored
682
683
        return jsonArray;
    }
hh authored
684
hh authored
685
686
687
688
689
690
691
692
693
    private JSONObject getRd_Out_M(@RequestBody ShipmentHeader shipmentHeader) {
        JSONObject Rd_Out_M = new JSONObject();
        Rd_Out_M.put("mno", shipmentHeader.getCode());
        Rd_Out_M.put("MGPK", shipmentHeader.getMOMID());
        Rd_Out_M.put("cBusType", shipmentHeader.getShipmentType());
        Rd_Out_M.put("TAID", shipmentHeader.getTAID());
        Rd_Out_M.put("FAID", shipmentHeader.getFAID());
        Rd_Out_M.put("UserNo",  shipmentHeader.getLastUpdatedBy());
        return Rd_Out_M;
hh authored
694
695
696
697
698
699
700
    }

    /**
     * @param action MOM行为
     * @param JObject 行为参数
     * @return
     */
701
    public Boolean postMOM(String action, JSONObject JObject){
hh authored
702
703
704
705
706
707
708

        return CallaMOM.getMsg(action, JObject);
//        String url = "http://192.168.101.127:31055//Handler/H_DJ2J_10.ashx";
//        String param = "sptype=JSP_W2M&action=" + action + "&onlydata=1&params=" + JObject;;
//        CallaMOM callMOM = new CallaMOM();
//        String data = callMOM.sendPost(url, param);
//        return JSONObject.parseObject(data);
hh authored
709
    }
hh authored
710
hh authored
711
hh authored
712
}