Blame view

src/main/java/com/huaheng/api/general/service/ShipmentApiService.java 16.7 KB
pengcheng authored
1
2
package com.huaheng.api.general.service;
周鸿 authored
3
4
5
6
7
8
9
10
11
12
13
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
pengcheng authored
14
15
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
pengcheng authored
16
import com.huaheng.api.general.domain.ShipmentDomain;
lector authored
17
import com.huaheng.common.constant.QuantityConstant;
pengcheng authored
18
19
20
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
pengcheng authored
21
import com.huaheng.framework.web.domain.AjaxResult;
pengcheng authored
22
23
24
25
26
27
28
29
30
31
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
32
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
pengcheng authored
33
34
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
lector authored
35
36
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
pengcheng authored
37
38
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
lector authored
39
40
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
pengcheng authored
41
42
43
44
45
46

/**
 * @author ricard
 * @time   19/11/11
 */
pengcheng authored
47
48
49
50
@Component
@Transactional
public class ShipmentApiService {
pengcheng authored
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private ShipmentDetailService shipmentDetailService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CustomerServiceImpl customerService;

    @Autowired
    private ShipmentTypeService shipmentTypeService;

    @Autowired
    private MaterialService materialService;
lector authored
72
73
74
75
76
77
    @Resource
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;

    @Resource
    private ShipmentDetailHistoryService shipmentDetailHistoryService;
78
79
80
    @Resource
    private InventoryDetailService inventoryDetailService;
pengcheng authored
81
    /**
周鸿 authored
82
83
     * 出库单下发
     * @param  shipmentDomain
pengcheng authored
84
85
     * @return
     */
86
    @Transactional(rollbackFor = Exception.class)
周鸿 authored
87
    public AjaxResult shipment(ShipmentDomain shipmentDomain) {
pengcheng authored
88
周鸿 authored
89
        // 1、判断出库主单和子单列是否为空
pengcheng authored
90
91
        ShipmentHeader shipmentHeader = shipmentDomain.getShipmentHeader();
        List<ShipmentDetail> shipmentDetails = shipmentDomain.getShipmentDetails();
周鸿 authored
92
        if (shipmentHeader == null) {
pengcheng authored
93
94
            return AjaxResult.error("出库主单为空");
        }
周鸿 authored
95
        if (shipmentDetails.size() < 1 || shipmentDetails == null) {
pengcheng authored
96
97
98
            return AjaxResult.error("出库子单为空");
        }
周鸿 authored
99
        // 2、检查出库主单的合法性
pengcheng authored
100
101
        AjaxResult ajaxResult = this.checkShipmentHeader(shipmentHeader);
周鸿 authored
102
103
        // 3、检查出库子单的合法性
        shipmentDetails = this.checkShipmentDetail(shipmentDetails, shipmentHeader);
pengcheng authored
104
周鸿 authored
105
        // 4、保存出库主表
pengcheng authored
106
        BigDecimal totalQty = new BigDecimal(0);
周鸿 authored
107
108
        for (ShipmentDetail item : shipmentDetails) {
            totalQty = totalQty.add(item.getQty());
pengcheng authored
109
        }
110
        shipmentHeader.setWarehouseCode(QuantityConstant.WAREHOUSECODE);
周鸿 authored
111
        shipmentHeader.setCompanyCode(shipmentHeader.getCompanyCode() == null ? QuantityConstant.COMPANYCODE : shipmentHeader.getCompanyCode());
112
        shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_ERP);
pengcheng authored
113
114
        shipmentHeader.setTotalLines(shipmentDetails.size());
        shipmentHeader.setTotalQty(totalQty);
游杰 authored
115
116
117
118
        String shipmentCode = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
        shipmentHeader.setCode(shipmentCode);
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
pengcheng authored
119
周鸿 authored
120
        if (!shipmentHeaderService.save(shipmentHeader)) {
pengcheng authored
121
122
123
            throw new ServiceException("保存出库主表失败");
        }
周鸿 authored
124
125
        // 5、保存出库子表
        for (ShipmentDetail shipmentDetail : shipmentDetails) {
游杰 authored
126
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
周鸿 authored
127
            shipmentDetail.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
128
129
            shipmentDetail.setReferCode(shipmentHeader.getReferCode());
            shipmentDetail.setReferId(shipmentHeader.getReferId());
pengcheng authored
130
131
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
周鸿 authored
132
133
134
135
136
137
138
139
            shipmentDetail.setInventorySts(shipmentDetail.getInventorySts() == null ? QuantityConstant.GOOD : shipmentDetail.getInventorySts());
            if (StringUtils.isNotEmpty(shipmentDetail.getMaterialCode())) {
                Material material = materialService.getMaterialByCode(shipmentDetail.getMaterialCode(), shipmentHeader.getWarehouseCode());
                shipmentDetail.setMaterialName(material.getName());
                shipmentDetail.setMaterialSpec(material.getSpec());
                shipmentDetail.setMaterialUnit(material.getUnit());
            }
            shipmentDetail.setCreated(new Date());
周鸿 authored
140
            shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
141
            shipmentDetail.setZoneCode(shipmentHeader.getZoneCode());
pengcheng authored
142
143
144
        }

