Blame view

src/main/java/com/huaheng/pc/shipment/shipmentDetail/service/ShipmentDetailServiceImpl.java 26.2 KB
1
2
package com.huaheng.pc.shipment.shipmentDetail.service;
pengcheng authored
3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
周鸿 authored
4
import com.huaheng.common.exception.BusinessException;
周鸿 authored
5
import com.huaheng.common.utils.Wrappers;
pengcheng authored
6
import com.huaheng.common.constant.QuantityConstant;
pengcheng authored
7
8
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
pengcheng authored
9
10
11
12
import com.huaheng.common.utils.DataUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
13
14
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
15
16
17
18
19
20
import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowDetail;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowHeader;
import com.huaheng.pc.config.statusFlow.service.StatusFlowDetailService;
import com.huaheng.pc.config.statusFlow.service.StatusFlowHeaderService;
21
22
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
pengcheng authored
23
24
import com.huaheng.pc.config.waveMaster.domain.WaveMaster;
import com.huaheng.pc.config.waveMaster.service.WaveMasterService;
25
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
26
27
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
周鸿 authored
28
import com.huaheng.pc.shipment.lockingWorkOrder.domain.LockingWorkOrder;
pengcheng authored
29
30
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
pengcheng authored
31
32
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.service.WaveService;
pengcheng authored
33
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
易文鹏 authored
34
import org.aspectj.weaver.ast.Var;
pengcheng authored
35
import org.springframework.beans.factory.annotation.Autowired;
36
37
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
pengcheng authored
38
39
import java.math.BigDecimal;
import java.util.ArrayList;
40
import java.util.List;
pengcheng authored
41
42
import java.util.Map;
43
44
45
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.shipment.shipmentDetail.mapper.ShipmentDetailMapper;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
pengcheng authored
46
47
import org.springframework.transaction.annotation.Transactional;
48
49
50
@Service
public class ShipmentDetailServiceImpl extends ServiceImpl<ShipmentDetailMapper, ShipmentDetail> implements ShipmentDetailService{
pengcheng authored
51
52
53
54
55
56
57
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private MaterialService materialService;
    @Resource
    private ShipmentDetailMapper shipmentDetailMapper;
pengcheng authored
58
59
60
61
    @Autowired
    private WaveMasterService waveMasterService;
    @Autowired
    private WaveService waveService;
62
63
64
65
66
67
    @Autowired
    private ShipmentPreferenceService shipmentPreferenceService;
    @Autowired
    private StatusFlowHeaderService statusFlowHeaderService;
    @Autowired
    private StatusFlowDetailService statusFlowDetailService;
68
69
    @Autowired
    private WarehouseService warehouseService;
xcq authored
70
71
    @Resource
    private ShipmentDetailService shipmentDetailService;
pengcheng authored
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    /**
     * 新增出库明细
     *
     * @param shipmentDetail
     * @return
     */
    @Override
    public AjaxResult insertDetail(ShipmentDetail shipmentDetail) {
        //查看主单是否存在
        LambdaQueryWrapper<ShipmentHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentHeader::getId,shipmentDetail.getShipmentId())
                .eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode());
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(lambdaQueryWrapper);
        if (shipmentHeader == null) {
            return AjaxResult.error("找不到主单据");
        }
pengcheng authored
89
        if (shipmentHeader.getFirstStatus() > QuantityConstant.SHIPMENT_HEADER_POOL) {
pengcheng authored
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
            //表示已经加入了波次
            return AjaxResult.error("主单据状态不允许新增明细");
        }
        //查找物料
        LambdaQueryWrapper<Material> lam = Wrappers.lambdaQuery();
        lam.eq(Material::getCode,shipmentDetail.getMaterialCode())
                .eq(Material::getWarehouseCode,ShiroUtils.getWarehouseCode());
        Material material = materialService.getOne(lam);
        if (material == null) {
            return AjaxResult.error("物料未找到");
        }
        if (StringUtils.isNotEmpty(material.getCompanyCode()) &&
                ShiroUtils.getCompanyCodeList().contains(material.getCompanyCode()) == false)
        {
            return AjaxResult.error("物料不属于当前货主!");
        }
        shipmentDetail.setId(null);
        shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
        shipmentDetail.setMaterialCode(material.getCode());
        shipmentDetail.setMaterialName(material.getName());
        shipmentDetail.setReferCode(shipmentHeader.getReferCode());
        shipmentDetail.setReferId(shipmentHeader.getReferId());
        shipmentDetail.setMaterialSpec(material.getSpec());
        shipmentDetail.setMaterialUnit(material.getUnit());
