package com.ruoyi.safe.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.safe.mapper.SafeLineInspectionHazardMapper;
import com.ruoyi.safe.pojo.SafeLineInspectionHazard;
import com.ruoyi.safe.service.SafeLineInspectionHazardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 安全生产--线路巡检隐患 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-06-29
*/
@Service
public class SafeLineInspectionHazardServiceImpl extends ServiceImpl implements SafeLineInspectionHazardService {
@Autowired
private SysUserMapper sysUserMapper;
@Override
public IPage pageSafeLineInspectionHazard(Page page, SafeLineInspectionHazard safeLineInspectionHazard) {
IPage result = this.lambdaQuery()
.eq(safeLineInspectionHazard.getInspectionId() != null, SafeLineInspectionHazard::getInspectionId, safeLineInspectionHazard.getInspectionId())
.like(safeLineInspectionHazard.getHazardCode() != null, SafeLineInspectionHazard::getHazardCode, safeLineInspectionHazard.getHazardCode())
.eq(safeLineInspectionHazard.getStatus() != null, SafeLineInspectionHazard::getStatus, safeLineInspectionHazard.getStatus())
.orderByDesc(SafeLineInspectionHazard::getCreateTime)
.page(page);
fillRectifyUserName(result.getRecords());
return result;
}
@Override
public List listByInspectionId(Integer inspectionId) {
List records = this.lambdaQuery()
.eq(SafeLineInspectionHazard::getInspectionId, inspectionId)
.orderByDesc(SafeLineInspectionHazard::getCreateTime)
.list();
fillRectifyUserName(records);
return records;
}
@Override
public boolean save(SafeLineInspectionHazard entity) {
if (entity.getHazardCode() == null || entity.getHazardCode().isEmpty()) {
entity.setHazardCode(generateHazardCode());
}
return super.save(entity);
}
@Override
public boolean updateById(SafeLineInspectionHazard entity) {
if ("已整改".equals(entity.getStatus())) {
Long currentUserId = SecurityUtils.getUserId();
entity.setRectifyUserId(currentUserId.intValue());
entity.setRectifyTime(LocalDateTime.now());
}
return super.updateById(entity);
}
private String generateHazardCode() {
long count = this.count() + 1;
return "YH" + String.format("%05d", count);
}
private void fillRectifyUserName(List records) {
if (records == null || records.isEmpty()) {
return;
}
List userIds = records.stream()
.map(SafeLineInspectionHazard::getRectifyUserId)
.filter(Objects::nonNull)
.map(Integer::longValue)
.distinct()
.collect(Collectors.toList());
if (userIds.isEmpty()) {
return;
}
Map userMap = sysUserMapper.selectUserByIds(userIds).stream()
.collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName, (v1, v2) -> v1));
records.forEach(record -> {
if (record.getRectifyUserId() != null) {
record.setRectifyUserName(userMap.getOrDefault(record.getRectifyUserId().longValue(), "未知用户"));
}
});
}
}