ICSASNService.java 22 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 65 66 67 68 69 70 71 72 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
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.*;
import com.huaheng.common.constant.QuantityConstant;
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.OkHttpUtils;
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.domain.Supplier;
import com.huaheng.pc.config.supplier.service.SupplierService;
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.srm.domain.*;
import com.huaheng.pc.srm.mapper.ICSASNMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@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;
    @Resource
    private WarehouseU8Service warehouseU8Service;


    public List<ICSASN> selectListEntity(ICSASN icsasn) {
        LambdaQueryWrapper<ICSASN> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(icsasn.getSTNO()), ICSASN::getSTNO, icsasn.getSTNO());
        queryWrapper.eq(StringUtils.isNotEmpty(icsasn.getWarehouseCode()), ICSASN::getWarehouseCode, icsasn.getWarehouseCode());
        List<ICSASN> list= this.list(queryWrapper);
        return list;
    }

    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;
    }


    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();
        //1、判断货主(账套号)
        if (StringUtils.isEmpty((icsasn.getCompanyCode()))) {
            return srmResult.error("账套号为空");
        }
        if (StringUtils.isEmpty((icsasn.getWarehouseCode()))) {
            return srmResult.error("仓库code为空");
        }
//        if (StringUtils.isEmpty((icsasn.getPurchaseCode()))) {
////            return srmResult.error("采购code为空");
//        }
        CompanyU8 companyWu = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getWarehouseCode, icsasn.getWarehouseCode())
                .like(CompanyU8::getUCompanyCode, icsasn.getCompanyCode())
                .last("order by id asc limit 1")
        );
        if (companyWu == null) {
            return srmResult.error(icsasn.getCompanyCode() + "账套号不存在");
        }
        //2、判断供应商是否存在
        Supplier supplier = supplierService.getSuppierByCode(icsasn.getVENDORCODE());
        if(supplier==null){
            runFailException.sendException("头表ID为:" + icsasn.getID() + "的送货单供应商:"+icsasn.getVENDORCODE()+"在wms不存在");
            return srmResult.error("头表ID为:" + icsasn.getID() + "的送货单供应商:"+icsasn.getVENDORCODE()+"在wms不存在");
        }
