package cn.iocoder.yudao.module.mes.service.pro.emergencyplan;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.StrUtil;
|
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;
|
import cn.iocoder.yudao.module.mes.controller.admin.pro.emergencyplan.vo.MesProEmergencyPlanImportExcelVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.pro.emergencyplan.vo.MesProEmergencyPlanImportRespVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.pro.emergencyplan.vo.MesProEmergencyPlanPageReqVO;
|
import cn.iocoder.yudao.module.mes.controller.admin.pro.emergencyplan.vo.MesProEmergencyPlanSaveReqVO;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.emergencyplan.MesProEmergencyPlanDO;
|
import cn.iocoder.yudao.module.mes.dal.mysql.pro.emergencyplan.MesProEmergencyPlanMapper;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_EMERGENCY_PLAN_IMPORT_LIST_IS_EMPTY;
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_WORK_ORDER_NOT_EXISTS;
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_WORK_ORDER_NOT_PREPARE;
|
|
/**
|
* MES 应急处置预案 Service 实现类
|
*
|
* @author xiaoyi
|
*/
|
@Service
|
@Validated
|
public class MesProEmergencyPlanServiceImpl implements MesProEmergencyPlanService {
|
|
private static final Integer STATUS_DRAFT = 0;
|
private static final Integer STATUS_PUBLISHED = 10;
|
|
@Resource
|
private MesProEmergencyPlanMapper planMapper;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Long createPlan(MesProEmergencyPlanSaveReqVO createReqVO) {
|
String code = generateCode();
|
if (planMapper.selectByCode(code) != null) {
|
throw exception(PRO_WORK_ORDER_NOT_EXISTS);
|
}
|
MesProEmergencyPlanDO plan = BeanUtils.toBean(createReqVO, MesProEmergencyPlanDO.class, req -> req
|
.setCode(code)
|
.setStatus(STATUS_DRAFT));
|
planMapper.insert(plan);
|
return plan.getId();
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void updatePlan(MesProEmergencyPlanSaveReqVO updateReqVO) {
|
MesProEmergencyPlanDO oldPlan = validatePlanCanModify(updateReqVO.getId());
|
MesProEmergencyPlanDO updateObj = BeanUtils.toBean(updateReqVO, MesProEmergencyPlanDO.class, req -> req
|
.setCode(oldPlan.getCode())
|
.setStatus(oldPlan.getStatus()));
|
planMapper.updateById(updateObj);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void deletePlan(Collection<Long> ids) {
|
if (CollUtil.isEmpty(ids)) {
|
return;
|
}
|
for (Long id : ids) {
|
validatePlanCanModify(id);
|
planMapper.deleteById(id);
|
}
|
}
|
|
@Override
|
public MesProEmergencyPlanDO getPlan(Long id) {
|
return planMapper.selectById(id);
|
}
|
|
@Override
|
public PageResult<MesProEmergencyPlanDO> getPlanPage(MesProEmergencyPlanPageReqVO pageReqVO) {
|
return planMapper.selectPage(pageReqVO);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void publishPlan(Long id) {
|
MesProEmergencyPlanDO plan = validatePlanExists(id);
|
if (!Objects.equals(plan.getStatus(), STATUS_DRAFT)) {
|
throw exception(PRO_WORK_ORDER_NOT_PREPARE);
|
}
|
planMapper.updateById(new MesProEmergencyPlanDO().setId(id).setStatus(STATUS_PUBLISHED));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public MesProEmergencyPlanImportRespVO importPlanList(List<MesProEmergencyPlanImportExcelVO> importList,
|
boolean updateSupport) {
|
if (CollUtil.isEmpty(importList)) {
|
throw exception(MES_PRO_EMERGENCY_PLAN_IMPORT_LIST_IS_EMPTY);
|
}
|
MesProEmergencyPlanImportRespVO respVO = MesProEmergencyPlanImportRespVO.builder()
|
.createNames(new ArrayList<>()).updateNames(new ArrayList<>())
|
.failureNames(new LinkedHashMap<>()).build();
|
AtomicInteger index = new AtomicInteger(1);
|
importList.forEach(importItem -> {
|
int currentIndex = index.getAndIncrement();
|
String key = StrUtil.blankToDefault(importItem.getName(), "第 " + currentIndex + " 行");
|
if (StrUtil.isBlank(importItem.getName())) {
|
respVO.getFailureNames().put(key, "预案名称不能为空");
|
return;
|
}
|
MesProEmergencyPlanSaveReqVO reqVO = BeanUtils.toBean(importItem, MesProEmergencyPlanSaveReqVO.class);
|
MesProEmergencyPlanDO existPlan = planMapper.selectByName(importItem.getName());
|
try {
|
if (existPlan == null) {
|
reqVO.setId(null);
|
createPlan(reqVO);
|
respVO.getCreateNames().add(importItem.getName());
|
} else if (updateSupport) {
|
reqVO.setId(existPlan.getId());
|
updatePlan(reqVO);
|
respVO.getUpdateNames().add(importItem.getName());
|
} else {
|
respVO.getFailureNames().put(key, "预案名称已存在");
|
}
|
} catch (ServiceException e) {
|
respVO.getFailureNames().put(key, e.getMessage());
|
}
|
});
|
return respVO;
|
}
|
|
private String generateCode() {
|
return "YJ" + DateUtil.format(java.time.LocalDateTime.now(), "yyyyMMddHHmmss")
|
+ String.format("%03d", (int) (Math.random() * 1000));
|
}
|
|
private MesProEmergencyPlanDO validatePlanExists(Long id) {
|
MesProEmergencyPlanDO plan = planMapper.selectById(id);
|
if (plan == null) {
|
throw exception(PRO_WORK_ORDER_NOT_EXISTS);
|
}
|
return plan;
|
}
|
|
private MesProEmergencyPlanDO validatePlanCanModify(Long id) {
|
MesProEmergencyPlanDO plan = validatePlanExists(id);
|
if (!Objects.equals(plan.getStatus(), STATUS_DRAFT)) {
|
throw exception(PRO_WORK_ORDER_NOT_PREPARE);
|
}
|
return plan;
|
}
|
|
}
|