ICSReceiptAPIService.java 18.1 KB
package com.huaheng.pc.u8.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.pc.u8.domain.ICSReceiptDetail;
import com.huaheng.pc.u8.domain.ICSReceiptHeader;
import com.huaheng.pc.u8.domain.ICSReceiptModel;
import com.huaheng.api.general.domain.ReceiptDomain;
import com.huaheng.api.general.service.ReceiptApiService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.documentWarning.service.IDocumentWarningService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * u8-wms入库下发或回传接口服务层
 *  @author huaheng
 *  @date 2019-1-4
 *
 */

@Service
public class ICSReceiptAPIService {

    @Resource
    private WarehouseU8Service warehouseU8Service;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CompanyService companyService;
    @Resource
    private IDocumentWarningService documentWarningService;
    @Resource
    private ICSReceiptHeaderService icsReceiptHeaderService;
    @Resource
    private ICSReceiptDetailService icsReceiptDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ReceiptApiService receiptApiService;




    //入库单下发
    //采购入库
    @Transactional
    public AjaxResult procureReceipt(@RequestBody ICSReceiptModel icsReceiptModel) {
        ICSReceiptHeader icsReceiptHeader=icsReceiptModel.getIcsReceiptHeader();
        List<ICSReceiptDetail> icsReceiptDetails=icsReceiptModel.getIcsReceiptDetails();

        if(StringUtils.isEmpty(icsReceiptHeader.getIsNew())){
            return AjaxResult.error("isNew为空");
        }
        String type=icsReceiptHeader.getType();
        String warehouseCode = null;
        if (type.equals("TI")) {
            warehouseCode = icsReceiptHeader.getIWHCode();
        } else {
            warehouseCode = icsReceiptHeader.getCWHCode();
        }
        //通过u8的货主和仓库匹配wms的仓库
        LambdaQueryWrapper<WarehouseU8> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WarehouseU8::getUCompanyCode,icsReceiptHeader.getCompanyCode())
        .eq(StringUtils.isNotNull(warehouseCode),WarehouseU8::getWarehouseCode,warehouseCode);
        List<WarehouseU8> list = warehouseU8Service.list(queryWrapper);
        if (list.size()==0) {
            return AjaxResult.error("系统没有此仓库");
        }
        WarehouseU8 warehouseU8 = list.get(0);


        companyService.checkwarehouseCompany(warehouseU8.getCompanyCode());

        //根据icsReceiptHeader生成receiptHeader

