ShipmentApiService.java 14.8 KB
package com.huaheng.api.general.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.domain.ReceiptDomain;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ricard
 * @time   19/11/11
 *
 */


@Component
@Transactional
public class ShipmentApiService {

    @Autowired
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private ShipmentDetailService shipmentDetailService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CustomerServiceImpl customerService;

    @Autowired
    private ShipmentTypeService shipmentTypeService;

    @Autowired
    private MaterialService materialService;

    @Resource
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;

    @Resource
    private ShipmentDetailHistoryService shipmentDetailHistoryService;

    /**
     *  出库单下发
     *
     * @param shipmentDomain
     * @return
     */
    @Transactional
    public AjaxResult shipment(ShipmentDomain shipmentDomain){

        //1、判断出库主单和子单列是否为空
        ShipmentHeader shipmentHeader = shipmentDomain.getShipmentHeader();
        List<ShipmentDetail> shipmentDetails = shipmentDomain.getShipmentDetails();
        if(shipmentHeader == null){
            return AjaxResult.error("出库主单为空");
        }
        if (shipmentDetails == null || shipmentDetails.size() < 1) {
            return AjaxResult.error("出库子单为空");
        }

        //2、检查出库主单的合法性
        AjaxResult ajaxResult = checkShipmentHeader(shipmentHeader);
        if (ajaxResult.hasErr()) {
            return AjaxResult.error(ajaxResult.getMsg());
        }
        //3、检查出库子单的合法性
        AjaxResult result = checkShipmentDetail(shipmentDetails);
        if (result.hasErr()) {
            return AjaxResult.error(result.getMsg());
        }

        //4、保存出库主表
        BigDecimal totalQty = new BigDecimal(0);
        for (ShipmentDetail item: shipmentDetails) {
            totalQty =  totalQty.add(item.getQty());
        }

        shipmentHeader.setTotalLines(shipmentDetails.size());
        shipmentHeader.setTotalQty(totalQty);

        String name="";
        if (shipmentHeader.getCreatedBy().trim().length()!=0) {
            name=":"+shipmentHeader.getCreatedBy();
        }
        shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_ERP+name);
        if (StringUtils.isNotEmpty(shipmentHeader.getShipmentNote())) {
            shipmentHeader.setShipmentNote(shipmentHeader.getShipmentNote());
        }
        if (StringUtils.isNotEmpty(shipmentHeader.getContainerCode())) {
            shipmentHeader.setContainerCode(shipmentHeader.getContainerCode());
        }
        if (StringUtils.isNotEmpty(shipmentHeader.getManage())) {
            shipmentHeader.setManage(shipmentHeader.getManage());
        }
        if (StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())) {
            shipmentHeader.setCustomerCode(shipmentHeader.getCustomerCode());
        }

        shipmentHeader.setWarehouseCode("CS0001");
        shipmentHeader.setCompanyCode("BHF");
        //String shipmentCode = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
        shipmentHeader.setCode(shipmentHeader.getReferCode());
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);//新建
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);//新建

        if(!shipmentHeaderService.save(shipmentHeader)){
            throw new ServiceException("保存出库主表失败");
        }

        //5、保存出库子表
        shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getCode, shipmentHeader.getCode()));
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
        for(ShipmentDetail shipmentDetail : shipmentDetails){
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
            if (StringUtils.isNotEmpty((shipmentDetail.getBatch()))) {
                shipmentDetail.setBatch(shipmentDetail.getBatch());
            }
            if (StringUtils.isNotEmpty((shipmentDetail.getLabelCode()))) {
                shipmentDetail.setBatch(shipmentDetail.getLabelCode());
            }
            shipmentDetail.setWarehouseCode("CS0001");
            shipmentDetail.setCompanyCode("BHF");
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode,shipmentDetail.getMaterialCode()));
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
            shipmentDetail.setMaterialCode(material.getCode());
            if (!shipmentDetailService.save(shipmentDetail)) {
                throw new ServiceException("保存入库明细失败");
            }
            shipmentDetailList.add(shipmentDetail);
        }
        /* 6.step回传出库单和出库明细单 */
        ShipmentDomain shipment = new ShipmentDomain();
        shipment.setShipmentHeader(shipmentHeader);
        shipment.setShipmentDetails(shipmentDetailList);
        //return AjaxResult.success(shipment);
        return AjaxResult.success();
    }


    /**
     * 检查出库主单的合法性
     * @param shipmentHeader
     * @return
     */
    public AjaxResult checkShipmentHeader(ShipmentHeader shipmentHeader){
        if (StringUtils.isEmpty(shipmentHeader.getReferCode()) ||  StringUtils.isEmpty(shipmentHeader.getShipmentType())) {
            return AjaxResult.error("出库单号或出库单类型为空");
        }

        //2、判断出库类型
        if (shipmentTypeService.getOne(new LambdaQueryWrapper<ShipmentType>().eq(ShipmentType::getCode, shipmentHeader.getShipmentType())) == null) {
            return AjaxResult.error("wms没有此出库类型");
        }

        //3、检查上游单号是否存在
        if (shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getReferCode, shipmentHeader.getReferCode()))!= null) {
            return AjaxResult.error("该单据已存在:" + shipmentHeader.getReferCode());
        }

        //4、检查客户
        //if(StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())){
        //    LambdaQueryWrapper<Customer> customerLamb = Wrappers.lambdaQuery();
        //    customerLamb.eq(Customer::getWarehouseCode,shipmentHeader.getWarehouseCode())
        //            .eq(Customer::getCode,shipmentHeader.getCustomerCode());
        //    Customer customer = customerService.getOne(customerLamb);
        //    if(customer == null){
        //        return AjaxResult.error("wms没有此客户");
        //    }
        //}
        return AjaxResult.success(shipmentHeader);
    }


    /**
     * 检查出库子单的合法性
     * @param shipmentDetails
     * @return
     */
    public AjaxResult checkShipmentDetail(List<ShipmentDetail> shipmentDetails){
        for(ShipmentDetail shipmentDetail : shipmentDetails) {
            /*判断必填字段是否为空且总数量不能为0*/
            if (shipmentDetail.getMaterialCode() == null ||
                    new BigDecimal(0).compareTo(shipmentDetail.getQty() != null ?
                            shipmentDetail.getQty() : new BigDecimal(0)) == 0) {
                return AjaxResult.error("出库明细字段有误,MaterialCode或qty");
            }

            //检查物料
            String materialCode = shipmentDetail.getMaterialCode();
            if(StringUtils.isEmpty(materialCode)){
               throw new ServiceException("物料为空");
            }
            if(materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode,materialCode)) == null) {
                return AjaxResult.error(materialCode + "WMS不存在此物料:",materialCode);
            }
        }
        return AjaxResult.success("");
    }

    @Transactional
    public AjaxResult remove(List<String> shipmentCodeList) {
        LambdaQueryWrapper<ShipmentHeader> headerQueryWrapper;
        for (String shipmentCode : shipmentCodeList) {
            headerQueryWrapper = Wrappers.lambdaQuery();
            headerQueryWrapper.eq(ShipmentHeader::getCode,shipmentCode);
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerQueryWrapper);
            if (shipmentHeader == null) {
                return AjaxResult.success("");
            }
            if ((shipmentHeader.getFirstStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN && shipmentHeader.getLastStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN) ||
                    (shipmentHeader.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_POOL & shipmentHeader.getLastStatus() < QuantityConstant.SHIPMENT_HEADER_POOL)) {
                ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                List<ShipmentDetailHistory> shipmentDetailHistoryList = new ArrayList<>();
                //查询入库单明细
                LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode);
                List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);

                //复制到入库历史实体
                com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentHeaderHistory, shipmentHeader);
                for (ShipmentDetail shipmentDetail : list) {
                    ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                    com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentDetailHistory, shipmentDetail);
                    shipmentDetailHistoryList.add(shipmentDetailHistory);
                }


                shipmentHeaderHistory.setLastUpdatedBy(ShiroUtils.getName());
                if (!shipmentHeaderService.removeById(shipmentHeader.getId())) {
                    throw new ServiceException("删除头表失败");
                }
                if (!shipmentHeaderHistoryService.save(shipmentHeaderHistory)) {
                    throw new ServiceException("新增历史出库单失败");
                }
                // 当存在明细时删除
                if (list.size() != 0) {
                    //删除入库明细
                    List<Integer> shipmentDetailIds = new ArrayList<>();
                    for (int i = 0; i < shipmentDetailHistoryList.size(); i++) {
                        shipmentDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getName());
                        shipmentDetailHistoryList.get(i).setShipmentId(shipmentHeaderHistory.getId());
                        shipmentDetailIds.add(shipmentDetailHistoryList.get(i).getId());
                    }
                    if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
                        throw new ServiceException("删除明细表失败");
                    }
                    if (!shipmentDetailHistoryService.saveBatch(shipmentDetailHistoryList)) {
                        throw new ServiceException("新增明细失败");
                    }
                }
            } else {
                return AjaxResult.success("出库单没有完成,无法删除");
            }
        }
        return AjaxResult.success("删除成功");
    }

    public AjaxResult search(String shipmentCode,String companyCode,String warehouseCode){
        if (companyCode==null){
            return AjaxResult.error("货主编码不能为空");
        }
        if (warehouseCode==null){
            return AjaxResult.error("仓库编码不能为空");
        }
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
        headerLambdaQuery.eq(ShipmentHeader::getWarehouseCode,warehouseCode)
                .eq(ShipmentHeader::getCompanyCode,companyCode)
                .eq(ShipmentHeader::getCode,shipmentCode);
        detailLambdaQuery.eq(ShipmentDetail::getWarehouseCode,warehouseCode)
                .eq(ShipmentDetail::getCompanyCode,companyCode)
                .eq(ShipmentDetail::getShipmentCode,shipmentCode);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetailList);
        return AjaxResult.success("查询成功",shipmentDomain);
    }
}