ShipmentPreferenceServiceImpl.java 7.38 KB
package com.huaheng.pc.config.shipmentPreference.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.config.configValue.domain.ConfigValue;
import com.huaheng.pc.config.configValue.service.ConfigValueService;
import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference;
import com.huaheng.pc.config.shipmentPreference.mapper.ShipmentPreferenceMapper;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowDetail;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowHeader;
import com.huaheng.pc.config.statusFlow.service.StatusFlowDetailService;
import com.huaheng.pc.config.statusFlow.service.StatusFlowHeaderService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("shipmentPreference")
public class ShipmentPreferenceServiceImpl extends ServiceImpl<ShipmentPreferenceMapper, ShipmentPreference> implements ShipmentPreferenceService {

    @Autowired
    private ShipmentPreferenceService shipmentPreferenceService;
    @Autowired
    private StatusFlowHeaderService statusFlowHeaderService;
    @Autowired
    private StatusFlowDetailService statusFlowDetailService;
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;
    @Autowired
    private ConfigValueService configValueService;


    /**
     * 1、找出出库首选项
     * 2、查看出库首选项的出库流程
     * 3、找到该状态流的出库流程明细
     * 4、判断单据是否按出库流程操作
     *
     * @param ids    出库单id
     * @param status 状态流
     * @return
     */
    @Override
    public List<ShipmentHeader> checkShipmentProcess(String ids, Integer status, String code) {

        LambdaQueryWrapper<ConfigValue> configValueLambdaQueryWrapper = Wrappers.lambdaQuery();
        configValueLambdaQueryWrapper.eq(ConfigValue::getModuleType, "shipment")
                .eq(ConfigValue::getWarehouseCode, ShiroUtils.getWarehouseCode());
        ConfigValue configValue = configValueService.getOne(configValueLambdaQueryWrapper);
        if (configValue == null) {
            throw new ServiceException("仓库的出库配置不存在");
        }

        //查找出库首选项
        ShipmentPreference shipmentPreference = shipmentPreferenceService.list().get(0);
        if (shipmentPreference == null) {
            throw new ServiceException("系统没有设置出库首选");
        }
        if (StringUtils.isEmpty(shipmentPreference.getShippingFlow())) {
            throw new ServiceException("出库首选没有绑定出库流程");
        }

        //查看出库流程
        LambdaQueryWrapper<StatusFlowHeader> statusFlowHeaderLam = Wrappers.lambdaQuery();
        statusFlowHeaderLam.eq(StatusFlowHeader::getCode, shipmentPreference.getShippingFlow())
                .eq(StatusFlowHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        StatusFlowHeader statusFlowHeader = statusFlowHeaderService.getOne(statusFlowHeaderLam);
        if (statusFlowHeader == null) {
            throw new ServiceException("出库首选绑定的出库流程不在系统中");
        }
        //查看流程明细
        LambdaQueryWrapper<StatusFlowDetail> statusFlowDetailLamb = Wrappers.lambdaQuery();
        statusFlowDetailLamb.eq(StatusFlowDetail::getHeaderId, statusFlowHeader.getId())
                .eq(StatusFlowDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(StatusFlowDetail::getFlowCode, status.toString());
        StatusFlowDetail statusFlowDetail = statusFlowDetailService.getOne(statusFlowDetailLamb);

        List<ShipmentHeader> shipmentHeaderList = new ArrayList<>();
        if (statusFlowDetail != null  && statusFlowDetail.getNessary().intValue() == 1) {
            if (StringUtils.isNotEmpty(ids)) {
                for (Integer id : Convert.toIntArray(ids)) {
                    //判断单据是否按出库流程操作
                    ShipmentHeader shipmentHeader = shipmentHeaderService.getById(id);
                    if (shipmentHeader == null || shipmentHeader.getFirstStatus() < status && statusFlowDetail.getNessary() == 1) {
                        throw new ServiceException("出库单ID为" + id + "的单据状态不对,请按照出库流程先审核单据");
                    }
                    if (shipmentHeader.getLastStatus() > QuantityConstant.SHIPMENT_HEADER_WAVE) {
                        throw new ServiceException("出库单ID为" + id + "的单据无法加入波次");
                    }
                    shipmentHeaderList.add(shipmentHeader);
                }
            } else {
                LambdaQueryWrapper<ShipmentHeader> lam = Wrappers.lambdaQuery();
                lam.eq(ShipmentHeader::getCode, code)
                        .eq(ShipmentHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
                ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(lam);
                if (shipmentHeader == null || shipmentHeader.getFirstStatus() < status) {
                    throw new ServiceException("单据状态不对,此操作不符合出库流程,请按照出库流程出库");
                }
                if (shipmentHeader.getLastStatus() > QuantityConstant.SHIPMENT_HEADER_WAVE) {
                    throw new ServiceException("出库单" + code + "的单据无法加入波次");
                }
                shipmentHeaderList.add(shipmentHeader);
            }
        }

        return shipmentHeaderList;
    }

    /**
     * 复制出库首选项表
     *
     * @param warehouseCode    原仓库编码
     * @param newWarehouseCode 新仓库编码
     * @return 是否复制成功
     */
    @Override
    @Transactional
    public boolean shipmentPreferenceCopy(String warehouseCode, String newWarehouseCode) {
        log.trace("开始复制出库首选项表");
        LambdaQueryWrapper<ShipmentPreference> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentPreference::getWarehouseCode, newWarehouseCode);
        if (!this.list(lambdaQueryWrapper).isEmpty()) {
            log.error("该仓库已存在");
            return false;
        }

        lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentPreference::getWarehouseCode, warehouseCode);
        List<ShipmentPreference> shipmentPreferenceList = this.list(lambdaQueryWrapper);

        for (ShipmentPreference shipmentPreference : shipmentPreferenceList) {
            shipmentPreference.setId(null);
            shipmentPreference.setWarehouseCode(newWarehouseCode);
        }

        if (this.saveBatch(shipmentPreferenceList)) {
            log.trace("复制出库首选项成功,新仓库编码是:" + newWarehouseCode);
            return true;
        } else {
            return false;
        }
    }
}