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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.wcs.domain.TaskFinishDomain;
import com.huaheng.api.wcs.service.warecellAllocation.WarecellAllocationService;
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.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
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.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.List;

@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;

    /**
     * 重入处理
     * 1、判断非空字段
     * 2、根据任务号查找任务
     * 3、修改任务目的库位,修改入库组盘的库位,修改库位状态
     */

    @Override
    @Transactional
    public AjaxResult OverrideHandle(TaskFinishDomain taskFinishDomain) {
        Boolean flag = true;

        //1、判断非空字段
        if(StringUtils.isEmpty(taskFinishDomain.getTaskNo())){
            return AjaxResult.error("任务号为空");
        }
        if(StringUtils.isEmpty(taskFinishDomain.getRedirectionLocationCode())){
            return AjaxResult.error("重入的库位编码为空");
        }



        //2、根据任务号查找任务
        TaskHeader taskHeader = taskHeaderService.getById(Integer.valueOf(taskFinishDomain.getTaskNo()));
        if(taskHeader == null){
            return AjaxResult.error("任务号错误,没有找到该任务");
        }
        if(taskHeader.getStatus() == 100){
            return AjaxResult.error("任务已完成");
        }

        //3、修改任务目的库位,修改入库组盘的库位,修改库位状态
        //修改原来目的库位的状态
        LambdaQueryWrapper<Location> locationLam = Wrappers.lambdaQuery();
        locationLam.eq(Location::getCode,taskHeader.getToLocation())
                .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
        Location location = locationService.getOne(locationLam);
        if(location == null){
            return AjaxResult.error("此任务的原目的库位在系统中不存在");
        }
        location.setStatus("empty");
        location.setContainerCode("");
        flag = locationService.updateById(location);
        if(flag == false){
            return AjaxResult.error("修改此任务的原目的库位错误");
        }


        /**
         *  查看新库位有两种情况
         *  1、重入的库位由wcs提供
         *  2、重入的库位由wms提供
         */

        Location newlocation =new Location();
        String code=ShiroUtils.getWarehouseCode();
        //重入的库位由wcs提供
        if(!"0".equals(taskFinishDomain.getRedirectionLocationCode())) {
            LambdaQueryWrapper<Location> locationLa = Wrappers.lambdaQuery();
            locationLa.eq(Location::getCode, taskFinishDomain.getRedirectionLocationCode())
                    .eq(Location::getStatus,"empty")
                    .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
            newlocation = locationService.getOne(locationLa);
            if (newlocation == null) {
                throw new ServiceException("新目的库位在系统中不存在或非空闲");
            }
            if(StringUtils.isNotEmpty(newlocation.getContainerCode())){
                throw new ServiceException("新目的库位在系统中已经有容器");
            }
        }else {
            //重入的库位由wms提供
            //查询任务明细
            LambdaQueryWrapper<TaskDetail> taskDetailLambda = Wrappers.lambdaQuery();
            taskDetailLambda.eq(TaskDetail::getTaskId, taskFinishDomain.getTaskNo());
            List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLambda);
            //查询入库组盘明细
            List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
            for (TaskDetail taskDetail : taskDetailList) {
                receiptContainerDetailList.add(receiptContainerDetailService.getById(taskDetail.getAllocationId()));
            }
            ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getById(receiptContainerDetailList.get(0).getReceiptContainerId());
            String locatingRule = warecellAllocationService.taskPositioning(receiptContainerDetailList.get(0));

            LambdaQueryWrapper<FilterConfigDetail> filterConfigDetailLambda = Wrappers.lambdaQuery();
            filterConfigDetailLambda.eq(FilterConfigDetail::getCode, locatingRule);
            FilterConfigDetail filterConfigDetail = filterConfigDetailService.getOne(filterConfigDetailLambda);

            //根据定位规则查询库位编码
            LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
            locationLambda.last(filterConfigDetail.getStatement());
            newlocation = locationService.getOne(locationLambda);
        }

        //修改任务
        taskHeader.setToLocation(newlocation.getCode());
        taskHeader.setExceptionCode("重入处理");
        flag = taskHeaderService.updateById(taskHeader);
        if(flag == false){
            throw new ServiceException("修改此任务错误");
        }

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

        //修改入库组盘
        if(taskHeader.getAllocationHeadId() == null){
            throw new ServiceException("找不到此任务的组盘头id");
        }
        ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getById(taskHeader.getAllocationHeadId());
        if(receiptContainerHeader == null){
            throw new ServiceException("找不到此任务的组盘头");
        }
        receiptContainerHeader.setToLocation(newlocation.getCode());
        flag = receiptContainerHeaderService.updateById(receiptContainerHeader);
        if(flag == false){
            throw new ServiceException("修改此任务对应的组盘头错误");
        }

        //修改入库组盘明细
        LambdaQueryWrapper<ReceiptContainerDetail> lam = Wrappers.lambdaQuery();
        lam.eq(ReceiptContainerDetail::getReceiptContainerId,receiptContainerHeader.getId())
                .eq(ReceiptContainerDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
        List<ReceiptContainerDetail> receiptContainerDetails = receiptContainerDetailService.list(lam);

        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        if(receiptContainerDetails != null && receiptContainerDetails.size()> 0){
            for(ReceiptContainerDetail receiptContainerDetail : receiptContainerDetails){
                receiptContainerDetail.setLocationCode(newlocation.getCode());
                receiptContainerDetailList.add(receiptContainerDetail);
            }
            flag = receiptContainerDetailService.updateBatchById(receiptContainerDetailList);
            if(flag == false){
                throw new ServiceException("修改此任务的组盘明细错误");
            }
        }
        return AjaxResult.success("重入处理成功");
    }
}