114
        shipmentDetail.setInventorySts(StringUtils.isEmpty(shipmentDetail.getInventorySts())?QuantityConstant.GOOD:shipmentDetail.getInventorySts());
pengcheng authored
115
116
117
118
119
120
        shipmentDetail.setCreated(null);
        shipmentDetail.setCreatedBy(ShiroUtils.getLoginName());
        shipmentDetail.setLastUpdated(null);
        shipmentDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        if (this.save(shipmentDetail)==true){
            //更新单据总行数与总数量
121
122
123
124
//            shipmentHeader.setTotalLines(shipmentHeader.getTotalLines() + 1);
//            shipmentHeader.setTotalQty(shipmentHeader.getTotalQty().add(shipmentDetail.getShipQty()));
//            shipmentHeaderService.saveOrUpdate(shipmentHeader);
            updateShipmentHeader(shipmentHeader);
pengcheng authored
125
126
            return AjaxResult.success("新增单据明细成功");
        }
xqs authored
127
        else {
pengcheng authored
128
            return AjaxResult.error("新增单据明细失败");
xqs authored
129
        }
pengcheng authored
130
131
132
133
134
135
136
137
138
139
140
141
    }


    /**
     * 删除出库单据明细
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult deleteDetail(String id) {
xqs authored
142
        if (StringUtils.isEmpty(id)) {
pengcheng authored
143
            return AjaxResult.error("id不能为空");
xqs authored
144
        }
pengcheng authored
145
146
147
148
149
150
151
152
        String[] ids = id.split(",");
        List<Map<String,Integer>> list = shipmentDetailMapper.SelectFirstStatus(id);
        if (list.size() < 1) {
            return AjaxResult.error("找不到主单据!");
        }
        if (list.size() > 1) {
            return AjaxResult.error("有多个主单据,不能一起删除!");
        }
pengcheng authored
153
        if (list.get(0).get("firstStatus") > QuantityConstant.SHIPMENT_HEADER_POOL) {
pengcheng authored
154
155
156
157
158
159
160
161
162
163
164
165
166
            return AjaxResult.error("单据状进入订单池,不允许删除明细");
        }
        Integer result = shipmentDetailMapper.batchDelete(ids);
        if (result > 0) {
            Integer headerId=list.get(0).get("id");
            Map<String,String> map= shipmentDetailMapper.StatisticalByReceiptId(headerId);
            if(DataUtils.getInteger(map.get("totalLines")) <= 0)    {
                shipmentHeaderService.removeById(headerId);
            }
            else    {
                //更新表头的总行数和总数量统计
                ShipmentHeader shipmentHeader = new ShipmentHeader();
                shipmentHeader.setId(headerId);
167
168
169
170
//                shipmentHeader.setTotalLines(DataUtils.getInteger(map.get("totalLines")));
//                shipmentHeader.setTotalQty(DataUtils.getBigDecimal(map.get("totalQty")));
//                shipmentHeaderService.saveOrUpdate(shipmentHeader);
                updateShipmentHeader(shipmentHeader);
pengcheng authored
171
172
173
            }
            return AjaxResult.success("删除单据明细成功");
        }
xqs authored
174
        else {
pengcheng authored
175
            return AjaxResult.error("删除单据明细失败");
xqs authored
176
        }
pengcheng authored
177
178
    }
肖超群 authored
179
180
181
    /**
    ** 查找没有出库完的详情
     */
pengcheng authored
182
183
    @Override
    public Integer countUnCompleted(Integer shipmentId) {
肖超群 authored
184
185
186
187
188
189
190
191
192
193
194
195
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentId);
        List<ShipmentDetail> shipmentDetailList = list(shipmentDetailLambdaQueryWrapper);
        int n = 0;
        for(ShipmentDetail shipmentDetail : shipmentDetailList) {
            BigDecimal qty = shipmentDetail.getQty();
            BigDecimal taskQty = shipmentDetail.getTaskQty();
            if(qty.compareTo(taskQty) > 0) {
                n++;
            }
        }
        return n;
pengcheng authored
196
    }
pengcheng authored
197
198
pengcheng authored
199
    /**
200
     * 查看选中的单据状态是否符合出库流程
pengcheng authored
201
202
203
204
205
     * 选中的单据加入波次
     * 根据选中的主单ID和波次主表的code,判断主单之和是否符合波次的限制条件,
     * 看此code的波次是否建成未开始执行,如果是则只需修改波次属性,否则创建新的波次,
     * 修改加入波次的子单,修改子单和主单的状态,并修改子单的waveId
     */
