package cn.iocoder.yudao.module.mes.service.safety;
|
|
import cn.hutool.core.collection.CollUtil;
|
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;
|
@Resource
|
private SafetyRefResolver refResolver;
|
|
// ==================== 巡检点位 ====================
|
|
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()));
|
applyPointRefs(point, reqVO.getWorkshopId(), reqVO.getOwnerId());
|
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()));
|
applyPointRefs(point, reqVO.getWorkshopId(), reqVO.getOwnerId());
|
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);
|
}
|
|
/**
|
* 点位的主数据引用:车间 + 责任人,从主数据带出名称落快照
|
*/
|
private void applyPointRefs(SafetyInspectionPointDO point, Long workshopId, Long ownerId) {
|
point.setWorkshopId(workshopId);
|
if (workshopId == null) {
|
point.setWorkshopName("");
|
} else {
|
point.setWorkshopName(refResolver.requireWorkshop(workshopId).getName());
|
}
|
point.setOwnerId(ownerId);
|
if (ownerId == null) {
|
point.setOwnerName("");
|
} else {
|
point.setOwnerName(refResolver.requireEmployee(ownerId, SAFETY_EMPLOYEE_REQUIRED).getName());
|
}
|
}
|
|
/**
|
* 批量回填点位的车间名与责任人姓名(用主数据最新值覆盖快照)
|
*/
|
public void fillPointRefs(List<SafetyInspectionPointDO> points) {
|
if (CollUtil.isEmpty(points)) {
|
return;
|
}
|
var workshopMap = refResolver.workshopMap(points.stream().map(SafetyInspectionPointDO::getWorkshopId).toList());
|
var employeeMap = refResolver.employeeMap(points.stream().map(SafetyInspectionPointDO::getOwnerId).toList());
|
points.forEach(point -> {
|
var workshop = workshopMap.get(point.getWorkshopId());
|
if (workshop != null) {
|
point.setWorkshopName(workshop.getName());
|
}
|
var employee = employeeMap.get(point.getOwnerId());
|
if (employee != null) {
|
point.setOwnerName(employee.getName());
|
}
|
});
|
}
|
|
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.setInspectorId(reqVO.getInspectorId());
|
inspection.setInspector(refResolver.requireEmployee(reqVO.getInspectorId(), SAFETY_EMPLOYEE_REQUIRED).getName());
|
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 void fillInspectorNames(List<SafetyInspectionDO> records) {
|
if (CollUtil.isEmpty(records)) {
|
return;
|
}
|
var employeeMap = refResolver.employeeMap(records.stream().map(SafetyInspectionDO::getInspectorId).toList());
|
records.forEach(record -> {
|
var employee = employeeMap.get(record.getInspectorId());
|
if (employee != null) {
|
record.setInspector(employee.getName());
|
}
|
});
|
}
|
|
/** 最近一次巡检时间(用于点位列表展示) */
|
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;
|
}
|
|
}
|