| | |
| | | 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 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; |
| | |
| | | @Service |
| | | @Validated |
| | | public class MesDvSubjectServiceImpl implements MesDvSubjectService { |
| | | |
| | | /** 导入时未填项目类型时的默认值:设备点检(字典 mes_dv_subject_type) */ |
| | | private static final Integer DEFAULT_SUBJECT_TYPE = 1; |
| | | |
| | | @Resource |
| | | private MesDvSubjectMapper subjectMapper; |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |