|  |  | 
 |  |  | package com.zy.asrs.wms.asrs.manage; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.zy.asrs.framework.exception.CoolException; | 
 |  |  | import com.zy.asrs.wms.asrs.controller.req.OutReq; | 
 |  |  | import com.zy.asrs.wms.asrs.entity.dto.OutDetlDto; | 
 |  |  | import com.zy.asrs.wms.asrs.entity.dto.OutLocDto; | 
 |  |  | import com.zy.asrs.wms.asrs.entity.enums.LocStsType; | 
 |  |  | import com.zy.asrs.wms.asrs.entity.param.OutParam; | 
 |  |  | import com.zy.asrs.wms.asrs.entity.*; | 
 |  |  | import com.zy.asrs.wms.asrs.entity.param.FieldParam; | 
 |  |  | import com.zy.asrs.wms.asrs.service.*; | 
 |  |  | import com.zy.asrs.wms.utils.OutUtils; | 
 |  |  | import org.slf4j.Logger; | 
 |  |  | import org.slf4j.LoggerFactory; | 
 |  |  | 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.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 出库管理 | 
 |  |  | 
 |  |  |  | 
 |  |  |     @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 WorkService workService; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private OrderService orderService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private OrderDetlService orderDetlService; | 
 |  |  |     @Autowired | 
 |  |  |     private OutUtils outUtils; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取库存 | 
 |  |  |      * | 
 |  |  |      * @param map | 
 |  |  |      */ | 
 |  |  |     public List<LocDetl> listSuitableMat(Map<String, Object> map) { | 
 |  |  |         List<FieldParam> param = new ArrayList<>(); | 
 |  |  |         map.entrySet().stream().filter(entry -> entry.getValue() != null).forEach(entry -> { | 
 |  |  |             FieldParam fieldParam = new FieldParam(); | 
 |  |  |             fieldParam.setName(entry.getKey()); | 
 |  |  |             fieldParam.setValue(entry.getValue()); | 
 |  |  |             param.add(fieldParam); | 
 |  |  |         }); | 
 |  |  |         //TODO 这里加上出库规则 | 
 |  |  |  | 
 |  |  |         List<LocDetl> stock = locDetlService.listSuitableMat(param, null); | 
 |  |  |         return stock; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据订单id获取库存 | 
 |  |  |      * | 
 |  |  |      * @param orderId 订单ID | 
 |  |  |      */ | 
 |  |  |     public List<LocDetl> listSuitableMatByOrder(Long orderId) { | 
 |  |  |         Order order = orderService.getById(orderId); | 
 |  |  |         List<OrderDetl> orderDetls = orderDetlService.getOrderDetlByOrderId(orderId); | 
 |  |  |         //TODO 这里加上出库规则 | 
 |  |  |         for (OrderDetl orderDetl : orderDetls) { | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |         List<FieldParam> param = new ArrayList<>(); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         List<LocDetl> stock = locDetlService.listSuitableMat(param, null); | 
 |  |  |         return stock; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据订单id获取库存 | 
 |  |  |      * | 
 |  |  |      * @param waveId 订单ID | 
 |  |  |      */ | 
 |  |  |     public List<LocDetl> listSuitableMatByWave(Long waveId) { | 
 |  |  |  | 
 |  |  |         List<FieldParam> param = new ArrayList<>(); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         List<LocDetl> stock = locDetlService.listSuitableMat(param, null); | 
 |  |  |         return stock; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 出库 | 
 |  |  |      * | 
 |  |  |      * @param outReq | 
 |  |  |      */ | 
 |  |  |     public void out(OutReq outReq) { | 
 |  |  |         Map<Long, List<OutReq.Detl>> map = outReq.getDetls().stream().collect(Collectors.groupingBy(OutReq.Detl::getLocDetlId)); | 
 |  |  |         map.forEach((k, v) -> { | 
 |  |  |             Loc loc = locService.getById(k); | 
 |  |  |             processTask(loc, v, outReq.getTargetSite()); | 
 |  |  |         }); | 
 |  |  |     @Transactional | 
 |  |  |     public void out(OutParam outParam) { | 
 |  |  |         if (outParam.getOperationPort() == null) { | 
 |  |  |             throw new CoolException("作业口不存在"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         List<OutLocDto> list = outUtils.merge(outParam); | 
 |  |  |         processTask(list); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void processTask(List<OutLocDto> list) { | 
 |  |  |         for (OutLocDto locDto : list) { | 
 |  |  |             long taskType = locDto.getAll() ? 101L : 103L; | 
 |  |  |  | 
 |  |  |     private void processTask(Loc loc, List<OutReq.Detl> detls, String targetSite) { | 
 |  |  |         // 保存工作档 | 
 |  |  |         Task task = new Task(); | 
 |  |  |         task.setTaskNo(workService.generateTaskNo(1L)); | 
 |  |  |         // 任务状态 | 
 |  |  |         task.setTaskSts(101L); | 
 |  |  |         // 任务类型 | 
 |  |  |         task.setTaskType(101L); | 
 |  |  |         // 优先级 | 
 |  |  |         task.setIoPri(1); | 
 |  |  |         // 源库位 | 
 |  |  |         task.setOriginLoc(loc.getLocNo()); | 
 |  |  |         // 目标站点 | 
 |  |  |         task.setTargetSite(targetSite); | 
 |  |  |         // 托盘码 | 
 |  |  |         task.setBarcode(loc.getBarcode()); | 
 |  |  |         boolean res = taskService.save(task); | 
 |  |  |         if (!res) { | 
 |  |  |             throw new CoolException("保存工作档失败"); | 
 |  |  |         } | 
 |  |  |         // 工作档明细保存 | 
 |  |  |         for (OutReq.Detl detl : detls) { | 
 |  |  |             LocDetl locDetl = locDetlService.getById(detl.getLocDetlId()); | 
 |  |  |             TaskDetl taskDetl = new TaskDetl(); | 
 |  |  |             taskDetl.sync(locDetl); | 
 |  |  |             taskDetl.setTaskId(task.getId()); | 
 |  |  |             if (!taskDetlService.save(taskDetl)) { | 
 |  |  |                 throw new CoolException("保存工作档明细失败"); | 
 |  |  |             Loc loc = locService.getById(locDto.getLocId()); | 
 |  |  |             if(loc == null) { | 
 |  |  |                 throw new CoolException("库位不存在"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (!loc.getLocStsId().equals(LocStsType.F.val())) { | 
 |  |  |                 throw new CoolException("库位状态不在库"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             Task task = new Task(); | 
 |  |  |             task.setTaskNo(workService.generateTaskNo(1L)); | 
 |  |  |             task.setTaskSts(101L); | 
 |  |  |             task.setTaskType(taskType); | 
 |  |  |             task.setIoPri(workService.generateIoPri(taskType)); | 
 |  |  |             task.setOriginLoc(loc.getLocNo()); | 
 |  |  |             task.setTargetSite(locDto.getOperationPort()); | 
 |  |  |             task.setBarcode(loc.getBarcode()); | 
 |  |  |             boolean res = taskService.save(task); | 
 |  |  |             if (!res) { | 
 |  |  |                 throw new CoolException("保存工作档失败"); | 
 |  |  |             } | 
 |  |  |             // 工作档明细保存 | 
 |  |  |             for (OutDetlDto detl : locDto.getDetls()) { | 
 |  |  |                 LocDetl locDetl = locDetlService.getById(detl.getDetlId()); | 
 |  |  |                 if(locDetl == null) { | 
 |  |  |                     throw new CoolException("明细不存在"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 TaskDetl taskDetl = new TaskDetl(); | 
 |  |  |                 taskDetl.sync(locDetl); | 
 |  |  |                 taskDetl.setTaskId(task.getId()); | 
 |  |  |                 taskDetl.setAnfme(detl.getAnfme()); | 
 |  |  |                 taskDetl.setStock(detl.getStock()); | 
 |  |  |                 if (!taskDetlService.save(taskDetl)) { | 
 |  |  |                     throw new CoolException("保存工作档明细失败"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 List<LocDetlField> locDetlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId())); | 
 |  |  |                 for (LocDetlField locDetlField : locDetlFields) { | 
 |  |  |                     TaskDetlField taskDetlField = new TaskDetlField(); | 
 |  |  |                     taskDetlField.sync(locDetlField); | 
 |  |  |                     taskDetlField.setDetlId(taskDetl.getId()); | 
 |  |  |                     boolean taskDetlFieldSave = taskDetlFieldService.save(taskDetlField); | 
 |  |  |                     if(!taskDetlFieldSave){ | 
 |  |  |                         throw new CoolException("明细扩展生成失败"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             //库位F => R | 
 |  |  |             loc.setLocStsId(LocStsType.R.val()); | 
 |  |  |             loc.setUpdateTime(new Date()); | 
 |  |  |             boolean locUpdate = locService.updateById(loc); | 
 |  |  |             if(!locUpdate){ | 
 |  |  |                 throw new CoolException("库位状态更新失败"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } |