skyouc
2024-12-21 c635d78b479510ebe2556a420948effcd30a0731
zy-asrs-wms/src/main/java/com/zy/asrs/wms/asrs/timer/TaskTimer.java
@@ -1,423 +1,423 @@
package com.zy.asrs.wms.asrs.timer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.*;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.Date;
import java.util.List;
@Component
public class TaskTimer {
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskDetlService taskDetlService;
    @Autowired
    private TaskDetlFieldService taskDetlFieldService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private LocDetlFieldService locDetlFieldService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private WaitPakinLogService waitPakinLogService;
    @Autowired
    private TaskDetlLogService taskDetlLogService;
    @Autowired
    private TaskDetlFieldLogService taskDetlFieldLogService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void inExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取入库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.COMPLETE_IN.id));
            if (list.isEmpty()) {
                return;
            }
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 1://入库
                        executeTask1(task);
                        break;
                    case 11://库位移转
                        executeTask11(task);
                        break;
                    case 53://拣料再入库
                        executeTask53(task);
                        break;
                    default:
                        throw new CoolException("未知任务类型");
                }
                task.setTaskSts(TaskStsType.UPDATED_IN.id);//100.库存更新完成
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    throw new CoolException("库存更新失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void outExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取出库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
            if (list.isEmpty()) {
                return;
            }
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 101://出库
                        executeTask101(task);
                        break;
                    case 103://拣料
                        executeTask103(task);
                        break;
                    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();
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
    //入库
    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("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            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(taskDetl.getAnfme());
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                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("插入明细扩展字段失败");
                }
            }
        }
        //组托通知档转历史档
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()).eq(WaitPakin::getHostId, hostId));
        if (waitPakins.isEmpty()) {
            throw new CoolException("组托通知档不存在");
        }
        for (WaitPakin waitPakin : waitPakins) {
            WaitPakinLog waitPakinLog = new WaitPakinLog();
            waitPakinLog.sync(waitPakin);
            if (!waitPakinLogService.save(waitPakinLog)) {
                throw new CoolException("组托通知档转历史档失败");
            }
            //删除组托通知档
            waitPakinService.removeById(waitPakin.getId());
        }
    }
    //库位移转
    private void executeTask11(Task task) {
        Long hostId = task.getHostId();
        Loc originLoc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        if (originLoc == null) {
            throw new CoolException("源库位不存在");
        }
        if (originLoc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        Loc targetLoc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        if (targetLoc == null) {
            throw new CoolException("目标库位不存在");
        }
        if (targetLoc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
        originLoc.setLocStsId(LocStsType.O.val());
        originLoc.setUpdateTime(new Date());
        originLoc.setBarcode("");
        if (!locService.updateById(originLoc)) {
            throw new CoolException("库位状态更新失败");
        }
        targetLoc.setLocStsId(LocStsType.F.val());
        targetLoc.setUpdateTime(new Date());
        targetLoc.setBarcode(task.getBarcode());
        if (!locService.updateById(targetLoc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            LocDetl locDetl = new LocDetl();
            locDetl.setLocId(targetLoc.getId());
            locDetl.setLocNo(targetLoc.getLocNo());
            locDetl.setMatId(taskDetl.getMatId());
            locDetl.setMatnr(taskDetl.getMat$().getMatnr());
            locDetl.setOrderNo(taskDetl.getOrderNo());
            locDetl.setBatch(taskDetl.getBatch());
            locDetl.setAnfme(taskDetl.getAnfme());
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                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("插入明细扩展字段失败");
                }
            }
        }
        List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, originLoc.getId()));
        for (LocDetl locDetl : locDetls) {
            boolean remove = locDetlFieldService.remove(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()));
            boolean result = locDetlService.removeById(locDetl.getId());
            if (!result) {
                throw new CoolException("清除明细失败");
            }
        }
    }
    //拣料再入库
    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("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            double anfme = taskDetl.getStock() - taskDetl.getAnfme();
            if (anfme <= 0) {
                continue;
            }
            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<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("插入明细扩展字段失败");
                }
            }
        }
    }
    //出库
    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));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        loc.setLocStsId(LocStsType.O.val());
        loc.setBarcode("");
        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) {
            if (!locDetlService.removeById(locDetl)) {
                throw new CoolException("删除库存明细失败");
            }
            List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()).eq(LocDetlField::getHostId, hostId));
            for (LocDetlField detlField : detlFields) {
                if (!locDetlFieldService.removeById(detlField)) {
                    throw new CoolException("删除明细扩展字段失败");
                }
            }
        }
    }
    //拣料出库
    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));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        loc.setLocStsId(LocStsType.O.val());
        loc.setBarcode("");
        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) {
            if (!locDetlService.removeById(locDetl)) {
                throw new CoolException("删除库存明细失败");
            }
            List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()).eq(LocDetlField::getHostId, hostId));
            for (LocDetlField detlField : detlFields) {
                if (!locDetlFieldService.removeById(detlField)) {
                    throw new CoolException("删除明细扩展字段失败");
                }
            }
        }
    }
}
package com.zy.asrs.wms.asrs.timer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.*;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.Date;
import java.util.List;
@Component
public class TaskTimer {
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskDetlService taskDetlService;
    @Autowired
    private TaskDetlFieldService taskDetlFieldService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private LocDetlFieldService locDetlFieldService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private WaitPakinLogService waitPakinLogService;
    @Autowired
    private TaskDetlLogService taskDetlLogService;
    @Autowired
    private TaskDetlFieldLogService taskDetlFieldLogService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
//    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void inExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取入库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.COMPLETE_IN.id));
            if (list.isEmpty()) {
                return;
            }
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 1://入库
                        executeTask1(task);
                        break;
                    case 11://库位移转
                        executeTask11(task);
                        break;
                    case 53://拣料再入库
                        executeTask53(task);
                        break;
                    default:
                        throw new CoolException("未知任务类型");
                }
                task.setTaskSts(TaskStsType.UPDATED_IN.id);//100.库存更新完成
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    throw new CoolException("库存更新失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
//    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void outExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取出库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
            if (list.isEmpty()) {
                return;
            }
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 101://出库
                        executeTask101(task);
                        break;
                    case 103://拣料
                        executeTask103(task);
                        break;
                    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();
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
    //入库
    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("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            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(taskDetl.getAnfme());
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                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("插入明细扩展字段失败");
                }
            }
        }
        //组托通知档转历史档
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()).eq(WaitPakin::getHostId, hostId));
        if (waitPakins.isEmpty()) {
            throw new CoolException("组托通知档不存在");
        }
        for (WaitPakin waitPakin : waitPakins) {
            WaitPakinLog waitPakinLog = new WaitPakinLog();
            waitPakinLog.sync(waitPakin);
            if (!waitPakinLogService.save(waitPakinLog)) {
                throw new CoolException("组托通知档转历史档失败");
            }
            //删除组托通知档
            waitPakinService.removeById(waitPakin.getId());
        }
    }
    //库位移转
    private void executeTask11(Task task) {
        Long hostId = task.getHostId();
        Loc originLoc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        if (originLoc == null) {
            throw new CoolException("源库位不存在");
        }
        if (originLoc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        Loc targetLoc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        if (targetLoc == null) {
            throw new CoolException("目标库位不存在");
        }
        if (targetLoc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
        originLoc.setLocStsId(LocStsType.O.val());
        originLoc.setUpdateTime(new Date());
        originLoc.setBarcode("");
        if (!locService.updateById(originLoc)) {
            throw new CoolException("库位状态更新失败");
        }
        targetLoc.setLocStsId(LocStsType.F.val());
        targetLoc.setUpdateTime(new Date());
        targetLoc.setBarcode(task.getBarcode());
        if (!locService.updateById(targetLoc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            LocDetl locDetl = new LocDetl();
            locDetl.setLocId(targetLoc.getId());
            locDetl.setLocNo(targetLoc.getLocNo());
            locDetl.setMatId(taskDetl.getMatId());
            locDetl.setMatnr(taskDetl.getMat$().getMatnr());
            locDetl.setOrderNo(taskDetl.getOrderNo());
            locDetl.setBatch(taskDetl.getBatch());
            locDetl.setAnfme(taskDetl.getAnfme());
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                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("插入明细扩展字段失败");
                }
            }
        }
        List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, originLoc.getId()));
        for (LocDetl locDetl : locDetls) {
            boolean remove = locDetlFieldService.remove(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()));
            boolean result = locDetlService.removeById(locDetl.getId());
            if (!result) {
                throw new CoolException("清除明细失败");
            }
        }
    }
    //拣料再入库
    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("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            double anfme = taskDetl.getStock() - taskDetl.getAnfme();
            if (anfme <= 0) {
                continue;
            }
            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<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("插入明细扩展字段失败");
                }
            }
        }
    }
    //出库
    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));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        loc.setLocStsId(LocStsType.O.val());
        loc.setBarcode("");
        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) {
            if (!locDetlService.removeById(locDetl)) {
                throw new CoolException("删除库存明细失败");
            }
            List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()).eq(LocDetlField::getHostId, hostId));
            for (LocDetlField detlField : detlFields) {
                if (!locDetlFieldService.removeById(detlField)) {
                    throw new CoolException("删除明细扩展字段失败");
                }
            }
        }
    }
    //拣料出库
    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));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
        loc.setLocStsId(LocStsType.O.val());
        loc.setBarcode("");
        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) {
            if (!locDetlService.removeById(locDetl)) {
                throw new CoolException("删除库存明细失败");
            }
            List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()).eq(LocDetlField::getHostId, hostId));
            for (LocDetlField detlField : detlFields) {
                if (!locDetlFieldService.removeById(detlField)) {
                    throw new CoolException("删除明细扩展字段失败");
                }
            }
        }
    }
}