package com.zy.asrs.wms.asrs.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.OrderSettleType; import com.zy.asrs.wms.asrs.entity.param.GeneratePakInParam; import com.zy.asrs.wms.asrs.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; import java.util.Random; @Service("workService") public class WorkServiceImpl implements WorkService { @Autowired private TaskService taskService; @Autowired private TaskDetlService taskDetlService; @Autowired private WaitPakinService waitPakinService; @Autowired private TaskTypeService taskTypeService; @Autowired private OrderDetlFieldService orderDetlFieldService; @Autowired private TaskDetlFieldService taskDetlFieldService; @Autowired private OrderService orderService; @Autowired private OrderDetlService orderDetlService; @Autowired private LocService locService; @Autowired private LocStsService locStsService; @Autowired private TaskLogService taskLogService; @Autowired private TaskDetlLogService taskDetlLogService; @Autowired private TaskDetlFieldLogService taskDetlFieldLogService; @Override public String generateTaskNo(Long taskType) { Random random = new Random(); int nextInt = random.nextInt(99999); return "R" + nextInt; } @Override public Integer generateIoPri(Long taskType) { return 10; } @Override public Loc generateLoc(Long taskType) { LocSts locSts = locStsService.getOne(new LambdaQueryWrapper().eq(LocSts::getLocSts, String.valueOf(LocStsType.O))); List list = locService.list(new LambdaQueryWrapper().eq(Loc::getLocStsId, locSts.getId())); if (list.isEmpty()) { throw new CoolException("没有空库位"); } return list.get(0); } @Override @Transactional public boolean generatePakIn(GeneratePakInParam param) { List waitPakins = waitPakinService.list(new LambdaQueryWrapper().eq(WaitPakin::getBarcode, param.getBarcode())); if (waitPakins.isEmpty()) { throw new CoolException("托盘未组托"); } TaskType taskType = taskTypeService.getById(param.getTaskType()); if (taskType == null) { throw new CoolException("任务类型不存在"); } //生成库位 Loc loc = this.generateLoc(param.getTaskType()); Task task = new Task(); task.setTaskNo(this.generateTaskNo(taskType.getId()));//任务号 task.setTaskSts(1L);//1.生成入库任务 task.setTaskType(taskType.getId());//任务类型 task.setIoPri(this.generateIoPri(taskType.getId()));//优先级 task.setOriginLoc(null); task.setTargetLoc(loc.getLocNo()); task.setOriginSite(param.getOriginSite()); task.setTargetSite(null); task.setBarcode(param.getBarcode());//托盘码 boolean taskSave = taskService.save(task); if (!taskSave) { throw new CoolException("任务生成失败"); } //生成任务明细 for (WaitPakin waitPakin : waitPakins) { TaskDetl taskDetl = new TaskDetl(); taskDetl.setTaskId(task.getId()); taskDetl.setTaskNo(task.getTaskNo()); taskDetl.setAnfme(waitPakin.getAnfme());//数量 taskDetl.setStock(0D);//库存 taskDetl.setBatch(waitPakin.getDetl$().getBatch());//批号 taskDetl.setBarcode(waitPakin.getBarcode()); taskDetl.setOrderId(waitPakin.getOrderId()); taskDetl.setOrderNo(waitPakin.getOrderNo()); taskDetl.setDetlId(waitPakin.getDetlId()); taskDetl.setMatId(waitPakin.getDetl$().getMatId()); boolean taskDetlSave = taskDetlService.save(taskDetl); if(!taskDetlSave){ throw new CoolException("任务明细生成失败"); } //生成明细扩展 List orderDetlFields = orderDetlFieldService.list(new LambdaQueryWrapper().eq(OrderDetlField::getDetlId, waitPakin.getDetlId())); for (OrderDetlField orderDetlField : orderDetlFields) { TaskDetlField taskDetlField = new TaskDetlField(); taskDetlField.setName(orderDetlField.getName()); taskDetlField.setFieldId(orderDetlField.getFieldId()); taskDetlField.setDetlId(taskDetl.getId()); taskDetlField.setValue(orderDetlField.getValue()); boolean taskDetlFieldSave = taskDetlFieldService.save(taskDetlField); if(!taskDetlFieldSave){ throw new CoolException("明细扩展生成失败"); } } //更新组托通知档 waitPakin.setIoStatus(1); waitPakin.setUpdateTime(new Date()); boolean waitPakinUpdate = waitPakinService.updateById(waitPakin); if(!waitPakinUpdate){ throw new CoolException("组托通知档更新失败"); } //更新订单明细数据 OrderDetl orderDetl = orderDetlService.getById(taskDetl.getDetlId()); if(orderDetl == null){ throw new CoolException("订单明细不存在"); } orderDetl.setWorkQty(orderDetl.getWorkQty() + taskDetl.getAnfme()); orderDetl.setUpdateTime(new Date()); boolean orderDetlUpdate = orderDetlService.updateById(orderDetl); if(!orderDetlUpdate){ throw new CoolException("订单明细更新失败"); } //获取订单 Order order = orderService.getById(taskDetl.getOrderId()); if(order == null){ throw new CoolException("订单不存在"); } //更新订单状态 if (order.getOrderSettle().equals(OrderSettleType.WAIT.val())) { order.setOrderSettle(OrderSettleType.WORKING.val()); order.setUpdateTime(new Date()); if (!orderService.updateById(order)) { throw new CoolException("订单数据更新失败"); } } } //库位O => S loc.setLocStsId(LocStsType.S.val()); loc.setUpdateTime(new Date()); boolean locUpdate = locService.updateById(loc); if(!locUpdate){ throw new CoolException("库位状态更新失败"); } return true; } @Override public boolean completeTask(Long taskId) { Task task = taskService.getById(taskId); if(task == null){ throw new CoolException("任务不存在"); } List taskDetls = taskDetlService.getTaskDetlByTaskId(taskId); if (taskDetls.isEmpty()) { throw new CoolException("任务明细不存在"); } if (task.getTaskType() < 100) { //入库 task.setTaskSts(99L);//99.入库完成 }else { //出库 task.setTaskSts(199L);//199.出库完成 } task.setUpdateTime(new Date()); if (!taskService.updateById(task)) { throw new CoolException("任务更新失败"); } return true; } @Override @Transactional public boolean cancelTask(Long taskId) { Task task = taskService.getById(taskId); if(task == null){ throw new CoolException("任务不存在"); } List taskDetls = taskDetlService.getTaskDetlByTaskId(taskId); if (taskDetls.isEmpty()) { throw new CoolException("任务明细不存在"); } //更新库位状态 Loc loc = null; switch (task.getTaskType().intValue()) { case 1://入库 case 53://拣料 case 54://并板 case 57://盘点 loc = locService.getOne(new LambdaQueryWrapper().eq(Loc::getLocNo, task.getTargetLoc())); if(loc == null){ throw new CoolException("库位不存在"); } if(loc.getLocStsId() != LocStsType.S.val()){ throw new CoolException("库位状态不处于S.入库预约"); } loc.setLocStsId(LocStsType.O.val()); loc.setUpdateTime(new Date()); if(!locService.updateById(loc)){ throw new CoolException("库位状态变更失败"); } break; case 101://出库 case 103://拣料 case 104://并板 case 107://盘点 loc = locService.getOne(new LambdaQueryWrapper().eq(Loc::getLocNo, task.getOriginLoc())); if(loc == null){ throw new CoolException("库位不存在"); } if(loc.getLocStsId() != LocStsType.R.val()){ throw new CoolException("库位状态不处于R.出库预约"); } loc.setLocStsId(LocStsType.F.val()); loc.setUpdateTime(new Date()); if(!locService.updateById(loc)){ throw new CoolException("库位状态变更失败"); } break; } //回滚订单 for (TaskDetl taskDetl : taskDetls) { OrderDetl orderDetl = orderDetlService.getById(taskDetl.getDetlId()); if(orderDetl == null){ throw new CoolException("订单明细不存在"); } //回滚工作数量 orderDetl.setWorkQty(orderDetl.getWorkQty() - taskDetl.getAnfme()); orderDetl.setUpdateTime(new Date()); boolean orderDetlUpdate = orderDetlService.updateById(orderDetl); if(!orderDetlUpdate){ throw new CoolException("工作数量回滚失败"); } //入库回滚组托通知档 if (task.getTaskType() == 1) { WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper().eq(WaitPakin::getDetlId, taskDetl.getDetlId()).eq(WaitPakin::getBarcode, task.getBarcode())); if(waitPakin == null){ throw new CoolException("组托通知档不存在"); } waitPakin.setIoStatus(0); waitPakin.setUpdateTime(new Date()); boolean updateWaitPakin = waitPakinService.updateById(waitPakin); if(!updateWaitPakin){ throw new CoolException("组托通知档回滚失败"); } } List detlFields = taskDetlFieldService.list(new LambdaQueryWrapper().eq(TaskDetlField::getDetlId, taskDetl.getId())); for (TaskDetlField detlField : detlFields) { //明细扩展字段数据保存至历史档 TaskDetlFieldLog taskDetlFieldLog = new TaskDetlFieldLog(); taskDetlFieldLog.sync(detlField); if (!taskDetlFieldLogService.save(taskDetlFieldLog)) { throw new CoolException("明细扩展字段转历史档案失败"); } //删除明细扩展 boolean removeField = taskDetlFieldService.removeById(detlField.getId()); if(!removeField){ throw new CoolException("回滚扩展明细失败"); } } //明细数据保存至历史档 TaskDetlLog taskDetlLog = new TaskDetlLog(); taskDetlLog.sync(taskDetl); if (!taskDetlLogService.save(taskDetlLog)) { throw new CoolException("明细数据转历史档案失败"); } //删除明细 boolean removeDetl = taskDetlService.removeById(taskDetl.getId()); if(!removeDetl){ throw new CoolException("回滚明细失败"); } } //数据保存至历史档 TaskLog taskLog = new TaskLog(); taskLog.sync(task); if (!taskLogService.save(taskLog)) { throw new CoolException("任务档案转历史档案失败"); } //删除任务 boolean removeTask = taskService.removeById(taskId); if(!removeTask){ throw new CoolException("回滚任务失败"); } return true; } }