Blame view

src/main/java/com/huaheng/api/general/service/ShipmentApiService.java 8.87 KB
pengcheng authored
1
2
package com.huaheng.api.general.service;
pengcheng authored
3
4
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
pengcheng authored
5
import com.huaheng.api.general.domain.ShipmentDomain;
pengcheng authored
6
7
8
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
pengcheng authored
9
import com.huaheng.framework.web.domain.AjaxResult;
pengcheng authored
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
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;
import org.springframework.beans.factory.annotation.Autowired;
pengcheng authored
25
26
27
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
pengcheng authored
28
29
30
31
32
33
34
35
36
37
38
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ricard
 * @time   19/11/11
 *
 */
pengcheng authored
39
40
41
42
@Component
@Transactional
public class ShipmentApiService {
pengcheng authored
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
    @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;

    /**
     *  出库单下发
     *
     * @param shipmentDomain
     * @return
     */
    @Transactional
pengcheng authored
71
72
    public AjaxResult shipment(ShipmentDomain shipmentDomain){
pengcheng authored
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
        //1、判断出库主单和子单列是否为空
        ShipmentHeader shipmentHeader = shipmentDomain.getShipmentHeader();
        List<ShipmentDetail> shipmentDetails = shipmentDomain.getShipmentDetails();
        if(shipmentHeader == null){
            return AjaxResult.error("出库主单为空");
        }
        if(shipmentDetails.size() < 1 || shipmentDetails == null ){
            return AjaxResult.error("出库子单为空");
        }

        //2、检查出库主单的合法性
        AjaxResult ajaxResult = this.checkShipmentHeader(shipmentHeader);

        //3、检查出库子单的合法性
        shipmentDetails =this.checkShipmentDetail(shipmentDetails,shipmentHeader);

        //4、保存出库主表
        BigDecimal totalQty = new BigDecimal(0);
        for (ShipmentDetail item: shipmentDetails) {
            totalQty.add(item.getShipQty());
        }
        shipmentHeader.setTotalLines(shipmentDetails.size());
        shipmentHeader.setTotalQty(totalQty);
        shipmentHeader.setCode(shipmentHeaderService.createCode(shipmentHeader.getShipmentType()));

        if(!shipmentHeaderService.save(shipmentHeader)){
            throw new ServiceException("保存出库主表失败");
        }

        //5、保存出库子表
        for(ShipmentDetail shipmentDetail : shipmentDetails){
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        }

        int num = 0;
mahuandong authored
109
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
pengcheng authored
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        if(shipmentDetails.size() >500 ){
            for(ShipmentDetail item : shipmentDetails){
                num++;
                shipmentDetailList.add(item);
                if(num % 500 ==0 || num == shipmentDetails.size()){
                    if(!shipmentDetailService.insertDetails(shipmentDetailList)){
                        throw new ServiceException("保存出库子表失败");
                    }
                    shipmentDetailList=new ArrayList<>();
                }
            }
        }else {
            if(!shipmentDetailService.insertDetails(shipmentDetails)){
                throw new ServiceException("保存出库子表失败");
            }
        }
mahuandong authored
126
        return AjaxResult.success("出库单下发成功");
huhai authored
127
128
129
130
131
132
133
134
135
136

//        //6回传添加对象
//        LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper =Wrappers.lambdaQuery();
//        lambdaQueryWrapper.eq(ShipmentDetail::getShipmentId ,shipmentHeader.getId());
//
//        ShipmentDomain jsonShipmentDomain =new ShipmentDomain();
//        jsonShipmentDomain.setShipmentHeader(shipmentHeader);
//        jsonShipmentDomain.setShipmentDetails(shipmentDetailService.list(lambdaQueryWrapper));
//
//        return AjaxResult.success(jsonShipmentDomain);
pengcheng authored
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
    }


    /**
     * 检查出库主单的合法性
     * @param shipmentHeader
     * @return
     */
    public  AjaxResult checkShipmentHeader(ShipmentHeader shipmentHeader){
        //1、判断仓库和货主
        if(StringUtils.isEmpty(shipmentHeader.getWarehouseCode())){
            return AjaxResult.error("仓库为空");
        }
        if(StringUtils.isEmpty(shipmentHeader.getCompanyCode())){
            return AjaxResult.error("货主为空");
        }


        LambdaQueryWrapper<Warehouse> warehouseLamb = Wrappers.lambdaQuery();
        warehouseLamb.eq(Warehouse::getCode,shipmentHeader.getWarehouseCode());
        Warehouse warehouse =warehouseService.getOne(warehouseLamb);
        if(warehouse == null){
            return AjaxResult.error("wms没有此仓库");
        }

        LambdaQueryWrapper<Company> companyLamb = Wrappers.lambdaQuery();
        companyLamb.eq(Company::getCode,shipmentHeader.getCompanyCode());
        Company company =companyService.getOne(companyLamb);
        if(company == null){
            return AjaxResult.error("wms没有此货主");
        }

        //2、判断出库类型
        LambdaQueryWrapper<ShipmentType> shipmentTypeLamb = Wrappers.lambdaQuery();
        shipmentTypeLamb.eq(ShipmentType::getCode,shipmentHeader.getShipmentType())
                    .eq(ShipmentType::getWarehouseCode,shipmentHeader.getWarehouseCode());
        ShipmentType shipmentType =shipmentTypeService.getOne(shipmentTypeLamb);
        if(shipmentType == null){
            return AjaxResult.error("wms没有此出库类型");
        }

        //3、检查上游单号是否存在
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLamb = Wrappers.lambdaQuery();
        shipmentHeaderLamb.eq(ShipmentHeader::getWarehouseCode,shipmentHeader.getWarehouseCode())
                .eq(ShipmentHeader::getReferCode,shipmentHeader.getReferCode());
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLamb);
        if(shipmentHeaders.size() > 0 || shipmentHeaders != null){
            return AjaxResult.success("出库单已经下发");
        }

        //4、检查客户
        if(StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())){
            LambdaQueryWrapper<Customer> customerLamb = Wrappers.lambdaQuery();
            customerLamb.eq(Customer::getWarehouseCode,shipmentHeader.getWarehouseCode())
                    .eq(Customer::getCode,shipmentHeader.getCustomerCode());
            Customer customer = customerService.getOne(customerLamb);
            if(customer == null){
                return AjaxResult.error("wms没有此客户");
            }
        }
        return AjaxResult.success(shipmentHeader);
    }


    /**
     * 检查出库子单的合法性
     * @param shipmentDetails
     * @return
     */
    public  List<ShipmentDetail> checkShipmentDetail(List<ShipmentDetail> shipmentDetails,ShipmentHeader shipmentHeader){

        for(ShipmentDetail shipmentDetail : shipmentDetails) {
            //1、检查物料
            if(StringUtils.isEmpty(shipmentDetail.getMaterialCode())){
               throw new ServiceException("物料为空");
            }
            LambdaQueryWrapper<Material> materialLamb = Wrappers.lambdaQuery();
            materialLamb.eq(Material::getCode,shipmentDetail.getMaterialCode())
                .eq(Material::getWarehouseCode,shipmentHeader.getWarehouseCode());
            Material material = materialService.getOne(materialLamb);
            if(material == null){
                throw new ServiceException("wms没有此物料");
            }

            //2、赋值物料属性
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
        }
        return shipmentDetails;
pengcheng authored
227
228
    }
}