package cn.iocoder.yudao.module.mes.service.dv.subject;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.util.ObjUtil;
|
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.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.validation.annotation.Validated;
|
|
import java.util.Collection;
|
import java.util.Collections;
|
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.subject.vo.MesDvSubjectImportExcelVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.dv.subject.vo.MesDvSubjectImportRespVO;
|
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 MesDvSubjectServiceImpl implements MesDvSubjectService {
|
|
@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
|
public MesDvSubjectImportRespVO importSubjectList(List<MesDvSubjectImportExcelVO> importList, boolean updateSupport) {
|
if (CollUtil.isEmpty(importList)) {
|
throw exception(DV_SUBJECT_IMPORT_LIST_IS_EMPTY);
|
}
|
MesDvSubjectImportRespVO respVO = MesDvSubjectImportRespVO.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 = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
|
if (StrUtil.isBlank(importItem.getCode())) {
|
respVO.getFailureCodes().put(key, "项目编码不能为空");
|
return;
|
}
|
try {
|
MesDvSubjectSaveReqVO saveReqVO = BeanUtils.toBean(importItem, MesDvSubjectSaveReqVO.class);
|
saveReqVO.setType(ObjUtil.defaultIfNull(importItem.getType(), 1));
|
saveReqVO.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
|
MesDvSubjectDO exist = subjectMapper.selectByCode(importItem.getCode());
|
if (exist == null) {
|
createSubject(saveReqVO);
|
respVO.getCreateCodes().add(saveReqVO.getCode());
|
} else if (updateSupport) {
|
saveReqVO.setId(exist.getId());
|
updateSubject(saveReqVO);
|
respVO.getUpdateCodes().add(saveReqVO.getCode());
|
} else {
|
respVO.getFailureCodes().put(key, "项目编码已存在:" + importItem.getCode());
|
}
|
} catch (ServiceException e) {
|
respVO.getFailureCodes().put(key, e.getMessage());
|
}
|
});
|
return respVO;
|
}
|
|
}
|