|  |  |  | 
|---|
|  |  |  | package com.vincent.rsf.server.api.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
|---|
|  |  |  | import com.vincent.rsf.framework.common.Cools; | 
|---|
|  |  |  | import com.vincent.rsf.framework.common.R; | 
|---|
|  |  |  | import com.vincent.rsf.framework.exception.CoolException; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.vincent.rsf.server.api.service.PdaCheckOrderService; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.CheckDiffExceStatus; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.OrderType; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.OrderWorkType; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.TaskStsType; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.service.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.constant.DictTypeCode; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.entity.DictData; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.service.DictDataService; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.service.DictTypeService; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  | 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.Map; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class PdaCheckOrderServiceImpl implements PdaCheckOrderService { | 
|---|
|  |  |  | 
|---|
|  |  |  | private DictTypeService dictTypeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DictDataService dictDataService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MatnrService matnrService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public R getCheckList(String code) { | 
|---|
|  |  |  | LambdaQueryWrapper<CheckDiff> lambdaQueryWrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | lambdaQueryWrapper.eq(!Cools.isEmpty(code),CheckDiff::getOrderCode,code); | 
|---|
|  |  |  | lambdaQueryWrapper.eq(!Cools.isEmpty(code), CheckDiff::getOrderCode, code); | 
|---|
|  |  |  | List<CheckDiff> waveList = checkDiffService.list(lambdaQueryWrapper); | 
|---|
|  |  |  | return R.ok(waveList); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public R getCheckTaskItemList(String barcode,String checkCode) { | 
|---|
|  |  |  | public R getCheckTaskItemList(String barcode, String checkCode) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<CheckDiffItem> checkDiffItems = checkDiffItemService.list(new LambdaQueryWrapper<CheckDiffItem>() | 
|---|
|  |  |  | .eq(CheckDiffItem::getBarcode, barcode) | 
|---|
|  |  |  | .eq(CheckDiffItem::getExceStatus, CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_INIT.val) | 
|---|
|  |  |  | .eq(CheckDiffItem::getOrderCode, checkCode) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | if (checkDiffItems.isEmpty()){ | 
|---|
|  |  |  | if (checkDiffItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("此条码不在当前盘点单中"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | lambdaQueryWrapper.eq(Task::getBarcode, barcode); | 
|---|
|  |  |  | Task task = taskService.getOne(lambdaQueryWrapper); | 
|---|
|  |  |  | if (null == task){ | 
|---|
|  |  |  | if (null == task) { | 
|---|
|  |  |  | throw new CoolException("未找到容器号对应任务"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!task.getTaskStatus().equals(TaskStsType.AWAIT.id)){ | 
|---|
|  |  |  | if (!task.getTaskStatus().equals(TaskStsType.AWAIT.id)) { | 
|---|
|  |  |  | return R.error("任务状态不是等待确认"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok(checkDiffItems); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public R saveCheckDiff(SaveCheckDiffParams params,Long loginUserId) { | 
|---|
|  |  |  | public R saveCheckDiff(SaveCheckDiffParams params, Long loginUserId) { | 
|---|
|  |  |  | CheckDiff checkDiff = checkDiffService.getById(params.getCheckId()); | 
|---|
|  |  |  | if (null == checkDiff){ | 
|---|
|  |  |  | if (null == checkDiff) { | 
|---|
|  |  |  | return R.error("未找到盘点差异单"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (checkDiff.getExceStatus().equals(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_END.val)){ | 
|---|
|  |  |  | if (checkDiff.getExceStatus().equals(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_END.val)) { | 
|---|
|  |  |  | return R.error("该盘点单已完成"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (CheckDiffItem checkDiffItem: params.getCheckDiffItems()){ | 
|---|
|  |  |  | CheckDiffItem diffItem = checkDiffItemService.getById(checkDiffItem.getId()); | 
|---|
|  |  |  | if (null == diffItem){ | 
|---|
|  |  |  | return R.error("数据错误,未找到差异单明细"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | checkDiffItem.setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_HOLD.val); | 
|---|
|  |  |  | if(!checkDiffItemService.updateById(checkDiffItem)){ | 
|---|
|  |  |  | throw new CoolException("更新差异单明细失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | checkDiff.setCheckQty(checkDiff.getCheckQty()+checkDiffItem.getCheckQty()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | checkDiff.setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_HOLD.val); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(!checkDiffService.updateById(checkDiff)){ | 
|---|
|  |  |  | throw new CoolException("更新差异单失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getBarcode, params.getContainer())); | 
|---|
|  |  |  | if (null == task){ | 
|---|
|  |  |  | if (null == task) { | 
|---|
|  |  |  | throw new CoolException("数据错误,未找到容器码对应盘点任务"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (CheckDiffItem ckDiffItem : params.getCheckDiffItems()) { | 
|---|
|  |  |  | CheckDiffItem diffItem = new CheckDiffItem(); | 
|---|
|  |  |  | if (Objects.isNull(ckDiffItem.getId())) { | 
|---|
|  |  |  | BeanUtils.copyProperties(ckDiffItem, diffItem); | 
|---|
|  |  |  | diffItem.setCheckId(params.getCheckId()) | 
|---|
|  |  |  | .setOrderCode(checkDiff.getOrderCode()) | 
|---|
|  |  |  | .setTaskId(task.getId()) | 
|---|
|  |  |  | .setBarcode(task.getBarcode()) | 
|---|
|  |  |  | .setAnfme(ckDiffItem.getCheckQty()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Matnr matnr = matnrService.getOne(new LambdaQueryWrapper<Matnr>().eq(Matnr::getCode, diffItem.getMatnrCode())); | 
|---|
|  |  |  | if (Objects.isNull(matnr)) { | 
|---|
|  |  |  | throw new CoolException("单据物料名称不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!checkDiffItemService.save(diffItem)) { | 
|---|
|  |  |  | throw new CoolException("新增明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //新增任务明细信息 | 
|---|
|  |  |  | TaskItem taskItem = new TaskItem(); | 
|---|
|  |  |  | BeanUtils.copyProperties(diffItem, diffItem); | 
|---|
|  |  |  | taskItem.setTaskId(task.getId()) | 
|---|
|  |  |  | .setOrderType(OrderType.ORDER_CHECK.type) | 
|---|
|  |  |  | .setSourceCode(diffItem.getOrderCode()) | 
|---|
|  |  |  | .setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_CHECK.type)) | 
|---|
|  |  |  | .setAnfme(diffItem.getCheckQty()) | 
|---|
|  |  |  | .setWorkQty(0.0) | 
|---|
|  |  |  | .setBatch(diffItem.getBatch()) | 
|---|
|  |  |  | .setMaktx(matnr.getName()) | 
|---|
|  |  |  | .setMatnrCode(diffItem.getMatnrCode()) | 
|---|
|  |  |  | .setSpec(diffItem.getSpec()) | 
|---|
|  |  |  | .setModel(diffItem.getModel()) | 
|---|
|  |  |  | .setMatnrId(matnr.getId()) | 
|---|
|  |  |  | .setCreateBy(loginUserId) | 
|---|
|  |  |  | //漏盘库存,新增没有盘点单明细ID | 
|---|
|  |  |  | .setSource(diffItem.getId()) | 
|---|
|  |  |  | .setSourceId(params.getCheckId()) | 
|---|
|  |  |  | .setCreateTime(new Date()) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setMemo("不在库明细,盘漏新增") | 
|---|
|  |  |  | .setUpdateTime(new Date()) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | //盘点单ID | 
|---|
|  |  |  | .setOrderId(checkDiff.getOrderId()); | 
|---|
|  |  |  | if (!taskItemService.save(taskItem)) { | 
|---|
|  |  |  | throw new CoolException("新增盘点明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | diffItem.setTaskItemId(taskItem.getId()).setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_HOLD.val); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!checkDiffItemService.updateById(diffItem)) { | 
|---|
|  |  |  | throw new CoolException("明细修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | diffItem = checkDiffItemService.getById(ckDiffItem.getId()); | 
|---|
|  |  |  | if (null == diffItem) { | 
|---|
|  |  |  | return R.error("数据错误,未找到差异单明细"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | diffItem.setCheckQty(ckDiffItem.getCheckQty()).setReason(ckDiffItem.getReason()).setMemo(ckDiffItem.getMemo()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | diffItem.setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_HOLD.val); | 
|---|
|  |  |  | if (!checkDiffItemService.updateById(diffItem)) { | 
|---|
|  |  |  | throw new CoolException("更新差异单明细失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!taskItemService.update(new LambdaUpdateWrapper<TaskItem>() | 
|---|
|  |  |  | .set(TaskItem::getAnfme, diffItem.getCheckQty()) | 
|---|
|  |  |  | .eq(TaskItem::getId, diffItem.getTaskItemId()))) { | 
|---|
|  |  |  | throw new CoolException("任务数量修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<CheckDiffItem> diffItems = checkDiffItemService.list(new LambdaQueryWrapper<CheckDiffItem>() | 
|---|
|  |  |  | .eq(CheckDiffItem::getCheckId, checkDiff.getId())); | 
|---|
|  |  |  | if (diffItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("盘点差异单明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Double sum = diffItems.stream().mapToDouble(CheckDiffItem::getCheckQty).sum(); | 
|---|
|  |  |  | checkDiff.setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_HOLD.val).setCheckQty(sum); | 
|---|
|  |  |  | if (!checkDiffService.updateById(checkDiff)) { | 
|---|
|  |  |  | throw new CoolException("更新差异单失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | task.setTaskStatus(TaskStsType.COMPLETE_OUT.id); | 
|---|
|  |  |  | task.setUpdateBy(loginUserId); | 
|---|
|  |  |  | task.setUpdateTime(new Date()); | 
|---|
|  |  |  | if (!taskService.updateById(task)){ | 
|---|
|  |  |  | if (!taskService.updateById(task)) { | 
|---|
|  |  |  | throw new CoolException("更新任务失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("盘点完成"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public R selectAddMatnr(Map<String, String> map) { | 
|---|
|  |  |  | String matnrCode = map.get("matnr"); | 
|---|
|  |  |  | if (Cools.isEmpty(matnrCode)){ | 
|---|
|  |  |  | return R.error("物料码为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Matnr matnr = matnrService.getOne(new LambdaQueryWrapper<Matnr>().eq(Matnr::getCode, matnrCode)); | 
|---|
|  |  |  | if (null == matnr){ | 
|---|
|  |  |  | return R.error("未找到编码对应的明细"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CheckDiffItem checkDiffItem = new CheckDiffItem() | 
|---|
|  |  |  | .setMatnrCode(matnr.getCode()) | 
|---|
|  |  |  | .setMaktx(matnr.getName()) | 
|---|
|  |  |  | .setAnfme(0D) | 
|---|
|  |  |  | .setCheckQty(0D) | 
|---|
|  |  |  | ; | 
|---|
|  |  |  | return R.ok(checkDiffItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public R getCheckTaskItemList2(String barcode) { | 
|---|
|  |  |  | LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | lambdaQueryWrapper.eq(Task::getBarcode, barcode); | 
|---|
|  |  |  | Task task = taskService.getOne(lambdaQueryWrapper); | 
|---|
|  |  |  | if (null == task) { | 
|---|
|  |  |  | throw new CoolException("未找到容器号对应任务"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!task.getTaskStatus().equals(TaskStsType.AWAIT.id)) { | 
|---|
|  |  |  | return R.error("任务状态不是等待确认"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId())); | 
|---|
|  |  |  | if (Cools.isEmpty(taskItems)){ | 
|---|
|  |  |  | throw new CoolException("未找到该容器码对应的任务明细"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String sourceCode = taskItems.stream().findFirst().map(TaskItem::getSourceCode).orElse(null); | 
|---|
|  |  |  | CheckDiff checkDiff = checkDiffService.getOne(new LambdaQueryWrapper<CheckDiff>().eq(CheckDiff::getOrderCode,sourceCode)); | 
|---|
|  |  |  | if (null == checkDiff) { | 
|---|
|  |  |  | return R.error("未找到盘点差异单"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<CheckDiffItem> checkDiffItems = checkDiffItemService.list(new LambdaQueryWrapper<CheckDiffItem>() | 
|---|
|  |  |  | .eq(CheckDiffItem::getBarcode, barcode) | 
|---|
|  |  |  | .eq(CheckDiffItem::getExceStatus, CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_INIT.val) | 
|---|
|  |  |  | .eq(CheckDiffItem::getOrderCode, sourceCode) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | if (checkDiffItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("此条码不在当前盘点单中"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok(Cools.add("checkDiffItems",checkDiffItems).add("checkDiff",checkDiff)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|