skyouc
2025-04-02 1c54a6ae33a15cf0c426e1e9c7ae07398a1e65e5
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -1,22 +1,27 @@
package com.vincent.rsf.server.manager.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vincent.rsf.server.api.entity.enums.OrderType;
import com.vincent.rsf.server.api.entity.enums.TaskStsType;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.entity.WaitPakin;
import com.vincent.rsf.server.manager.entity.WaitPakinItem;
import com.vincent.rsf.server.api.entity.enums.TaskType;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.mapper.TaskMapper;
import com.vincent.rsf.server.manager.entity.Task;
import com.vincent.rsf.server.manager.service.TaskService;
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.manager.service.WaitPakinItemService;
import com.vincent.rsf.server.manager.service.WaitPakinService;
import com.vincent.rsf.server.manager.utils.LocManageUtil;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.enums.LocStsType;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@@ -26,19 +31,27 @@
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private TaskItemService taskItemService;
    @Autowired
    private WaitPakinItemService waitPakinItemService;
    @Autowired
    private LocService locService;
    /**
     * @param
     * @param loginUserId
     * @return
     * @author Ryan
     * @description 生成任务列表
     * @param
     * @return
     * @time 2025/3/29 15:59
     */
    @Override
    public R generateTasks(List<WaitPakin> waitPakin) {
    @Transactional(rollbackFor = Exception.class)
    public synchronized R generateTasks(List<WaitPakin> waitPakin, Long loginUserId) {
        if (Objects.isNull(waitPakin) || waitPakin.isEmpty()) {
            throw new CoolException("参数不能为空!!");
        }
@@ -48,15 +61,67 @@
        if (waitPakins.isEmpty()) {
            throw new CoolException("组拖信息不存在!!");
        }
        /**获取组拖明细**/
        List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, ids));
        if (waitPakinItems.isEmpty()) {
            throw new CoolException("数据错误:组拖明细不存在");
        }
        Task task = new Task();
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
        task.setTaskCode(ruleCode).setTaskStatus(TaskStsType.GENERATE_IN.id.shortValue());
        waitPakins.forEach(pakin -> {
            List<TaskItem> taskItems = new ArrayList<>();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.GENERATE_IN.id.shortValue())
                    .setTaskType(TaskType.TASK_TYPE_IN.type.shortValue())
                    .setTargLoc(LocManageUtil.getTargetLoc())
                    .setBarcode(pakin.getBarcode())
                    .setCreateBy(loginUserId)
                    .setUpdateBy(loginUserId)
                    .setTargSite(LocManageUtil.getTargetSite());
            if (!this.save(task)) {
                throw new CoolException("任务保存失败!!");
            }
            if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, pakin.getCode())
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type).set(Loc::getBarcode, pakin.getBarcode()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
            List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, pakin.getId()));
            if (waitPakinItems.isEmpty()) {
                throw new CoolException("数据错误:组拖明细不存在");
            }
            waitPakinItems.forEach(item -> {
                TaskItem taskItem = new TaskItem();
                BeanUtils.copyProperties(item, taskItem);
                taskItem.setTaskId(task.getId())
                        .setOrderType(OrderType.ORDER_RECEIPT.type)
                        .setSource(item.getId())
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId)
                        .setOrderId(item.getAsnId())
                        .setOrderItemId(item.getAsnItemId());
                taskItems.add(taskItem);
            });
            if (!taskItemService.saveBatch(taskItems)) {
                throw new CoolException("任务明细保存失败!!");
            }
        });
        return R.ok("任务生成完毕!");
    }
    /**
    * @author Ryan
    * @description 完成任务
    * @param
    * @return
    * @time 2025/4/2 11:15
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R completeTask(String id) {
        return null;
    }
}