Blame view

src/main/java/com/huaheng/pc/srm/service/ICSASNService.java 19 KB
周鸿 authored
1
2
3
4
5
6
package com.huaheng.pc.srm.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.srm.domain.*;
周鸿 authored
7
import com.huaheng.common.constant.QuantityConstant;
周鸿 authored
8
9
10
11
12
import com.huaheng.common.exception.RunFailException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.http.HttpUtils;
xumiao authored
13
import com.huaheng.common.utils.http.OkHttpUtils;
tongzhonghao authored
14
import com.huaheng.framework.aspectj.ApiLogAspect;
周鸿 authored
15
16
17
18
19
20
21
22
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.company.domain.CompanyU8;
import com.huaheng.pc.config.company.service.CompanyU8Service;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.supplier.service.SupplierService;
xumiao authored
23
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
tongzhonghao authored
24
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
周鸿 authored
25
26
27
28
29
import com.huaheng.pc.srm.domain.*;
import com.huaheng.pc.srm.mapper.ICSASNDetailMapper;
import com.huaheng.pc.srm.mapper.ICSASNMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
tongzhonghao authored
30
import org.springframework.http.HttpHeaders;
周鸿 authored
31
32
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
周鸿 authored
33
import org.springframework.transaction.interceptor.TransactionAspectSupport;
周鸿 authored
34
35
36
37

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
周鸿 authored
38
import java.util.stream.Collectors;
周鸿 authored
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

@Service
public class ICSASNService extends ServiceImpl<ICSASNMapper, ICSASN> {

    private static final Logger logger = LoggerFactory.getLogger(ICSASNService.class);

    @Resource
    private ICSASNService icsasnService;
    @Resource
    private ICSASNDetailService icsasnDetailService;
    @Resource
    RunFailException runFailException;
    @Resource
    SupplierService supplierService;
    @Resource
    private MaterialService materialService;
    @Resource
    private SrmHeaderService srmHeaderService;
    @Resource
    private SrmDetailService srmDetailService;
    @Resource
    private ICSITEMLotService icsitemLotService;
    @Resource
    private AddressService addressService;
    @Resource
    private CompanyU8Service companyU8Service;


    public List<ICSASN> selectListEntity(ICSASN icsasn) {
        LambdaQueryWrapper<ICSASN> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(icsasn.getSTNO()),ICSASN::getSTNO,icsasn.getSTNO());
xumiao authored
70
        queryWrapper.eq(StringUtils.isNotEmpty(icsasn.getWarehouseCode()),ICSASN::getWarehouseCode,icsasn.getWarehouseCode());
周鸿 authored
71
72
        return this.list(queryWrapper);
    }
周鸿 authored
73
74
75
76
77
78
79
80
81
    public List<ICSASN> selectBySTNO(List<String> STNOS) {
        if(STNOS!=null&&STNOS.size()>0){
            String[] split=STNOS.toArray(new String[0]);
            LambdaQueryWrapper<ICSASN> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(ICSASN::getSTNO,split);
            return this.list(queryWrapper);
        }
        return null;
    }
