package cn.iocoder.yudao.module.mes.service.dv.subject;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
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.subject.vo.MesDvSubjectImportExcelVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.subject.vo.MesDvSubjectImportRespVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.subject.vo.MesDvSubjectPageReqVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.subject.vo.MesDvSubjectSaveReqVO;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.subject.MesDvSubjectDO;
|
import cn.iocoder.yudao.module.mes.dal.mysql.dv.subject.MesDvSubjectMapper;
|
import cn.iocoder.yudao.module.mes.service.dv.checkplan.MesDvCheckPlanSubjectService;
|
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.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.Collections;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
|
|
/**
|
* MES 点检保养项目 Service 实现类
|
*
|
* @author 超级管理员
|
*/
|
@Service
|
@Validated
|
public class MesDvSubjectServiceImpl implements MesDvSubjectService {
|
|
/** 导入时未填项目类型时的默认值:设备点检(字典 mes_dv_subject_type) */
|
private static final Integer DEFAULT_SUBJECT_TYPE = 1;
|
|
@Resource
|
private MesDvSubjectMapper subjectMapper;
|
|
@Resource
|
@Lazy
|
private MesDvCheckPlanSubjectService checkPlanSubjectService;
|
|
@Override
|
public Long createSubject(MesDvSubjectSaveReqVO createReqVO) {
|
// 校验编码唯一
|
validateSubjectCodeUnique(null, createReqVO.getCode());
|
|
// 插入
|
MesDvSubjectDO subject = BeanUtils.toBean(createReqVO, MesDvSubjectDO.class);
|
subjectMapper.insert(subject);
|
return subject.getId();
|
}
|
|
@Override
|
public void updateSubject(MesDvSubjectSaveReqVO updateReqVO) {
|
// 校验存在
|
validateSubjectExists(updateReqVO.getId());
|
// 校验编码唯一
|
validateSubjectCodeUnique(updateReqVO.getId(), updateReqVO.getCode());
|
|
// 更新
|
MesDvSubjectDO updateObj = BeanUtils.toBean(updateReqVO, MesDvSubjectDO.class);
|
subjectMapper.updateById(updateObj);
|
}
|
|
@Override
|
public void deleteSubject(Long id) {
|
// 校验存在
|
validateSubjectExists(id);
|
// 校验是否被点检保养方案引用
|
if (checkPlanSubjectService.getCheckPlanSubjectCountBySubjectId(id) > 0) {
|
throw exception(DV_SUBJECT_USED_BY_CHECK_PLAN);
|
}
|
|
// 删除
|
subjectMapper.deleteById(id);
|
}
|
|
@Override
|
public void validateSubjectExists(Long id) {
|
if (subjectMapper.selectById(id) == null) {
|
throw exception(DV_SUBJECT_NOT_EXISTS);
|
}
|
}
|
|
@Override
|
public void validateSubjectExistsAndEnable(Long id) {
|
MesDvSubjectDO subject = subjectMapper.selectById(id);
|
if (subject == null) {
|
throw exception(DV_SUBJECT_NOT_EXISTS);
|
}
|
if (ObjUtil.notEqual(CommonStatusEnum.ENABLE.getStatus(), subject.getStatus())) {
|
throw exception(DV_SUBJECT_IS_DISABLE);
|
}
|
}
|
|
private void validateSubjectCodeUnique(Long id, String code) {
|
if (code == null) {
|
return;
|
}
|
MesDvSubjectDO subject = subjectMapper.selectByCode(code);
|
if (subject == null) {
|
return;
|
}
|
if (ObjUtil.notEqual(subject.getId(), id)) {
|
throw exception(DV_SUBJECT_CODE_DUPLICATE);
|
}
|
}
|
|
@Override
|
public MesDvSubjectDO getSubject(Long id) {
|
return subjectMapper.selectById(id);
|
}
|
|
@Override
|
public PageResult<MesDvSubjectDO> getSubjectPage(MesDvSubjectPageReqVO pageReqVO) {
|
return subjectMapper.selectPage(pageReqVO);
|
}
|
|
@Override
|
public List<MesDvSubjectDO> getSubjectList() {
|
return subjectMapper.selectList();
|
}
|
|
@Override
|
public List<MesDvSubjectDO> getSubjectList(Collection<Long> ids) {
|
if (CollUtil.isEmpty(ids)) {
|
return Collections.emptyList();
|
}
|
return subjectMapper.selectByIds(ids);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public MesDvSubjectImportRespVO importSubjectList(List<MesDvSubjectImportExcelVO> importSubjectList,
|
boolean updateSupport) {
|
if (CollUtil.isEmpty(importSubjectList)) {
|
throw exception(DV_SUBJECT_IMPORT_LIST_IS_EMPTY);
|
}
|
|
MesDvSubjectImportRespVO respVO = MesDvSubjectImportRespVO.builder()
|
.createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
|
.failureCodes(new LinkedHashMap<>()).build();
|
for (int i = 0; i < importSubjectList.size(); i++) {
|
MesDvSubjectImportExcelVO importItem = importSubjectList.get(i);
|
int rowNo = i + 1;
|
// 1. 项目名称必填
|
if (StrUtil.isBlank(importItem.getName())) {
|
respVO.getFailureCodes().put("第 " + rowNo + " 行", "项目名称不能为空");
|
continue;
|
}
|
// 2. 编码为空时自动生成
|
String code = StrUtil.blankToDefault(importItem.getCode(),
|
"JX" + DateUtil.format(LocalDateTime.now(), "yyyyMMdd") + String.format("%03d", rowNo));
|
String key = code;
|
if (code.length() > 64) {
|
respVO.getFailureCodes().put("第 " + rowNo + " 行",
|
"项目编码[" + code + "]长度不能超过 64 个字符,请缩短后重新导入");
|
continue;
|
}
|
// 3. 类型默认设备点检
|
Integer type = importItem.getType() == null ? DEFAULT_SUBJECT_TYPE : importItem.getType();
|
Integer status = importItem.getStatus() == null
|
? CommonStatusEnum.ENABLE.getStatus() : importItem.getStatus();
|
|
// 4. 创建 or 更新
|
MesDvSubjectDO existSubject = subjectMapper.selectByCode(code);
|
if (existSubject == null) {
|
MesDvSubjectDO subject = new MesDvSubjectDO();
|
subject.setCode(code);
|
subject.setName(importItem.getName());
|
subject.setType(type);
|
subject.setContent(importItem.getContent());
|
subject.setStandard(importItem.getStandard());
|
subject.setStatus(status);
|
subject.setRemark(importItem.getRemark());
|
subjectMapper.insert(subject);
|
respVO.getCreateCodes().add(code);
|
} else if (updateSupport) {
|
MesDvSubjectDO updateObj = new MesDvSubjectDO();
|
updateObj.setId(existSubject.getId());
|
updateObj.setName(importItem.getName());
|
updateObj.setType(type);
|
updateObj.setContent(importItem.getContent());
|
updateObj.setStandard(importItem.getStandard());
|
updateObj.setStatus(status);
|
updateObj.setRemark(importItem.getRemark());
|
subjectMapper.updateById(updateObj);
|
respVO.getUpdateCodes().add(code);
|
} else {
|
respVO.getFailureCodes().put(key, "项目编码[" + code + "]已存在,如需覆盖请勾选「支持更新」");
|
}
|
}
|
return respVO;
|
}
|
|
}
|