Blame view

src/main/java/com/huaheng/pc/srm/service/ICSASNService.java 19.2 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
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;
xumiao authored
12
import com.huaheng.common.utils.http.OkHttpUtils;
周鸿 authored
13
14
15
16
17
18
19
20
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;
tongzhonghao authored
21
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
周鸿 authored
22
23
24
25
import com.huaheng.pc.srm.domain.*;
import com.huaheng.pc.srm.mapper.ICSASNMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
tongzhonghao authored
26
import org.springframework.http.HttpHeaders;
周鸿 authored
27
28
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
周鸿 authored
29
import org.springframework.transaction.interceptor.TransactionAspectSupport;
周鸿 authored
30
31
32
33

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
周鸿 authored
34
import java.util.stream.Collectors;
周鸿 authored
35
36
37
38
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

@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();
65
66
        queryWrapper.eq(StringUtils.isNotEmpty(icsasn.getSTNO()), ICSASN::getSTNO, icsasn.getSTNO());
        queryWrapper.eq(StringUtils.isNotEmpty(icsasn.getWarehouseCode()), ICSASN::getWarehouseCode, icsasn.getWarehouseCode());
周鸿 authored
67
68
        return this.list(queryWrapper);
    }
69
周鸿 authored
70
    public List<ICSASN> selectBySTNO(List<String> STNOS) {
71
72
        if (STNOS != null && STNOS.size() > 0) {
            String[] split = STNOS.toArray(new String[0]);
周鸿 authored
73
            LambdaQueryWrapper<ICSASN> queryWrapper = Wrappers.lambdaQuery();
74
            queryWrapper.in(ICSASN::getSTNO, split);
周鸿 authored
75
76
77
78
            return this.list(queryWrapper);
        }
        return null;
    }
周鸿 authored
79
80
81
82
83
84
85


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

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

        return srmResult.success("送货单列表推送成功");
    }
221
    public SrmHeader getSrmHeader(ICSASN icsasn) {
周鸿 authored
222
223
224
225
226
227
228
        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());
229
        srmHeader.setWarehouseCode(icsasn.getWarehouseCode());
周鸿 authored
230
231
232
233
        return srmHeader;

    }
234
    public SrmDetail getSrmDetail(ICSASNDetail icsasnDetail, Integer headerId, CompanyU8 companyWu) {
周鸿 authored
235
236
237
238
239
240
241
242
243
244
245
246
247
248
        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());
249
        srmDetail.setWarehouseCode(icsasnDetail.getWarehouseCode());
250
251
        srmDetail.setProductionBatch(icsasnDetail.getCdefine22());
        srmDetail.setBatchNumberContent(icsasnDetail.getCdefine23());
周鸿 authored
252
253
254
255
        return srmDetail;
    }
周鸿 authored
256
    public Address getSRMAddress(String warehouseCode) {
周鸿 authored
257
258
        Address address = new Address();
        address.setParam("SRM");
周鸿 authored
259
        address.setWarehouseCode(warehouseCode);
260
        address = addressService.selectEntity(address);
周鸿 authored
261
262
263
264
265
        return address;
    }


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


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

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

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


    //入库返回
338
    public AjaxResult returnICSRuternASN(List<ICSRuternASN> icsRuternASNs, String warehouseCode) {
周鸿 authored
339
        String json = JSON.toJSONString(icsRuternASNs);
340
        logger.info("WMS入库回传:{}", json);
周鸿 authored
341
        Address address = this.getSRMAddress(warehouseCode);
342
        if (address == null) {
周鸿 authored
343
344
345
            return AjaxResult.error("未配置SRM API地址");
        }
346
        String url = address.getUrl() + "WareHouseLotInfo";
周鸿 authored
347
        String JsonParam = JSON.toJSONString(icsRuternASNs);
348
349
        SRMResult srmResult = this.sendReturn(url, JsonParam, warehouseCode);
        if (!srmResult.hasError()) {
周鸿 authored
350
            return AjaxResult.success("成功");
351
352
        } else {
            logger.info("SRM入库返回信息:{}", srmResult.getMessage());
周鸿 authored
353
354
355
356
357
            return AjaxResult.error(srmResult.getMessage());
        }
    }
周鸿 authored
358
359
360
    @Transactional
    public AjaxResult delASNAndDetail(List<ICSASN> list) {
        SRMResult srmResult = new SRMResult();
361
        if (list.isEmpty()) {
周鸿 authored
362
363
364
365
            return AjaxResult.error("请勿提交空数据!");
        }

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

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