package cn.iocoder.yudao.module.mes.service.safety;
|
|
import cn.hutool.core.util.StrUtil;
|
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.safety.vo.inspection.*;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyInspectionDO;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyInspectionPointDO;
|
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyInspectionMapper;
|
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyInspectionPointMapper;
|
import cn.iocoder.yudao.module.mes.enums.safety.SafetyInspectCycleEnum;
|
import cn.iocoder.yudao.module.mes.enums.safety.SafetyInspectionPointTypeEnum;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.time.LocalDateTime;
|
import java.util.List;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.module.mes.enums.SafetyErrorCodeConstants.*;
|
|
/**
|
* 安全生产 - 巡检点位与巡检记录 Service
|
*
|
* <p>覆盖高压配电室、机组区等日常安全巡检。巡检发现异常时可直接转隐患上报。</p>
|
*
|
* @author 超级管理员
|
*/
|
@Service
|
@Validated
|
public class SafetyInspectionService {
|
|
@Resource
|
private SafetyInspectionPointMapper pointMapper;
|
@Resource
|
private SafetyInspectionMapper inspectionMapper;
|
|
// ==================== 巡检点位 ====================
|
|
public Long createPoint(SafetyInspectionPointSaveReqVO reqVO) {
|
reqVO.setId(null);
|
String code = StrUtil.trim(reqVO.getCode());
|
if (pointMapper.selectByCode(code) != null) {
|
throw exception(SAFETY_POINT_CODE_DUPLICATE);
|
}
|
SafetyInspectionPointDO point = BeanUtils.toBean(reqVO, SafetyInspectionPointDO.class);
|
point.setCode(code);
|
point.setName(StrUtil.trim(reqVO.getName()));
|
point.setInspectCycle(StrUtil.blankToDefault(reqVO.getInspectCycle(), SafetyInspectCycleEnum.DAILY.getCycle()));
|
pointMapper.insert(point);
|
return point.getId();
|
}
|
|
public void updatePoint(SafetyInspectionPointSaveReqVO reqVO) {
|
SafetyInspectionPointDO old = validatePointExists(reqVO.getId());
|
String code = StrUtil.trim(reqVO.getCode());
|
SafetyInspectionPointDO byCode = pointMapper.selectByCode(code);
|
if (byCode != null && !byCode.getId().equals(old.getId())) {
|
throw exception(SAFETY_POINT_CODE_DUPLICATE);
|
}
|
SafetyInspectionPointDO point = BeanUtils.toBean(reqVO, SafetyInspectionPointDO.class);
|
point.setCode(code);
|
point.setName(StrUtil.trim(reqVO.getName()));
|
pointMapper.updateById(point);
|
}
|
|
public void deletePoint(Long id) {
|
validatePointExists(id);
|
pointMapper.deleteById(id);
|
}
|
|
public SafetyInspectionPointDO getPoint(Long id) {
|
return validatePointExists(id);
|
}
|
|
public PageResult<SafetyInspectionPointDO> getPointPage(SafetyInspectionPointPageReqVO reqVO) {
|
return pointMapper.selectPage(reqVO);
|
}
|
|
/**
|
* 启用中的巡检点位列表(移动端巡检选择用)
|
*/
|
public List<SafetyInspectionPointDO> getEnabledPointList() {
|
return pointMapper.selectListByStatus(0);
|
}
|
|
public static void fillPointDisplay(SafetyInspectionPointDO point, SafetyInspectionPointRespVO respVO) {
|
respVO.setPointTypeName(SafetyInspectionPointTypeEnum.getNameByType(point.getPointType()));
|
respVO.setInspectCycleName(SafetyInspectCycleEnum.getNameByCycle(point.getInspectCycle()));
|
}
|
|
// ==================== 巡检记录 ====================
|
|
public Long createInspection(SafetyInspectionSaveReqVO reqVO) {
|
reqVO.setId(null);
|
SafetyInspectionPointDO point = validatePointExists(reqVO.getPointId());
|
if (Integer.valueOf(1).equals(point.getStatus())) {
|
throw exception(SAFETY_POINT_DISABLED);
|
}
|
SafetyInspectionDO inspection = buildInspection(reqVO, point);
|
inspectionMapper.insert(inspection);
|
return inspection.getId();
|
}
|
|
public void updateInspection(SafetyInspectionSaveReqVO reqVO) {
|
SafetyInspectionDO old = validateInspectionExists(reqVO.getId());
|
SafetyInspectionPointDO point = validatePointExists(reqVO.getPointId());
|
SafetyInspectionDO inspection = buildInspection(reqVO, point);
|
inspection.setId(old.getId());
|
inspectionMapper.updateById(inspection);
|
}
|
|
public void deleteInspection(Long id) {
|
validateInspectionExists(id);
|
inspectionMapper.deleteById(id);
|
}
|
|
public SafetyInspectionDO getInspection(Long id) {
|
return validateInspectionExists(id);
|
}
|
|
public PageResult<SafetyInspectionDO> getInspectionPage(SafetyInspectionPageReqVO reqVO) {
|
return inspectionMapper.selectPage(reqVO);
|
}
|
|
private SafetyInspectionDO buildInspection(SafetyInspectionSaveReqVO reqVO, SafetyInspectionPointDO point) {
|
SafetyInspectionDO inspection = BeanUtils.toBean(reqVO, SafetyInspectionDO.class);
|
inspection.setPointId(point.getId());
|
inspection.setPointName(point.getName());
|
inspection.setPointType(point.getPointType());
|
inspection.setInspector(StrUtil.trim(reqVO.getInspector()));
|
inspection.setResult(StrUtil.blankToDefault(reqVO.getResult(), "NORMAL"));
|
inspection.setInspectTime(reqVO.getInspectTime() == null ? LocalDateTime.now() : reqVO.getInspectTime());
|
inspection.setHazardCount(reqVO.getHazardCount() == null ? 0 : reqVO.getHazardCount());
|
inspection.setRemark(reqVO.getRemark() == null ? "" : reqVO.getRemark().trim());
|
return inspection;
|
}
|
|
/** 最近一次巡检时间(用于点位列表展示) */
|
public LocalDateTime getLastInspectTime(Long pointId) {
|
SafetyInspectionDO latest = inspectionMapper.selectLatestByPointId(pointId);
|
return latest == null ? null : latest.getInspectTime();
|
}
|
|
private SafetyInspectionPointDO validatePointExists(Long id) {
|
SafetyInspectionPointDO point = id == null ? null : pointMapper.selectById(id);
|
if (point == null) {
|
throw exception(SAFETY_POINT_NOT_EXISTS);
|
}
|
return point;
|
}
|
|
private SafetyInspectionDO validateInspectionExists(Long id) {
|
SafetyInspectionDO inspection = id == null ? null : inspectionMapper.selectById(id);
|
if (inspection == null) {
|
throw exception(SAFETY_INSPECTION_NOT_EXISTS);
|
}
|
return inspection;
|
}
|
|
}
|