| package com.zy.asrs.common.wms.service.impl; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.zy.asrs.common.domain.dto.DetlDto; | 
| import com.zy.asrs.common.domain.param.MobileAdjustParam; | 
| import com.zy.asrs.common.openapi.entity.dto.MergeBoardManyDto; | 
| import com.zy.asrs.common.openapi.entity.dto.PickMergeDto; | 
| import com.zy.asrs.common.openapi.entity.dto.PickMergeOrderDto; | 
| import com.zy.asrs.common.openapi.entity.param.CombParam; | 
| import com.zy.asrs.common.openapi.entity.param.WarehouseOutMergeBoardManyParam; | 
| import com.zy.asrs.common.openapi.entity.param.WarehouseOutPickMergeParam; | 
| import com.zy.asrs.common.web.BaseController; | 
| import com.zy.asrs.common.wms.entity.*; | 
| import com.zy.asrs.common.wms.mapper.LocDetlMapper; | 
| import com.zy.asrs.common.wms.service.*; | 
| import com.zy.asrs.framework.common.BaseRes; | 
| import com.zy.asrs.framework.common.Cools; | 
| import com.zy.asrs.framework.common.R; | 
| import com.zy.asrs.framework.exception.CoolException; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.ibatis.session.ExecutorType; | 
| import org.apache.ibatis.session.SqlSession; | 
| import org.apache.ibatis.session.SqlSessionFactory; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.math.BigDecimal; | 
| import java.util.*; | 
|   | 
| /** | 
|  * 移动端服务核心类 | 
|  */ | 
| @Slf4j | 
| @Service | 
| public class MobileServiceImpl implements MobileService { | 
|   | 
|     @Autowired | 
|     private WaitPakinService waitPakinService; | 
|     @Autowired | 
|     private MatService matService; | 
|     @Autowired | 
|     private OrderService orderService; | 
|     @Autowired | 
|     private OrderDetlService orderDetlService; | 
|     @Autowired | 
|     private BasDevpService basDevpService; | 
|     @Autowired | 
|     private WrkMastService wrkMastService; | 
|     @Autowired | 
|     private WrkDetlService wrkDetlService; | 
|     @Autowired | 
|     private LocMastService locMastService; | 
|     @Autowired | 
|     private LocDetlService locDetlService; | 
|     @Autowired | 
|     private WaitPakinLogService waitPakinLogService; | 
|     @Autowired | 
|     private AdjDetlService adjDetlService; | 
|   | 
|     @Override | 
|     @Transactional | 
|     public void comb(CombParam param, Long userId, Long hostId) { | 
|         if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) { | 
|             throw new CoolException(BaseRes.PARAM); | 
|         } | 
|         // 判断是否有相同条码的数据 | 
|         if (waitPakinService.count(new LambdaQueryWrapper<WaitPakin>(). | 
|                 eq(WaitPakin::getZpallet, param.getBarcode()).eq(WaitPakin::getIoStatus, "N")) > 0) { | 
|             throw new CoolException(param.getBarcode() + "数据正在进行入库"); | 
|         } | 
|         Date now = new Date(); | 
|   | 
|         // 无单组托 | 
|         if (Cools.isEmpty(param.getCombMats().get(0).getOrderNo())) { | 
|   | 
|             // 生成入库通知档 | 
|             List<DetlDto> detlDtos = new ArrayList<>(); | 
|             param.getCombMats().forEach(elem -> { | 
|                 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.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, detlDto.getMatnr()).eq(Mat::getHostId, hostId)); | 
|                 if (Cools.isEmpty(mat)) { | 
|                     throw new CoolException(detlDto.getMatnr() + "商品档案不存在"); | 
|                 } | 
|                 WaitPakin waitPakin = new WaitPakin(); | 
|                 waitPakin.sync(mat); | 
|                 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.setFullPlt(param.getFullPlt()); | 
|                 waitPakin.setOwner(param.getOwner()); | 
|                 if (!waitPakinService.save(waitPakin)) { | 
|                     throw new CoolException("保存入库通知档失败"); | 
|                 } | 
|             } | 
|             // 关联组托 | 
|         }  else { | 
|             // 生成入库通知档 | 
|             List<DetlDto> detlDtos = new ArrayList<>(); | 
|             param.getCombMats().forEach(elem -> { | 
|                 Order order = orderService.selectByNo(elem.getOrderNo(), hostId); | 
|                 if (order.getSettle() > 2) { | 
|                     throw new CoolException("单据编号已过期"); | 
|                 } | 
|                 // 订单明细数量校验 | 
|                 OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), elem.getMatnr(), elem.getBatch(),hostId); | 
|                 if (elem.getAnfme() > orderDetl.getEnableQty()) { | 
|                     throw new CoolException(orderDetl.getMatnr() + "入库数量不合法"); | 
|                 } | 
|                 // 修改订单明细数量 | 
|                 if (!orderDetlService.increaseWorkQtyByOrderNo(order.getOrderNo(), elem.getMatnr(), elem.getBatch(), elem.getAnfme(), hostId)) { | 
|                     throw new CoolException("修改单据明细数量失败"); | 
|                 } | 
|   | 
|                 DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme()); | 
|                 detlDto.setWarehouse(orderDetl.getThreeCode());//来源仓库 | 
|                 detlDto.setOrigin(orderDetl.getSupp());//来源单号 | 
|                 detlDto.setOriginNo(orderDetl.getSuppCode());//来源序号 | 
|                 if (DetlDto.has(detlDtos, detlDto)) { | 
|                     DetlDto one = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch()); | 
|                     assert one != null; | 
|                     one.setAnfme(one.getAnfme() + detlDto.getAnfme()); | 
|                 } else { | 
|                     detlDto.setOrderNo(order.getOrderNo()); | 
|                     detlDtos.add(detlDto); | 
|                 } | 
|             }); | 
|             for (DetlDto detlDto : detlDtos) { | 
|                 Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, detlDto.getMatnr()).eq(Mat::getHostId, hostId)); | 
|                 if (Cools.isEmpty(mat)) { | 
|                     throw new CoolException(detlDto.getMatnr() + "商品档案不存在"); | 
|                 } | 
|   | 
|                 Order order = orderService.selectByNo(detlDto.getOrderNo(), hostId); | 
|   | 
|                 WaitPakin waitPakin = new WaitPakin(); | 
|                 waitPakin.sync(mat); | 
|                 waitPakin.setOrderNo(order.getOrderNo());   // 单据编号 | 
|                 waitPakin.setBatch(detlDto.getBatch());     // 序列码 | 
|                 waitPakin.setZpallet(param.getBarcode());   // 托盘码 | 
|                 waitPakin.setIoStatus("N");     // 入出状态 | 
|                 waitPakin.setAnfme(detlDto.getAnfme());  // 数量 | 
|                 waitPakin.setThreeCode(detlDto.getWarehouse());  // 仓库 | 
|                 waitPakin.setSupp(detlDto.getOrigin());  // 来源单号 | 
|                 waitPakin.setSuppCode(detlDto.getOriginNo());  // 来源序号 | 
|                 waitPakin.setStatus("Y");    // 状态 | 
|                 waitPakin.setAppeUser(userId); | 
|                 waitPakin.setAppeTime(now); | 
|                 waitPakin.setModiUser(userId); | 
|                 waitPakin.setModiTime(now); | 
|                 waitPakin.setOwner(Optional.ofNullable(param.getOwner()).orElse("")); | 
|                 if (!waitPakinService.save(waitPakin)) { | 
|                     throw new CoolException("保存入库通知档失败"); | 
|                 } | 
|                 orderService.updateSettle(order.getId(), 2L, userId, hostId); | 
|             } | 
|   | 
|         } | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public void adjust(MobileAdjustParam param, Long userId, Long hostId) { | 
|         BasDevp basDevp = basDevpService.getOne(new LambdaQueryWrapper<BasDevp>().eq(BasDevp::getDevNo, param.getStaNo()).eq(BasDevp::getHostId, hostId)); | 
|         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.getOne(new LambdaQueryWrapper<WrkMast>().eq(WrkMast::getWrkNo, param.getWrkNo()).eq(WrkMast::getHostId, hostId)); | 
|         if (wrkMast.getWrkSts() < 10) { | 
|             throw new CoolException("盘点无效,任务已盘点再入库"); | 
|         } | 
|         Date now = new Date(); | 
|         List<WrkDetl> wrkDetls = wrkDetlService.list(new LambdaQueryWrapper<WrkDetl>().eq(WrkDetl::getWrkNo, wrkMast.getWrkNo()).eq(WrkDetl::getHostId, hostId)); | 
|   | 
|         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(), hostId)) { | 
|                             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(), hostId)) { | 
|                 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.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, wrkDetl.getMatnr()).eq(Mat::getHostId, hostId)); | 
|             wrkDetl.sync(mat); | 
|             wrkDetl.setOrderNo(orderNo); | 
|             wrkDetl.setModiTime(now); | 
|             wrkDetl.setModiUser(userId); | 
|             wrkDetl.setAppeTime(now); | 
|             wrkDetl.setAppeUser(userId); | 
|             if (!wrkDetlService.save(wrkDetl)) { | 
|                 throw new CoolException("添加" + wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "序列码任务明细失败"); | 
|             } | 
|         } | 
|   | 
|         // 修改盘点任务主档状态 | 
|         wrkMast.setFullPlt(wrkDetlService.count(new LambdaQueryWrapper<WrkDetl>().eq(WrkDetl::getWrkNo, wrkMast.getWrkNo()).eq(WrkDetl::getHostId, hostId)) != 0 ? "Y" : "N"); | 
|         wrkMast.setModiTime(now); | 
|         wrkMast.setModiUser(String.valueOf(userId)); | 
|         if (!wrkMastService.updateById(wrkMast)) { | 
|             throw new CoolException("修改盘点任务主档失败"); | 
|         } | 
|   | 
|     } | 
|   | 
|     @Override | 
|     @Transactional | 
|     public R WarehouseIn(String locNo, String barcode,Long hostId,Long userId) { | 
|         //判断库位状态 | 
|         LocMast locMast = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, locNo).eq(LocMast::getHostId,hostId).eq(LocMast::getWhsType,1L)); | 
|         if (Cools.isEmpty(locMast)){ | 
|             return R.error("该库位不存在"); | 
|         } | 
| //        if (!locMast.getLocSts().equals("O")){ | 
| //            return R.error("该库位状态为:"+locMast.getLocSts()+",禁止入库"); | 
| //        } | 
|         //查询组托数据 | 
|         List<WaitPakin> list = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getZpallet, barcode).eq(WaitPakin::getHostId, hostId)); | 
|         if (Cools.isEmpty(list)){ | 
|             return R.error("未找到组托信息"); | 
|         } | 
|         Date now = new Date(); | 
|   | 
|         //插入库存数据 | 
|         for (WaitPakin waitPakin: list){ | 
|             //处理库存信息 | 
|             AdjDetl adjDetl = new AdjDetl(); | 
|             Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>() | 
|                     .eq(Mat::getMatnr, waitPakin.getMatnr()).eq(Mat::getHostId, hostId)); | 
|             LocDetl locDetl1=locDetlService.getOne(new LambdaQueryWrapper<LocDetl>() | 
|                     .eq(LocDetl::getMatnr,waitPakin.getMatnr()) | 
|                     .eq(LocDetl::getHostId,hostId) | 
|                     .eq(LocDetl::getLocNo,locNo)); | 
|             double i=0.0; | 
|             if(!Cools.isEmpty(locDetl1)){ | 
|                 i=locDetl1.getAnfme(); | 
|                 locDetl1.setAnfme(locDetl1.getAnfme()+waitPakin.getAnfme()); | 
|                 locDetlService.updateById(locDetl1); | 
|                 adjDetl.setMatnr(locDetl1.getMatnr()); | 
|                 adjDetl.setBatch(locDetl1.getBatch()); | 
|                 adjDetl.setAdjQty(locDetl1.getAnfme()); | 
|             }else{ | 
|                 LocDetl locDetl = new LocDetl(); | 
|                 locDetl.sync(mat); | 
|                 locDetl.setLocNo(locNo); | 
| //            locDetl.setBarcode(waitPakin.getBarcode()); | 
|                 locDetl.setZpallet(waitPakin.getZpallet()); | 
|                 locDetl.setAnfme(waitPakin.getAnfme()); | 
|                 locDetl.setBatch(waitPakin.getBatch()); | 
| //            locDetl.setWeight(waitPakin.getWeight()); | 
|                 locDetl.setHostId(hostId); | 
|                 locDetl.setModiTime(now); | 
|                 locDetl.setAppeTime(now); | 
|                 locDetlService.save(locDetl); | 
|                 adjDetl.setMatnr(locDetl.getMatnr()); | 
|                 adjDetl.setBatch(locDetl.getBatch()); | 
|                 adjDetl.setAdjQty(locDetl.getAnfme()); | 
|             } | 
|   | 
|             try { | 
|   | 
|                 adjDetl.setLocNo(locNo); | 
|                 adjDetl.setOriQty(i); | 
|                 adjDetl.setMemo("平庫上架"); | 
|                 adjDetl.setModiUser(userId); | 
|                 adjDetl.setAppeUser(userId); | 
|                 adjDetl.setAppeTime(new Date()); | 
|                 adjDetl.setModiTime(new Date()); | 
|                 adjDetl.setOrderNo(waitPakin.getOrderNo()); | 
|                 if (!adjDetlService.save(adjDetl)) { | 
|                     throw new CoolException("保存库存调整记录失败"); | 
|                 } | 
|             }catch (Exception e){ | 
|                 e.printStackTrace(); | 
|                 throw new CoolException("更新库存数据出错" + e.getMessage()); | 
|             } | 
|   | 
|             //是否属于订单数据 | 
|             if (!Cools.isEmpty(waitPakin.getOrderNo())){ | 
|                 Order order = orderService.selectByNo(waitPakin.getOrderNo(), hostId); | 
|                 if (Cools.isEmpty(order)){ | 
|                     throw new CoolException("平库上架查询订单失败"); | 
|                 } | 
|                 OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), waitPakin.getMatnr(), waitPakin.getBatch(),hostId); | 
|                 if (Cools.isEmpty(orderDetl)){ | 
|                     throw new CoolException("平库上架查询订单明细失败"); | 
|                 } | 
|                 // 修改订单明细完成数量 | 
|                 if (!orderDetlService.increase(order.getId(), hostId, waitPakin.getMatnr(), waitPakin.getBatch(), waitPakin.getAnfme())) { | 
|                     throw new CoolException("修改单据明细数量失败"); | 
|                 } | 
|                 // 修改订单状态 作业中 ===>> 已完成 | 
|                 orderService.checkComplete(waitPakin.getOrderNo(), hostId); | 
|   | 
|             } | 
|         } | 
|         //修改库位状态 | 
|         locMast.setLocSts("F"); | 
|         locMast.setModiTime(now); | 
|         locMastService.updateById(locMast); | 
|   | 
|   | 
|   | 
|         // 保存入库通知档历史档 | 
|         if (!waitPakinLogService.saveToHistory(barcode, hostId)) { | 
|             throw new CoolException("保存组托数据失败"); | 
|         } | 
|         // 删除入库通知档 | 
|         if (!waitPakinService.remove(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getZpallet, barcode).eq(WaitPakin::getHostId, hostId))) { | 
|             throw new CoolException("删除组托数据失败"); | 
|         } | 
|         return R.ok("上架成功"); | 
|     } | 
|   | 
|     @Override | 
|     @Transactional | 
|     public R WarehouseOut(CombParam combParam, Long hostId , Long userId) { | 
|         //判断库位状态 | 
|         LocMast locMast = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, combParam.getLocno()).eq(LocMast::getHostId,hostId).eq(LocMast::getWhsType,1L)); | 
|         if (Cools.isEmpty(locMast)){ | 
|             return R.error("该库位不存在"); | 
|         } | 
|         if (!locMast.getLocSts().equals("F")){ | 
|             return R.error("该库位状态为:"+locMast.getLocSts()+",禁止出库"); | 
|         } | 
|         //查询库存数据 | 
|         for (CombParam.CombMat combMat :combParam.getCombMats()){ | 
|             LocDetl locDetl = locDetlService.getOne(new LambdaQueryWrapper<LocDetl>() | 
|                     .eq(LocDetl::getLocNo, combParam.getLocno()) | 
|                     .eq(LocDetl::getHostId, hostId) | 
|                     .eq(LocDetl::getZpallet, combMat.getZpallet()) | 
|                     .eq(LocDetl::getMatnr, combMat.getMatnr()) | 
| //                    .eq(LocDetl::getBatch, combMat.getBatch()) | 
|             ); | 
|             if (Cools.isEmpty(locDetl)){ | 
|                 return R.error("未查询到库存数据"); | 
|             } | 
|             if (combMat.getAnfme() > locDetl.getAnfme()){ | 
|                 return R.error("下架数量错误,超出库存数量"); | 
|             } | 
|             if (!Cools.isEmpty(combParam.getOrderNo())){ | 
|                 Order order = orderService.selectByNo(combParam.getOrderNo(), hostId); | 
|                 if (Cools.isEmpty(order)){ | 
|                     continue; | 
|                 } | 
|                 if (order.getSettle() == 1){ | 
|                     orderService.updateSettle(order.getId(), 2L, 9527L, hostId); | 
|                 } | 
|                 OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), combMat.getMatnr(), combMat.getBatch(),hostId); | 
|                 if (Cools.isEmpty(orderDetl)){ | 
|                     continue; | 
|                 } | 
|                 if (orderDetl.getAnfme() < orderDetl.getWorkQty()+combMat.getAnfme()){ | 
|                     combMat.setAnfme(orderDetl.getAnfme()-orderDetl.getWorkQty()); | 
|                 } | 
|                 // 修改订单明细作业数量 | 
|                 if (!orderDetlService.increaseWorkQtyByOrderNo(combParam.getOrderNo(), combMat.getMatnr(), combMat.getBatch(), combMat.getAnfme(), hostId)) { | 
|                     throw new CoolException("修改单据明细工作数量失败"); | 
|                 } | 
|                 // 修改订单明细完成数量 | 
|                 if (!orderDetlService.increase(order.getId(), hostId, combMat.getMatnr(), combMat.getBatch(), combMat.getAnfme())) { | 
|                     throw new CoolException("修改单据明细完成数量失败"); | 
|                 } | 
|                 // 修改订单状态 作业中 ===>> 已完成 | 
|                 orderService.checkComplete(combParam.getOrderNo(), hostId); | 
|             } | 
|             try { | 
|                 double i=0.0; | 
|                 double j=locDetl.getAnfme(); | 
|                 //处理库存信息 | 
|                 if (combMat.getAnfme().equals(locDetl.getAnfme())){ | 
|                     //库存相等删除数据 | 
|                     locDetlService.removeById(locDetl); | 
|                     int count = locDetlService.count(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocNo,combParam.getLocno()).eq(LocDetl::getHostId, hostId)); | 
|                     if (count == 0){ | 
|                         LocMast locMast1 = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, combParam.getLocno()).eq(LocMast::getHostId, hostId)); | 
|                         locMast1.setLocSts("O"); | 
|                         locMastService.updateById(locMast1); | 
|                     } | 
|                 }else { | 
|                     BigDecimal subtract = BigDecimal.valueOf(locDetl.getAnfme()).subtract(BigDecimal.valueOf(combMat.getAnfme())); | 
|                     i=subtract.byteValue(); | 
|                     locDetl.setAnfme(subtract.doubleValue()); | 
|                     locDetlService.updateById(locDetl); | 
|                 } | 
|   | 
|                 try { | 
|                     //处理库存信息 | 
|                     AdjDetl adjDetl = new AdjDetl(); | 
|                     adjDetl.setLocNo(locDetl.getLocNo()); | 
|                     adjDetl.setMatnr(locDetl.getMatnr()); | 
|                     adjDetl.setBatch(locDetl.getBatch()); | 
|                     adjDetl.setOriQty(j); | 
|                     adjDetl.setAdjQty(i); | 
|                     adjDetl.setMemo("平庫下架"); | 
|                     adjDetl.setModiUser(userId); | 
|                     adjDetl.setAppeUser(userId); | 
|                     adjDetl.setAppeTime(new Date()); | 
|                     adjDetl.setModiTime(new Date()); | 
|                     adjDetl.setOrderNo(combParam.getOrderNo()); | 
|                     adjDetl.setHostId(hostId); | 
|                     if (!adjDetlService.save(adjDetl)) { | 
|                         throw new CoolException("保存库存调整记录失败"); | 
|                     } | 
|                 }catch (Exception e){ | 
|                     e.printStackTrace(); | 
|                     throw new CoolException("更新库存数据出错" + e.getMessage()); | 
|                 } | 
|             }catch (Exception e){ | 
|                 throw new CoolException("更新库存数据出错"+e.getMessage()); | 
|             } | 
|         } | 
|   | 
|   | 
|   | 
|   | 
|   | 
|         return R.ok(); | 
|     } | 
|   | 
|     @Override | 
|     @Transactional | 
|     public R WarehouseOutV1(CombParam combParam, Long hostId,Long userId) { | 
|         //判断库位状态 | 
|         LocMast locMast = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, combParam.getLocno()).eq(LocMast::getHostId,hostId).eq(LocMast::getWhsType,0L)); | 
|         if (Cools.isEmpty(locMast)){ | 
|             return R.error("该库位不存在"); | 
|         } | 
|         if (!locMast.getLocSts().equals("P")){ | 
|             return R.error("该库位状态为:"+locMast.getLocSts()+",不满足并板途中拣选条件!!!"); | 
|         } | 
|         //查询库存数据 | 
|         for (CombParam.CombMat combMat :combParam.getCombMats()){ | 
|             List<LocDetl> locDetlList = locDetlService.list(new LambdaQueryWrapper<LocDetl>() | 
|                     .eq(LocDetl::getLocNo, combParam.getLocno()) | 
|                     .eq(LocDetl::getHostId, hostId) | 
| //                    .eq(LocDetl::getZpallet, combMat.getZpallet()) | 
|                     .eq(LocDetl::getMatnr, combMat.getMatnr()) | 
| //                    .eq(LocDetl::getBatch, combMat.getBatch()) | 
|             ); | 
|             if (locDetlList.isEmpty()){ | 
|                 return R.error("未查询到库存数据"); | 
|             } | 
|             if (locDetlList.size()>1){ | 
|                 return R.error("库存数据大于1条,为保证数据准确性,请用正常流程出库!!!"); | 
|             } | 
|             LocDetl locDetl = locDetlList.get(0); | 
|             if (combMat.getAnfme() > locDetl.getAnfme()){ | 
|                 return R.error("拣选数量错误,超出库存数量"); | 
|             } | 
|             if (!Cools.isEmpty(combMat.getOrderNo())){ | 
|                 Order order = orderService.selectByNo(combMat.getOrderNo(), hostId); | 
|                 if (Cools.isEmpty(order)){ | 
|                     continue; | 
|                 } | 
|                 if (order.getSettle() == 1){ | 
|                     orderService.updateSettle(order.getId(), 2L, 9527L, hostId); | 
|                 } | 
|                 OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), combMat.getMatnr(), combMat.getBatch(),hostId); | 
|                 if (Cools.isEmpty(orderDetl)){ | 
|                     continue; | 
|                 } | 
|                 if (orderDetl.getAnfme() < (orderDetl.getWorkQty()+combMat.getAnfme())){ | 
|                     throw new CoolException("拣选数量出错,订单剩余出库数量小于已出库数量与将拣选数量之和!!!"); | 
|                 } | 
|                 // 修改订单明细作业数量 | 
|                 if (!orderDetlService.increaseWorkQtyByOrderNo(combMat.getOrderNo(), combMat.getMatnr(), combMat.getBatch(), combMat.getAnfme(), hostId)) { | 
|                     throw new CoolException("修改单据明细工作数量失败"); | 
|                 } | 
|                 // 修改订单明细完成数量 | 
|                 if (!orderDetlService.increase(order.getId(), hostId, combMat.getMatnr(), combMat.getBatch(), combMat.getAnfme())) { | 
|                     throw new CoolException("修改单据明细完成数量失败"); | 
|                 } | 
|                 // 修改订单状态 作业中 ===>> 已完成 | 
|                 orderService.checkComplete(combMat.getOrderNo(), hostId); | 
|             } | 
|             try { | 
|                 //处理库存信息 | 
|                 AdjDetl adjDetl = new AdjDetl(); | 
|                 adjDetl.setLocNo(locDetl.getLocNo()); | 
|                 adjDetl.setMatnr(locDetl.getMatnr()); | 
|                 adjDetl.setBatch(locDetl.getBatch()); | 
|                 adjDetl.setOriQty(locDetl.getAnfme()); | 
|                 adjDetl.setMemo("并板途中拣料"); | 
|                 adjDetl.setModiUser(userId); | 
|                 adjDetl.setAppeUser(userId); | 
|                 adjDetl.setAppeTime(new Date()); | 
|                 adjDetl.setModiTime(new Date()); | 
|                 adjDetl.setOrderNo(combMat.getOrderNo()); | 
|                 if (combMat.getAnfme().equals(locDetl.getAnfme())){ | 
|                     adjDetl.setAdjQty(0D); | 
|                     //库存相等删除数据 | 
|                     locDetlService.removeById(locDetl); | 
|                     int count = locDetlService.count(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocNo,combParam.getLocno()).eq(LocDetl::getHostId, hostId)); | 
|                     if (count == 0){ | 
|                         LocMast locMast1 = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, combParam.getLocno()).eq(LocMast::getHostId, hostId)); | 
| //                        locMast1.setLocSts("O"); | 
| //                        locMastService.updateById(locMast1); | 
|                     } | 
|                 } else { | 
|                     BigDecimal subtract = BigDecimal.valueOf(locDetl.getAnfme()).subtract(BigDecimal.valueOf(combMat.getAnfme())); | 
|                     locDetl.setAnfme(subtract.doubleValue()); | 
|                     locDetlService.updateById(locDetl); | 
|                     adjDetl.setAdjQty(subtract.doubleValue()); | 
|                 } | 
|   | 
|                 if (!adjDetlService.save(adjDetl)) { | 
|                     throw new CoolException("保存库存调整记录失败"); | 
|                 } | 
|             }catch (Exception e){ | 
|                 throw new CoolException("更新库存数据出错"+e.getMessage()); | 
|             } | 
|         } | 
|         return R.ok(); | 
|     } | 
|   | 
|     @Override | 
|     @Transactional | 
|     public R WarehouseOutPickMergeV1(WarehouseOutPickMergeParam param, Long hostId,Long userId) { | 
|         //判断库位状态 | 
|         LocMast locMast = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, param.getLocNo()).eq(LocMast::getHostId,hostId).eq(LocMast::getWhsType,0L)); | 
|         if (Cools.isEmpty(locMast)){ | 
|             return R.error("该库位不存在"); | 
|         } | 
|         if (!locMast.getLocSts().equals("P")){ | 
|             return R.error("该库位状态为:"+locMast.getLocSts()+",不满足拣料途中并板条件!!!"); | 
|         } | 
|   | 
|         HashMap<String, OrderDetl> mapData = new HashMap<>(); | 
|         for (OrderDetl orderDetl : param.getCombMats()) { | 
|             String key = orderDetl.getMatnr(); | 
|             if (!Cools.isEmpty(orderDetl.getBatch())) { | 
|                 key += "_" + orderDetl.getBatch(); | 
|             } | 
|   | 
|             OrderDetl value = mapData.get(key); | 
|             if(value == null){ | 
|                 mapData.put(key, orderDetl); | 
|             }else { | 
|                 value.setAnfme(value.getAnfme() + orderDetl.getAnfme()); | 
|                 mapData.put(key, value); | 
|             } | 
|         } | 
|   | 
|         List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocNo, locMast.getLocNo())); | 
|   | 
|         HashMap<String, PickMergeDto> locData = new HashMap<>(); | 
|         for (LocDetl locDetl : locDetls) { | 
|             String matKey = locDetl.getMatnr(); | 
|             if (!Cools.isEmpty(locDetl.getBatch())) { | 
|                 matKey += "_" + locDetl.getBatch(); | 
|             } | 
|   | 
|             PickMergeDto dto = new PickMergeDto(); | 
|             dto.setOriginAnfme(locDetl.getAnfme()); | 
|             dto.setLocDetl(locDetl); | 
|             dto.setOrders(new ArrayList<>()); | 
|             locData.put(matKey, dto); | 
|         } | 
|   | 
|         for (Map.Entry<String, OrderDetl> entry : mapData.entrySet()) { | 
|             OrderDetl orderDetl = entry.getValue(); | 
|             LocDetl locDetl = new LocDetl(); | 
|             locDetl.sync(orderDetl); | 
|             locDetl.setId(null); | 
|             locDetl.setLocNo(param.getLocNo()); | 
|             locDetl.setBarcode(locMast.getBarcode()); | 
|             Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr,locDetl.getMatnr())); | 
|             locDetl.setHostId(hostId); | 
|             locDetl.setMaktx(mat.getMaktx()); | 
|             if(hostId==1){ | 
|                 locDetl.setOwner("宁波仓"); | 
|             }else{ | 
|                 locDetl.setOwner("新昌仓"); | 
|             } | 
|   | 
|             String matKey = locDetl.getMatnr(); | 
|             if (!Cools.isEmpty(locDetl.getBatch())) { | 
|                 matKey += "_" + locDetl.getBatch(); | 
|             } | 
|   | 
|             PickMergeDto dto = locData.get(matKey); | 
|             if (dto == null) { | 
|                 ArrayList<PickMergeOrderDto> orders = new ArrayList<>(); | 
|                 orders.add(new PickMergeOrderDto(locDetl.getOrderNo(), locDetl.getAnfme())); | 
|   | 
|                 dto = new PickMergeDto(); | 
|                 dto.setOriginAnfme(locDetl.getAnfme()); | 
|                 dto.setLocDetl(locDetl); | 
|                 dto.setOrders(orders); | 
|   | 
|                 locData.put(matKey, dto); | 
|             }else { | 
|                 LocDetl detl = dto.getLocDetl(); | 
|                 List<PickMergeOrderDto> orders = dto.getOrders(); | 
|   | 
|                 detl.setAnfme(detl.getAnfme() + locDetl.getAnfme()); | 
|                 orders.add(new PickMergeOrderDto(locDetl.getOrderNo(), locDetl.getAnfme())); | 
|   | 
|                 dto.setLocDetl(detl); | 
|                 dto.setOrders(orders); | 
|   | 
|                 locData.put(matKey, dto); | 
|             } | 
|         } | 
|   | 
|   | 
|         for (Map.Entry<String, PickMergeDto> entry : locData.entrySet()) { | 
|             PickMergeDto dto = entry.getValue(); | 
|   | 
|             Double originAnfme = dto.getOriginAnfme(); | 
|             LocDetl locDetl = dto.getLocDetl(); | 
|             List<PickMergeOrderDto> orders = dto.getOrders(); | 
|   | 
|             if (locDetl.getId() == null) { | 
|                 if (!locDetlService.save(locDetl)) { | 
|                     throw new CoolException("并板库存插入失败"); | 
|                 } | 
|             }else { | 
|                 if (!locDetlService.updateById(locDetl)) { | 
|                     throw new CoolException("并板库存更新失败"); | 
|                 } | 
|             } | 
|   | 
|             if (!orders.isEmpty()){ | 
|                 for (PickMergeOrderDto orderDto : orders) { | 
|                     Order order = orderService.selectByNo(orderDto.getOrderNo(), hostId); | 
|                     if (Cools.isEmpty(order)){ | 
|                         continue; | 
|                     } | 
|                     if (order.getSettle() == 1){ | 
|                         orderService.updateSettle(order.getId(), 2L, 9527L, hostId); | 
|                     } | 
|                     OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), locDetl.getMatnr(), locDetl.getBatch(),hostId); | 
|                     if (Cools.isEmpty(orderDetl)){ | 
|                         continue; | 
|                     } | 
|                     if (orderDetl.getAnfme() < (orderDetl.getWorkQty() + orderDto.getAnfme())) { | 
|                         throw new CoolException("并板数量出错,订单剩余出库数量小于已出库数量与将并板数量之和!!!"); | 
|                     } | 
|                     // 修改订单明细作业数量 | 
|                     if (!orderDetlService.increaseWorkQtyByOrderNo(orderDto.getOrderNo(), locDetl.getMatnr(), locDetl.getBatch(), orderDto.getAnfme(), hostId)) { | 
|                         throw new CoolException("修改单据明细工作数量失败"); | 
|                     } | 
|                     // 修改订单明细完成数量 | 
|                     if (!orderDetlService.increase(order.getId(), hostId, locDetl.getMatnr(), locDetl.getBatch(), orderDto.getAnfme())) { | 
|                         throw new CoolException("修改单据明细完成数量失败"); | 
|                     } | 
|                     // 修改订单状态 作业中 ===>> 已完成 | 
|                     orderService.checkComplete(orderDto.getOrderNo(), hostId); | 
|   | 
|                     try { | 
|                         //处理库存信息 | 
|                         AdjDetl adjDetl = new AdjDetl(); | 
|                         adjDetl.setLocNo(locDetl.getLocNo()); | 
|                         adjDetl.setMatnr(locDetl.getMatnr()); | 
|                         adjDetl.setBatch(locDetl.getBatch()); | 
|                         adjDetl.setOriQty(originAnfme); | 
|                         adjDetl.setAdjQty(locDetl.getAnfme()); | 
|                         adjDetl.setMemo("拣料途中并板"); | 
|                         adjDetl.setModiUser(userId); | 
|                         adjDetl.setAppeUser(userId); | 
|                         adjDetl.setAppeTime(new Date()); | 
|                         adjDetl.setModiTime(new Date()); | 
|                         adjDetl.setOrderNo(order.getOrderNo()); | 
|                         if (!adjDetlService.save(adjDetl)) { | 
|                             throw new CoolException("保存库存调整记录失败"); | 
|                         } | 
|                     }catch (Exception e){ | 
|                         e.printStackTrace(); | 
|                         throw new CoolException("更新库存数据出错" + e.getMessage()); | 
|                     } | 
|                 } | 
|             } | 
|   | 
|   | 
|         } | 
|         return R.ok(); | 
|     } | 
|   | 
|     @Override | 
|     @Transactional | 
|     public R WarehouseOutMergeBoardMany(WarehouseOutMergeBoardManyParam param, Long hostId,Long userId) { | 
|         WrkMast wrkMast = wrkMastService.selectByBarcode(param.getBarcode(), hostId); | 
|         if (wrkMast == null) { | 
|             throw new CoolException("工作档不存在"); | 
|         } | 
|   | 
|         if (wrkMast.getIoType() != 104) { | 
|             throw new CoolException("工作类型不属于并板出库"); | 
|         } | 
|   | 
|         if (wrkMast.getWrkSts() != 199) { | 
|             throw new CoolException("工作状态不满足多次并板入库状态"); | 
|         } | 
|   | 
|         List<WrkDetl> wrkDetls = wrkDetlService.list(new LambdaQueryWrapper<WrkDetl>().eq(WrkDetl::getWrkNo, wrkMast.getWrkNo())); | 
|   | 
|         HashMap<String, MergeBoardManyDto> wrkData = new HashMap<>(); | 
|         for (WrkDetl wrkDetl : wrkDetls) { | 
|             String matKey = wrkDetl.getMatnr(); | 
|             if (!Cools.isEmpty(wrkDetl.getBatch())) { | 
|                 matKey += "_" + wrkDetl.getBatch() + "-" +wrkDetl.getOrderNo(); | 
|             } | 
|   | 
|             MergeBoardManyDto dto = new MergeBoardManyDto(); | 
|             dto.setOriginAnfme(wrkDetl.getAnfme()); | 
|             dto.setWrkDetl(wrkDetl); | 
|             dto.setOrders(new ArrayList<>()); | 
|             wrkData.put(matKey, dto); | 
|         } | 
|   | 
|         for (OrderDetl orderDetl : param.getCombMats()) { | 
|             String matKey = orderDetl.getMatnr(); | 
|             if (!Cools.isEmpty(orderDetl.getBatch())) { | 
|                 matKey += "_" + orderDetl.getBatch() + "-" +orderDetl.getOrderNo(); | 
|             } | 
|   | 
|             MergeBoardManyDto dto = wrkData.get(matKey); | 
|             if(dto == null){ | 
|                 WrkDetl wrkDetl = new WrkDetl(); | 
|                 wrkDetl.sync(orderDetl); | 
|                 Mat mat=matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr,wrkDetl.getMatnr())); | 
|                 wrkDetl.sync(mat); | 
|                 wrkDetl.setId(null); | 
|                 wrkDetl.setWrkNo(wrkMast.getWrkNo()); | 
|                 wrkDetl.setWrkMastId(wrkMast.getId()); | 
|                 wrkDetl.setZpallet(wrkMast.getBarcode()); | 
|                 wrkDetl.setIoTime(new Date()); | 
|                 wrkDetl.setHostId(hostId); | 
|                 wrkDetl.setAppeUser(userId); | 
|                 wrkDetl.setAppeTime(new Date()); | 
|                 wrkDetl.setModiUser(userId); | 
|                 wrkDetl.setModiTime(new Date()); | 
|                 ArrayList<PickMergeOrderDto> orderDtos = new ArrayList<>(); | 
|                 orderDtos.add(new PickMergeOrderDto(orderDetl.getOrderNo(), orderDetl.getAnfme())); | 
|   | 
|                 dto = new MergeBoardManyDto(); | 
|                 dto.setOriginAnfme(orderDetl.getAnfme()); | 
|                 dto.setWrkDetl(wrkDetl); | 
|                 dto.setOrders(orderDtos); | 
|             }else { | 
|                 WrkDetl wrkDetl = dto.getWrkDetl(); | 
|                 List<PickMergeOrderDto> orders = dto.getOrders(); | 
|   | 
|                 wrkDetl.setAnfme(wrkDetl.getAnfme() + orderDetl.getAnfme()); | 
|                 wrkDetl.setIoTime(new Date()); | 
|                 wrkDetl.setHostId(hostId); | 
|                 wrkDetl.setModiUser(userId); | 
|                 wrkDetl.setModiTime(new Date()); | 
|                 orders.add(new PickMergeOrderDto(orderDetl.getOrderNo(), orderDetl.getAnfme())); | 
|                 dto.setWrkDetl(wrkDetl); | 
|                 dto.setOrders(orders); | 
|             } | 
|   | 
|             wrkData.put(matKey, dto); | 
|         } | 
|   | 
|         for (Map.Entry<String, MergeBoardManyDto> entry : wrkData.entrySet()) { | 
|             MergeBoardManyDto dto = entry.getValue(); | 
|             List<PickMergeOrderDto> orders = dto.getOrders(); | 
|             WrkDetl wrkDetl = dto.getWrkDetl(); | 
|   | 
|             if (wrkDetl.getId() == null) { | 
|                 if (!wrkDetlService.save(wrkDetl)) { | 
|                     throw new CoolException("多次并板插入工作明细失败"); | 
|                 } | 
|             }else { | 
|                 if (!wrkDetlService.updateById(wrkDetl)) { | 
|                     throw new CoolException("多次并板更新工作明细失败"); | 
|                 } | 
|             } | 
|   | 
|             if (!orders.isEmpty()){ | 
|                 for (PickMergeOrderDto orderDto : orders) { | 
|                     Order order = orderService.selectByNo(orderDto.getOrderNo(), hostId); | 
|                     if (Cools.isEmpty(order)){ | 
|                         continue; | 
|                     } | 
|                     if (order.getSettle() == 1){ | 
|                         orderService.updateSettle(order.getId(), 2L, 9527L, hostId); | 
|                     } | 
|                     OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), wrkDetl.getMatnr(), wrkDetl.getBatch(),hostId); | 
|                     if (Cools.isEmpty(orderDetl)){ | 
|                         continue; | 
|                     } | 
|                     if (orderDetl.getAnfme() < (orderDetl.getWorkQty() + orderDto.getAnfme())) { | 
|                         throw new CoolException("拣选数量出错,订单剩余出库数量小于已出库数量与将拣选数量之和!!!"); | 
|                     } | 
|                     // 修改订单明细作业数量 | 
|                     if (!orderDetlService.increaseWorkQtyByOrderNo(orderDto.getOrderNo(), wrkDetl.getMatnr(), wrkDetl.getBatch(), orderDto.getAnfme(), hostId)) { | 
|                         throw new CoolException("修改单据明细工作数量失败"); | 
|                     } | 
|                     // 修改订单明细完成数量 | 
|                     if (!orderDetlService.increase(order.getId(), hostId, wrkDetl.getMatnr(), wrkDetl.getBatch(), orderDto.getAnfme())) { | 
|                         throw new CoolException("修改单据明细完成数量失败"); | 
|                     } | 
|                     // 修改订单状态 作业中 ===>> 已完成 | 
|                     orderService.checkComplete(orderDto.getOrderNo(), hostId); | 
|   | 
|   | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return R.ok(); | 
|     } | 
|   | 
| } |