//        String supplierName = supplierService.getSuppierNameByCode(icsasn.getVENDORCODE());
//        if (supplierName == null) {
//        }
        //3、操作头表icsasn和srmHeader
        Integer headerId = null;
        ICSASN queryHeaderEntry = new ICSASN();
        queryHeaderEntry.setSTNO(icsasn.getSTNO());
        queryHeaderEntry.setWarehouseCode(icsasn.getWarehouseCode());
        if (this.selectListEntity(queryHeaderEntry).size() > 0) {
            //①判断送货单头表是否存在,已存在,则更新
            if (!this.updateById(icsasn)) {
                runFailException.sendException("更新送货单头表失败,头表ID为:" + icsasn.getID());
                return srmResult.error("更新送货单头表失败,头表ID为:" + icsasn.getID());
            }
            SrmHeader srmHeader = new SrmHeader();
            srmHeader.setCode(icsasn.getSTNO());
            srmHeader.setWarehouseCode(icsasn.getWarehouseCode());
            srmHeader = srmHeaderService.selectFirstModel(srmHeader);
            SrmHeader model = this.getSrmHeader(icsasn);
            if(srmHeader!=null){
                model.setId(srmHeader.getId());
            }
            srmHeaderService.saveOrUpdate(model);
            headerId = model.getId();
        } else {
            //②判断送货单头表是否存在,不存在,则插入
            if (!this.save(icsasn)) {
                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();
        }
        //3、操作明细表icsasndetail和srmDetail
        if (icsOrder.getIcsasnDetailList().size() > 0) {
            //①icsasnDetail添加物料信息
            for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                if (StringUtils.isEmpty((icsasnDetail.getPurchaseCode()))) {
                    return srmResult.error("采购code为空");
                }
                if(icsasnDetail.getPmdt002()==null){
                    runFailException.sendException("ID为:" + icsasnDetail.getID() + "的采购项次Pmdt002为空");
                    return srmResult.error("ID为:" + icsasnDetail.getID() + "的采购项次Pmdt002为空");
                }
                if(icsasnDetail.getPmdt003()==null){
                    runFailException.sendException("ID为:" + icsasnDetail.getID() + "的采购项序Pmdt003为空");
                    return srmResult.error("ID为:" + icsasnDetail.getID() + "的采购项序Pmdt003为空");
                }
                if(icsasnDetail.getPmdt004()==null){
                    runFailException.sendException("ID为:" + icsasnDetail.getID() + "的采购分批序Pmdt004为空");
                    return srmResult.error("ID为:" + icsasnDetail.getID() + "的采购分批序Pmdt004为空");
                }
                //查询SRM有没有推送物料条码
//                ICSITEMLot queryIcsitemLot = new ICSITEMLot();
//                queryIcsitemLot.setLotNO(icsasnDetail.getLOTNO());
//                queryIcsitemLot.setWarehouseCode(icsasn.getWarehouseCode());
//                ICSITEMLot icsitemLot = icsitemLotService.selectFirstModel(queryIcsitemLot);
//                if (icsitemLot == null) {
//                    runFailException.sendException("ID为:" + icsasnDetail.getID() + "的物料条码没有同步到wms");
//                    return srmResult.error("ID为:" + icsasnDetail.getID() + "的物料条码没有同步到wms");
//                }
                String materialCode=icsasnDetail.getMaterialCode();
                Material material = materialService.getMaterialByCode(materialCode);
//                Material material = materialService.getMaterialByCode(icsitemLot.getItemCode(), icsasn.getWarehouseCode());
                if (material == null) {
                    runFailException.sendException("ID为:" + icsasnDetail.getID() + "的物料:"+materialCode+"条码在wms不存在");
                    return srmResult.error("ID为:" + icsasnDetail.getID() + "的物料:"+materialCode+"在wms不存在");
                }
                icsasnDetail.setMaterialCode(material.getCode());
                icsasnDetail.setMaterialName(material.getName());
                icsasnDetail.setSpec(material.getSpec());
                icsasnDetail.setMaterialUnit(material.getUnit());
                icsasnDetail.setWarehouseCode(icsasn.getWarehouseCode());
            }
            //②查询送货单号STNO是否存在明细
            ICSASNDetail queryDetailEntry = new ICSASNDetail();
            queryDetailEntry.setSTNO(icsasn.getSTNO());
            queryDetailEntry.setWarehouseCode(icsasn.getWarehouseCode());
            List<ICSASNDetail> icsasnDetailList = icsasnDetailService.selectListEntity(queryDetailEntry);
            if (icsasnDetailList.size() > 0) {
                //③送货单号STNO已存在的明细进行更新
                for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                    //查询srmdetail表,如果有该条码
                    queryDetailEntry.setID(icsasnDetail.getID());
                    queryDetailEntry.setWarehouseCode(icsasnDetail.getWarehouseCode());
                    if (icsasnDetailService.selectListEntity(queryDetailEntry).size() > 0) {
                        if (!icsasnDetailService.updateById(icsasnDetail)) {
                            runFailException.sendException("更新送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("更新送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = new SrmDetail();
                        srmDetail.setMark(icsasnDetail.getID());
                        srmDetail.setWarehouseCode(icsasnDetail.getWarehouseCode());
                        srmDetail = srmDetailService.selectFirstModel(srmDetail);
                        SrmDetail model = this.getSrmDetail(icsasnDetail, srmDetail.getHeaderId(), companyWu);
                        model.setId(srmDetail.getId());
                        srmDetailService.updateById(model);
                    } else {
                        if (!icsasnDetailService.save(icsasnDetail)) {
                            runFailException.sendException("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = this.getSrmDetail(icsasnDetail, headerId, companyWu);
                        srmDetailService.save(srmDetail);
                    }
                }
            } else {
                //④送货单号STNO不存在的明细进行新增
                for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                    if (icsasnDetail.getSTNO().equals(icsasn.getSTNO())) {
                        if (!icsasnDetailService.save(icsasnDetail)) {
                            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());
        srmHeader.setWarehouseCode(icsasn.getWarehouseCode());
//        srmHeader.setPurchaseCode(icsasn.getPurchaseCode());
        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.setMaterialUnit(icsasnDetail.getMaterialUnit());
        srmDetail.setQty(icsasnDetail.getPLANQTY());
        srmDetail.setConfirmQty(icsasnDetail.getRECEIVEQTY());
        srmDetail.setOperator(icsasnDetail.getMUSERName());
        srmDetail.setOperatorTime(icsasnDetail.getMTIME());
        srmDetail.setCreated(new Date());
        srmDetail.setWarehouseCode(icsasnDetail.getWarehouseCode());
        srmDetail.setProductionBatch(icsasnDetail.getCdefine22());
        srmDetail.setBatchNumberContent(icsasnDetail.getCdefine23());
        if(srmDetail.getMaterialCode().startsWith("1")){
            srmDetail.setCheckCode(2);
        }
        srmDetail.setPmdt002(icsasnDetail.getPmdt002());
        srmDetail.setPmdt003(icsasnDetail.getPmdt003());
        srmDetail.setPmdt004(icsasnDetail.getPmdt004());
        srmDetail.setPurchaseCode(icsasnDetail.getPurchaseCode());
        if(StringUtils.isNotEmpty(icsasnDetail.getUWarehouseCode())){
            WarehouseU8 warehouseU8 = warehouseU8Service.getWarehouseCode(srmDetail.getWarehouseCode(),icsasnDetail.getUWarehouseCode());
            if (warehouseU8 != null){
                srmDetail.setErpWarehouseCode(warehouseU8.getErpCode());
                srmDetail.setUWarehouseCode(warehouseU8.getUWarehouseCode());
            }
        }
        return srmDetail;
    }


    public Address getSRMAddress(String warehouseCode) {
        Address address = new Address();
        address.setParam("SRM-V");
        address.setWarehouseCode(warehouseCode);
        address = addressService.selectEntity(address);
        return address;
    }


    @Transactional
    public SRMResult sendReturn(String url, String json, String warehouseCode) {
        if (StringUtils.isEmpty(warehouseCode)) {
            warehouseCode = "CS0001";
        }
        SRMResult ajaxResult = new SRMResult();
        String msg = "";
        ApiLog log = null;
        HttpHeaders headers = new HttpHeaders();
//        try {
//            log = ApiLogAspect.initApiLog("post", url,
//                    json, headers, warehouseCode);
//            log.setWarehouseCode(warehouseCode);
            String result = OkHttpUtils.bodypost(url, json, warehouseCode);
            if (StringUtils.isEmpty(result)) {
                return new SRMResult().error("接口地址错误或服务器连接不到或返回为空");
//                throw new ServiceException("接口地址错误或服务器连接不到或返回为空");
            }
            ajaxResult = JSON.parseObject(result, SRMResult.class);
//        } catch (Exception e) {
//            e.printStackTrace();
////            ApiLogAspect.setApiLogException(log, e);
//        } finally {
////            ApiLogAspect.finishApiLog(log, headers, msg);
//        }
        return ajaxResult;
    }


    //采购到货单返回
    public AjaxResult returnICSPOArrive(List<ICSPOArrive> icspoArrives, String warehouseCode) {
        String json = JSON.toJSONString(icspoArrives);
//        logger.info("WMS送货单确认回传:{}", json);
        Address address = this.getSRMAddress(warehouseCode);
        if (address == null) {
            return AjaxResult.error("未配置SRM API地址");
        }
        String url = address.getUrl() + "POArrive";
        String JsonParam = JSON.toJSONString(icspoArrives);
        logger.info("WMS送货单提交SRM:{}", JsonParam);
        SRMResult srmResult = this.sendReturn(url, JsonParam, warehouseCode);
        logger.info("SRM送货单返回信息:{}", srmResult);
        if (!srmResult.hasError()) {
            return AjaxResult.success(srmResult.getData());
        } else {
            logger.info("SRM采购到货单回传:{}", srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }

    //质检返回
    public AjaxResult returnICSinspection(List<ICSinspection> icSinspections, String warehouseCode) {
        String json = JSON.toJSONString(icSinspections);
        logger.info("WMS质检信息回传:{}", json);
        Address address = this.getSRMAddress(warehouseCode);
        if (address == null) {
            return AjaxResult.error("未配置SRM API地址");
        }

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

        String JsonParam = JSON.toJSONString(icSinspections);
        SRMResult srmResult = this.sendReturn(url, JsonParam, warehouseCode);
        if (!srmResult.hasError()) {
            return AjaxResult.success("成功");
        } else {
            logger.info("SRM质检返回信息:{}", srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }


    //入库返回
    public AjaxResult returnICSRuternASN(List<ICSRuternASN> icsRuternASNs, String warehouseCode) {
        String json = JSON.toJSONString(icsRuternASNs);
        logger.info("WMS入库回传:{}", json);
        Address address = this.getSRMAddress(warehouseCode);
        if (address == null) {
            return AjaxResult.error("未配置SRM API地址");
        }

        String url = address.getUrl() + "WareHouseLotInfo";
        String JsonParam = JSON.toJSONString(icsRuternASNs);
        SRMResult srmResult = this.sendReturn(url, JsonParam, warehouseCode);
        if (!srmResult.hasError()) {
            return AjaxResult.success("成功");
        } else {
            logger.info("SRM入库返回信息:{}", srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }


    @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("送货单号不能为空值!");
        }
        List<String> warehouseCodes = list.stream().map(ICSASN::getWarehouseCode).collect(Collectors.toList());
        if (warehouseCodes.size() == 0) {
            return AjaxResult.error("仓库code不能为空值!");
        }
        // 查询送货单头表
        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("送货单删除成功!");
    }
}