OverrideHandleServiceImpl.java 9.76 KB
package com.huaheng.api.wcs.service.overrideHandle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.api.wcs.domain.TaskFinishDomain;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationService;
import com.huaheng.api.wcs.service.warecellAllocation.WarecellAllocationService;
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.framework.web.service.ConfigService;
import com.huaheng.pc.config.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
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.receiving.service.ReceivingService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class OverrideHandleServiceImpl implements OverrideHandleService {


    @Autowired
    private TaskHeaderService taskHeaderService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Autowired
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private WarecellAllocationService warecellAllocationService;
    @Resource
    private FilterConfigDetailService filterConfigDetailService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private ReceivingService receivingService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationAllocationService locationAllocationService;
    @Resource
    private ConfigService configService;
    @Resource
    private MaterialService materialService;
    /**
     * 重入处理
     * 1、判断非空字段
     * 2、根据任务号查找任务
     * 3、修改任务目的库位,修改入库组盘的库位,修改库位状态
     */

    @Override
    @Transactional
    public AjaxResult OverrideHandle(TaskFinishDomain taskFinishDomain) {

        String taskNo = taskFinishDomain.getTaskNo();

        //1、判断非空字段
        if(StringUtils.isEmpty(taskNo)) {
            return AjaxResult.error("任务号为空");
        }
        if(StringUtils.isEmpty(taskFinishDomain.getWarehouseCode())){
            throw new ServiceException("仓库号为空");
        }
        //2、根据任务号查找任务
        TaskHeader taskHeader = taskHeaderService.getById(taskNo);
        if(taskHeader == null){
            return AjaxResult.error("任务号错误,没有找到该任务");
        }
        String warehouseCode = taskHeader.getWarehouseCode();
        int status = taskHeader.getStatus();
        if(status == QuantityConstant.TASK_STATUS_COMPLETED) {
            return AjaxResult.error("任务已完成");
        }

        //3、修改任务目的库位,修改入库组盘的库位,修改库位状态
        //修改原来目的库位的状态
        LambdaQueryWrapper<Location> locationLam = Wrappers.lambdaQuery();
        locationLam.eq(Location::getCode,taskHeader.getToLocation())
                .eq(Location::getWarehouseCode, warehouseCode);
        Location location = locationService.getOne(locationLam);
        if(location == null) {
            return AjaxResult.error("此任务的原目的库位在系统中不存在");
        }
        /**
         *  重入的库位由wms提供
         */
        String newLocationCode = null;
        //重入的库位由wms提供
        //查询任务明细
        String value = configService.getKey(QuantityConstant.RULE_ALLOCATION,warehouseCode);
        if (StringUtils.isEmpty(value)) {
            return AjaxResult.error("未绑定定位规则");
        }
        int allocationRule = Integer.parseInt(value);
        List<String> locationTypeCodeList = new ArrayList<>();
        String locationType = location.getLocationType();
        if(StringUtils.isNotEmpty(locationType)) {
            locationTypeCodeList.add(locationType);
        }
        int high = location.getHigh();
        String area = location.getArea();
        String roadWay = location.getRoadway();
        List<String> roadWays = new ArrayList<>();
        roadWays.add(roadWay);
        String containerCode = taskHeader.getContainerCode();
        List<TaskDetail> taskDetailList = taskDetailService.findByTaskId(taskHeader.getId());
        String materialAreaCode = null;
        if(taskDetailList != null && taskDetailList.size() > 0) {
            String materialCode = taskDetailList.get(0).getMaterialCode();
            Material material = materialService.getMaterialByCode(materialCode, warehouseCode);
            materialAreaCode = material.getMaterialAreaCode();
        }
        String locationCode = locationAllocationService.allocation(allocationRule,
              locationTypeCodeList, high, area, roadWays, warehouseCode, containerCode, materialAreaCode);
        if (StringUtils.isEmpty(locationCode)) {
            return AjaxResult.error("没有库位可分配");
        }
        int taskType = taskHeader.getTaskType();
        if (taskType == QuantityConstant.TASK_TYPE_WHOLERECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT) {
            //查询入库组盘明细
            List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
            if(taskDetailList != null) {
                for (TaskDetail taskDetail : taskDetailList) {
                    receiptContainerDetailList.add(receiptContainerDetailService.
                            getById(taskDetail.getAllocationId()));
                }
            }
            ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getById(receiptContainerDetailList.get(0).getReceiptContainerId());
            LambdaUpdateWrapper<ReceiptContainerDetail> wrapper = Wrappers.lambdaUpdate();
            wrapper.set(ReceiptContainerDetail::getLocationCode, locationCode)
                    .in(ReceiptContainerDetail::getId, receiptContainerDetailList.stream().map(ReceiptContainerDetail::getId).collect(Collectors.toList()));
            receiptContainerDetailService.update(wrapper);
            receiptContainerHeader.setToLocation(locationCode);
            receiptContainerHeaderService.updateById(receiptContainerHeader);
        }
        newLocationCode = locationCode;


        if (StringUtils.isEmpty(newLocationCode)) {
            return AjaxResult.error("没有库位可以分配");
        }
        locationService.updateStatus(newLocationCode, QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        String toLocationCode = taskHeader.getToLocation();

        //修改任务
        taskHeader.setToLocation(newLocationCode);
        taskHeader.setExceptionCode("重入处理");
        taskHeader.setIsDoubleIn(QuantityConstant.DOUBLE_IN);
        String originLocationCode = taskHeader.getOriginLocation();
        if(StringUtils.isEmpty(originLocationCode)) {
            taskHeader.setOriginLocation(toLocationCode);
        }
        if(!taskHeaderService.updateById(taskHeader)){
            throw new ServiceException("修改此任务错误");
        }

        //修改子任务
        LambdaQueryWrapper<TaskDetail> taskDetailLam = Wrappers.lambdaQuery();
        taskDetailLam.eq(TaskDetail::getWarehouseCode, warehouseCode)
                .eq(TaskDetail::getTaskId,taskHeader.getId());
        List<TaskDetail> taskDetails = taskDetailService.list(taskDetailLam);
        taskDetailList = new ArrayList<>();
        if(taskDetails != null && taskDetails.size()> 0) {
            for(TaskDetail taskDetail : taskDetails){
                taskDetail.setToLocation(newLocationCode);
                taskDetailList.add(taskDetail);
            }
            if(!taskDetailService.updateBatchById(taskDetailList)) {
                throw new ServiceException("修改此任务的明细错误");
            }
        }

        Map<String, String> map = new HashMap<>();
        map.put("RedirectionLocationCode", newLocationCode);
        map.put("taskNo", String.valueOf(taskHeader.getId()));
        return AjaxResult.success("重入处理成功").setData(map);
    }
}