|  |  | 
 |  |  | package com.zy.asrs.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
 |  |  | import com.core.common.BaseRes; | 
 |  |  | import com.core.common.Cools; | 
 |  |  | import com.core.common.DateUtils; | 
 |  |  | import com.core.common.SnowflakeIdWorker; | 
 |  |  | import com.core.exception.CoolException; | 
 |  |  | import com.zy.asrs.entity.MatCode; | 
 |  |  | import com.zy.asrs.entity.WaitPakin; | 
 |  |  | import com.zy.asrs.entity.*; | 
 |  |  | import com.zy.asrs.entity.param.CombParam; | 
 |  |  | import com.zy.asrs.service.MatCodeService; | 
 |  |  | import com.zy.asrs.service.MobileService; | 
 |  |  | import com.zy.asrs.service.WaitPakinService; | 
 |  |  | import com.zy.asrs.entity.param.MobileAdjustParam; | 
 |  |  | import com.zy.asrs.entity.param.OpenOrderPakinParam; | 
 |  |  | import com.zy.asrs.service.*; | 
 |  |  | import com.zy.asrs.task.core.ReturnT; | 
 |  |  | import com.zy.asrs.task.handler.WorkLogHandler; | 
 |  |  | import com.zy.asrs.utils.MatUtils; | 
 |  |  | import com.zy.common.constant.MesConstant; | 
 |  |  | import com.zy.common.entity.Parameter; | 
 |  |  | import com.zy.common.model.DetlDto; | 
 |  |  | import com.zy.common.model.MesCombParam; | 
 |  |  | import com.zy.common.utils.HttpHandler; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | 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.Iterator; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 移动端服务核心类 | 
 |  |  |  * Created by vincent on 2020/6/28 | 
 |  |  |  */ | 
 |  |  | @Slf4j | 
 |  |  | @Service | 
 |  |  | public class MobileServiceImpl implements MobileService { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private MatCodeService matCodeService; | 
 |  |  |     private MatService matService; | 
 |  |  |     @Autowired | 
 |  |  |     private WaitPakinService waitPakinService; | 
 |  |  |     @Autowired | 
 |  |  |     private OrderService orderService; | 
 |  |  |     @Autowired | 
 |  |  |     private OrderDetlService orderDetlService; | 
 |  |  |     @Autowired | 
 |  |  |     private BasDevpService basDevpService; | 
 |  |  |     @Autowired | 
 |  |  |     private WrkMastService wrkMastService; | 
 |  |  |     @Autowired | 
 |  |  |     private WrkDetlService wrkDetlService; | 
 |  |  |     @Autowired | 
 |  |  |     private PackService packService; | 
 |  |  |     @Autowired | 
 |  |  |     private OpenService openService; | 
 |  |  |     @Autowired | 
 |  |  |     private SnowflakeIdWorker snowflakeIdWorker; | 
 |  |  |     @Autowired | 
 |  |  |     private ApiLogService apiLogService; | 
 |  |  |     @Autowired | 
 |  |  |     private WorkLogHandler workLogHandler; | 
 |  |  |     @Autowired | 
 |  |  |     private AdjDetlService adjDetlService; | 
 |  |  |     @Autowired | 
 |  |  |     private CheckRecordService checkRecordService; | 
 |  |  |     @Autowired | 
 |  |  |     private LocDetlService locDetlService; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void comb(CombParam param, Long userId) { | 
 |  |  |         if (Cools.isEmpty(param.getBarcode()) || param.getCombMats().isEmpty()) { | 
 |  |  |         if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) { | 
 |  |  |             throw new CoolException(BaseRes.PARAM); | 
 |  |  |         } | 
 |  |  |         int count = waitPakinService.selectCount(new EntityWrapper<WaitPakin>(). | 
 |  |  |                 eq("barcode", param.getBarcode()).eq("status", "N")); | 
 |  |  |         if (count > 0) { | 
 |  |  |             throw new CoolException("条码数据已存在"); | 
 |  |  |         // 判断是否有相同条码的数据 | 
 |  |  | //        if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>(). | 
 |  |  | //                eq("zpallet", param.getBarcode()).eq("io_status", "N")) > 0) { | 
 |  |  | //            throw new CoolException(param.getBarcode() + "数据正在进行入库"); | 
 |  |  | //        } | 
 |  |  |         if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>(). | 
 |  |  |                 eq("zpallet", param.getBarcode())) > 0) { | 
 |  |  |             throw new CoolException(param.getBarcode() + "组托数据已存在"); | 
 |  |  |         } | 
 |  |  |         for (CombParam.CombMat combMat : param.getCombMats()) { | 
 |  |  |             MatCode matCode = matCodeService.selectById(combMat.getMatNo()); | 
 |  |  |             if (Cools.isEmpty(matCode)) { | 
 |  |  |                 throw new CoolException("物料数据错误"); | 
 |  |  |  | 
 |  |  |         // todo: 不下线重新入库 | 
 |  |  |         WrkMast wrkMast = wrkMastService.selectByBarcode(param.getBarcode()); | 
 |  |  |         if (wrkMast != null && wrkMast.getWrkSts() == 18) { | 
 |  |  |             ReturnT<String> start = workLogHandler.start(wrkMast); | 
 |  |  |             if (!start.isSuccess()) { | 
 |  |  |                 log.error("工作档[workNo={}]历史档处理失败", wrkMast.getWrkNo()); | 
 |  |  |             } | 
 |  |  |             WaitPakin waitPakin = new WaitPakin( | 
 |  |  |                     param.getBarcode(),    // 托盘码 | 
 |  |  |                     matCode.getMatNo(),    // 物料编码 | 
 |  |  |                     matCode.getMatName(),    // 物料描述 | 
 |  |  |                     combMat.getCount(),    // 数量 | 
 |  |  |                     matCode.getStr1(),    // 单位 | 
 |  |  |                     "N",    // 状态 | 
 |  |  |                     null,    // 备注 | 
 |  |  |                     new Date(),    // 修改时间 | 
 |  |  |                     userId,    // 修改人员 | 
 |  |  |                     new Date(),    // 添加时间 | 
 |  |  |                     userId    // 创建者 | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         int countLoc = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet",param.getBarcode())); | 
 |  |  |         int countWrk = wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("zpallet",param.getBarcode())); | 
 |  |  | //        int countWrk = wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("zpallet",param.getBarcode()) | 
 |  |  | //                                            .last(" wrk_no IN ( SELECT wrk_no FROM asr_wrk_mast WHERE wrk_sts != 15 )")); | 
 |  |  |         if (wrkMast != null && wrkMast.getWrkSts()==18) countWrk = 0; | 
 |  |  |         if (countLoc > 0 || countWrk > 0) { | 
 |  |  |             throw new CoolException("工作档/库存条码数据已存在===>>" + param.getBarcode()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Date now = new Date(); | 
 |  |  |         int matType = 0; | 
 |  |  |  | 
 |  |  |         // 无单组托 | 
 |  |  |         if (Cools.isEmpty(param.getOrderNo())) { | 
 |  |  |  | 
 |  |  |             // 生成入库通知档 | 
 |  |  |             List<DetlDto> detlDtos = new ArrayList<>(); | 
 |  |  |             param.getCombMats().forEach(elem -> { | 
 |  |  |                 elem.setBatch(""); | 
 |  |  |                 DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme(), elem.getMemo(), elem.getCstmr()); | 
 |  |  |  | 
 |  |  |                 if (Cools.isEmpty(detlDto.getBatch())){ | 
 |  |  |                     String batch = DateUtils.convert(new Date(),DateUtils.yyyyMMdd); | 
 |  |  |                     detlDto.setBatch(batch); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 if (DetlDto.has(detlDtos, detlDto)) { | 
 |  |  |                     DetlDto one = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch()); | 
 |  |  |                     assert one != null; | 
 |  |  |                     one.setAnfme(one.getAnfme() + detlDto.getAnfme()); | 
 |  |  |                 } else { | 
 |  |  |                     detlDtos.add(detlDto); | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             for (DetlDto detlDto : detlDtos) { | 
 |  |  |                 Mat mat = matService.selectByMatnr(detlDto.getMatnr()); | 
 |  |  |                 if (Cools.isEmpty(mat)) { | 
 |  |  |                     throw new CoolException(detlDto.getMatnr() + "商品档案不存在"); | 
 |  |  |                 } | 
 |  |  |                 WaitPakin waitPakin = new WaitPakin(); | 
 |  |  |                 waitPakin.sync(mat); | 
 |  |  |                 waitPakin.setBatch(""); | 
 |  |  |                 waitPakin.setZpallet(param.getBarcode());   // 托盘码 | 
 |  |  |                 waitPakin.setIoStatus("N");     // 入出状态 | 
 |  |  |                 waitPakin.setAnfme(detlDto.getAnfme());  // 数量 | 
 |  |  |                 waitPakin.setStatus("Y");    // 状态 | 
 |  |  |                 waitPakin.setAppeUser(userId); | 
 |  |  |                 waitPakin.setAppeTime(now); | 
 |  |  |                 waitPakin.setModiUser(userId); | 
 |  |  |                 waitPakin.setModiTime(now); | 
 |  |  |                 waitPakin.setMemo(detlDto.getMemo()); | 
 |  |  |                 waitPakin.setFrozen(param.getFrozen()); // 是否冻结 | 
 |  |  |                 waitPakin.setManu(detlDto.getCstmr()); | 
 |  |  |                 waitPakin.setMatType(mat.getMatType());//物料类型 | 
 |  |  | //                if (Cools.isEmpty(mat.getMatType())){ | 
 |  |  | //                    throw new CoolException("物料类型异常"+mat.getMatnr()); | 
 |  |  | //                }else { | 
 |  |  | //                    if (matType == 0){ | 
 |  |  | //                        matType = mat.getMatType(); | 
 |  |  | //                    }else if (matType!=mat.getMatType()){ | 
 |  |  | //                        throw new CoolException("请组托同一类型的物料"); | 
 |  |  | //                    } | 
 |  |  | //                } | 
 |  |  |                 if (!waitPakinService.insert(waitPakin)) { | 
 |  |  |                     throw new CoolException("保存入库通知档失败"); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         // 关联组托 | 
 |  |  |         } else { | 
 |  |  |             Order order = orderService.selectByNo(param.getOrderNo()); | 
 |  |  |             if (order.getSettle() > 2) { | 
 |  |  |                 throw new CoolException("单据编号已过期"); | 
 |  |  |             } | 
 |  |  |             // 生成入库通知档 | 
 |  |  |             List<DetlDto> detlDtos = new ArrayList<>(); | 
 |  |  |             param.getCombMats().forEach(elem -> { | 
 |  |  |                 elem.setBatch(""); | 
 |  |  |                 // 订单明细数量校验 | 
 |  |  |                 OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), elem.getMatnr(), elem.getBatch()); | 
 |  |  |                 if (elem.getAnfme() > orderDetl.getEnableQty()) { | 
 |  |  |                     throw new CoolException(orderDetl.getMatnr() + "入库数量不合法"); | 
 |  |  |                 } | 
 |  |  |                 // 修改订单明细数量 | 
 |  |  |                 if (!orderDetlService.increase(order.getId(), elem.getMatnr(), elem.getBatch(), elem.getAnfme())) { | 
 |  |  |                     throw new CoolException("修改单据明细数量失败"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme()); | 
 |  |  |                 if (DetlDto.has(detlDtos, detlDto)) { | 
 |  |  |                     DetlDto one = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch()); | 
 |  |  |                     assert one != null; | 
 |  |  |                     one.setAnfme(one.getAnfme() + detlDto.getAnfme()); | 
 |  |  |                 } else { | 
 |  |  |                     detlDtos.add(detlDto); | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |             for (DetlDto detlDto : detlDtos) { | 
 |  |  |                 Mat mat = matService.selectByMatnr(detlDto.getMatnr()); | 
 |  |  |                 if (Cools.isEmpty(mat)) { | 
 |  |  |                     throw new CoolException(detlDto.getMatnr() + "商品档案不存在"); | 
 |  |  |                 } | 
 |  |  |                 WaitPakin waitPakin = new WaitPakin(); | 
 |  |  |                 waitPakin.sync(mat); | 
 |  |  |                 waitPakin.setOrderNo(order.getOrderNo());   // 单据编号 | 
 |  |  |                 waitPakin.setBatch("");     // 序列码 | 
 |  |  |                 waitPakin.setZpallet(param.getBarcode());   // 托盘码 | 
 |  |  |                 waitPakin.setIoStatus("N");     // 入出状态 | 
 |  |  |                 waitPakin.setAnfme(detlDto.getAnfme());  // 数量 | 
 |  |  |                 waitPakin.setStatus("Y");    // 状态 | 
 |  |  |                 waitPakin.setAppeUser(userId); | 
 |  |  |                 waitPakin.setAppeTime(now); | 
 |  |  |                 waitPakin.setModiUser(userId); | 
 |  |  |                 waitPakin.setModiTime(now); | 
 |  |  |                 waitPakin.setMatType(mat.getMatType());//物料类型 | 
 |  |  | //                if (Cools.isEmpty(mat.getMatType())){ | 
 |  |  | //                    throw new CoolException("物料类型异常"+mat.getMatnr()); | 
 |  |  | //                }else { | 
 |  |  | //                    if (matType == 0){ | 
 |  |  | //                        matType = mat.getMatType(); | 
 |  |  | //                    }else if (matType!=mat.getMatType()){ | 
 |  |  | //                        throw new CoolException("请组托同一类型的物料"); | 
 |  |  | //                    } | 
 |  |  | //                } | 
 |  |  |                 if (!waitPakinService.insert(waitPakin)) { | 
 |  |  |                     throw new CoolException("保存入库通知档失败"); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             orderService.updateSettle(order.getId(), 2L, userId); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void adjustNew(MobileAdjustParam param, Boolean re, Long userId) { | 
 |  |  |         WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", param.getBarcode())); | 
 |  |  |         if (Cools.isEmpty(wrkMast)){ | 
 |  |  |             throw new CoolException("托盘码:"+param.getBarcode()+" 无有效工作档"); | 
 |  |  |         }else if (wrkMast.getIoType()!=107){ | 
 |  |  |             throw new CoolException("托盘码:"+param.getBarcode()+" 所属工作档非盘点任务"); | 
 |  |  |         }else if (wrkMast.getWrkSts()!=17.0){ | 
 |  |  |             throw new CoolException("托盘码:"+param.getBarcode()+" 所属工作档工作状态不是出库完成"); | 
 |  |  |         }else { } | 
 |  |  |  | 
 |  |  |         List<WrkDetl> wrkDetls = param.getWrkDetls(); | 
 |  |  |         String recordRecordLog="托盘码"+param.getBarcode()+"盘点记录:"; | 
 |  |  |         AdjDetl adjDetl=new AdjDetl(); | 
 |  |  |         adjDetl.setAppeTime(new Date()); | 
 |  |  |         adjDetl.setAppeUser(userId); | 
 |  |  |         adjDetl.setModiTime(new Date()); | 
 |  |  |         adjDetl.setModiUser(userId); | 
 |  |  |         adjDetl.setLocNo(wrkMast.getSourceLocNo()); | 
 |  |  |         CheckRecord checkRecord=new CheckRecord(); | 
 |  |  |         checkRecord.setLocNo(wrkMast.getSourceLocNo()); | 
 |  |  |         checkRecord.setBarcode(param.getBarcode()); | 
 |  |  |         checkRecord.setCreateTime(new Date()); | 
 |  |  |         for (WrkDetl wrkDetl:wrkDetls){ | 
 |  |  |             WrkDetl wrkDetlOld = wrkDetlService.selectOne(new EntityWrapper<WrkDetl>() | 
 |  |  |                     .eq("wrk_no", wrkMast.getWrkNo()) | 
 |  |  |                     .eq("matnr",wrkDetl.getMatnr()) | 
 |  |  | //                    .eq("batch",Cools.isEmpty(wrkDetl.getBatch())?"":wrkDetl.getBatch()) | 
 |  |  |             ); | 
 |  |  |             if (!waitPakinService.insert(waitPakin)) { | 
 |  |  |                 throw new CoolException("保存数据失败"); | 
 |  |  | //            LocDetl locDetl = locDetlService.selectOne(new EntityWrapper<LocDetl>() | 
 |  |  | //                            .eq("loc_no", wrkMast.getLocNo()) | 
 |  |  | //                            .eq("matnr",wrkDetl.getMatnr()) | 
 |  |  | //                            .eq("batch",wrkDetl.getBatch()) | 
 |  |  | //            ); | 
 |  |  |  | 
 |  |  |             adjDetl.setMatnr(wrkDetl.getMatnr()); | 
 |  |  |             adjDetl.setBatch(wrkDetl.getBatch()); | 
 |  |  |             adjDetl.setAdjQty(wrkDetl.getAnfme()); | 
 |  |  |  | 
 |  |  |             checkRecord.setMatnr(wrkDetl.getMatnr()); | 
 |  |  |             checkRecord.setMaktx(wrkDetl.getMaktx()); | 
 |  |  |             checkRecord.setSpecs(wrkDetl.getSpecs()); | 
 |  |  |             checkRecord.setBatch(wrkDetl.getBatch()); | 
 |  |  |             checkRecord.setConfirmQty(wrkDetl.getAnfme()); | 
 |  |  |             if (Cools.isEmpty(wrkDetlOld)){ | 
 |  |  |                 wrkDetl.setWrkNo(wrkMast.getWrkNo()); | 
 |  |  |                 wrkDetl.setZpallet(wrkMast.getBarcode()); | 
 |  |  |                 wrkDetlService.insert(wrkDetl); | 
 |  |  |                 recordRecordLog=recordRecordLog+"\n"+"新增一条明细:"+"\t商品编号:"+wrkDetl.getMatnr()+"\t批号:"+wrkDetl.getBatch()+"\t数量:"+wrkDetl.getAnfme(); | 
 |  |  |                 adjDetl.setOriQty(0.0); | 
 |  |  |                 adjDetlService.insert(adjDetl); | 
 |  |  |                 checkRecord.setAnfme(0.0); | 
 |  |  |                 if (!re) { | 
 |  |  |                     checkRecordService.insert(checkRecord); | 
 |  |  |                 } | 
 |  |  |             }else if (wrkDetlOld.getAnfme()!=wrkDetl.getAnfme()){ | 
 |  |  |                 wrkDetlService.updateAnfme(wrkDetl.getAnfme(),wrkMast.getWrkNo(),wrkDetl.getMatnr(),wrkDetl.getBatch()); | 
 |  |  |                 recordRecordLog=recordRecordLog+"\n"+"更新一条明细:"+"\t商品编号:"+wrkDetl.getMatnr()+"\t批号:"+wrkDetl.getBatch()+"\t原数量:"+wrkDetlOld.getAnfme()+"\t新数量:"+wrkDetl.getAnfme(); | 
 |  |  |                 adjDetl.setOriQty(wrkDetlOld.getAnfme()); | 
 |  |  |                 adjDetlService.insert(adjDetl); | 
 |  |  |  | 
 |  |  |                 checkRecord.setAnfme(wrkDetlOld.getAnfme()); | 
 |  |  |                 if (!re) { | 
 |  |  |                     checkRecordService.insert(checkRecord); | 
 |  |  |                 } | 
 |  |  |             }else if (wrkDetlOld.getAnfme()==wrkDetl.getAnfme()){ | 
 |  |  |                 recordRecordLog=recordRecordLog+"\n"+"一条明细保持不变:"+"\t商品编号:"+wrkDetl.getMatnr()+"\t批号:"+wrkDetl.getBatch()+"\t数量:"+wrkDetlOld.getAnfme(); | 
 |  |  |                 continue; | 
 |  |  |             }else { | 
 |  |  |                 throw new CoolException("托盘码:"+param.getBarcode()+" 未知异常,请联系管理员"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         log.info(recordRecordLog); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void adjust(MobileAdjustParam param, Long userId) { | 
 |  |  |         BasDevp basDevp = basDevpService.selectById(param.getStaNo()); | 
 |  |  |         if (null == basDevp || basDevp.getWrkNo() == null) { | 
 |  |  |             throw new CoolException(param.getStaNo() + "盘点站无效"); | 
 |  |  |         } | 
 |  |  |         if (!param.getWrkNo().equals(basDevp.getWrkNo())) { | 
 |  |  |             throw new CoolException(param.getStaNo() + "盘点站更新,请重新检索"); | 
 |  |  |         } | 
 |  |  |         WrkMast wrkMast = wrkMastService.selectById(param.getWrkNo()); | 
 |  |  |         if (wrkMast.getWrkSts() < 10) { | 
 |  |  |             throw new CoolException("盘点无效,任务已盘点再入库"); | 
 |  |  |         } | 
 |  |  |         Date now = new Date(); | 
 |  |  |         List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo()); | 
 |  |  |  | 
 |  |  |         List<WrkDetl> list = param.getWrkDetls(); | 
 |  |  |  | 
 |  |  |         // 修改数量 | 
 |  |  |         Iterator<WrkDetl> iterator = wrkDetls.iterator(); | 
 |  |  |         while (iterator.hasNext()) { | 
 |  |  |             WrkDetl wrkDetl = iterator.next(); | 
 |  |  |             Iterator<WrkDetl> iterator1 = list.iterator(); | 
 |  |  |             while (iterator1.hasNext()) { | 
 |  |  |                 WrkDetl wrkDetl1 = iterator1.next(); | 
 |  |  |                 if (wrkDetl1.getAnfme() == 0) { | 
 |  |  |                     iterator1.remove(); | 
 |  |  |                 } | 
 |  |  |                 if (wrkDetl.getMatnr().equals(wrkDetl1.getMatnr()) && Cools.eq(wrkDetl.getBatch(), wrkDetl1.getBatch())) { | 
 |  |  |                     if (!wrkDetl.getAnfme().equals(wrkDetl1.getAnfme())) { | 
 |  |  |                         // todo 盘点记录、保存调整记录 | 
 |  |  |                         // 修改明细 | 
 |  |  |                         if (!wrkDetlService.updateAnfme(wrkDetl1.getAnfme(), wrkMast.getWrkNo(), wrkDetl.getMatnr(), wrkDetl.getBatch())) { | 
 |  |  |                             throw new CoolException(wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "序列码修改数量失败"); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     iterator.remove(); | 
 |  |  |                     iterator1.remove(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 删除明细 | 
 |  |  |         for (WrkDetl wrkDetl : wrkDetls) { | 
 |  |  |             // todo 盘点记录、保存调整记录 | 
 |  |  |             if (!wrkDetlService.updateAnfme(-1.0D, wrkMast.getWrkNo(), wrkDetl.getMatnr(), wrkDetl.getBatch())) { | 
 |  |  |                 throw new CoolException("删除" + wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "序列码任务明细失败"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 添加明细 | 
 |  |  |         for (WrkDetl wrkDetl : list) { | 
 |  |  |             if (wrkDetl.getAnfme() == 0.0D) { continue; } | 
 |  |  |             // todo 盘点记录、保存调整记录 | 
 |  |  |             String orderNo = wrkDetl.getOrderNo(); | 
 |  |  |             Mat mat = matService.selectByMatnr(wrkDetl.getMatnr()); | 
 |  |  |             wrkDetl.sync(mat); | 
 |  |  |             wrkDetl.setOrderNo(orderNo); | 
 |  |  |             wrkDetl.setModiTime(now); | 
 |  |  |             wrkDetl.setModiUser(userId); | 
 |  |  |             wrkDetl.setAppeTime(now); | 
 |  |  |             wrkDetl.setAppeUser(userId); | 
 |  |  |             if (!wrkDetlService.insert(wrkDetl)) { | 
 |  |  |                 throw new CoolException("添加" + wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "序列码任务明细失败"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 修改盘点任务主档状态 | 
 |  |  |         wrkMast.setFullPlt(wrkDetlService.selectByWrkNo(wrkMast.getWrkNo()).size() != 0?"Y":"N"); | 
 |  |  |         wrkMast.setModiTime(now); | 
 |  |  |         wrkMast.setModiUser(userId); | 
 |  |  |         if (!wrkMastService.updateById(wrkMast)) { | 
 |  |  |             throw new CoolException("修改盘点任务主档失败"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void packComb(CombParam param, Long userId) { | 
 |  |  |         if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) { | 
 |  |  |             throw new CoolException(BaseRes.PARAM); | 
 |  |  |         } | 
 |  |  |         // 判断是否有相同条码的数据 | 
 |  |  |         if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>(). | 
 |  |  |                 eq("zpallet", param.getBarcode()).eq("io_status", "N")) > 0) { | 
 |  |  |             throw new CoolException(param.getBarcode() + "数据正在进行入库"); | 
 |  |  |         } | 
 |  |  |         Date now = new Date(); | 
 |  |  |  | 
 |  |  |         boolean packDown = Parameter.get().getPackDown().equals("true"); | 
 |  |  |  | 
 |  |  |         // 无单组托 | 
 |  |  |         if (Cools.isEmpty(param.getOrderNo())) { | 
 |  |  |  | 
 |  |  |             // 生成入库通知档 | 
 |  |  |             List<DetlDto> detlDtos = new ArrayList<>(); | 
 |  |  |             param.getCombMats().forEach(elem -> { | 
 |  |  |                 // 打包上线数据校验 | 
 |  |  |                 if (packDown) { | 
 |  |  |                     Pack pack = packService.selectByBarcode(elem.getMatnr()); | 
 |  |  |                     if (pack == null) { | 
 |  |  |                         throw new CoolException(elem.getMatnr() + "条码冗余,请检查!"); | 
 |  |  |                     } | 
 |  |  |                     if (pack.getSettle() != 1) { | 
 |  |  |                         throw new CoolException(elem.getMatnr() + "条码已下线,请检查!"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 Mat analyse = MatUtils.analyseMat(elem.getMatnr()); | 
 |  |  |                 // 条码、物料代码、序列号、数量 | 
 |  |  |                 DetlDto detlDto = new DetlDto(elem.getMatnr(), analyse.getMatnr(), analyse.getBarcode(), elem.getAnfme()); | 
 |  |  | //                DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme()); | 
 |  |  |                 if (DetlDto.has(detlDtos, detlDto)) { | 
 |  |  |                     DetlDto one = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch()); | 
 |  |  |                     assert one != null; | 
 |  |  |                     one.setAnfme(one.getAnfme() + detlDto.getAnfme()); | 
 |  |  |                 } else { | 
 |  |  |                     detlDtos.add(detlDto); | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |  | 
 |  |  |             if (packDown) { | 
 |  |  |                 MesCombParam mesCombParam = new MesCombParam(); | 
 |  |  |                 mesCombParam.setZpallet(param.getBarcode()); | 
 |  |  |                 mesCombParam.setPakinTime(DateUtils.convert(now)); | 
 |  |  |                 mesCombParam.setLgort("5008"); | 
 |  |  |                 mesCombParam.setPlantCode("5000"); | 
 |  |  |                 mesCombParam.setFromCode("5012-20"); | 
 |  |  |                 mesCombParam.setStationCode("JJQ-PFZPDB-XX"); | 
 |  |  |                 for (DetlDto detlDto : detlDtos) { | 
 |  |  |                     mesCombParam.getList().add(new MesCombParam.Detl(detlDto.getOrderNo(), detlDto.getAnfme())); | 
 |  |  |                 } | 
 |  |  |                 String response = ""; | 
 |  |  |                 boolean success = false; | 
 |  |  |                 try { | 
 |  |  |                     response = new HttpHandler.Builder() | 
 |  |  |                             .setUri(MesConstant.URL) | 
 |  |  |                             .setPath(MesConstant.PACK_DOWN_URL) | 
 |  |  |                             .setJson(JSON.toJSONString(mesCombParam)) | 
 |  |  |                             .build() | 
 |  |  |                             .doPost(); | 
 |  |  |                     JSONObject jsonObject = JSON.parseObject(response); | 
 |  |  |                     if (jsonObject.getInteger("code").equals(200)) { | 
 |  |  |                         success = true; | 
 |  |  |                     } else if (jsonObject.getInteger("code").equals(500)) { | 
 |  |  |                         log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PACK_DOWN_URL, JSON.toJSONString(mesCombParam), response); | 
 |  |  |                         throw new CoolException(jsonObject.getString("msg")); | 
 |  |  |                     } else { | 
 |  |  |                         log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PACK_DOWN_URL, JSON.toJSONString(mesCombParam), response); | 
 |  |  |                         throw new CoolException("上报mes系统失败"); | 
 |  |  |                     } | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     log.error("fail", e); | 
 |  |  |                     throw new CoolException(e.getMessage()); | 
 |  |  |                 } finally { | 
 |  |  |                     try { | 
 |  |  |                         // 保存接口日志 | 
 |  |  |                         apiLogService.save( | 
 |  |  |                                 "打包下线帮托上报", | 
 |  |  |                                 MesConstant.URL + MesConstant.PACK_DOWN_URL, | 
 |  |  |                                 null, | 
 |  |  |                                 "127.0.0.1", | 
 |  |  |                                 JSON.toJSONString(mesCombParam), | 
 |  |  |                                 response, | 
 |  |  |                                 success | 
 |  |  |                         ); | 
 |  |  |                     } catch (Exception e) { log.error("", e); } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             // 生成入库单据 | 
 |  |  |             String orderNo = "PACK" + snowflakeIdWorker.nextId(); | 
 |  |  |             OpenOrderPakinParam openParam = new OpenOrderPakinParam(); | 
 |  |  |             openParam.setOrderNo(orderNo); | 
 |  |  |             openParam.setOrderTime(DateUtils.convert(now)); | 
 |  |  |             openParam.setOrderType("打包入库单"); | 
 |  |  |             openParam.setOrderDetails(detlDtos); | 
 |  |  |             openService.pakinOrderCreate(openParam); | 
 |  |  |             Order order = orderService.selectByNo(orderNo); | 
 |  |  |             if (null == order) { | 
 |  |  |                 throw new CoolException("生成单据失败"); | 
 |  |  |             } | 
 |  |  |             if (!orderService.updateSettle(order.getId(), 2L, userId)) { | 
 |  |  |                 throw new CoolException("修改单据状态失败"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             // 生成入库通知档 | 
 |  |  |             for (DetlDto detlDto : detlDtos) { | 
 |  |  |  | 
 |  |  |                 // 修改作业数量 ---------------------------------------- | 
 |  |  |                 // 订单明细数量校验 | 
 |  |  |                 OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), detlDto.getMatnr(), detlDto.getBatch()); | 
 |  |  |                 if (detlDto.getAnfme() > orderDetl.getEnableQty()) { | 
 |  |  |                     throw new CoolException(orderDetl.getMatnr() + "入库数量不合法"); | 
 |  |  |                 } | 
 |  |  |                 // 修改订单明细数量 | 
 |  |  |                 if (!orderDetlService.increase(order.getId(), detlDto.getMatnr(), detlDto.getBatch(), detlDto.getAnfme())) { | 
 |  |  |                     throw new CoolException("修改单据明细数量失败"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 // 保存入库通知档 | 
 |  |  |                 Mat mat = matService.selectByMatnr(detlDto.getMatnr()); | 
 |  |  |                 if (Cools.isEmpty(mat)) { | 
 |  |  |                     throw new CoolException(detlDto.getMatnr() + "商品档案不存在"); | 
 |  |  |                 } | 
 |  |  |                 WaitPakin waitPakin = new WaitPakin(); | 
 |  |  |                 waitPakin.sync(mat); | 
 |  |  |                 waitPakin.setOrderNo(orderNo); | 
 |  |  |                 waitPakin.setBatch(detlDto.getBatch()); | 
 |  |  |                 waitPakin.setZpallet(param.getBarcode());   // 托盘码 | 
 |  |  |                 waitPakin.setIoStatus("N");     // 入出状态 | 
 |  |  |                 waitPakin.setAnfme(detlDto.getAnfme());  // 数量 | 
 |  |  |                 waitPakin.setStatus("Y");    // 状态 | 
 |  |  |                 waitPakin.setAppeUser(userId); | 
 |  |  |                 waitPakin.setAppeTime(now); | 
 |  |  |                 waitPakin.setModiUser(userId); | 
 |  |  |                 waitPakin.setModiTime(now); | 
 |  |  |                 waitPakin.setMatType(mat.getMatType());//物料类型 | 
 |  |  |                 if (!waitPakinService.insert(waitPakin)) { | 
 |  |  |                     throw new CoolException("保存入库通知档失败"); | 
 |  |  |                 } | 
 |  |  |                 // 修改打包数据状态 | 
 |  |  |                 Pack pack = packService.selectByBarcode(detlDto.getOrderNo()); | 
 |  |  |                 pack.setSettle(2L); | 
 |  |  |                 pack.setUpdateTime(now); | 
 |  |  |                 pack.setUpdateBy(userId); | 
 |  |  |                 if (!packService.updateById(pack)) { | 
 |  |  |                     throw new CoolException("修改打包数据异常"); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void pikingToFull(String barcode) { | 
 |  |  |         wrkMastService.selectByBarcode(barcode); | 
 |  |  |     } | 
 |  |  | } |