pengcheng authored
206
207
208
    @Override
    @Transactional
    public void saveWave(String ids, String code) {
pengcheng authored
209
        Integer status = QuantityConstant.SHIPMENT_HEADER_POOL;
210
        List<ShipmentHeader> shipmentHeaderList =shipmentPreferenceService.checkShipmentProcess(ids,status,code);
211
pengcheng authored
212
213
214
215
216
217
218
219
220
        //找到波次主表,看系统是否有此波次
        LambdaQueryWrapper<WaveMaster> lam=Wrappers.lambdaQuery();
        lam.eq(WaveMaster::getCode,code)
                .eq(WaveMaster::getWarehouseCode,ShiroUtils.getWarehouseCode());
        WaveMaster waveMaster=waveMasterService.getOne(lam);
        if(waveMaster == null){
            throw new ServiceException("系统没有此波次");
        }
pengcheng authored
221
        if(Convert.toIntArray(ids).length >waveMaster.getMaxShipments()){
pengcheng authored
222
223
224
225
            throw new ServiceException("加入波次的单据数量超过波次的单据限制");
        }

        List<ShipmentDetail> shipmentDetailList=new ArrayList<>();
pengcheng authored
226
        BigDecimal qty=new BigDecimal(0);
pengcheng authored
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
        //检查出库子表是否有处于波次的
        for (Integer id : Convert.toIntArray(ids))
        {
            LambdaQueryWrapper<ShipmentDetail> lamDetail=Wrappers.lambdaQuery();
            lamDetail.eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
                    .eq(ShipmentDetail::getShipmentId,id);
            List<ShipmentDetail> shipmentDetails=this.list(lamDetail);
            if(shipmentDetails == null || shipmentDetails.size() == 0){
                throw new ServiceException("系统没有主单id为"+id+"的子单");
            }

            //查看是否有单据处于波次中
            for(ShipmentDetail shipmentDetail : shipmentDetails) {
                if (shipmentDetail.getWaveId() != 0) {
                    throw new ServiceException("主单id为" + id + "子单id为" + shipmentDetail.getId() + "的子单已加入波次,不可再加入波次");
                }
肖超群 authored
243
                if(shipmentDetail.getQty().compareTo(shipmentDetail.getTaskQty())!=0) {
pengcheng authored
244
                    shipmentDetailList.add(shipmentDetail);
肖超群 authored
245
                    qty = qty.add(shipmentDetail.getQty().subtract(shipmentDetail.getTaskQty()));
pengcheng authored
246
                }
pengcheng authored
247
248
249
250
251
252
253
            }
        }

        if(shipmentDetailList.size()>waveMaster.getMaxLines()){
            throw new ServiceException("加入波次的总行数超过波次的行数限制");
        }
pengcheng authored
254
255
256
257
        Boolean flag=false;

        //查看波次是否建成未执行
        LambdaQueryWrapper<Wave> waveLam = Wrappers.lambdaQuery();
pengcheng authored
258
        waveLam.eq(Wave::getStatus,QuantityConstant.WAVE_STATUS_BUILD)
pengcheng authored
259
260
261
262
263
264
265
266
267
268
269
270
271
                .eq(Wave::getWaveMode,code)
                .eq(Wave::getWarehouseCode,ShiroUtils.getWarehouseCode());
        Wave wave = waveService.getOne(waveLam);
        if(wave != null && (wave.getTotalShipments()+Convert.toIntArray(ids).length)<= waveMaster.getMaxShipments()
        && (wave.getTotalLines()+shipmentDetailList.size()<= waveMaster.getMaxLines())){
            //修改波次
            wave.setTotalShipments(Convert.toIntArray(ids).length + wave.getTotalShipments());
            wave.setTotalLines(shipmentDetailList.size() + wave.getTotalLines());
            wave.setTotalQty(qty.add(wave.getTotalQty()));
            wave.setLastUpdatedBy(ShiroUtils.getLoginName());

        }else {
            //创建波次
pengcheng authored
272
            wave=new Wave();
pengcheng authored
273
274
275
            wave.setWarehouseCode(ShiroUtils.getWarehouseCode());
            wave.setMasterCode(code);
            wave.setWaveName(waveMaster.getName());
pengcheng authored
276
            wave.setStatus(QuantityConstant.WAVE_STATUS_BUILD);
pengcheng authored
277
            wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_BUILD);
pengcheng authored
278
279
280
281
282
283
284
285
            wave.setTotalShipments(Convert.toIntArray(ids).length);
            wave.setTotalLines(shipmentDetailList.size());
            wave.setTotalQty(qty);
            wave.setCreatedBy(ShiroUtils.getLoginName());
            flag = waveService.save(wave);
            if (flag == false) {
                throw new ServiceException("波次建立失败");
            }
pengcheng authored
286
287
        }
