ShippingCombinationService.java 6.93 KB
package com.huaheng.pc.shipment.shippingCombination.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.pc.config.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.configValue.domain.ConfigValue;
import com.huaheng.pc.config.configValue.service.ConfigValueService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialServiceImpl;
import com.huaheng.pc.config.materialType.domain.MaterialType;
import com.huaheng.pc.config.materialType.service.MaterialTypeService;
import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shippingCombination.mapper.ShippingCombinationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class ShippingCombinationService {

    @Resource
    private ShippingCombinationMapper shippingCombinationMapper;
    @Autowired
    InventoryDetailService inventoryDetailService;
    @Autowired
    ConfigValueService configValueService;
    @Autowired
    FilterConfigDetailService filterConfigDetailService;
    @Autowired
    ShipmentPreferenceService shipmentPreferenceService;
    @Autowired
    MaterialServiceImpl materialService;
    @Autowired
    MaterialTypeService materialTypeService;



    //根据分配规则查找库存
    public List<InventoryDetail> getInventorys(ShipmentDetail shipmentDetail) {
        LambdaQueryWrapper<ConfigValue> configValueLambdaQueryWrapper=Wrappers.lambdaQuery();
        configValueLambdaQueryWrapper.eq(ConfigValue::getModuleType,"shipment")
                .eq(ConfigValue::getWarehouseCode,shipmentDetail.getWarehouseCode());
        ConfigValue configValue=configValueService.getOne(configValueLambdaQueryWrapper);
        if(configValue==null){
            throw new ServiceException("仓库的出库配置不存在");
        }

        //查找出库首选项
        LambdaQueryWrapper<ShipmentPreference> slam=Wrappers.lambdaQuery();
        slam.eq(ShipmentPreference::getCode,configValue.getIdentifier())
                .eq(ShipmentPreference::getWarehouseCode,configValue.getWarehouseCode());
        ShipmentPreference shipmentPreference=shipmentPreferenceService.getOne(slam);
        if(shipmentPreference==null){
            throw new ServiceException("仓库的出库配置中出库首选项不存在");
        }

        List<InventoryDetail> list=new ArrayList<>();
        FilterConfigDetail filterConfigDetail=new FilterConfigDetail();
        LambdaQueryWrapper<FilterConfigDetail> filterConfigDetailLambdaQueryWrapper= Wrappers.lambdaQuery();
        filterConfigDetailLambdaQueryWrapper.eq(FilterConfigDetail::getWarehouseCode,shipmentDetail.getWarehouseCode());

        //出库子单的分配规则有时,优先出库子单的分配规则
        if(StringUtils.isNotEmpty(shipmentDetail.getAllocationRule())){
            filterConfigDetailLambdaQueryWrapper.eq(FilterConfigDetail::getCode,shipmentDetail.getAllocationRule());
            filterConfigDetail=filterConfigDetailService.getOne(filterConfigDetailLambdaQueryWrapper);
            if(filterConfigDetail==null){
                throw new ServiceException("出库子单出库规则配置不存在");
            }

            //根据sql查库存
            try {
                list = inventoryDetailService.selectBysql(filterConfigDetail.getStatement(),shipmentDetail,filterConfigDetail.getStatements(),shipmentPreference);
            }catch (Exception e){
                throw new ServiceException("sql错误");
            }
            return list;
        }

        //出库子单的分配规则没有时,优先物料的分配规则
        LambdaQueryWrapper<Material> materialLambdaQueryWrapper=Wrappers.lambdaQuery();
        materialLambdaQueryWrapper.eq(Material::getCode,shipmentDetail.getMaterialCode())
                .eq(Material::getWarehouseCode,shipmentDetail.getWarehouseCode());
        Material material=materialService.getOne(materialLambdaQueryWrapper);
        if(StringUtils.isNotEmpty(material.getAllocationRule())){
            filterConfigDetailLambdaQueryWrapper.eq(FilterConfigDetail::getCode,material.getAllocationRule());
            filterConfigDetail=filterConfigDetailService.getOne(filterConfigDetailLambdaQueryWrapper);
            if(filterConfigDetail==null){
                throw new ServiceException("物料出库规则配置不存在");
            }

            //根据sql查库存
            list=inventoryDetailService.selectBysql(filterConfigDetail.getStatement(),shipmentDetail,filterConfigDetail.getStatements(),shipmentPreference);
            return list;
        }

        //物料的分配规则没有时,优先物料种类的分配规则
        LambdaQueryWrapper<MaterialType> materialTypelam=Wrappers.lambdaQuery();
        materialTypelam.eq(MaterialType::getCode,material.getType())
                .eq(MaterialType::getWarehouseCode,shipmentDetail.getWarehouseCode());
        MaterialType materialType=materialTypeService.getOne(materialTypelam);
        if(StringUtils.isNotEmpty(materialType.getAllocationRule())){
            filterConfigDetailLambdaQueryWrapper.eq(FilterConfigDetail::getCode,materialType.getAllocationRule());
            filterConfigDetail=filterConfigDetailService.getOne(filterConfigDetailLambdaQueryWrapper);
            if(filterConfigDetail==null){
                throw new ServiceException("物料种类出库规则配置不存在");
            }

            //根据sql查库存
            list=inventoryDetailService.selectBysql(filterConfigDetail.getStatement(),shipmentDetail,filterConfigDetail.getStatements(),shipmentPreference);
            return list;
        }

        //都没有时,默认仓库的分配规则

        //查找分配规则
        filterConfigDetailLambdaQueryWrapper.eq(FilterConfigDetail::getCode,shipmentPreference.getAllocationRule());
        filterConfigDetail=filterConfigDetailService.getOne(filterConfigDetailLambdaQueryWrapper);
        if(filterConfigDetail==null){
            throw new ServiceException("出库首选项中出库规则配置不存在");
        }

        //根据sql查库存
        list=inventoryDetailService.selectBysql(filterConfigDetail.getStatement(),shipmentDetail,filterConfigDetail.getStatements(),shipmentPreference);
        return list;
    }

}