package com.ruoyi.safe.service.impl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.safe.pojo.SafeLineInspection;
import com.ruoyi.safe.pojo.SafeLineInspectionRecord;
import com.ruoyi.safe.mapper.SafeLineInspectionRecordMapper;
import com.ruoyi.safe.service.SafeLineInspectionRecordService;
import com.ruoyi.safe.service.SafeLineInspectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
/**
*
* 安全生产--线路巡检记录 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-06-29
*/
@Service
public class SafeLineInspectionRecordServiceImpl extends ServiceImpl implements SafeLineInspectionRecordService {
@Autowired
private SafeLineInspectionService safeLineInspectionService;
@Override
public List listByInspectionId(Integer inspectionId) {
return this.lambdaQuery()
.eq(SafeLineInspectionRecord::getInspectionId, inspectionId)
.orderByDesc(SafeLineInspectionRecord::getCheckTime)
.list();
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(SafeLineInspectionRecord entity) {
// 保存巡检记录
boolean result = super.save(entity);
if (result && entity.getInspectionId() != null) {
// 更新巡检任务状态为"巡检中",并设置巡检人为当前登录用户
updateInspectionStatus(entity.getInspectionId());
}
return result;
}
/**
* 更新巡检任务状态为"巡检中",并添加当前用户到巡检人列表
*/
private void updateInspectionStatus(Integer inspectionId) {
SafeLineInspection inspection = safeLineInspectionService.getById(inspectionId);
if (inspection != null && "待巡检".equals(inspection.getStatus())) {
Long currentUserId = SecurityUtils.getUserId();
String currentUserIdStr = String.valueOf(currentUserId);
// 添加当前用户到巡检人列表(如果不存在)
String inspectorId = inspection.getInspectorId();
if (inspectorId == null || inspectorId.isEmpty()) {
inspectorId = currentUserIdStr;
} else if (!inspectorId.contains(currentUserIdStr)) {
inspectorId = inspectorId + "," + currentUserIdStr;
}
inspection.setStatus("巡检中");
inspection.setInspectorId(inspectorId);
inspection.setUpdateTime(LocalDateTime.now());
inspection.setUpdateUser(currentUserId.intValue());
safeLineInspectionService.updateById(inspection);
}
}
}