|
@@ -1,6 +1,8 @@
|
|
|
package cn.iocoder.yudao.module.pms.service.maintain;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
@@ -9,22 +11,33 @@ import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainPageR
|
|
|
import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainSaveReqVO;
|
|
|
import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainSaveVO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.failure.IotFailureReportDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.iotlockstock.IotLockStockDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.iotoutbound.IotOutboundDO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.IotMaintainDO;
|
|
|
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.material.IotMaintainMaterialDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.failure.IotFailureReportMapper;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.iotlockstock.IotLockStockMapper;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.iotoutbound.IotOutboundMapper;
|
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.maintain.IotMaintainMapper;
|
|
|
import cn.iocoder.yudao.module.pms.dal.mysql.maintain.material.IotMaintainMaterialMapper;
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_LOCK_STOCK_FUSHU;
|
|
|
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_MAINTAIN_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
@@ -40,6 +53,12 @@ public class IotMaintainServiceImpl implements IotMaintainService {
|
|
|
private IotMaintainMapper iotMaintainMapper;
|
|
|
@Resource
|
|
|
private IotMaintainMaterialMapper iotMaintainMaterialMapper;
|
|
|
+ @Resource
|
|
|
+ private IotLockStockMapper iotLockStockMapper;
|
|
|
+ @Resource
|
|
|
+ private IotOutboundMapper iotOutboundMapper;
|
|
|
+ @Autowired
|
|
|
+ private IotFailureReportMapper iotFailureReportMapper;
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@@ -59,19 +78,94 @@ public class IotMaintainServiceImpl implements IotMaintainService {
|
|
|
return bean;
|
|
|
}).collect(Collectors.toList());
|
|
|
iotMaintainMaterialMapper.insertBatch(collect);
|
|
|
- // 返回
|
|
|
+ //本地库存减少且记录出库
|
|
|
+ if ("finished".equals(createReqVO.getMaintain().getStatus())){
|
|
|
+ localStockAndOut(createReqVO);
|
|
|
+ }
|
|
|
return iotMaintain.getId();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateIotMaintain(IotMaintainSaveReqVO updateReqVO) {
|
|
|
+ public void updateIotMaintain(IotMaintainSaveVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
- validateIotMaintainExists(updateReqVO.getId());
|
|
|
+ validateIotMaintainExists(updateReqVO.getMaintain().getId());
|
|
|
// 更新
|
|
|
- IotMaintainDO updateObj = BeanUtils.toBean(updateReqVO, IotMaintainDO.class);
|
|
|
+ IotMaintainDO updateObj = BeanUtils.toBean(updateReqVO.getMaintain(), IotMaintainDO.class);
|
|
|
iotMaintainMapper.updateById(updateObj);
|
|
|
+
|
|
|
+ List<IotMaintainMaterialSaveReqVO> maintainMaterials = updateReqVO.getMaintainMaterials();
|
|
|
+ //删除工单物料
|
|
|
+ iotMaintainMaterialMapper.deleteByMap(ImmutableMap.of("maintainId", updateObj.getId()));
|
|
|
+ //插入工单物料
|
|
|
+ List<IotMaintainMaterialDO> collect = maintainMaterials.stream().map(e -> {
|
|
|
+ IotMaintainMaterialDO bean = BeanUtils.toBean(e, IotMaintainMaterialDO.class);
|
|
|
+ bean.setMaintainId(updateObj.getId());
|
|
|
+ bean.setDeleted(false);
|
|
|
+ return bean;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ iotMaintainMaterialMapper.insertBatch(collect);
|
|
|
+ //本地库存减少且记录出库
|
|
|
+ if ("finished".equals(updateReqVO.getMaintain().getStatus())){
|
|
|
+ localStockAndOut(updateReqVO);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 减少本地库存并记录出库
|
|
|
+ * @param maintainSaveVO 维修工单信息
|
|
|
+ */
|
|
|
+ private void localStockAndOut(IotMaintainSaveVO maintainSaveVO) {
|
|
|
+ List<IotMaintainMaterialSaveReqVO> maintainMaterials = maintainSaveVO.getMaintainMaterials();
|
|
|
+ //判断物料不为空
|
|
|
+ if (CollUtil.isNotEmpty(maintainMaterials)) {
|
|
|
+ maintainMaterials.forEach(e ->{
|
|
|
+ String code = e.getCode();
|
|
|
+ //减去本地库存
|
|
|
+ if ("本地库存".equals(e.getSourceType())){
|
|
|
+ List<IotLockStockDO> stockDOS = iotLockStockMapper.selectByMap(ImmutableMap.of("material_code", code, "dept_id", maintainSaveVO.getMaintain().getDeptId()));
|
|
|
+ if (CollUtil.isNotEmpty(stockDOS)) {
|
|
|
+ IotLockStockDO iotLockStockDO = stockDOS.get(0);
|
|
|
+ BigDecimal quantity = iotLockStockDO.getQuantity();
|
|
|
+ BigDecimal subtract = quantity.subtract(BigDecimal.valueOf(e.getDepleteCount()));
|
|
|
+ if (subtract.compareTo(BigDecimal.ZERO) < 0){
|
|
|
+ throw new ServiceException(IOT_LOCK_STOCK_FUSHU);
|
|
|
+ }
|
|
|
+ iotLockStockDO.setQuantity(subtract);
|
|
|
+ iotLockStockMapper.updateById(iotLockStockDO);
|
|
|
+ //todo 安全库存提醒?
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //记录出库
|
|
|
+ IotOutboundDO outboundDO = new IotOutboundDO();
|
|
|
+ outboundDO.setDeptId(maintainSaveVO.getMaintain().getDeptId());
|
|
|
+ outboundDO.setMaterialCode(code);
|
|
|
+ outboundDO.setMaterialName(e.getName());
|
|
|
+ outboundDO.setDeliveryTime(LocalDateTimeUtil.now());
|
|
|
+ outboundDO.setReason("故障维修");
|
|
|
+ outboundDO.setStatus(0);
|
|
|
+ outboundDO.setType(1);
|
|
|
+ outboundDO.setDeleted(false);
|
|
|
+ outboundDO.setUnit(e.getUnit());
|
|
|
+ outboundDO.setUnitPrice(e.getPrice());
|
|
|
+ iotOutboundMapper.insert(outboundDO);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //更新故障上报的状态为工单处理完成
|
|
|
+ IotMaintainSaveReqVO iotMaintain = maintainSaveVO.getMaintain();
|
|
|
+ if (Objects.nonNull(iotMaintain.getFailureId())){
|
|
|
+ IotFailureReportDO iotFailureReportDO = iotFailureReportMapper.selectById(iotMaintain.getFailureId());
|
|
|
+ if (Objects.nonNull(iotFailureReportDO)){
|
|
|
+ iotFailureReportDO.setStatus("orderfinished");
|
|
|
+ iotFailureReportMapper.updateById(iotFailureReportDO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //todo 异步消息通知逻辑
|
|
|
+ CompletableFuture.runAsync(()->{
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void deleteIotMaintain(Long id) {
|
|
|
// 校验存在
|