CodingServiceImpl.java 5.79 KB
package com.huaheng.api.jindie.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.huaheng.api.general.domain.ReceiptDomain;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.api.general.service.ReceiptApiService;
import com.huaheng.api.general.service.ShipmentApiService;
import com.huaheng.api.jindie.domain.GoodReceiptDomain;
import com.huaheng.api.jindie.domain.GoodReceiving;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
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.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class CodingServiceImpl implements CodingService{

    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptApiService receiptApiService;
    @Resource
    private ShipmentApiService shipmentApiService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult goodReceiving(GoodReceiving goodReceiving) {
        Integer qc = goodReceiving.getQc();
        Integer accessory = goodReceiving.getAccessory();
        String referCode = goodReceiving.getReferCode();
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>().eq(ReceiptHeader::getReferCode, referCode));
        if(receiptHeader==null){
//            return AjaxResult.error("系统为查询到此单据:"+referCode);
            return AjaxResult.success();
        }
        List<ReceiptDetail> receiptDetails = receiptDetailService.list(new LambdaQueryWrapper<ReceiptDetail>().eq(ReceiptDetail::getReferCode, referCode));
        if(CollectionUtils.isEmpty(receiptDetails)){
//            return AjaxResult.error("系统为查询到此单据明细:"+referCode);
            return AjaxResult.success();
        }
        if(StringUtils.isNotNull(receiptHeader.getQc())&&StringUtils.isNotNull(receiptHeader.getAccessory())){
//            return AjaxResult.error("已提交过质检:"+referCode);
            return AjaxResult.success();
        }
        if(qc==null){
            return AjaxResult.error("qc:为空:"+referCode);
        }
        if(accessory==null){
            return AjaxResult.error("accessory:为空:"+referCode);
        }
        receiptHeader.setQc(qc);
        receiptHeader.setAccessory(accessory);
        receiptHeaderService.updateById(receiptHeader);
        /** step1 判断是否合格 */
        if(QuantityConstant.GOOD_RECEIVING_QC==qc){
            /** step1.1 生成来料退货单*/
            AjaxResult cgtl = createShipment(referCode, receiptHeader, receiptDetails, "CGTL");
            if(cgtl.hasErr()){
                return AjaxResult.error("质检不合格时生成退料单失败"+cgtl.getMsg());
            }else {
                return AjaxResult.success("质检不合格时生成退料单成功");
            }
        }
        /** step2 判断是否配件包 */
        if(QuantityConstant.GOOD_RECEIVING_ACCESSORY==accessory){
            /** step2.1 是 生成调拨单 */
            AjaxResult zjdb = createShipment(referCode, receiptHeader, receiptDetails, "ZJDB");
            if(zjdb.hasErr()){
                return AjaxResult.error("是配件包时生成调拨单失败"+zjdb.getMsg());
            }else {
                return AjaxResult.success("是配件包时生成调拨单成功");
            }
        }
        return AjaxResult.success();
    }
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createShipment(String referCode,ReceiptHeader receiptHeader,List<ReceiptDetail> receiptDetails,String shipmentType){
        ShipmentDomain shipmentDomain=new ShipmentDomain();
        ShipmentHeader shipmentHeader=new ShipmentHeader();
        shipmentHeader.setWarehouseCode("CS0001");
        shipmentHeader.setCompanyCode("WB");
        shipmentHeader.setReferCode(referCode);
        shipmentHeader.setReferCodeType(receiptHeader.getReceiptType());
        shipmentHeader.setShipmentType(shipmentType);
        shipmentHeader.setReferLineId(receiptHeader.getReferId());
        shipmentHeader.setCreatedBy(receiptHeader.getCreatedBy());
        shipmentHeader.setSourcePlatform("ERP");
        shipmentDomain.setShipmentHeader(shipmentHeader);
        List<ShipmentDetail> shipmentDetails=new ArrayList<>();
        receiptDetails.forEach(receiptDetail -> {
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setWarehouseCode("CS0001");
            shipmentDetail.setCompanyCode("WB");
            shipmentDetail.setReferCode(referCode);
            shipmentDetail.setMaterialCode(receiptDetail.getMaterialCode());
            shipmentDetail.setMaterialName(receiptDetail.getMaterialName());
            shipmentDetail.setBatch(receiptDetail.getBatch());
            shipmentDetail.setLineId(receiptDetail.getLineId());
            shipmentDetail.setShipQty(receiptDetail.getTotalQty());
            shipmentDetails.add(shipmentDetail);
        });
        shipmentDomain.setShipmentDetails(shipmentDetails);
        return  shipmentApiService.shipment(shipmentDomain);
    }

}