package cn.iocoder.yudao.module.mes.service.dv.maintenrecord;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.util.ObjUtil;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.maintenrecord.vo.MesDvMaintenRecordPageReqVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.maintenrecord.vo.MesDvMaintenRecordSaveReqVO;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.maintenrecord.MesDvMaintenRecordDO;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.maintenrecord.MesDvMaintenRecordLineDO;
|
import cn.iocoder.yudao.module.mes.dal.mysql.dv.maintenrecord.MesDvMaintenRecordMapper;
|
import cn.iocoder.yudao.module.mes.enums.dv.MesDvMaintenRecordStatusEnum;
|
import cn.iocoder.yudao.module.mes.enums.dv.MesDvCheckPlanTypeEnum;
|
import cn.iocoder.yudao.module.mes.service.dv.checkplan.MesDvCheckPlanService;
|
import cn.iocoder.yudao.module.mes.service.dv.machinery.MesDvMachineryService;
|
import jakarta.annotation.Resource;
|
import org.springframework.context.annotation.Lazy;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.util.List;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.maintenrecord.vo.MesDvMaintenRecordImportExcelVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.maintenrecord.vo.MesDvMaintenRecordImportRespVO;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryDO;
|
import cn.iocoder.yudao.module.mes.dal.mysql.dv.machinery.MesDvMachineryMapper;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.checkplan.MesDvCheckPlanDO;
|
import cn.iocoder.yudao.module.mes.dal.mysql.dv.checkplan.MesDvCheckPlanMapper;
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
import cn.hutool.core.util.StrUtil;
|
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
/**
|
* MES 设备保养记录 Service 实现类
|
*
|
* @author 超级管理员
|
*/
|
@Service
|
@Validated
|
public class MesDvMaintenRecordServiceImpl implements MesDvMaintenRecordService {
|
|
@Resource
|
private MesDvCheckPlanMapper checkPlanMapper;
|
|
@Resource
|
private MesDvMachineryMapper machineryMapper;
|
|
@Resource
|
private MesDvMaintenRecordMapper maintenRecordMapper;
|
|
@Resource
|
@Lazy
|
private MesDvMaintenRecordLineService maintenRecordLineService;
|
@Resource
|
private MesDvMachineryService machineryService;
|
@Resource
|
private MesDvCheckPlanService checkPlanService;
|
|
@Override
|
public Long createMaintenRecord(MesDvMaintenRecordSaveReqVO createReqVO) {
|
// 1. 校验关联数据
|
validateMaintenRecordRelation(createReqVO);
|
|
// 2. 插入
|
MesDvMaintenRecordDO maintenRecord = BeanUtils.toBean(createReqVO, MesDvMaintenRecordDO.class);
|
maintenRecord.setStatus(MesDvMaintenRecordStatusEnum.PREPARE.getStatus());
|
maintenRecordMapper.insert(maintenRecord);
|
return maintenRecord.getId();
|
}
|
|
@Override
|
public void updateMaintenRecord(MesDvMaintenRecordSaveReqVO updateReqVO) {
|
// 1.1 校验存在,且状态为草稿
|
validateMaintenRecordDraft(updateReqVO.getId());
|
// 1.2 校验关联数据
|
validateMaintenRecordRelation(updateReqVO);
|
|
// 2. 更新
|
MesDvMaintenRecordDO updateObj = BeanUtils.toBean(updateReqVO, MesDvMaintenRecordDO.class);
|
maintenRecordMapper.updateById(updateObj);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void submitMaintenRecord(Long id) {
|
// 1.1 校验状态为草稿
|
validateMaintenRecordDraft(id);
|
// 1.2 校验至少有一条明细
|
List<MesDvMaintenRecordLineDO> lines = maintenRecordLineService.getMaintenRecordLineListByRecordId(id);
|
if (CollUtil.isEmpty(lines)) {
|
throw exception(MAINTEN_RECORD_NO_LINE);
|
}
|
|
// 2. 状态改为已提交
|
MesDvMaintenRecordDO record = maintenRecordMapper.selectById(id);
|
MesDvMaintenRecordDO updateObj = new MesDvMaintenRecordDO();
|
updateObj.setId(id);
|
updateObj.setStatus(MesDvMaintenRecordStatusEnum.SUBMITTED.getStatus());
|
maintenRecordMapper.updateById(updateObj);
|
|
// 3. 回写设备台账的【最近保养时间】
|
if (record.getMaintenTime() != null) {
|
machineryService.updateMachineryLastMaintenTime(record.getMachineryId(), record.getMaintenTime());
|
}
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void deleteMaintenRecord(Long id) {
|
// 校验存在,且状态为草稿
|
validateMaintenRecordDraft(id);
|
|
// 删除
|
maintenRecordMapper.deleteById(id);
|
// 级联删除子表
|
maintenRecordLineService.deleteMaintenRecordLineByRecordId(id);
|
}
|
|
private void validateMaintenRecordRelation(MesDvMaintenRecordSaveReqVO reqVO) {
|
// 校验设备是否存在
|
machineryService.validateMachineryExists(reqVO.getMachineryId());
|
// 校验保养计划是否存在
|
if (reqVO.getPlanId() != null) {
|
checkPlanService.validateCheckPlanExistsAndType(reqVO.getPlanId(), MesDvCheckPlanTypeEnum.MAINTENANCE.getType());
|
}
|
}
|
|
@Override
|
public void validateMaintenRecordExists(Long id) {
|
if (maintenRecordMapper.selectById(id) == null) {
|
throw exception(MAINTEN_RECORD_NOT_EXISTS);
|
}
|
}
|
|
@Override
|
public Long getMaintenRecordCountByMachineryId(Long machineryId) {
|
return maintenRecordMapper.selectCountByMachineryId(machineryId);
|
}
|
|
@Override
|
public MesDvMaintenRecordDO validateMaintenRecordDraft(Long id) {
|
MesDvMaintenRecordDO record = maintenRecordMapper.selectById(id);
|
if (record == null) {
|
throw exception(MAINTEN_RECORD_NOT_EXISTS);
|
}
|
if (ObjUtil.notEqual(MesDvMaintenRecordStatusEnum.PREPARE.getStatus(), record.getStatus())) {
|
throw exception(MAINTEN_RECORD_NOT_DRAFT);
|
}
|
return record;
|
}
|
|
@Override
|
public MesDvMaintenRecordDO getMaintenRecord(Long id) {
|
return maintenRecordMapper.selectById(id);
|
}
|
|
@Override
|
public PageResult<MesDvMaintenRecordDO> getMaintenRecordPage(MesDvMaintenRecordPageReqVO pageReqVO) {
|
return maintenRecordMapper.selectPage(pageReqVO);
|
}
|
|
|
@Override
|
public MesDvMaintenRecordImportRespVO importMaintenRecordList(List<MesDvMaintenRecordImportExcelVO> importList, boolean updateSupport) {
|
if (CollUtil.isEmpty(importList)) {
|
throw exception(DV_MAINTEN_RECORD_IMPORT_LIST_IS_EMPTY);
|
}
|
MesDvMaintenRecordImportRespVO respVO = MesDvMaintenRecordImportRespVO.builder()
|
.createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
|
.failureCodes(new LinkedHashMap<>()).build();
|
AtomicInteger index = new AtomicInteger(1);
|
importList.forEach(importItem -> {
|
int currentIndex = index.getAndIncrement();
|
String key = "第 " + currentIndex + " 行";
|
if (StrUtil.isBlank(importItem.getMachineryCode())) {
|
respVO.getFailureCodes().put(key, "设备编码不能为空");
|
return;
|
}
|
if (importItem.getMaintenTime() == null) {
|
respVO.getFailureCodes().put(key, "保养时间不能为空");
|
return;
|
}
|
MesDvMachineryDO machinery = machineryMapper.selectByCode(importItem.getMachineryCode());
|
if (machinery == null) {
|
respVO.getFailureCodes().put(key, "设备不存在:" + importItem.getMachineryCode());
|
return;
|
}
|
Long planId = null;
|
if (StrUtil.isNotBlank(importItem.getPlanCode())) {
|
MesDvCheckPlanDO plan = checkPlanMapper.selectByCode(importItem.getPlanCode());
|
if (plan == null) {
|
respVO.getFailureCodes().put(key, "点检保养方案不存在:" + importItem.getPlanCode());
|
return;
|
}
|
planId = plan.getId();
|
}
|
try {
|
MesDvMaintenRecordSaveReqVO saveReqVO = BeanUtils.toBean(importItem, MesDvMaintenRecordSaveReqVO.class);
|
saveReqVO.setMachineryId(machinery.getId());
|
saveReqVO.setPlanId(planId);
|
Long id = createMaintenRecord(saveReqVO);
|
respVO.getCreateCodes().add(String.valueOf(id));
|
} catch (ServiceException e) {
|
respVO.getFailureCodes().put(key, e.getMessage());
|
}
|
});
|
return respVO;
|
}
|
|
}
|