        int num = 0;
mahuandong authored
145
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
周鸿 authored
146
147
148
149
150
151
        // 检查卷号是否存在库存表
        for (ShipmentDetail item : shipmentDetails) {
            if (StringUtils.isNotEmpty(item.getRollNumber())) {
                boolean tag = inventoryDetailService.isRollNumber(item, null);
                if (!tag) {
                    throw new ServiceException(item.getRollNumber() + ",卷号不存在");
152
153
154
                }
            }
        }
周鸿 authored
155
156
        if (shipmentDetails.size() > 500) {
            for (ShipmentDetail item : shipmentDetails) {
pengcheng authored
157
158
                num++;
                shipmentDetailList.add(item);
周鸿 authored
159
160
                if (num % 500 == 0 || num == shipmentDetails.size()) {
                    if (!shipmentDetailService.insertDetails(shipmentDetailList)) {
pengcheng authored
161
162
                        throw new ServiceException("保存出库子表失败");
                    }
周鸿 authored
163
                    shipmentDetailList = new ArrayList<>();
pengcheng authored
164
165
                }
            }
周鸿 authored
166
167
        } else {
            if (!shipmentDetailService.insertDetails(shipmentDetails)) {
pengcheng authored
168
169
170
                throw new ServiceException("保存出库子表失败");
            }
        }
周鸿 authored
171
        return AjaxResult.success("出库单下发成功").setData(shipmentCode);
pengcheng authored
172
173
174
175
    }

    /**
     * 检查出库主单的合法性
周鸿 authored
176
     * @param  shipmentHeader
pengcheng authored
177
178
     * @return
     */
