ICSASNService.java 13.4 KB
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.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;
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;
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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@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());
        return this.list(queryWrapper);
    }

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

    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();

        //判断账套号
        if(StringUtils.isEmpty((icsasn.getCompanyCode()))){
            return srmResult.error("账套号为空");
        }

        CompanyU8 companyWu = new CompanyU8();
        companyWu.setUCompanyCode(icsasn.getCompanyCode());
        companyWu = companyU8Service.getByCode(companyWu);
        if(companyWu == null){
            return srmResult.error(icsasn.getCompanyCode() +"账套号不存在");
        }


        //查看供应商是否存在

        String supplierName = supplierService.getSuppierNameByCode(icsasn.getVENDORCODE());
        if(supplierName == null){
            runFailException.sendException("头表ID为:" + icsasn.getID()+"的送货单供应商在wms不存在");
            return srmResult.error("头表ID为:" + icsasn.getID()+"的送货单供应商在wms不存在");
        }

        Integer headerId = null;
        ICSASN queryHeaderEntry = new ICSASN();
        queryHeaderEntry.setSTNO(icsasn.getSTNO());
        if (this.selectListEntity(queryHeaderEntry).size() > 0) {
            if (!this.update(icsasn)) {
                runFailException.sendException("更新送货单头表失败,头表ID为:" + icsasn.getID());
                return srmResult.error("更新送货单头表失败,头表ID为:" + icsasn.getID());
            }


            //修改本地送货单头表
            SrmHeader srmHeader = new SrmHeader();
            srmHeader.setCode(icsasn.getSTNO());
            srmHeader = srmHeaderService.selectFirstModel(srmHeader);

            SrmHeader model = this.getSrmHeader(icsasn);
            model.setId(srmHeader.getId());
            srmHeaderService.updateById(model);
            headerId = srmHeader.getId();
        } else {
            if (!this.insert(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();
        }
        if (icsOrder.getIcsasnDetailList().size() > 0) {
            for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                ICSITEMLot queryIcsitemLot = new ICSITEMLot();
                queryIcsitemLot.setLotNO(icsasnDetail.getLOTNO());
                ICSITEMLot icsitemLot = icsitemLotService.selectFirstModel(queryIcsitemLot);
                if(icsitemLot == null){
                    runFailException.sendException("ID为:"+icsasnDetail.getID()+"的物料条码没有同步到wms");
                    return srmResult.error("ID为:"+icsasnDetail.getID()+"的物料条码没有同步到wms");
                }

                Material material = materialService.getMaterialByCode(icsitemLot.getItemCode());
                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() );
            }
            ICSASNDetail queryDetailEntry = new ICSASNDetail();
            queryDetailEntry.setSTNO(icsasn.getSTNO());
            List<ICSASNDetail> icsasnDetailList = icsasnDetailService.selectListEntity(queryDetailEntry);
            if (icsasnDetailList.size() > 0) {
                for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                    queryDetailEntry.setID(icsasnDetail.getID());
                    if (icsasnDetailService.selectListEntity(queryDetailEntry).size() > 0) {
                        if (!icsasnDetailService.update(icsasnDetail)) {
                            runFailException.sendException("更新送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("更新送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = new SrmDetail();
                        srmDetail.setMark(icsasnDetail.getID());
                        srmDetail = srmDetailService.selectFirstModel(srmDetail);
                        SrmDetail model = this.getSrmDetail(icsasnDetail,srmDetail.getHeaderId(),companyWu);
                        model.setId(srmDetail.getId());

                        srmDetailService.updateById(model);
                    } else {
                        if (!icsasnDetailService.insert(icsasnDetail) ) {
                            runFailException.sendException("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                            return srmResult.error("插入送货单明细失败,头表ID为:" + icsasnDetail.getID());
                        }
                        SrmDetail srmDetail = this.getSrmDetail(icsasnDetail,headerId,companyWu);
                        srmDetailService.save(srmDetail);
                    }
                }
            } else {
                for (ICSASNDetail icsasnDetail : icsOrder.getIcsasnDetailList()) {
                    if(icsasnDetail.getSTNO().equals(icsasn.getSTNO())) {
                        if (!icsasnDetailService.insert(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());
        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());
        return srmDetail;
    }



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


    @Transactional
    public String sendReturn(String url,String json){
        String result = HttpUtils.bodypost(url, json,"CS0001");
        if(StringUtils.isEmpty(result)){
            throw new ServiceException("SRM接口地址错误或服务器连接不到或返回为空");
        }
        return result;
    }


    //采购到货单返回
    public AjaxResult returnICSPOArrive(List<ICSPOArrive> icspoArrives) {
        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);

       String result = this.sendReturn(url,JsonParam);
        logger.info("SRM送货单返回信息:{}",result);
        SRMResult srmResult = JSON.parseObject(result, SRMResult.class);
        if(srmResult.isSuccess() == true){
            return AjaxResult.success(srmResult.getData());
        }else {
            logger.info("SRM采购到货单回传:{}",srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }

    //质检返回
    public AjaxResult returnICSinspection(List<ICSinspection> icSinspections) {
        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);
        String result = this.sendReturn(url,JsonParam);
        SRMResult srmResult = JSON.parseObject(result, SRMResult.class);
        if(srmResult.isSuccess() == true){
            return AjaxResult.success("成功");
        }else {
            logger.info("SRM质检返回信息:{}",srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }


    //入库返回
    public AjaxResult returnICSRuternASN(List<ICSRuternASN> icsRuternASNs) {
        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);
        String result = this.sendReturn(url,JsonParam);
        SRMResult srmResult = JSON.parseObject(result, SRMResult.class);
        if(srmResult.isSuccess() == true){
            return AjaxResult.success("成功");
        }else {
            logger.info("SRM入库返回信息:{}",srmResult.getMessage());
            return AjaxResult.error(srmResult.getMessage());
        }
    }


}