pengcheng authored
288
        //修改出库子单,加入波次ID,并修改状态为波次
pengcheng authored
289
290
        for(ShipmentDetail shipmentDetail :shipmentDetailList){
            shipmentDetail.setWaveId(wave.getId());
pengcheng authored
291
292
            if(shipmentDetail.getStatus()<QuantityConstant.SHIPMENT_HEADER_WAVE) {
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
pengcheng authored
293
            }
pengcheng authored
294
295
296
297
298
299
        }

        flag = this.updateBatchById(shipmentDetailList);
        if(flag == false){
            throw new ServiceException("出库子单加入波次失败");
        }
pengcheng authored
300
301
        for(ShipmentHeader shipmentHeader :shipmentHeaderList){
pengcheng authored
302
303
            shipmentHeader.setWaveId(wave.getId());
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
pengcheng authored
304
305
306
            if(shipmentHeader.getLastStatus()<QuantityConstant.SHIPMENT_HEADER_WAVE) {
                shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
            }
pengcheng authored
307
308
309
310
311
312
        }
        flag = shipmentHeaderService.updateBatchById(shipmentHeaderList);
        if(flag == false){
            throw new ServiceException("修改主单状态失败");
        }
pengcheng authored
313
    }
pengcheng authored
314
315
316
317
318
319
320


    //查看最高状态和最低状态
    @Override
    public Map<String,Integer> selectStatus(Integer id) {
        return shipmentDetailMapper.selectStatus(id);
    }
321
322

    @Override
pengcheng authored
323
    public List<TaskDetail> getShipmentQtyLast7Days() {
324
325
326
327
        return shipmentDetailMapper.getShipmentQtyLast7Days();
    }

    @Override
pengcheng authored
328
    public List<TaskDetail> getWarehouseShipment() {
329
330
331
332
        return shipmentDetailMapper.getWarehouseShipment();
    }

    @Override
pengcheng authored
333
    public List<TaskDetail> getCompanyShipment() {
334
335
        return shipmentDetailMapper.getCompanyShipment();
    }
336
337

    @Override
pengcheng authored
338
    public Boolean insertDetails(List<ShipmentDetail> shipmentDetails) {
xcq authored
339
340
        boolean flag = shipmentDetailService.saveBatch(shipmentDetails);
        if(flag){
pengcheng authored
341
342
343
344
345
            return true;
        }else {
            return false;
        }
    }
346
347
348
349
350
351
352
353

    /**
     * 更新入库单头表状态
     * @param shipmentHeader 入库单按头表
     * @return
     */
    @Override
    public AjaxResult updateShipmentHeader(ShipmentHeader shipmentHeader) {
tongzhonghao authored
354
        BigDecimal totalQty = BigDecimal.ZERO;
355
356
357
358
359
360
        int totalLines = 0;
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
        List<ShipmentDetail> shipmentDetailList =  list(shipmentDetailLambdaQueryWrapper);
        for(ShipmentDetail shipmentDetail : shipmentDetailList) {
            totalLines++;
tongzhonghao authored
361
            totalQty = totalQty.add(shipmentDetail.getQty());
362
363
364
365
366
367
368
369
370
        }
        shipmentHeader.setTotalQty(totalQty);
        shipmentHeader.setTotalLines(totalLines);
        if (!shipmentHeaderService.updateById(shipmentHeader)){
            return AjaxResult.error("出库头表更新失败");
        }
        return AjaxResult.success("入库头表更新成功");
    }
周鸿 authored
371
372
373
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertExcelData(List<ShipmentDetail> list, boolean updateSupport, String operName) {
374
周鸿 authored
375
376
377
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new BusinessException("导入数据不能为空!");
        }
378
        ShipmentHeader shipmentHeader=createHeader(list);