周鸿 authored
179
180
181
    public AjaxResult checkShipmentHeader(ShipmentHeader shipmentHeader) {
        // 1、判断仓库和货主
        if (StringUtils.isEmpty(shipmentHeader.getWarehouseCode())) {
pengcheng authored
182
183
            return AjaxResult.error("仓库为空");
        }
周鸿 authored
184
        if (StringUtils.isEmpty(shipmentHeader.getCompanyCode())) {
pengcheng authored
185
186
187
188
            return AjaxResult.error("货主为空");
        }

        LambdaQueryWrapper<Warehouse> warehouseLamb = Wrappers.lambdaQuery();
周鸿 authored
189
190
191
        warehouseLamb.eq(Warehouse::getCode, shipmentHeader.getWarehouseCode());
        Warehouse warehouse = warehouseService.getOne(warehouseLamb);
        if (warehouse == null) {
pengcheng authored
192
193
194
195
            return AjaxResult.error("wms没有此仓库");
        }

        LambdaQueryWrapper<Company> companyLamb = Wrappers.lambdaQuery();
周鸿 authored
196
197
198
        companyLamb.eq(Company::getCode, shipmentHeader.getCompanyCode());
        Company company = companyService.getOne(companyLamb);
        if (company == null) {
pengcheng authored
199
200
201
            return AjaxResult.error("wms没有此货主");
        }
周鸿 authored
202
        // 2、判断出库类型
pengcheng authored
203
        LambdaQueryWrapper<ShipmentType> shipmentTypeLamb = Wrappers.lambdaQuery();
周鸿 authored
204
205
206
        shipmentTypeLamb.eq(ShipmentType::getCode, shipmentHeader.getShipmentType()).eq(ShipmentType::getWarehouseCode, shipmentHeader.getWarehouseCode());
        ShipmentType shipmentType = shipmentTypeService.getOne(shipmentTypeLamb);
        if (shipmentType == null) {
pengcheng authored
207
208
209
            return AjaxResult.error("wms没有此出库类型");
        }
周鸿 authored
210
        // 3、检查上游单号是否存在
pengcheng authored
211
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLamb = Wrappers.lambdaQuery();
周鸿 authored
212
        shipmentHeaderLamb.eq(ShipmentHeader::getWarehouseCode, shipmentHeader.getWarehouseCode()).eq(ShipmentHeader::getReferCode, shipmentHeader.getReferCode());
pengcheng authored
213
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLamb);
周鸿 authored
214
        if (shipmentHeaders.size() > 0 || shipmentHeaders != null) {
pengcheng authored
215
216
217
            return AjaxResult.success("出库单已经下发");
        }
周鸿 authored
218
219
        // 4、检查客户
        if (StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())) {
pengcheng authored
220
            LambdaQueryWrapper<Customer> customerLamb = Wrappers.lambdaQuery();
周鸿 authored
221
            customerLamb.eq(Customer::getWarehouseCode, shipmentHeader.getWarehouseCode()).eq(Customer::getCode, shipmentHeader.getCustomerCode());
pengcheng authored
222
            Customer customer = customerService.getOne(customerLamb);
周鸿 authored
223
            if (customer == null) {
pengcheng authored
224
225
226
227
228
229
230
231
                return AjaxResult.error("wms没有此客户");
            }
        }
        return AjaxResult.success(shipmentHeader);
    }

    /**
     * 检查出库子单的合法性
周鸿 authored
232
     * @param  shipmentDetails
pengcheng authored
233
234
     * @return
     */
周鸿 authored
235
    public List<ShipmentDetail> checkShipmentDetail(List<ShipmentDetail> shipmentDetails, ShipmentHeader shipmentHeader) {
pengcheng authored
236
周鸿 authored
237
238
239
240
        for (ShipmentDetail shipmentDetail : shipmentDetails) {
            // 1、检查物料
            if (StringUtils.isEmpty(shipmentDetail.getMaterialCode())) {
                throw new ServiceException("物料为空");
pengcheng authored
241
242
            }
            LambdaQueryWrapper<Material> materialLamb = Wrappers.lambdaQuery();
周鸿 authored
243
244
            materialLamb.eq(Material::getCode, shipmentDetail.getMaterialCode()).eq(StringUtils.isNotEmpty(shipmentHeader.getWarehouseCode()),
                Material::getWarehouseCode, shipmentHeader.getWarehouseCode());
pengcheng authored
245
            Material material = materialService.getOne(materialLamb);
周鸿 authored
246
            if (material == null) {
pengcheng authored
247
248
249
                throw new ServiceException("wms没有此物料");
            }
周鸿 authored
250
            // 2、赋值物料属性
pengcheng authored
251
252
253
254
255
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
        }
        return shipmentDetails;
pengcheng authored
256
    }
