TransferTaskService.java 13.8 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.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
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.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
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.List;
import java.util.stream.Collectors;

/**
 * 移库任务
 */
@Service
public class TransferTaskService {

    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationService locationService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;

    /**
     * 创建立库移库任务
     * @param sourceLocationCode 源库位
     * @param desLocationCode 目的库位
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createTransferTask(String sourceLocationCode, String desLocationCode) {
        Location sourceLocation = locationService.findLocationByCode(sourceLocationCode);
        Location desLocation = locationService.findLocationByCode(desLocationCode);
        Integer preTaskNo = 0;
        if (StringUtils.isNull(sourceLocation)) {
            return AjaxResult.error("源库位:" + sourceLocation + "未找到");
        }
        if (!"empty".equals(sourceLocation.getStatus())) {
            return AjaxResult.error("源库位:" + sourceLocation + "状态非空闲");
        }
        if (StringUtils.isEmpty(sourceLocation.getContainerCode())) {
            return AjaxResult.error("源库位:" + sourceLocation + "不存在托盘");
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
        if (inventoryHeaderService.getUncompleteReceiptContainer(sourceLocationCode) > 0) {
            return AjaxResult.error("源库位:" + sourceLocation + "存在入库组盘,不能移库");
        }
        if (inventoryHeaderService.getUncompleteShipmentContainer(sourceLocationCode) > 0) {
            return AjaxResult.error("源库位:" + sourceLocation + "存在出库组盘,不能移库");
        }
        if (StringUtils.isNull(desLocation)) {
            return AjaxResult.error("目标库位:" + desLocationCode + "未找到");
        }
        if (!"empty".equals(desLocation.getStatus())) {
            return AjaxResult.error("目标库位:" + desLocationCode + "状态非空闲");
        }
        if (StringUtils.isNotEmpty(desLocation.getContainerCode())) {
            return AjaxResult.error("目标库位:" + desLocationCode + "存在托盘");
        }
        if (inventoryHeaderService.getUncompleteReceiptContainer(desLocationCode) > 0) {
            return AjaxResult.error("目标库位:" + desLocationCode + "存在入库组盘,不能移库");
        }

        if (taskHeaderService.getUncompleteTaskInNear(desLocation) > 0) {
            return AjaxResult.error("目标库位:" + desLocationCode + "旁边存在任务,请完成任务以后再分配");
        }

        if(sourceLocation.getRowFlag() == 1) {
            Location location1 = locationService.getNear(sourceLocation);  //内侧库位
            String locationCode = location1.getCode();
            if(StringUtils.isNotEmpty(location1.getContainerCode())) {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, locationCode)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
                if(taskHeader != null) {
                    preTaskNo = taskHeader.getId();
                } else {
                    return AjaxResult.error("源库位:" + sourceLocationCode + "旁边库位有托盘无法移库");
                }
            } else {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getToLocation, locationCode)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
                if(taskHeader != null) {
                    return AjaxResult.error("源库位:" + sourceLocationCode + "旁边库位有任务无法移库");
                }
            }
        }

        TaskHeader taskHeader = new TaskHeader();
        taskHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        taskHeader.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
        taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_TRANSFER);
        taskHeader.setContainerCode(sourceLocation.getContainerCode());
        taskHeader.setFromLocation(sourceLocationCode);
        taskHeader.setToLocation(desLocationCode);
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        taskHeader.setPreTaskNo(preTaskNo);
        if (taskHeaderService.save(taskHeader)) {
            locationService.updateStatus(sourceLocationCode, "lock");
            locationService.updateStatus(desLocationCode, "lock");
            containerService.updateLocationCodeAndStatus(sourceLocation.getContainerCode(), sourceLocationCode, "lock");
        } else {
            throw new ServiceException("创建任务失败");
        }
        LambdaUpdateWrapper<InventoryDetail> detailLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        detailLambdaUpdateWrapper.eq(InventoryDetail::getLocationCode, sourceLocationCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(detailLambdaUpdateWrapper);
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());;
            taskDetail.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
            taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            taskDetail.setMaterialName(inventoryDetail.getMaterialName());
            taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
            taskDetail.setQty(inventoryDetail.getQty());
            taskDetail.setFromLocation(inventoryDetail.getLocationCode());
            taskDetail.setToLocation(desLocationCode);
            taskDetail.setContainerCode(inventoryDetail.getContainerCode());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            taskDetail.setReferenceCode(inventoryDetail.getReferCode());
            taskDetail.setAttribute1(inventoryDetail.getAttribute1());
            taskDetail.setAttribute2(inventoryDetail.getAttribute2());
            taskDetail.setAttribute3(inventoryDetail.getAttribute3());
            taskDetail.setBatch(inventoryDetail.getBatch());
            taskDetail.setLot(inventoryDetail.getLot());
            taskDetail.setProjectNo(inventoryDetail.getProjectNo());
            taskDetail.setManufactureDate(inventoryDetail.getManufactureDate());
            taskDetail.setExpirationDate(inventoryDetail.getExpirationDate());
            taskDetail.setAgingDate(inventoryDetail.getAgingDate());
            taskDetail.setInventorySts(inventoryDetail.getInventorySts());
            taskDetail.setFromInventoryId(inventoryDetail.getId());
            taskDetail.setToInventoryId(inventoryDetail.getId());
            inventoryDetail.setLockedQty(inventoryDetail.getQty());
            if (!taskDetailService.save(taskDetail) || !inventoryDetailService.updateById(inventoryDetail)) {
                throw new ServiceException("创建任务失败");
            }
        }
        return AjaxResult.success(taskHeader.getId());
    }

    /**
     * 完成移库任务
     * @param taskHeader
     */
    public void completeTransferTask(TaskHeader taskHeader) {
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetailList = taskDetailService.findByTaskId(taskHeader.getId());
        /* 库存头表id*/
        List<Integer> inventoryHeadIdList = new ArrayList<>();
        for (TaskDetail taskDetail : taskDetailList) {
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
            InventoryDetail inventoryDetail = inventoryDetailService.getById(taskDetail.getToInventoryId());
            inventoryHeadIdList.add(inventoryDetail.getInventoryHeaderId());
            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setWarehouseCode(taskDetail.getWarehouseCode());
            inventoryTransaction.setCompanyCode(taskDetail.getCompanyCode());
            inventoryTransaction.setLocationCode(taskDetail.getToLocation());
            inventoryTransaction.setContainerCode(taskDetail.getContainerCode());
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFERINTO);
            inventoryTransaction.setMaterialCode(taskDetail.getMaterialCode());
            inventoryTransaction.setMaterialName(taskDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(taskDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(taskDetail.getMaterialUnit());
            inventoryTransaction.setTaskQty(taskDetail.getQty());
            inventoryTransaction.setInventorySts(taskDetail.getInventorySts());
            inventoryTransaction.setReferCode(taskDetail.getReferenceCode());
            inventoryTransaction.setBatch(taskDetail.getBatch());
            inventoryTransaction.setLot(taskDetail.getLot());
            inventoryTransaction.setProjectNo(taskDetail.getProjectNo());
            inventoryTransaction.setQcCheck(taskDetail.getQcCheck());
            inventoryTransaction.setManufactureDate(taskDetail.getManufactureDate());
            inventoryTransaction.setExpirationDate(taskDetail.getExpirationDate());
            inventoryTransaction.setAgingDate(taskDetail.getAgingDate());
            inventoryTransaction.setAttributeId(String.valueOf(taskDetail.getAttributeId()));
            inventoryTransaction.setAttribute1(taskDetail.getAttribute1());
            inventoryTransaction.setAttribute2(taskDetail.getAttribute2());
            inventoryTransaction.setAttribute3(taskDetail.getAttribute3());
            inventoryTransactionService.save(inventoryTransaction);
            inventoryTransaction.setLocationCode(taskDetail.getFromLocation());
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFEROUT);
            inventoryTransactionService.save(inventoryTransaction);
        }
        if (!taskDetailService.saveOrUpdateBatch(taskDetailList) ||
                !taskHeaderService.saveOrUpdate(taskHeader)) {
            throw new ServiceException("任务单据状态更新失败!");
        }
        locationService.updateContainerCodeAndStatus(taskHeader.getFromLocation(), "", "empty");
        locationService.updateContainerCodeAndStatus(taskHeader.getToLocation(), taskHeader.getContainerCode(), "empty");
        containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(), taskHeader.getToLocation(), "some");
        inventoryHeadIdList = inventoryHeadIdList.stream().distinct().collect(Collectors.toList());
        LambdaUpdateWrapper<InventoryHeader> headerUpdateWrapper = Wrappers.lambdaUpdate();
        headerUpdateWrapper.set(InventoryHeader::getLocationCode, taskHeader.getToLocation())
                .in(InventoryHeader::getId, inventoryHeadIdList);
        LambdaUpdateWrapper<InventoryDetail> detailUpdateWrapper = Wrappers.lambdaUpdate();
        detailUpdateWrapper.set(InventoryDetail::getLocationCode, taskHeader.getToLocation())
                .set(InventoryDetail::getLockedQty, BigDecimal.ZERO)
                .in(InventoryDetail::getInventoryHeaderId, inventoryHeadIdList);
        if (!inventoryHeaderService.update(headerUpdateWrapper) ||
                !inventoryDetailService.update(detailUpdateWrapper)) {
            throw new ServiceException("完成任务失败");
        }
    }
}