周鸿 authored
379
380
381
382
383
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ShipmentDetail importData : list) {
周鸿 authored
384
            /*if(StringUtils.isEmpty(importData.getWarehouseCode())){
385
386
387
388
389
390
391
392
393
394
                failureNum++;
                String msg = "<br/>" + failureNum + "、仓库编码为空 导入失败:";
                failureMsg.append(msg );
                continue;
            }
            if(StringUtils.isEmpty(importData.getCompanyCode())){
                failureNum++;
                String msg = "<br/>" + failureNum + "、货主编码为空 导入失败:";
                failureMsg.append(msg );
                continue;
周鸿 authored
395
            }*/
396
397
398
399
400
401
            if(StringUtils.isNull(importData.getQty())){
                failureNum++;
                String msg = "<br/>" + failureNum + "、数量为空 导入失败:";
                failureMsg.append(msg );
                continue;
            }
周鸿 authored
402
403
            try {
                importData.setShipmentId(shipmentHeader.getId());
404
                importData.setShipmentCode(shipmentHeader.getCode());
周鸿 authored
405
406
407
408
                Material material = materialService.getMaterialByCode(importData.getMaterialCode());
                importData.setMaterialName(material.getName());
                importData.setMaterialSpec(material.getSpec());
                importData.setMaterialUnit(material.getUnit());
周鸿 authored
409
410
411
412
413


                importData.setWarehouseCode(shipmentHeader.getWarehouseCode());
                importData.setCompanyCode(shipmentHeader.getCompanyCode());
//                importData.setCompanyCode(QuantityConstant.COMPANY_CS);
tongzhonghao authored
414
                importData.setInventorySts(QuantityConstant.GOOD);
周鸿 authored
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
                this.save(importData);
                successNum++;
                successMsg.append("<br/>" + successNum + "、数据 " + importData.getMaterialCode() + " 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、物料编码 " + importData.getMaterialCode() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }
433
434
    public ShipmentHeader createHeader(List<ShipmentDetail> list){
        ShipmentHeader shipmentHeader=new ShipmentHeader();
xumiao authored
435
        String code =shipmentHeaderService.createCode(QuantityConstant.SHIPMENT_TYPE_PO,ShiroUtils.getWarehouseCode());
436
437
438
439
440
441
        shipmentHeader.setId(null);
        shipmentHeader.setLastUpdated(null);
        shipmentHeader.setLastUpdatedBy((ShiroUtils.getLoginName()));
        shipmentHeader.setCreated(null);
        shipmentHeader.setCreatedBy((ShiroUtils.getLoginName()));
        shipmentHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
442
443
444
445
446
447
        if (shipmentHeader.getWarehouseCode().equals(QuantityConstant.WAREHOUSE_CS)) {
            shipmentHeader.setCompanyCode(QuantityConstant.COMPANY_CS);
        } else {
            shipmentHeader.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
        }
//        shipmentHeader.setCompanyCode(QuantityConstant.COMPANY_CS);
448
        shipmentHeader.setCode(code);
tongzhonghao authored
449
        shipmentHeader.setShipmentType(QuantityConstant.SHIPMENT_TYPE_PO);
450
451
452
453
454
455
456
457
458
459
        shipmentHeader.setTotalLines(list.size());
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
        BigDecimal bigDecimal = list.stream().map(ShipmentDetail::getQty).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        shipmentHeader.setTotalQty(bigDecimal);
        if (!shipmentHeaderService.save(shipmentHeader)) {
            throw new ServiceException("导入失败,添加出库单头表失败");
        }
        return shipmentHeader;
    }
周鸿 authored
460
周鸿 authored
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    @Override
    public ShipmentDetail createShipmentDetail(ShipmentHeader header, LockingWorkOrder lockingWorkOrder) throws Exception {
        if (header == null || lockingWorkOrder == null){
            throw new Exception("找不到主单据");
        }
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        // 公司编码
        shipmentDetail.setCompanyCode(ShiroUtils.getCompanyCode());
        // 公司id
//        shipmentDetail.setCompanyId(QuantityConstant.COMPANYID);
        // 库存状态
        shipmentDetail.setInventorySts("good");
        // 工作令
        shipmentDetail.setMoCode(lockingWorkOrder.getWorkOrder());
        // 物料编码
        shipmentDetail.setMaterialCode(lockingWorkOrder.getBomCode());
        // 数量
        shipmentDetail.setQty(lockingWorkOrder.getBnum());
        // 头表编码
        shipmentDetail.setShipmentCode(header.getCode());
        // 头表id
        shipmentDetail.setShipmentId(header.getId());
        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentDetail.getShipmentId());
        if (shipmentHeader == null) {
            throw new Exception("找不到主单据");
        }
        if (shipmentHeader.getFirstStatus() > 100) {
            throw new Exception("主单据状态不允许新增明细");
        }
        Material material = new Material();
        if(shipmentDetail.getMaterialCode().length() == 11) {
            material.setCode(shipmentDetail.getMaterialCode());
        }else {
            material.setBarcode(shipmentDetail.getMaterialCode());
        }
        material = materialService.getMaterialByDomain(material);
        if (material == null) {
            throw new Exception("物料未找到");
        }
        if (StringUtils.isNotEmpty(material.getCompanyCode()) && ShiroUtils.getUser() != null &&
                ShiroUtils.getCompanyCodeList().contains(material.getCompanyCode()) == false)
        {
            throw new Exception("物料不属于当前货主!");
        }
        shipmentDetail.setId(null);
//        shipmentDetail.setWarehouseId(ShiroUtils.getWarehouseId());
        shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
        shipmentDetail.setMaterialId(material.getId());
        shipmentDetail.setMaterialCode(material.getCode());
        shipmentDetail.setMaterialName(material.getName());
        shipmentDetail.setCreated(null);
        shipmentDetail.setUWarehouseCode(shipmentHeader.getUWarehouseCode());
        if (ShiroUtils.getUser() == null){
            shipmentDetail.setCreatedBy(shipmentDetail.getCreatedBy());
            shipmentDetail.setLastUpdatedBy(shipmentDetail.getCreatedBy());
        }else {
            shipmentDetail.setCreatedBy(ShiroUtils.getLoginName());
            shipmentDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        }
        shipmentDetail.setLastUpdated(null);
        return shipmentDetail;
//        if (this.insert(shipmentDetail) > 0){
//            //更新单据总行数与总数量
//            shipmentHeader.setTotalLines(shipmentHeader.getTotalLines() + 1);
//            shipmentHeader.setTotalQty(shipmentHeader.getTotalQty().add(shipmentDetail.getQty()));
//            shipmentHeaderService.updateByModel(shipmentHeader);
//            return shipmentDetail;
//        }
//        else{
//            throw new RuntimeException("新增单据明细失败!");
//        }
    }
周鸿 authored
533
534
    @Override
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
    public List<ShipmentDetail> createShipmentDetailQuick(ShipmentHeader header, List<InventoryDetail> inventoryDetails)  {

        List<ShipmentDetail> list=new ArrayList<>();
        for (InventoryDetail inventoryDetail : inventoryDetails) {

            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setCompanyCode(header.getCompanyCode());
            shipmentDetail.setInventorySts(inventoryDetail.getInventorySts());
            shipmentDetail.setMoCode(inventoryDetail.getMoCode());
            shipmentDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            shipmentDetail.setMaterialName(inventoryDetail.getMaterialName());
            shipmentDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            shipmentDetail.setQty(inventoryDetail.getQty());
            shipmentDetail.setShipmentCode(header.getCode());
            shipmentDetail.setShipmentId(header.getId());
            shipmentDetail.setReferId(inventoryDetail.getId());
            shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            shipmentDetail.setCreated(null);
            shipmentDetail.setUWarehouseCode(header.getUWarehouseCode());
            if (ShiroUtils.getUser() == null){
                shipmentDetail.setCreatedBy(shipmentDetail.getCreatedBy());
                shipmentDetail.setLastUpdatedBy(shipmentDetail.getCreatedBy());
            }else {
                shipmentDetail.setCreatedBy(ShiroUtils.getLoginName());
                shipmentDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            }
            list.add(shipmentDetail);
        }
        if(list!=null&&list.size()>0){
            shipmentDetailService.saveBatch(list);
        }

        return list;

    }


    @Override
573
574
575
576
577
578
579
580
581
582
583
584
585
586
    public List<ShipmentDetail> getListByShipmentHeaderId(Integer id,String warehouseCode) {
        if(id==0 || StringUtils.isEmpty(warehouseCode)){
            return null;
        }
        Warehouse warehouse = warehouseService.getOne(new LambdaQueryWrapper<Warehouse>()
                .eq(Warehouse::getCode, warehouseCode));
        if(warehouse==null){
            return null;
        }
        return this.list(new LambdaQueryWrapper<ShipmentDetail>()
                .eq(ShipmentDetail::getShipmentId,id)
                .eq(ShipmentDetail::getWarehouseCode,warehouseCode));
    }
587
}