        Date date = null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(type.equals("PCI") ) {
                date = sdf.parse(icsReceiptHeader.getDPODate());
            }else {
                date = sdf.parse(icsReceiptHeader.getDDate());
            }
        }catch (ParseException e){
            if(icsReceiptHeader.getCCode()!= null) {
                documentWarningService.ErpSendWarning(icsReceiptHeader.getCCode(), "时间转换异常");
            }else {
                documentWarningService.ErpSendWarning(icsReceiptHeader.getCTVCode(), "时间转换异常");
            }
            throw new ServiceException("时间转换异常");
        }
        ReceiptHeader receiptHeader=new ReceiptHeader();
        String maker = icsReceiptHeader.getCMaker();
        receiptHeader.setReceiptType(type);
        receiptHeader.setId(null);
        receiptHeader.setLastUpdatedBy(maker);
        receiptHeader.setCreatedBy(maker);
        receiptHeader.setLastUpdated(date);
        receiptHeader.setCreated(date);
        receiptHeader.setReceiptNote(icsReceiptHeader.getCMemo());
        receiptHeader.setSupplierCode(icsReceiptHeader.getCVenCode());
        receiptHeader.setWarehouseCode(warehouseU8.getWarehouseCode());
        receiptHeader.setUWarehouseCode(warehouseCode);
        receiptHeader.setCompanyCode(warehouseU8.getCompanyCode());
        if (type.equals("TI")) {
            receiptHeader.setReferCode(icsReceiptHeader.getCTVCode());
        } else if(type.equals("PCI")){
            receiptHeader.setReferCode(icsReceiptHeader.getCPOID());
        }else {
            receiptHeader.setReferCode(icsReceiptHeader.getCCode());
        }
        if (StringUtils.isEmpty(receiptHeader.getReferCode())) {
            documentWarningService.ErpSendWarning("", "没有单号");
            throw new ServiceException("没有单号");
        }
        checkReceiptBill(icsReceiptHeader,receiptHeader.getReferCode());
        //将主表插入到新的接收主表里去
        boolean save = icsReceiptHeaderService.save(icsReceiptHeader);
        if (!save) {
            documentWarningService.ErpSendWarning(receiptHeader.getReferCode(), "入库主单传输失败!");
            throw new ServiceException("入库主单传输失败!");
        }

        //将子表插入到新的接收子表里去
        for (ICSReceiptDetail item : icsReceiptDetails) {
            if (icsReceiptHeader.getType().equals("TI")) {
                item.setCCode(icsReceiptHeader.getCTVCode());
            } else {
                item.setCCode(icsReceiptHeader.getCCode());
            }
            boolean result1 = icsReceiptDetailService.save(item);
            if (!result1) {
                documentWarningService.ErpSendWarning(receiptHeader.getReferCode(), "入库子单传输失败!");
                throw new ServiceException("入库子单传输失败!");
            }
        }

        //根据icsShipmentDetail生成shipmentAetail
        List<ReceiptDetail> receiptDetails = new ArrayList<ReceiptDetail>();
        for (ICSReceiptDetail item : icsReceiptDetails) {
            //查找物料
            LambdaQueryWrapper<Material> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(Material::getCode,item.getCInvCode())
                    .eq(Material::getWarehouseCode,warehouseU8.getWarehouseCode());
            Material material = materialService.getOne(queryWrapper1);
            if (material == null) {
                documentWarningService.ErpSendWarning(receiptHeader.getReferCode(), "该物料不存在:" + item.getCInvCode());
                throw new ServiceException("该物料不存在:" + item.getCInvCode());
            }

            ReceiptDetail receiptDetail = new ReceiptDetail();
            receiptDetail.setId(null);
                receiptDetail.setAutoId(item.getAutoID());
            if (icsReceiptHeader.getType().equals("TI")) {
                receiptDetail.setAutoId(item.getITrIds());
            }
            receiptDetail.setReferCode(receiptHeader.getReferCode());
            if(StringUtils.isNotEmpty(item.getCDefine22())) {
                Boolean flag = StringUtils.isChineseChar(item.getCDefine22());
                if (item.getCDefine22().length() == 19 && flag == false) {
                    receiptDetail.setMoCode(item.getCDefine22());
                } else {
                    receiptDetail.setBatch(item.getCDefine22());
                }
            }                    receiptDetail.setLot(item.getCDefine23());
            receiptDetail.setWarehouseCode(warehouseU8.getWarehouseCode());
            receiptDetail.setMaterialCode(item.getCInvCode());
            receiptDetail.setZoneCode(material.getZoneCode());
            receiptDetail.setCreated(date);
            receiptDetail.setMaterialUnit(item.getCComUnitName());
            receiptDetail.setCreatedBy(maker);
            receiptDetail.setLastUpdated(date);
            receiptDetail.setLastUpdatedBy(maker);
            if (!item.getCompanyCode().equals(icsReceiptHeader.getCompanyCode())) {
                documentWarningService.ErpSendWarning(receiptHeader.getReferCode(), "子单货主与主单不对应");
                throw new ServiceException("子单货主与主单不对应");
            }
            receiptDetail.setUWarehouseCode(receiptHeader.getUWarehouseCode());
            receiptDetail.setCompanyCode(warehouseU8.getCompanyCode());
            receiptDetail.setProjectNo(item.getCItemName());
            receiptDetail.setQty(item.getIQuantity().abs());
//            receiptDetail.setQtyCompleted(item.getiQuantity());
            receiptDetail.setInventorySts(QuantityConstant.GOOD);
            if (item.getIrowno() != null && StringUtils.isNotEmpty(item.getIrowno().toString())) {
                receiptDetail.setReferLineNum(item.getIrowno().toString());
            }
            receiptDetails.add(receiptDetail);
        }

        //调用公共API
        ReceiptDomain receiptDomain = new ReceiptDomain();
        receiptDomain.setReceiptHeader(receiptHeader);
        receiptDomain.setReceiptDetails(receiptDetails);
        AjaxResult ajaxResult = receiptApiService.receipt(receiptDomain);
        if(ajaxResult.getCode()==200&&icsReceiptHeader.getIsNew().equals("1")){
            return AjaxResult.success("入库单下发成功");
        }else if (ajaxResult.getCode()==200&&icsReceiptHeader.getIsNew().equals("0")) {
            return AjaxResult.success("入库单更新成功");
        }else {
            return ajaxResult;
        }

    }

    /**
     * 采购到货 or 退货入库
     * 采购到货单 | 销售退货入库单
     * @param icsReceiptModel
     * @return
     * @throws ParseException
     */
    @Transactional
    public AjaxResult procureArrive(ICSReceiptModel icsReceiptModel){
        ICSReceiptHeader icsReceiptHeader = icsReceiptModel.getIcsReceiptHeader();
        List<ICSReceiptDetail> icsReceiptDetails = icsReceiptModel.getIcsReceiptDetails();

//        String currentWarehouseCode = icsReceiptHeader.getCWarehouseCode();
//        Warehouse warehouse = warehouseService.getOne(new LambdaQueryWrapper<Warehouse>()
//                .eq(Warehouse::getCode, currentWarehouseCode));
//        if(warehouse==null){
//            return AjaxResult.error("本系统中没有编号:【"+currentWarehouseCode+"】的仓库。");
//        }

        if (!icsReceiptHeader.getType().equals("SRI") && !icsReceiptHeader.getType().equals("PI")) {
            return AjaxResult.error("入库类型不匹配");
        }

        if(StringUtils.isEmpty(icsReceiptHeader.getIsNew())){
            return AjaxResult.error("isNew为空");
        }
        Date date = null;
        try{
            date = DateUtils.parseDate(icsReceiptHeader.getDPODate(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        }catch (ParseException e){
            if(icsReceiptHeader.getCCode()!= null) {
                documentWarningService.ErpSendWarning(icsReceiptHeader.getCCode(), "时间转换异常");
            }else {
                documentWarningService.ErpSendWarning(icsReceiptHeader.getCDLCode(), "时间转换异常");
            }
            return AjaxResult.error("时间:【"+icsReceiptHeader.getDPODate()+"】不能被转换,时间转换异常。");
        }

        String code=null;
        if("SRI".equals(icsReceiptHeader.getType())){
            code=icsReceiptHeader.getCDLCode();
        }else {
            code=icsReceiptHeader.getCCode();
        }
        if(StringUtils.isEmpty(code)){
            if("SRI".equals(icsReceiptHeader.getType())){
                documentWarningService.ErpSendWarning("", "销售退货入库单没有单号");
            }else {
                documentWarningService.ErpSendWarning("", "采购到货单下发没有单号");
            }
            return AjaxResult.error("没有单号");
        }

        return AjaxResult.success();
    }

    /**
     * 判断入库单是否可以更新
     * @param icsReceiptHeader   U8入库单
     * @param code  U8单号
     */
    private  void  checkReceiptBill(ICSReceiptHeader icsReceiptHeader,String code){
        LambdaQueryWrapper<ICSReceiptHeader> queryWrapper4 = Wrappers.lambdaQuery();
        if(icsReceiptHeader.getType().equals("TI")){
            queryWrapper4.eq(ICSReceiptHeader::getCTVCode,icsReceiptHeader.getCTVCode());
        }else if(icsReceiptHeader.getType().equals("SRI")){
            queryWrapper4.eq(ICSReceiptHeader::getCTVCode,icsReceiptHeader.getCDLCode());
        }else if(icsReceiptHeader.getType().equals("PCI")){
            queryWrapper4.eq(ICSReceiptHeader::getCTVCode,icsReceiptHeader.getCPOID());
        }else {
            queryWrapper4.eq(ICSReceiptHeader::getCTVCode,icsReceiptHeader.getCCode());
        }
        ICSReceiptHeader icsr = icsReceiptHeaderService.getOne(queryWrapper4);

        LambdaQueryWrapper<ReceiptHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReceiptHeader::getReferCode,code);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(queryWrapper);

        if (receiptHeader != null||icsr!=null) {
            if(icsReceiptHeader.getIsNew().equals("1")){
                throw  new ServiceException("单号重复,已跳过");
            } else if(icsReceiptHeader.getIsNew().equals("0")){
                if(receiptHeader!=null){

                    LambdaQueryWrapper<TaskDetail> queryWrapper3 = Wrappers.lambdaQuery();
                    queryWrapper3.eq(TaskDetail::getBillCode,receiptHeader.getCode());
                    List<TaskDetail> taskDetailList = taskDetailService.list(queryWrapper3);
                    if(taskDetailList!=null&&taskDetailList.size()>0){
                        taskDetailList.forEach(taskDetail -> {
                            Integer status = taskDetail.getStatus();
                            if(status<120){
                                throw  new ServiceException("更新失敗,此入库单存在未完成的任务");
                            }
                        });
                    }
                    LambdaQueryWrapper<ReceiptContainerDetail> queryWrapper1 = Wrappers.lambdaQuery();
                    queryWrapper1.eq(ReceiptContainerDetail::getReceiptCode,receiptHeader.getCode());
                    List<ReceiptContainerDetail> receiptContainerDetails = receiptContainerDetailService.list(queryWrapper1);

                    if(receiptContainerDetails!=null&&receiptContainerDetails.size()>0){
                        ReceiptContainerDetail receiptContainerDetail = receiptContainerDetails.get(0);

                        LambdaQueryWrapper<ReceiptContainerHeader> queryWrapper2 = Wrappers.lambdaQuery();
                        queryWrapper2.eq(ReceiptContainerHeader::getId,receiptContainerDetail.getReceiptContainerId());
                        ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(queryWrapper2);
                        if(receiptContainerHeader!=null){
                            if(receiptContainerHeader.getStatus()<20){
                                throw  new ServiceException("更新失敗,此入库单存在未完成的组盘");
                            }
                        }
                    }
                    receiptHeaderService.removeById(receiptHeader.getId());
                    LambdaQueryWrapper<ReceiptDetail> queryWrapper2 = Wrappers.lambdaQuery();
                    queryWrapper2.eq(ReceiptDetail::getReferCode,code);
                    receiptDetailService.remove(queryWrapper2);
                }
                icsReceiptHeaderService.removeById(icsr.getID());

                boolean remove = icsReceiptDetailService.remove(new LambdaQueryWrapper<ICSReceiptDetail>()
                        .eq(ICSReceiptDetail::getCCode, code));
            }
            else {
                throw new ServiceException("isNew值无法识别");
            }
        }
    }

    public Boolean isSameMonth(Date date){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(new Date());
        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);
        return isSameDate;
    }

}