pjb
2025-03-08 83b51a5a0774ea8ecb9a06304af3b956a21307c8
zy-asrs-wms/src/main/java/com/zy/asrs/wms/asrs/timer/TaskTimer.java
@@ -1,6 +1,7 @@
package com.zy.asrs.wms.asrs.timer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.zy.asrs.framework.exception.CoolException;
@@ -8,6 +9,7 @@
import com.zy.asrs.wms.asrs.entity.enums.LocStsType;
import com.zy.asrs.wms.asrs.entity.enums.TaskStsType;
import com.zy.asrs.wms.asrs.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -17,6 +19,7 @@
import java.util.Date;
import java.util.List;
@Slf4j
@Component
public class TaskTimer {
@@ -56,9 +59,15 @@
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private CacheSiteService cacheSiteService;
//    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    /**
     * 入库执行任务
     */
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void inExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
@@ -98,8 +107,31 @@
        }
    }
    /**
     * // fixme 舍弃
     * 1. 定时拉取需要执行回库任务列表数据,
     * 2. 删除原始库位明细,原始库位状态置成O.空库
     * 3.
     */
//    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public void rollbackStock() {
        //获取需要执行回库的任务,更新库存信息
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskType, 53).eq(Task::getTaskSts, TaskStsType.WCS_CONVEYOR_START.id));
        if (tasks.isEmpty()) {
            return;
        }
        tasks.forEach(task -> {
            //删除原始库位明细,状态置为O.空库
            executeTask103(task);
        });
        //todo 需添加一个任务状态或临时状态,否则会一直轮循,程序报错(库位状态不处于R.出库预约)
    }
    @Scheduled(cron = "0/10 * * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void outExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
@@ -111,7 +143,7 @@
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 101://出库
                    case 101://出库xx
                        executeTask101(task);
                        break;
                    case 103://拣料
@@ -120,13 +152,11 @@
                    default:
                        throw new CoolException("未知任务类型");
                }
                task.setTaskSts(TaskStsType.UPDATED_OUT.id);//200.库存更新完成
                if (!taskService.updateById(task)) {
                    throw new CoolException("库存更新失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@@ -138,7 +168,6 @@
    //入库
    private void executeTask1(Task task) {
        Long hostId = task.getHostId();
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        if (loc == null) {
            throw new CoolException("库位不存在");
@@ -189,7 +218,6 @@
                }
            }
        }
        //组托通知档转历史档
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()).eq(WaitPakin::getHostId, hostId));
        if (waitPakins.isEmpty()) {
@@ -198,6 +226,7 @@
        for (WaitPakin waitPakin : waitPakins) {
            WaitPakinLog waitPakinLog = new WaitPakinLog();
            waitPakinLog.sync(waitPakin);
            waitPakinLog.setId(null);
            if (!waitPakinLogService.save(waitPakinLog)) {
                throw new CoolException("组托通知档转历史档失败");
            }
@@ -289,10 +318,13 @@
    }
    /**
     * 拣料再入库,根据任务目标库位,生成新库存信息
     * @param task
     */
    //拣料再入库
    private void executeTask53(Task task) {
        Long hostId = task.getHostId();
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        if (loc == null) {
            throw new CoolException("库位不存在");
@@ -305,6 +337,7 @@
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
@@ -314,37 +347,35 @@
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        //修改库存明细
        for (TaskDetl taskDetl : taskDetls) {
            double anfme = taskDetl.getStock() - taskDetl.getAnfme();
            if (anfme <= 0) {
                continue;
            }
                LocDetl  locdetl =  locDetlService.getOne(new LambdaQueryWrapper<LocDetl>()
                        .eq(LocDetl::getLocId, loc.getId())
                        .eq(LocDetl::getBatch, taskDetl.getBatch())
                        .eq(LocDetl::getMatId, taskDetl.getMatId()));
            LocDetl locDetl = new LocDetl();
            locDetl.setLocId(loc.getId());
            locDetl.setLocNo(loc.getLocNo());
            locDetl.setMatId(taskDetl.getMatId());
            locDetl.setMatnr(taskDetl.getMat$().getMatnr());
            locDetl.setOrderNo(taskDetl.getOrderNo());
            locDetl.setBatch(taskDetl.getBatch());
            locDetl.setAnfme(anfme);
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                throw new CoolException("插入库存明细失败");
            }
                List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locdetl.getId()).eq(LocDetlField::getHostId, hostId));
                if (!detlFields.isEmpty()) {
                    if (!locDetlFieldService.removeBatchByIds(detlFields)) {
                        throw new CoolException("删除明细扩展字段失败");
                    }
                }
            //添加库存明细扩展字段
            List<TaskDetlField> detlFields = taskDetlFieldService.list(new LambdaQueryWrapper<TaskDetlField>().eq(TaskDetlField::getDetlId, taskDetl.getId()).eq(TaskDetlField::getHostId, hostId));
            for (TaskDetlField detlField : detlFields) {
                LocDetlField locDetlField = new LocDetlField();
                locDetlField.setDetlId(locDetl.getId());
                locDetlField.setFieldId(detlField.getFieldId());
                locDetlField.setName(detlField.getName());
                locDetlField.setValue(detlField.getValue());
                locDetlField.setHostId(hostId);
                if (!locDetlFieldService.save(locDetlField)) {
                    throw new CoolException("插入明细扩展字段失败");
                if (!locDetlService.removeById(locdetl)) {
                    throw new CoolException("库存明细删除失败!!");
                }
            } else {
                LocDetl  locdetl =  locDetlService.getOne(new LambdaQueryWrapper<LocDetl>()
                        .eq(LocDetl::getLocId, loc.getId())
                        .eq(LocDetl::getBatch, taskDetl.getBatch())
                        .eq(LocDetl::getMatId, taskDetl.getMatId()));
                locdetl.setWorkQty(0.0);
                locdetl.setAnfme(anfme);
                if (!locDetlService.updateById(locdetl)) {
                    throw new CoolException("库存明细更新失败!!");
                }
            }
        }
@@ -353,7 +384,8 @@
    //出库
    private void executeTask101(Task task) {
        Long hostId = task.getHostId();
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
//        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
@@ -385,14 +417,21 @@
        }
    }
    /**
     * 将原有库存明细删除
     * 库存状态设置为空库状态
     * @param task
     */
    //拣料出库
    private void executeTask103(Task task) {
        Long hostId = task.getHostId();
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        //FIXME 暂时注释HOSTID筛选条件
//        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
@@ -405,6 +444,7 @@
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<LocDetl> detlList = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, loc.getId()).eq(LocDetl::getHostId, hostId));
        //删除库存明细
        for (LocDetl locDetl : detlList) {