CycleCountTaskService.java 18.4 KB
package com.huaheng.pc.task.taskHeader.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.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.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetailChild;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailChildService;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.cycleCountHeader.domain.CycleCountHeader;
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 盘点服务层
 * */
@Service
public class CycleCountTaskService {


    @Resource
    private CycleCountDetailService cycleCountDetailService;
    @Resource
    private CycleCountHeaderService cycleCountHeaderService;
    @Resource
    private CycleCountDetailChildService cycleCountDetailChildService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private LocationService locationService;
    @Resource
    private ContainerService containerService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryDetailService inventoryDetailService;





    /**
     * 盘点任务取消
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cycleCountCancelTask(Integer[] taskIds) {
        for (int taskId : taskIds) {
            TaskHeader taskHeader = taskHeaderService.getById(taskId);
            if (taskHeader == null) {
                return AjaxResult.error("任务" + taskId + "未找到,操作中止");
            }
            if (taskHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
                return AjaxResult.error("存在任务" + taskHeader.getId() + "已下发或执行,操作中止");
            }
            //查出任务明细
            TaskDetail taskDetail1 = new TaskDetail();
            taskDetail1.setTaskId(taskHeader.getId());
            taskDetail1.setWarehouseCode(taskHeader.getWarehouseCode());
            taskDetail1.setCompanyCode(taskHeader.getCompanyCode());
            LambdaQueryWrapper<TaskDetail> td = Wrappers.lambdaQuery(taskDetail1);
            List<TaskDetail> taskDetailList = taskDetailService.list(td);
            if (taskDetailList.size() < 1) {
                throw new ServiceException("任务明细条目错误");
            }
            TaskDetail taskDetail = taskDetailList.get(0);
            //删除子任务
            LambdaQueryWrapper<TaskDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(TaskDetail::getTaskId, taskHeader.getId());
            taskDetailService.remove(lambdaQueryWrapper);
            //删除主任务
            taskHeaderService.removeById(taskHeader.getId());
            //盘点取消任务,恢复明细状态为1
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_CYCLECOUNT)) {

                CycleCountDetail cycleCountDetail = new CycleCountDetail();
                cycleCountDetail.setCompanyCode(taskDetail.getCompanyCode());
                cycleCountDetail.setWarehouseCode(taskDetail.getWarehouseCode());
                cycleCountDetail.setLocationCode(taskDetail.getFromLocation());
                cycleCountDetail.setContainerCode(taskDetail.getContainerCode());
                cycleCountDetail.setCycleCountHeadCode(taskDetail.getBillCode());//盘点单Code
                cycleCountDetail.setId(taskDetail.getBillDetailId());//盘点细单ID
                LambdaQueryWrapper<CycleCountDetail> lam = Wrappers.lambdaQuery(cycleCountDetail);
                List<CycleCountDetail> cycleCountDetailList = cycleCountDetailService.list(lam);//
                for (CycleCountDetail item : cycleCountDetailList) {
                    item.setTaskHeaderId(null);
                    item.setTaskDetailId(null);
                    item.setLastUpdated(new Date());
                    item.setLastUpdatedBy(ShiroUtils.getLoginName());
                    item.setEnableStatus(1);
                    cycleCountDetailService.saveOrUpdate(item);
                }

            }
            if (taskHeader.getToLocation() != null) {
                //更新托盘、库位状态
                locationService.updateStatus(taskHeader.getToLocation(), QuantityConstant.STATUS_LOCATION_EMPTY);
            }
            LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, taskHeader.getContainerCode());
            InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
            Container container = new Container();
            container.setStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
            if(inventoryHeader != null) {
                if(inventoryHeader.getContainerStatus().equals(QuantityConstant.STATUS_CONTAINER_SOME)) {
                    container.setStatus(QuantityConstant.STATUS_CONTAINER_SOME);
                }
            }
            LambdaUpdateWrapper<Container> containerUpdateWrapper = Wrappers.lambdaUpdate();
            containerUpdateWrapper.eq(Container::getCode, taskHeader.getContainerCode());
            containerService.update(container, containerUpdateWrapper);

        }
        return AjaxResult.success("取消任务成功!");
    }

    /**
     * 生成单条盘点任务
     * @param cycleCoutDetailId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createCycleCoutTaskByDetailId(Integer cycleCoutDetailId) {
        /*任务主表中存在库位,在盘点明细中生成任务时,不同的容器生成不同的主任务*/
        //校验有没有相同容器正在执行任务.
        CycleCountDetail cycleCountDetail = cycleCountDetailService.getById(cycleCoutDetailId);
        if(StringUtils.isNull(cycleCountDetail)){
            return AjaxResult.error("盘点明细ID错误,没有该条明细");
        }
        //查出明细的子单
        CycleCountDetailChild child = new CycleCountDetailChild();
        child.setCycleCountHeadCode(cycleCountDetail.getCycleCountHeadCode());
        child.setCycleCountDetailId(cycleCountDetail.getId());
        LambdaQueryWrapper<CycleCountDetailChild> childLambdaQueryWrapper = Wrappers.lambdaQuery(child);
        List<CycleCountDetailChild> childList = cycleCountDetailChildService.list(childLambdaQueryWrapper);
        if(childList.size() < 1){
            return AjaxResult.error("盘点明细没有子单");
        }
        Location location = locationService.getLocationByCode(
                cycleCountDetail.getLocationCode(), ShiroUtils.getWarehouseCode());
        /*if(!loc.getStatus().equals(QuantityConstant.STATUS_EMPTY)){
            return AjaxResult.error(cycleCountDetail.getLocationCode() + "库位不在空闲状态,请先完成其他任务,操作失败!");
        }*/
        if(StringUtils.isEmpty(location.getContainerCode())){
            return AjaxResult.error(cycleCountDetail.getLocationCode() + "库位中没有容器,操作失败!");
        }
        //生成任务同时锁定库位
        locationService.updateStatus(location.getCode(), QuantityConstant.STATUS_LOCATION_LOCK);
        //每个明细单生成一张主任务,子单就是任务明细。
        TaskHeader task = new TaskHeader();
        task.setWarehouseCode(ShiroUtils.getWarehouseCode());
        task.setCompanyCode(cycleCountDetail.getCompanyCode());
        task.setAllocationHeadId(cycleCountDetail.getId());//明细ID写入主任务
        if(location != null) {
            task.setZoneCode(location.getZoneCode());
        }
        task.setReferenceCode(cycleCountDetail.getCycleCountHeadCode()); //写入盘点主单号
        task.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_CYCLECOUNT);
        task.setTaskType(QuantityConstant.TASK_TYPE_CYCLECOUNT);
        task.setContainerCode(cycleCountDetail.getContainerCode());
        task.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        task.setFromLocation(cycleCountDetail.getLocationCode());
        task.setToLocation(cycleCountDetail.getLocationCode());
        task.setCreated(new Date());
        task.setCreatedBy(ShiroUtils.getLoginName());
        task.setLastUpdatedBy(ShiroUtils.getLoginName());
        task.setLastUpdated(new Date());
        if(taskHeaderService.save(task)){
            //锁定库位状态
            locationService.updateStatus(location.getContainerCode(), QuantityConstant.STATUS_LOCATION_LOCK);
            cycleCountDetail.setTaskHeaderId(task.getId()); //盘点明细修改状态task数据源
        }else{
            throw new ServiceException("盘点任务主表生成失败!");
        }

        //写入任务细表
        TaskDetail taskDetail = new TaskDetail();
        for(CycleCountDetailChild item: childList){

            taskDetail.setTaskId(task.getId());//主单ID
            taskDetail.setTaskType(task.getTaskType());
            taskDetail.setFromLocation(task.getFromLocation());
            taskDetail.setToLocation(task.getToLocation());
            taskDetail.setContainerCode(task.getContainerCode());
            taskDetail.setInternalTaskType(task.getTaskType());
            taskDetail.setWarehouseCode(task.getWarehouseCode());
            taskDetail.setBillCode(item.getCycleCountHeadCode());
            taskDetail.setBillDetailId(item.getCycleCountDetailId());
            taskDetail.setMaterialCode(item.getMaterialCode());
            taskDetail.setMaterialName(item.getMaterialName());
            taskDetail.setMaterialSpec(item.getMaterialSpec());
            taskDetail.setMaterialUnit(item.getMaterialUnit());
            taskDetail.setQty(item.getSystemQty());
            //taskDetail.setBatch(cycleCountDetail.getBatch());
            //taskDetail.setFromInventoryId(cycleCountDetail.getInventoryDetailId());
            //taskDetail.setLot(cycleCountDetail.getLot());
            //taskDetail.setProjectNo(cycleCountDetail.getProjectNo());
            taskDetail.setCompanyCode(cycleCountDetail.getCompanyCode());
            taskDetail.setCreated(new Date());
            taskDetail.setCreatedBy(ShiroUtils.getLoginName());
            taskDetail.setLastUpdated(new Date());
            taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            if(taskDetailService.save(taskDetail) == false){
                throw new ServiceException("盘点任务明细生成失败!");
            }
            // 修改子单状态
            item.setChildStatus(QuantityConstant.CYCLECOUNT_STATUS_BUILDTASK);
            cycleCountDetailChildService.updateById(item);
        }

        //修改细单状态
        cycleCountDetail.setTaskHeaderId(task.getId()); //盘点明细修改状态task数据源
        cycleCountDetail.setLastUpdated(new Date());
        cycleCountDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_BUILDTASK);
        cycleCountDetail.setTaskHeaderId(task.getId());
        //cycleCountDetail.setTaskDetailId(taskDetail.getId());
        cycleCountDetailService.updateById(cycleCountDetail);
        //修改主单状态
        CycleCountHeader cycleCountHeader = new CycleCountHeader();
        cycleCountHeader.setCode(cycleCountDetail.getCycleCountHeadCode());
        cycleCountHeader.setWarehouseCode(cycleCountDetail.getWarehouseCode());
        //cycleCountHeader.setCompanyCode(cycleCountDetail.getCompanyCode());
        LambdaQueryWrapper<CycleCountHeader> lamb = Wrappers.lambdaQuery(cycleCountHeader);
        cycleCountHeader = cycleCountHeaderService.getOne(lamb);
        cycleCountHeader.setStatusCyc(QuantityConstant.CYCLECOUNT_STATUS_BUILDTASK);
        cycleCountHeaderService.updateById(cycleCountHeader);
        return AjaxResult.success("盘点任务生成成功");
    }


    /**
     * 盘点完成
     *盘点有差异完成时状态为105
     * @param taskHeader
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult completeCycleCountTask(TaskHeader taskHeader) {
        /*盘点完成,传入任务主单,查出任务明细,通过任务明细查找盘点的明细单,
        完成任务同时,修改盘点细单和主单的状态,完成后库存锁复位*/
        //修改任务主单状态
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
        taskHeader.setLastUpdatedBy(ShiroUtils.getLoginName()); //更新用户
        taskHeader.setLastUpdated(new Date());   //更新时间
        //task更新明细单状态
        TaskDetail taskDetail = new TaskDetail();
        taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
        taskDetail.setTaskType(taskHeader.getTaskType());
        taskDetail.setTaskId(taskHeader.getId());
        LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(taskDetail);
        List<TaskDetail> taskDetailList = taskDetailService.list(lambdaQueryWrapper);//查询子单
        //盘点明细
        CycleCountDetail cycleCountDetail = cycleCountDetailService.getById(taskHeader.getAllocationHeadId());
        List<TaskDetail> list = new CopyOnWriteArrayList<>();
        List<CycleCountDetailChild> cycChildList = new ArrayList<>();
        boolean difference = false;
        //修改任务明细状态的同时查找到盘点明细的条目并修改状态,最后修改主单状态
        for (TaskDetail item : taskDetailList) {
            item.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
            item.setLastUpdatedBy(ShiroUtils.getLoginName()); //更新用户
            item.setLastUpdated(new Date());   //更新时间
            list.add(item);
            //盘点子单状态
            CycleCountDetailChild cycleCountDetailChild = new CycleCountDetailChild();
            cycleCountDetailChild.setCycleCountHeadCode(cycleCountDetail.getCycleCountHeadCode());
            cycleCountDetailChild.setCycleCountDetailId(cycleCountDetail.getId());
            LambdaQueryWrapper<CycleCountDetailChild> childLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountDetailChild);
            List<CycleCountDetailChild> childList = cycleCountDetailChildService.list(childLambdaQueryWrapper);
            if(childList.size() > 0){
                for (CycleCountDetailChild child : childList){
                    //判断是否差异状态
                    if(child.getGapQty().compareTo(BigDecimal.ZERO) != 0){
                        difference = true;
                        //有差异
                        child.setChildStatus(QuantityConstant.CYCLECOUNT_STATUS_DIFFERENCE);
                    }else{
                        child.setChildStatus(QuantityConstant.CYCLECOUNT_STATUS_COMPLETED);
                    }
                    cycChildList.add(child);
                }
            }
            //取消库存盘点锁
            InventoryHeader inventoryHeader = inventoryHeaderService.getById(cycleCountDetail.getInventoryDetailId());
            if(StringUtils.isNull(inventoryHeader)){
                throw new ServiceException("库存头错误!");
            }
            inventoryHeader.setLocking(1);
            inventoryHeader.setLockRemark("");
            inventoryHeaderService.updateById(inventoryHeader);
            //库存明细最后盘点时间
            LambdaQueryWrapper<InventoryDetail> inventoryDetail = Wrappers.lambdaQuery();
            inventoryDetail.eq(InventoryDetail::getWarehouseCode,inventoryHeader.getWarehouseCode())
                    .eq(InventoryDetail::getInventoryHeaderId,inventoryHeader.getId());
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetail);
            if(inventoryDetailList.size() < 1 ){
                throw new ServiceException("盘点的库存明细错误!");
            }
            inventoryDetailList.stream().forEach(i -> i.setLastCycleCountDate(new Date()));
            inventoryDetailService.updateBatchById(inventoryDetailList);
        }
        if (!taskHeaderService.saveOrUpdate(taskHeader) ||
                !taskDetailService.saveOrUpdateBatch(list) ||
                !cycleCountDetailChildService.saveOrUpdateBatch(cycChildList)
        ) {
            throw new ServiceException("盘点任务单据状态更新失败!");
        }
        //盘点明细状态
        if(difference){
            //有差异
            cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_DIFFERENCE);
        }else {
            cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_COMPLETED);
        }
        cycleCountDetail.setCompletedBy(taskHeader.getCreatedBy());
        cycleCountDetail.setCompletedAt(new Date());
        cycleCountDetailService.updateById(cycleCountDetail);
        //更新主单状态
        cycleCountHeaderService.updataHeaderStatus(cycleCountDetail.getCycleCountHeadCode());
        //释放库位
        locationService.updateStatus(cycleCountDetail.getLocationCode(), QuantityConstant.STATUS_LOCATION_EMPTY);

        return AjaxResult.success("完成盘点任务成功!");

    }
















}