TaskDetailServiceImpl.java 18.4 KB
package com.huaheng.pc.task.taskDetail.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
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.cycleCountPreference.domain.CycleCountPreference;
import com.huaheng.pc.config.cycleCountPreference.service.CycleCountPreferenceService;
import com.huaheng.pc.config.receiptPreference.domain.ReceiptPreference;
import com.huaheng.pc.config.receiptPreference.service.ReceiptPreferenceService;
import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.task.taskDetail.mapper.TaskDetailMapper;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TaskDetailServiceImpl extends ServiceImpl<TaskDetailMapper, TaskDetail> implements TaskDetailService{


    @Resource
    private CycleCountDetailService cycleCountDetailService;
    @Resource
    private CycleCountPreferenceService cycleCountPreferenceService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ReceiptPreferenceService receiptPreferenceService;
    @Resource
    private ShipmentPreferenceService shipmentPreferenceService;
    @Resource
    private FilterConfigDetailService filterConfigDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private IUserService userService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private ShipmentDetailService shipmentDetailService;


    /**
     * 盘点任务首选项
     * @param taskDetailList
     * @return
     */
    @Transactional
    @Override
    public List<TaskDetail> preferenceRealize(List<TaskDetail> taskDetailList) {
        /*查出盘点明细,找出盘点首选项,根据首选项显示字段*/
        List<TaskDetail> taskDetails = new ArrayList<>();

        for (TaskDetail item:taskDetailList){
            //盘点明细
            CycleCountDetail cycleCountDetailtemp = new CycleCountDetail();
            cycleCountDetailtemp.setCycleCountHeadCode(item.getBillCode());
            cycleCountDetailtemp.setId(item.getBillDetailId());
            LambdaQueryWrapper<CycleCountDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountDetailtemp);
            CycleCountDetail cycleCountDetail = cycleCountDetailService.getOne(detailLambdaQueryWrapper);
            if(cycleCountDetail == null){
                throw new SecurityException("没有找到子任务对应的盘点明细!");
            }
            //找盘点首选项
            CycleCountPreference cycleCountPreferenceTemp = new CycleCountPreference();
            cycleCountPreferenceTemp.setCode(cycleCountDetail.getPreferenceCode());
            cycleCountPreferenceTemp.setWarehouseCode(cycleCountDetail.getWarehouseCode());
            LambdaQueryWrapper<CycleCountPreference> preferenceLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountPreferenceTemp);
            CycleCountPreference cycleCountPreference = cycleCountPreferenceService.getOne(preferenceLambdaQueryWrapper);
            if(cycleCountPreference == null){
                throw new SecurityException("没有找到盘点首选项!");
            }
            //根据首选项判断数据
            if(cycleCountPreference.getPromptLocation() == false){
                //库位
                item.setFromLocation("");
                item.setToLocation("");
            }
            if(cycleCountPreference.getPromptLpn() == false){
                //容器
                item.setContainerCode("");
            }
            if(cycleCountPreference.getPromptItem() == false){
                //物料
                item.setMaterialCode("");
                item.setMaterialName("");
                item.setMaterialUnit("");
                item.setMaterialSpec("");
            }
            if(cycleCountPreference.getPromptQuantity() == false){
                //系统数量
                item.setQty(null);
            }
            taskDetails.add(item);
        }
        return taskDetails;
    }

    /**
     * 任务位置拣货单打印
     * 根据任务头表id查询任务明细,根据上架或拣货货规则排序
     * @param ids 头表id数组
     * @return
     */
    @Override
    public List<TaskDetail> findByTaskId(Integer[] ids) {
        Integer taskType = taskHeaderService.getById(ids[0]).getTaskType();
        String filterConfigCode = "";
        if (taskType.equals(QuantityConstant.TASK_TYPE_WHOLERECEIPT) || taskType.equals(QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT)) {
            ReceiptPreference receiptPreference = receiptPreferenceService.list().get(0);
            filterConfigCode = receiptPreference.getListingRules();
        }
        if (taskType.equals(QuantityConstant.TASK_TYPE_WHOLESHIPMENT) || taskType.equals(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT)) {
            ShipmentPreference shipmentPreference = shipmentPreferenceService.list().get(0);
            filterConfigCode = shipmentPreference.getShipmentPickingRule();
        }
        FilterConfigDetail filterConfigDetail = new FilterConfigDetail();
        if (StringUtils.isNotEmpty(filterConfigCode)) {
            LambdaQueryWrapper<FilterConfigDetail> detailQueryWrapper = Wrappers.lambdaQuery();
            detailQueryWrapper.eq(FilterConfigDetail::getCode, filterConfigCode);
            filterConfigDetail = filterConfigDetailService.getOne(detailQueryWrapper);
        }
        //Integer数组转object数组
        Object[] objects = new Object[ids.length];
        try{
            for(int i = 0; i < ids.length ; i++){
                Object a =  (ids[i]);
                objects[i] = a;
            }
        }catch (Exception e){
            throw new ServiceException(e.toString());
        }
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .in(TaskDetail::getTaskId, objects)
                .last(StringUtils.isNotEmpty(filterConfigDetail.getStatement()), filterConfigDetail.getStatement());
        return this.list(queryWrapper);
    }

    /**
     * 根据任务头表id查询任务明细
     * @param id
     * @return
     */
    @Override
    public List<TaskDetail> findByTaskId(Integer id) {
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getTaskId, id);
        return this.list(queryWrapper);
    }
    @Override
    public void createTaskDetailByReceipt(TaskHeader task, ReceiptContainerDetail item){
        TaskDetail taskDetail = new TaskDetail();
        taskDetail.setTaskId(task.getId());
        taskDetail.setTaskType(task.getTaskType());
        taskDetail.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
        taskDetail.setWarehouseCode(task.getWarehouseCode());
        taskDetail.setAllocationId(item.getId());
        taskDetail.setCompanyCode(task.getCompanyCode());
        taskDetail.setMaterialCode(item.getMaterialCode());
        taskDetail.setMaterialName(item.getMaterialName());
        taskDetail.setMaterialSpec(item.getMaterialSpec());
        taskDetail.setMaterialUnit(item.getMaterialUnit());
        taskDetail.setInventorySts(item.getInventorySts());
        taskDetail.setBillCode(item.getReceiptCode());
        taskDetail.setBillDetailId(item.getReceiptDetailId());
        taskDetail.setQty(item.getQty());
        taskDetail.setContainerCode(task.getContainerCode());
        taskDetail.setFromLocation(task.getFromLocation());
        taskDetail.setGoodsShelfNo(task.getGoodsShelfNo());
        if(task.getToLocation() != null) {
            taskDetail.setToLocation(task.getToLocation());
        }
        taskDetail.setBatch(item.getBatch());
        taskDetail.setProjectNo(item.getProjectNo());
        if (!this.save(taskDetail)) {
            throw new ServiceException("生成任务明细失败");
        }
    }

    @Override
    public List<TaskDetail> selectListEntityUndone(TaskDetail taskDetail) {
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getContainerCode()),TaskDetail::getContainerCode, taskDetail.getContainerCode());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getStatus()),TaskDetail::getStatus, taskDetail.getStatus());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getNumber()),TaskDetail::getNumber, taskDetail.getNumber());
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getWarehouseCode()),TaskDetail::getWarehouseCode, taskDetail.getWarehouseCode());

        return this.list(queryWrapper);
    }
    @Override
    public List<TaskDetail> selectListByGoodshelf(TaskDetail taskDetail) {
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getContainerCode()),TaskDetail::getContainerCode, taskDetail.getContainerCode());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getStatus()),TaskDetail::getStatus, taskDetail.getStatus());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getNumber()),TaskDetail::getNumber, taskDetail.getNumber());
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getWarehouseCode()),TaskDetail::getWarehouseCode, taskDetail.getWarehouseCode());
        queryWrapper.lt(TaskDetail::getStatus,100);
        return this.list(queryWrapper);
    }

    @Override
    public void updateByInventory(TaskDetail taskDetail) {
        InventoryDetail inventory = new InventoryDetail();
        inventory.setContainerCode(taskDetail.getContainerCode());
        try {
            inventory.setCompanyCode(taskDetail.getCompanyCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
        inventory.setWarehouseCode(taskDetail.getWarehouseCode());
        inventory.setMaterialCode(taskDetail.getMaterialCode());
        List<InventoryDetail> inventories = inventoryDetailService.selectListEntityByEqual(inventory);
        BigDecimal number = inventoryDetailService.addQty(inventories);
        taskDetail.setWarehouseQty(number);
        if(QuantityConstant.TASK_TYPE_WHOLESHIPMENT==taskDetail.getTaskType()|| QuantityConstant.TASK_TYPE_SORTINGSHIPMENT==taskDetail.getTaskType()) {
            if (number.compareTo(taskDetail.getQty()) > 0) {
                taskDetail.setRemainder((number.subtract(taskDetail.getQty())));
            } else {
                taskDetail.setRemainder(new BigDecimal(0));
            }
        }else if(QuantityConstant.TASK_TYPE_WHOLERECEIPT==taskDetail.getTaskType() || QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT==taskDetail.getTaskType()){
            taskDetail.setRemainder((number.add(taskDetail.getQty())));
        }

        taskDetail.setLastUpdatedBy(ShiroUtils.getUserName());
        inventoryDetailService.selectUwarehouse(taskDetail);
    }

    @Override
    public void updateTask(TaskDetail taskDetail, BigDecimal number) {
        if(taskDetail.getTaskType().equals("s")){
            //修改出库子单据,修改出库组盘明细
            ShipmentDetail shipmentDetail=shipmentDetailService.getById(taskDetail.getBillDetailId());
            if(shipmentDetail==null){
                throw new ServiceException("找不到子单");
            }
            shipmentDetail.setStatus(200);
            shipmentDetail.setQtyCompleted(shipmentDetail.getQtyCompleted().subtract(taskDetail.getQty()).add(number));
            if(!shipmentDetailService.updateById(shipmentDetail)){
                throw new ServiceException("修改子单据失败");
            };

            ShipmentHeader shipmentHeader=shipmentHeaderService.getById(taskDetail.getBillId());
            if(shipmentHeader==null){
                throw new ServiceException("找不到主单");
            }
            if(shipmentHeader.getLastStatus()>200){
                shipmentHeader.setLastStatus(200);
                if(!shipmentHeaderService.updateById(shipmentHeader)){
                    throw new ServiceException("修改主单据失败");
                }
            }
            //修改出库组盘明细
            ShipmentContainerDetail shipmentContainerDetail=shipmentContainerDetailService.getById(taskDetail.getAllocationId());
            if(shipmentContainerDetail==null){
                throw new ServiceException("找不到组盘明细");
            }
            shipmentContainerDetail.setQty(number);
            if(!shipmentContainerDetailService.updateById(shipmentContainerDetail)){
                throw new ServiceException("修改组盘明细失败");
            };
        }else {
            //修改入库子单据,修改入库组盘明细
            ReceiptDetail receiptDetail=receiptDetailService.getById(taskDetail.getBillDetailId());
            if(receiptDetail==null){
                throw new ServiceException("找不到子单");
            }
            receiptDetail.setStatus((short)200);
            receiptDetail.setTaskQty(receiptDetail.getTaskQty().subtract(taskDetail.getQty()).add(number));
            if(!receiptDetailService.updateById(receiptDetail)){
                throw new ServiceException("修改子单据失败");
            };

            ReceiptHeader receiptHeader=receiptHeaderService.getById(taskDetail.getBillId());
            if(receiptHeader==null){
                throw new ServiceException("找不到主单");
            }
            if(receiptHeader.getLastStatus()>200){
                receiptHeader.setLastStatus(200);
                if(!receiptHeaderService.updateById(receiptHeader)){
                    throw new ServiceException("修改主单据失败");
                }
            }
            //修改出库组盘明细
            ReceiptContainerDetail receiptContainerDetail=receiptContainerDetailService.getById(taskDetail.getAllocationId());
            if(receiptContainerDetail==null){
                throw new ServiceException("找不到组盘明细");
            }
            receiptContainerDetail.setQty(number);
            if(!receiptContainerDetailService.updateById(receiptContainerDetail)){
                throw new ServiceException("修改组盘明细失败");
            };
        }
        //修改子任务
        taskDetail.setQty(number);
        if(!this.updateById(taskDetail)){
            throw new ServiceException("修改子任务失败");
        }
    }

    public List<TaskDetail> selectByIdList(List<Integer> ids) {
        if (ids == null){
            return null;
        }
        String[] idArray = ids.toArray(new String[ids.size()]);
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(TaskDetail::getId, idArray);
        return this.list(queryWrapper);
    }
    @Override
    public AjaxResult updateTaskDetail(String ids, String code) {
        if (StringUtils.isEmpty(ids) || StringUtils.isEmpty(code)){
            return AjaxResult.error("容器编码不能为空!");
        }
        List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
        if (idList.isEmpty()){
            return AjaxResult.error("任务详情id不能为空值!");
        }
        List<TaskDetail>  taskDetails = this.selectByIdList(idList);
        if (taskDetails.isEmpty()){
            return AjaxResult.error("未查询到任务详情!");
        }
        taskDetails.forEach(item->{
            item.setContainerCode(code);
            item.setLastUpdatedBy(ShiroUtils.getLoginName());
        });
        this.updateContainerCode(taskDetails);
        return AjaxResult.success("容器修改成功!");
    }
    public int updateContainerCode(List<TaskDetail> list) {
        if (list.isEmpty()){
            return 0;
        }
        for (int a=0;a<list.size();a++){
            LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(TaskDetail::getId, list.get(a).getId());
            TaskDetail taskDetail=new TaskDetail();
            taskDetail.setLastUpdatedBy(list.get(a).getLastUpdatedBy());
            this.update(taskDetail,queryWrapper);
        }
        return 1;
    }

}