lector authored
257
258
259
260
261
262

    @Transactional
    public AjaxResult remove(List<String> shipmentCodeList) {
        LambdaQueryWrapper<ShipmentHeader> headerQueryWrapper;
        for (String shipmentCode : shipmentCodeList) {
            headerQueryWrapper = Wrappers.lambdaQuery();
周鸿 authored
263
            headerQueryWrapper.eq(ShipmentHeader::getCode, shipmentCode);
lector authored
264
265
266
267
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerQueryWrapper);
            if (shipmentHeader == null) {
                return AjaxResult.success("");
            }
周鸿 authored
268
269
270
271
            if ((shipmentHeader.getFirstStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN
                && shipmentHeader.getLastStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN)
                || (shipmentHeader.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_POOL
                    & shipmentHeader.getLastStatus() < QuantityConstant.SHIPMENT_HEADER_POOL)) {
lector authored
272
273
                ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                List<ShipmentDetailHistory> shipmentDetailHistoryList = new ArrayList<>();
周鸿 authored
274
                // 查询入库单明细
lector authored
275
276
277
278
                LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode);
                List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);
周鸿 authored
279
                // 复制到入库历史实体
lector authored
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
                com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentHeaderHistory, shipmentHeader);
                for (ShipmentDetail shipmentDetail : list) {
                    ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                    com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentDetailHistory, shipmentDetail);
                    shipmentDetailHistoryList.add(shipmentDetailHistory);
                }

                shipmentHeaderHistory.setLastUpdatedBy(ShiroUtils.getLoginName());
                if (!shipmentHeaderService.removeById(shipmentHeader.getId())) {
                    throw new ServiceException("删除头表失败");
                }
                if (!shipmentHeaderHistoryService.save(shipmentHeaderHistory)) {
                    throw new ServiceException("新增历史出库单失败");
                }
                // 当存在明细时删除
                if (list.size() != 0) {
周鸿 authored
296
                    // 删除入库明细
lector authored
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
                    List<Integer> shipmentDetailIds = new ArrayList<>();
                    for (int i = 0; i < shipmentDetailHistoryList.size(); i++) {
                        shipmentDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getLoginName());
                        shipmentDetailHistoryList.get(i).setShipmentId(shipmentHeaderHistory.getId());
                        shipmentDetailIds.add(shipmentDetailHistoryList.get(i).getId());
                    }
                    if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
                        throw new ServiceException("删除明细表失败");
                    }
                    if (!shipmentDetailHistoryService.saveBatch(shipmentDetailHistoryList)) {
                        throw new ServiceException("新增明细失败");
                    }
                }
            } else {
                return AjaxResult.success("出库单没有完成,无法删除");
            }
        }
        return AjaxResult.success("删除成功");
    }
周鸿 authored
317
318
    public AjaxResult search(String shipmentCode, String companyCode, String warehouseCode) {
        if (companyCode == null) {
lector authored
319
320
            return AjaxResult.error("货主编码不能为空");
        }
周鸿 authored
321
        if (warehouseCode == null) {
lector authored
322
323
324
325
326
            return AjaxResult.error("仓库编码不能为空");
        }
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
周鸿 authored
327
328
329
330
        headerLambdaQuery.eq(ShipmentHeader::getWarehouseCode, warehouseCode).eq(ShipmentHeader::getCompanyCode, companyCode).eq(ShipmentHeader::getCode,
            shipmentCode);
        detailLambdaQuery.eq(ShipmentDetail::getWarehouseCode, warehouseCode).eq(ShipmentDetail::getCompanyCode, companyCode).eq(ShipmentDetail::getShipmentCode,
            shipmentCode);
lector authored
331
332
333
334
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetailList);
周鸿 authored
335
        return AjaxResult.success("查询成功", shipmentDomain);
lector authored
336
    }
337
周鸿 authored
338
    public AjaxResult confirmShipment(String shipmentCode) {
339
340
341
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
周鸿 authored
342
343
        headerLambdaQuery.eq(ShipmentHeader::getCode, shipmentCode);
        detailLambdaQuery.eq(ShipmentDetail::getShipmentCode, shipmentCode);
344
345
346
347
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetailList);
周鸿 authored
348
        return AjaxResult.success("查询成功", shipmentDomain);
349
    }
pengcheng authored
350
}