周鸿 authored
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97


    public boolean update(ICSASN icsasn) {
        return this.update(icsasn);
    }

    /**
     *  接收送货单信息
     *
     * @param icsOrder
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public SRMResult saveASNAndDetail(ICSOrder icsOrder) {
        SRMResult srmResult = new SRMResult();
        ICSASN icsasn = icsOrder.getIcsasn();
xumiao authored
98
        //1、判断货主(账套号)
周鸿 authored
99
100
101
        if(StringUtils.isEmpty((icsasn.getCompanyCode()))){
            return srmResult.error("账套号为空");
        }
102
103
104
        if(StringUtils.isEmpty((icsasn.getWarehouseCode()))){
            return srmResult.error("仓库code为空");
        }
xumiao authored
105
106
107
108
109
        CompanyU8 companyWu = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getWarehouseCode,icsasn.getWarehouseCode())
                .like(CompanyU8::getUCompanyCode,icsasn.getCompanyCode())
                .last("order by id asc limit 1")
        );
周鸿 authored
110
111
112
        if(companyWu == null){
            return srmResult.error(icsasn.getCompanyCode() +"账套号不存在");
        }
xumiao authored
113
114
        //2、判断供应商是否存在
        String supplierName = supplierService.getSuppierNameByCode(icsasn.getVENDORCODE(),icsasn.getWarehouseCode());
周鸿 authored
115
116
117
118
        if(supplierName == null){
            runFailException.sendException("头表ID为:" + icsasn.getID()+"的送货单供应商在wms不存在");
            return srmResult.error("头表ID为:" + icsasn.getID()+"的送货单供应商在wms不存在");
        }
xumiao authored
119
        //3、操作头表icsasn和srmHeader
周鸿 authored
120
121
122
        Integer headerId = null;
        ICSASN queryHeaderEntry = new ICSASN();
        queryHeaderEntry.setSTNO(icsasn.getSTNO());
xumiao authored
123
        queryHeaderEntry.setWarehouseCode(icsasn.getWarehouseCode());
周鸿 authored
124
        if (this.selectListEntity(queryHeaderEntry).size() > 0) {
xumiao authored
125
            //①判断送货单头表是否存在,已存在,则更新
xumiao authored
126
            if (!this.updateById(icsasn)) {
周鸿 authored
127
128
129
130
131
                runFailException.sendException("更新送货单头表失败,头表ID为:" + icsasn.getID());
                return srmResult.error("更新送货单头表失败,头表ID为:" + icsasn.getID());
            }
            SrmHeader srmHeader = new SrmHeader();
            srmHeader.setCode(icsasn.getSTNO());
xumiao authored
132
            srmHeader.setWarehouseCode(icsasn.getWarehouseCode());
周鸿 authored
133
134
135
136
137
138
            srmHeader = srmHeaderService.selectFirstModel(srmHeader);
            SrmHeader model = this.getSrmHeader(icsasn);
            model.setId(srmHeader.getId());
            srmHeaderService.updateById(model);
            headerId = srmHeader.getId();
        } else {
xumiao authored
139
            //②判断送货单头表是否存在,不存在,则插入
xumiao authored
140
            if (!this.save(icsasn)) {
周鸿 authored
141
142
143
144
145
146
147
148
149
150
                runFailException.sendException("插入送货单头表失败,头表ID为:" + icsasn.getID());
                return srmResult.error("插入送货单头表失败,头表ID为:" + icsasn.getID());
            }
            SrmHeader model = this.getSrmHeader(icsasn);
            srmHeaderService.save(model);
            SrmHeader srmHeader = new SrmHeader();
            srmHeader.setCode(model.getCode());
            srmHeader = srmHeaderService.selectFirstModel(srmHeader);
            headerId = srmHeader.getId();
        }
xumiao authored
151
        //3、操作明细表icsasndetail和srmDetail
周鸿 authored
152
        if (icsOrder.getIcsasnDetailList().size() > 0) {
xumiao authored
153
            //①icsasnDetail添加物料信息
周鸿 authored
154
            for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
xumiao authored
155
                //查询SRM有没有推送物料条码
周鸿 authored
156
157
                ICSITEMLot queryIcsitemLot = new ICSITEMLot();
                queryIcsitemLot.setLotNO(icsasnDetail.getLOTNO());
xumiao authored
158
                queryIcsitemLot.setWarehouseCode(icsasn.getWarehouseCode());
周鸿 authored
159
160
161
162
163
                ICSITEMLot icsitemLot = icsitemLotService.selectFirstModel(queryIcsitemLot);
                if(icsitemLot == null){
                    runFailException.sendException("ID为:"+icsasnDetail.getID()+"的物料条码没有同步到wms");
                    return srmResult.error("ID为:"+icsasnDetail.getID()+"的物料条码没有同步到wms");
                }
xumiao authored
164
                Material material = materialService.getMaterialByCode(icsitemLot.getItemCode(),icsasn.getWarehouseCode());
周鸿 authored
165
166
167
168
169
170
171
                if(material == null){
                    runFailException.sendException("ID为:"+icsasnDetail.getID()+"的物料条码在wms不存在");
                    return srmResult.error("ID为:"+icsasnDetail.getID()+"的物料条码在wms不存在");
                }
                icsasnDetail.setMaterialCode(material.getCode());
                icsasnDetail.setMaterialName(material.getName());
                icsasnDetail.setSpec(material.getSpec() );
xumiao authored
172
                icsasnDetail.setWarehouseCode(material.getWarehouseCode());
周鸿 authored
173
            }
xumiao authored
174
            //②查询送货单号STNO是否存在明细
周鸿 authored
175
176
            ICSASNDetail queryDetailEntry = new ICSASNDetail();
            queryDetailEntry.setSTNO(icsasn.getSTNO());
xumiao authored
177
            queryDetailEntry.setWarehouseCode(icsasn.getWarehouseCode());
周鸿 authored
178
179
            List<ICSASNDetail> icsasnDetailList = icsasnDetailService.selectListEntity(queryDetailEntry);
            if (icsasnDetailList.size() > 0) {
xumiao authored
180
                //③送货单号STNO已存在的明细进行更新
周鸿 authored
181
182
                for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                    queryDetailEntry.setID(icsasnDetail.getID());
xumiao authored
183
                    queryDetailEntry.setWarehouseCode(icsasnDetail.getWarehouseCode());
周鸿 authored
184
                    if (icsasnDetailService.selectListEntity(queryDetailEntry).size() > 0) {
xumiao authored
185
                        if (!icsasnDetailService.updateById(icsasnDetail)) {
周鸿 authored
186
187
188
189
190
                            runFailException.sendException("更新送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("更新送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = new SrmDetail();
                        srmDetail.setMark(icsasnDetail.getID());
xumiao authored
191
                        srmDetail.setWarehouseCode(icsasnDetail.getWarehouseCode());
周鸿 authored
192
193
194
195
196
                        srmDetail = srmDetailService.selectFirstModel(srmDetail);
                        SrmDetail model = this.getSrmDetail(icsasnDetail,srmDetail.getHeaderId(),companyWu);
                        model.setId(srmDetail.getId());
                        srmDetailService.updateById(model);
                    } else {
xumiao authored
197
                        if (!icsasnDetailService.save(icsasnDetail) ) {
周鸿 authored
198
199
200
201
202
203
204
205
                            runFailException.sendException("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = this.getSrmDetail(icsasnDetail,headerId,companyWu);
                        srmDetailService.save(srmDetail);
                    }
                }
            } else {
xumiao authored
206
                //④送货单号STNO不存在的明细进行新增
周鸿 authored
207
208
                for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                    if(icsasnDetail.getSTNO().equals(icsasn.getSTNO())) {
xumiao authored
209
                        if (!icsasnDetailService.save(icsasnDetail) ) {
周鸿 authored
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
                            runFailException.sendException("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = this.getSrmDetail(icsasnDetail, headerId,companyWu);
                        srmDetailService.save(srmDetail);
                    }
                }
            }
        }

        return srmResult.success("送货单列表推送成功");
    }


    public SrmHeader getSrmHeader(ICSASN icsasn){
        SrmHeader srmHeader = new SrmHeader();
        srmHeader.setCode(icsasn.getSTNO());
        srmHeader.setMark(icsasn.getID());
        srmHeader.setSupplierCode(icsasn.getVENDORCODE());
        srmHeader.setOperator(icsasn.getMUSERName());
        srmHeader.setOperatorTime(icsasn.getMTIME());
        srmHeader.setCreated(new Date());
232
        srmHeader.setWarehouseCode(icsasn.getWarehouseCode());
周鸿 authored
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
        return srmHeader;

    }

    public SrmDetail getSrmDetail(ICSASNDetail icsasnDetail,Integer headerId,CompanyU8 companyWu) {
        SrmDetail srmDetail = new SrmDetail();
        srmDetail.setHeaderId(headerId);
        srmDetail.setCode(icsasnDetail.getSTNO());
        srmDetail.setCompanyCode(companyWu.getUCompanyCode());
        srmDetail.setMark(icsasnDetail.getID());
        srmDetail.setLineCode(icsasnDetail.getLOTNO());
        srmDetail.setMaterialCode(icsasnDetail.getMaterialCode());
        srmDetail.setMaterialName(icsasnDetail.getMaterialName());
        srmDetail.setSpecification(icsasnDetail.getSpec());
        srmDetail.setQty(icsasnDetail.getPLANQTY());
        srmDetail.setConfirmQty(icsasnDetail.getRECEIVEQTY());
        srmDetail.setOperator(icsasnDetail.getMUSERName());
        srmDetail.setOperatorTime(icsasnDetail.getMTIME());
        srmDetail.setCreated(new Date());
252
        srmDetail.setWarehouseCode(icsasnDetail.getWarehouseCode());
253
254
        srmDetail.setProductionBatch(icsasnDetail.getCdefine22());
        srmDetail.setBatchNumberContent(icsasnDetail.getCdefine23());
周鸿 authored
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
        return srmDetail;
    }



    public Address getSRMAddress(){
        Address address = new Address();
        address.setParam("SRM");
        address.setWarehouseCode("KS0001");
        address=addressService.selectEntity(address);
        return address;
    }


    @Transactional
tongzhonghao authored
270
    public SRMResult sendReturn(String url,String json,String warehouseCode){
周鸿 authored
271
272
273
        if(StringUtils.isEmpty(warehouseCode)){
            warehouseCode="CS0001";
        }
tongzhonghao authored
274
275
276
277
278
279
280
        SRMResult ajaxResult = new SRMResult().error("");
        String msg = "";
        ApiLog log = null;
        HttpHeaders headers = new HttpHeaders();
        try{
            log = ApiLogAspect.initApiLog("post", url,
                    json, headers, warehouseCode);
xumiao authored
281
            String result = OkHttpUtils.bodypost(url, json,warehouseCode);
tongzhonghao authored
282
283
284
285
286
287
288
289
290
            if(StringUtils.isEmpty(result)){
                throw new ServiceException("接口地址错误或服务器连接不到或返回为空");
            }
            ajaxResult = JSON.parseObject(result, SRMResult.class);
        }catch (Exception e){
            e.printStackTrace();
            ApiLogAspect.setApiLogException(log, e);
        }finally {
            ApiLogAspect.finishApiLog(log, headers, msg);
周鸿 authored
291
        }
tongzhonghao authored
292
        return ajaxResult;
周鸿 authored
293
294
295
296
    }


    //采购到货单返回
周鸿 authored
297
    public AjaxResult returnICSPOArrive(List<ICSPOArrive> icspoArrives,String warehouseCode) {
周鸿 authored
298
299
300
301
302
303
304
305
306
        String json = JSON.toJSONString(icspoArrives);
        logger.info("WMS送货单确认回传:{}",json);
       Address address = this.getSRMAddress();
       if(address == null){
           return AjaxResult.error("未配置SRM API地址");
       }
       String url = address.getUrl() +"POArrive";
       String JsonParam = JSON.toJSONString(icspoArrives);
tongzhonghao authored
307
308
309
        SRMResult srmResult = this.sendReturn(url,JsonParam,warehouseCode);
        logger.info("SRM送货单返回信息:{}",srmResult);
        if(!srmResult.hasError()){
周鸿 authored
310
311
312
313
314
315
316
317
            return AjaxResult.success(srmResult.getData());
        }else {
            logger.info("SRM采购到货单回传:{}",srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }

    //质检返回
周鸿 authored
318
    public AjaxResult returnICSinspection(List<ICSinspection> icSinspections,String warehouseCode) {
周鸿 authored
319
320
321
322
323
324
325
326
327
328
        String json = JSON.toJSONString(icSinspections);
        logger.info("WMS质检信息回传:{}",json);
        Address address = this.getSRMAddress();
        if(address == null){
            return AjaxResult.error("未配置SRM API地址");
        }

        String url = address.getUrl() +"POTest";

        String JsonParam = JSON.toJSONString(icSinspections);
tongzhonghao authored
329
330
        SRMResult srmResult = this.sendReturn(url,JsonParam,warehouseCode);
        if(!srmResult.hasError()){
周鸿 authored
331
332
333
334
335
336
337
338
339
            return AjaxResult.success("成功");
        }else {
            logger.info("SRM质检返回信息:{}",srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }


    //入库返回
周鸿 authored
340
    public AjaxResult returnICSRuternASN(List<ICSRuternASN> icsRuternASNs,String warehouseCode) {
周鸿 authored
341
342
343
344
345
346
347
348
349
        String json = JSON.toJSONString(icsRuternASNs);
        logger.info("WMS入库回传:{}",json);
        Address address = this.getSRMAddress();
        if(address == null){
            return AjaxResult.error("未配置SRM API地址");
        }

        String url = address.getUrl() +"WareHouseLotInfo";
        String JsonParam = JSON.toJSONString(icsRuternASNs);
tongzhonghao authored
350
351
        SRMResult srmResult = this.sendReturn(url,JsonParam,warehouseCode);
        if(!srmResult.hasError()){
周鸿 authored
352
353
354
355
356
357
358
359
            return AjaxResult.success("成功");
        }else {
            logger.info("SRM入库返回信息:{}",srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }
周鸿 authored
360
361
362
363
364
365
366
367
368
369
370
    @Transactional
    public AjaxResult delASNAndDetail(List<ICSASN> list) {
        SRMResult srmResult = new SRMResult();
        if (list.isEmpty()){
            return AjaxResult.error("请勿提交空数据!");
        }

        List<String> STNOS = list.stream().map(ICSASN::getSTNO).collect(Collectors.toList());
        if (STNOS.size() == 0){
            return AjaxResult.error("送货单号不能为空值!");
        }
371
372
373
374
        List<String> warehouseCodes = list.stream().map(ICSASN::getWarehouseCode).collect(Collectors.toList());
        if (warehouseCodes.size() == 0){
            return AjaxResult.error("仓库code不能为空值!");
        }
周鸿 authored
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
        // 查询送货单头表
        List<ICSASN> icsasnList = selectBySTNO(STNOS);
        // 差未到送货单头表
        List<SrmHeader> srmHeaderList = srmHeaderService.selectBySTNO(STNOS);
        if (srmHeaderList.size() == 0 || icsasnList.size() == 0){
            return AjaxResult.success("未查询到送货单信息,请检查送货单号是否正确!");
        }
        for (SrmHeader header : srmHeaderList){
            // 不是未到货单不允许删除
            if (!QuantityConstant.NO_THE_ARRIVAL.equals(header.getEnable())){
                return AjaxResult.error("送货单号:"+ header.getCode()+ "单据已执行不允许删除!");
            }
        }
        // 查询送货单详情表
        List<ICSASNDetail> icsasnDetailList = icsasnDetailService.selectBySTNO(STNOS);
        List<SrmDetail> srmDetailList = srmDetailService.selectBySTNO(STNOS);
        if (icsasnList.size() == srmHeaderList.size() && icsasnDetailList.size() == srmDetailList.size()){
            try {
                this.removeByIds(icsasnList.stream().map(ICSASN::getID).collect(Collectors.toList()));
                icsasnDetailService.removeByIds(icsasnDetailList.stream().map(ICSASNDetail::getID).collect(Collectors.toList()));
                srmHeaderService.removeByIds(srmHeaderList.stream().map(SrmHeader::getId).collect(Collectors.toList()));
                srmDetailService.removeByIds(srmDetailList.stream().map(SrmDetail::getId).collect(Collectors.toList()));
            }catch (Exception e){
                // 删除失败事务回滚
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("系统异常:"+e.getMessage());
            }

        }else {
            return AjaxResult.error("送货单数据条目不匹配,请检查数据是否正常!");
        }

        return AjaxResult.success("送货单删除成功!");
    }
周鸿 authored
410
}