|
@@ -0,0 +1,540 @@
|
|
|
|
|
+package com.zksy.ops.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import com.zksy.common.core.domain.TreeSelect;
|
|
|
|
|
+import com.zksy.common.core.domain.entity.SysDept;
|
|
|
|
|
+import com.zksy.common.core.domain.entity.SysUser;
|
|
|
|
|
+import com.zksy.common.exception.ServiceException;
|
|
|
|
|
+import com.zksy.common.utils.SecurityUtils;
|
|
|
|
|
+import com.zksy.common.utils.StringUtils;
|
|
|
|
|
+import com.zksy.ops.domain.InspectTask;
|
|
|
|
|
+import com.zksy.ops.dto.InspectTaskCompleteDTO;
|
|
|
|
|
+import com.zksy.ops.dto.InspectTaskPageDTO;
|
|
|
|
|
+import com.zksy.ops.mapper.InspectTaskMapper;
|
|
|
|
|
+import com.zksy.ops.service.InspectTaskService;
|
|
|
|
|
+import com.zksy.system.service.ISysDeptService;
|
|
|
|
|
+import com.zksy.system.service.ISysUserService;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.time.format.DateTimeParseException;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.Random;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 运维管理-巡检任务 Service 实现
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author zksy
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class InspectTaskServiceImpl extends ServiceImpl<InspectTaskMapper, InspectTask>
|
|
|
|
|
+ implements InspectTaskService {
|
|
|
|
|
+
|
|
|
|
|
+ /** 巡检编号前缀 */
|
|
|
|
|
+ private static final String INSPECT_NO_PREFIX = "XJ";
|
|
|
|
|
+
|
|
|
|
|
+ /** 巡检编号最大生成重试次数(并发唯一性兜底) */
|
|
|
|
|
+ private static final int INSPECT_NO_MAX_RETRY = 5;
|
|
|
|
|
+
|
|
|
|
|
+ /** 状态:待巡检 */
|
|
|
|
|
+ private static final int STATUS_PENDING = 0;
|
|
|
|
|
+
|
|
|
|
|
+ /** 状态:已完成 */
|
|
|
|
|
+ private static final int STATUS_DONE = 1;
|
|
|
|
|
+
|
|
|
|
|
+ /** 删除标志:存在 */
|
|
|
|
|
+ private static final String DEL_FLAG_NORMAL = "0";
|
|
|
|
|
+
|
|
|
|
|
+ /** 现场照片地址最大长度,对应 photo_url varchar(1000) */
|
|
|
|
|
+ private static final int PHOTO_URL_MAX_LENGTH = 1000;
|
|
|
|
|
+
|
|
|
|
|
+ private static final DateTimeFormatter DATE_TIME_FORMATTER =
|
|
|
|
|
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ISysUserService userService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ISysDeptService deptService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分页查询巡检任务列表
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Page<InspectTask> findByPage(InspectTaskPageDTO dto) {
|
|
|
|
|
+ log.info("分页查询巡检任务列表-入参:{}", dto);
|
|
|
|
|
+ if (dto.getPageNum() == null || dto.getPageNum() < 1) {
|
|
|
|
|
+ dto.setPageNum(1L);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getPageSize() == null || dto.getPageSize() < 1) {
|
|
|
|
|
+ dto.setPageSize(10L);
|
|
|
|
|
+ }
|
|
|
|
|
+ Page<InspectTask> page = new Page<>(dto.getPageNum(), dto.getPageSize());
|
|
|
|
|
+ LambdaQueryWrapper<InspectTask> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 关键字:模糊匹配巡检编号、巡检内容
|
|
|
|
|
+ if (StringUtils.isNotEmpty(dto.getKeyword())) {
|
|
|
|
|
+ String keyword = dto.getKeyword().trim();
|
|
|
|
|
+ wrapper.and(w -> w.like(InspectTask::getInspectTaskNo, keyword)
|
|
|
|
|
+ .or()
|
|
|
|
|
+ .like(InspectTask::getInspectContent, keyword));
|
|
|
|
|
+ }
|
|
|
|
|
+ // 状态精确匹配
|
|
|
|
|
+ wrapper.eq(dto.getStatus() != null, InspectTask::getStatus, dto.getStatus());
|
|
|
|
|
+ // 计划巡检时间范围
|
|
|
|
|
+ LocalDateTime planTimeStart = parseDateTime(dto.getPlanTimeStart(), false);
|
|
|
|
|
+ LocalDateTime planTimeEnd = parseDateTime(dto.getPlanTimeEnd(), true);
|
|
|
|
|
+ wrapper.ge(planTimeStart != null, InspectTask::getPlanTime, planTimeStart);
|
|
|
|
|
+ wrapper.le(planTimeEnd != null, InspectTask::getPlanTime, planTimeEnd);
|
|
|
|
|
+ // 数据权限:仅查询指派给当前用户或当前用户创建的记录
|
|
|
|
|
+ Long dataAuthUserId = currentUserId();
|
|
|
|
|
+ if (dataAuthUserId == null) {
|
|
|
|
|
+ // 未获取到登录用户时返回空结果,避免越权查看
|
|
|
|
|
+ return new Page<>(dto.getPageNum(), dto.getPageSize());
|
|
|
|
|
+ }
|
|
|
|
|
+ // 超级管理员(userId=1)可查看全部记录
|
|
|
|
|
+ if (!SecurityUtils.isAdmin(dataAuthUserId)) {
|
|
|
|
|
+ wrapper.and(w -> w.eq(InspectTask::getAssignUserId, dataAuthUserId)
|
|
|
|
|
+ .or()
|
|
|
|
|
+ .eq(InspectTask::getCreateBy, dataAuthUserId));
|
|
|
|
|
+ }
|
|
|
|
|
+ wrapper.orderByDesc(InspectTask::getPlanTime)
|
|
|
|
|
+ .orderByDesc(InspectTask::getCreateTime);
|
|
|
|
|
+
|
|
|
|
|
+ Page<InspectTask> result = this.page(page, wrapper);
|
|
|
|
|
+ // 批量翻译指派巡检人名称,避免逐条查库
|
|
|
|
|
+ fillAssignUserName(result.getRecords());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取巡检任务详情
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public InspectTask getDetailById(Long inspectTaskId) {
|
|
|
|
|
+ log.info("获取巡检任务详情-入参:{}", inspectTaskId);
|
|
|
|
|
+ if (inspectTaskId == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ InspectTask task = this.getById(inspectTaskId);
|
|
|
|
|
+ if (task == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务不存在或已被删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ fillAssignUserName(Collections.singletonList(task));
|
|
|
|
|
+ fillPhotoList(task);
|
|
|
|
|
+ return task;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增巡检任务
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public Long addTask(InspectTask task) {
|
|
|
|
|
+ log.info("新增巡检任务-入参:{}", task.getInspectContent());
|
|
|
|
|
+ if (StringUtils.isEmpty(task.getInspectContent())) {
|
|
|
|
|
+ throw new ServiceException("巡检内容不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (task.getAssignUserId() == null) {
|
|
|
|
|
+ throw new ServiceException("指派巡检人不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ validateAssignUserInScope(task.getAssignUserId());
|
|
|
|
|
+ if (task.getPlanTime() == null) {
|
|
|
|
|
+ throw new ServiceException("计划巡检时间不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ Long userId = currentUserId();
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ task.setInspectTaskId(null);
|
|
|
|
|
+ task.setInspectTaskNo(generateInspectTaskNo());
|
|
|
|
|
+ task.setStatus(STATUS_PENDING);
|
|
|
|
|
+ task.setDelFlag(DEL_FLAG_NORMAL);
|
|
|
|
|
+ // 新增不写入执行结果字段
|
|
|
|
|
+ task.setRealTime(null);
|
|
|
|
|
+ task.setProblem(null);
|
|
|
|
|
+ task.setProcess(null);
|
|
|
|
|
+ task.setPhotoUrl(null);
|
|
|
|
|
+ task.setCreateBy(userId);
|
|
|
|
|
+ task.setCreateTime(now);
|
|
|
|
|
+ task.setUpdateBy(userId);
|
|
|
|
|
+ task.setUpdateTime(now);
|
|
|
|
|
+ this.save(task);
|
|
|
|
|
+ return task.getInspectTaskId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改巡检任务计划信息
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public boolean editTask(InspectTask task) {
|
|
|
|
|
+ log.info("修改巡检任务-入参:{}", task.getInspectTaskId());
|
|
|
|
|
+ if (task.getInspectTaskId() == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(task.getInspectContent())) {
|
|
|
|
|
+ throw new ServiceException("巡检内容不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (task.getAssignUserId() == null) {
|
|
|
|
|
+ throw new ServiceException("指派巡检人不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ validateAssignUserInScope(task.getAssignUserId());
|
|
|
|
|
+ if (task.getPlanTime() == null) {
|
|
|
|
|
+ throw new ServiceException("计划巡检时间不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ InspectTask exist = this.getById(task.getInspectTaskId());
|
|
|
|
|
+ if (exist == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务不存在或已被删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Integer.valueOf(STATUS_PENDING).equals(exist.getStatus())) {
|
|
|
|
|
+ throw new ServiceException("仅待巡检状态的巡检任务允许编辑");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 仅更新计划字段,不修改巡检编号、执行结果、状态与删除标志
|
|
|
|
|
+ LambdaUpdateWrapper<InspectTask> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ updateWrapper.set(InspectTask::getInspectContent, task.getInspectContent())
|
|
|
|
|
+ .set(InspectTask::getAssignUserId, task.getAssignUserId())
|
|
|
|
|
+ .set(InspectTask::getPlanTime, task.getPlanTime())
|
|
|
|
|
+ .set(InspectTask::getUpdateBy, currentUserId())
|
|
|
|
|
+ .set(InspectTask::getUpdateTime, LocalDateTime.now())
|
|
|
|
|
+ .eq(InspectTask::getInspectTaskId, task.getInspectTaskId())
|
|
|
|
|
+ .eq(InspectTask::getStatus, STATUS_PENDING)
|
|
|
|
|
+ .eq(InspectTask::getDelFlag, DEL_FLAG_NORMAL);
|
|
|
|
|
+ return this.update(updateWrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 完成巡检
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public boolean completeTask(InspectTaskCompleteDTO dto) {
|
|
|
|
|
+ log.info("完成巡检-入参:{}", dto.getInspectTaskId());
|
|
|
|
|
+ if (dto.getInspectTaskId() == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(dto.getRealTime())) {
|
|
|
|
|
+ throw new ServiceException("实际巡检时间不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(dto.getPhotoUrl())) {
|
|
|
|
|
+ throw new ServiceException("现场巡检照片不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getPhotoUrl().length() > PHOTO_URL_MAX_LENGTH) {
|
|
|
|
|
+ throw new ServiceException("现场照片数量过多,请减少后重试");
|
|
|
|
|
+ }
|
|
|
|
|
+ LocalDateTime realTime = parseDateTime(dto.getRealTime(), false);
|
|
|
|
|
+ if (realTime == null) {
|
|
|
|
|
+ throw new ServiceException("实际巡检时间格式不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+ InspectTask exist = this.getById(dto.getInspectTaskId());
|
|
|
|
|
+ if (exist == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务不存在或已被删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Integer.valueOf(STATUS_PENDING).equals(exist.getStatus())) {
|
|
|
|
|
+ throw new ServiceException("该巡检任务已完成,不可重复提交");
|
|
|
|
|
+ }
|
|
|
|
|
+ LambdaUpdateWrapper<InspectTask> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ updateWrapper.set(InspectTask::getRealTime, realTime)
|
|
|
|
|
+ .set(InspectTask::getProblem, dto.getProblem())
|
|
|
|
|
+ .set(InspectTask::getProcess, dto.getProcess())
|
|
|
|
|
+ .set(InspectTask::getPhotoUrl, dto.getPhotoUrl())
|
|
|
|
|
+ .set(InspectTask::getStatus, STATUS_DONE)
|
|
|
|
|
+ .set(InspectTask::getUpdateBy, currentUserId())
|
|
|
|
|
+ .set(InspectTask::getUpdateTime, LocalDateTime.now())
|
|
|
|
|
+ .eq(InspectTask::getInspectTaskId, dto.getInspectTaskId())
|
|
|
|
|
+ // 状态门禁:仅待巡检且未删除的记录可被完成,防止并发重复提交
|
|
|
|
|
+ .eq(InspectTask::getStatus, STATUS_PENDING)
|
|
|
|
|
+ .eq(InspectTask::getDelFlag, DEL_FLAG_NORMAL);
|
|
|
|
|
+ boolean updated = this.update(updateWrapper);
|
|
|
|
|
+ if (!updated) {
|
|
|
|
|
+ throw new ServiceException("该巡检任务已完成或已被删除,无法重复完成巡检");
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除巡检任务(逻辑删除)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public boolean deleteTask(Long inspectTaskId) {
|
|
|
|
|
+ log.info("删除巡检任务-入参:{}", inspectTaskId);
|
|
|
|
|
+ if (inspectTaskId == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ InspectTask exist = this.getById(inspectTaskId);
|
|
|
|
|
+ if (exist == null) {
|
|
|
|
|
+ throw new ServiceException("巡检任务不存在或已被删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Integer.valueOf(STATUS_PENDING).equals(exist.getStatus())) {
|
|
|
|
|
+ throw new ServiceException("已完成的巡检任务不允许删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ // removeById 由 MyBatis-Plus 逻辑删除改写为 del_flag='2',数据库记录保留
|
|
|
|
|
+ return this.removeById(inspectTaskId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成全局唯一巡检编号:XJ + 14位时间戳 + 4位随机数
|
|
|
|
|
+ */
|
|
|
|
|
+ private String generateInspectTaskNo() {
|
|
|
|
|
+ for (int i = 0; i < INSPECT_NO_MAX_RETRY; i++) {
|
|
|
|
|
+ String no = INSPECT_NO_PREFIX
|
|
|
|
|
+ + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
|
|
|
|
|
+ + String.format("%04d", new Random().nextInt(10000));
|
|
|
|
|
+ Long count = this.count(new LambdaQueryWrapper<InspectTask>()
|
|
|
|
|
+ .eq(InspectTask::getInspectTaskNo, no));
|
|
|
|
|
+ if (count == null || count == 0) {
|
|
|
|
|
+ return no;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ throw new ServiceException("巡检编号生成失败,请重试");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量翻译指派巡检人ID为用户名
|
|
|
|
|
+ */
|
|
|
|
|
+ private void fillAssignUserName(List<InspectTask> records) {
|
|
|
|
|
+ if (records == null || records.isEmpty()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<Long, String> userMap = loadUserMap();
|
|
|
|
|
+ for (InspectTask record : records) {
|
|
|
|
|
+ record.setAssignUserName(translateAssignUser(record.getAssignUserId(), userMap));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 一次性加载全部可用用户,构建 userId -> nickName 映射
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<Long, String> loadUserMap() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<SysUser> users = userService.selectAllEnabledUser();
|
|
|
|
|
+ if (users == null || users.isEmpty()) {
|
|
|
|
|
+ return new HashMap<>(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<Long, String> userMap = new HashMap<>(users.size() * 2);
|
|
|
|
|
+ for (SysUser user : users) {
|
|
|
|
|
+ if (user.getUserId() != null) {
|
|
|
|
|
+ userMap.put(user.getUserId(),
|
|
|
|
|
+ StringUtils.isNotEmpty(user.getNickName()) ? user.getNickName() : user.getUserName());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return userMap;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("加载巡检指派人用户映射失败", e);
|
|
|
|
|
+ return new HashMap<>(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将单个指派巡检人ID翻译为用户名,未匹配到时保留原ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private String translateAssignUser(Long assignUserId, Map<Long, String> userMap) {
|
|
|
|
|
+ if (assignUserId == null) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ return userMap.getOrDefault(assignUserId, String.valueOf(assignUserId));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将逗号分隔的照片地址拆分为数组,便于前端预览
|
|
|
|
|
+ */
|
|
|
|
|
+ private void fillPhotoList(InspectTask task) {
|
|
|
|
|
+ if (task == null || StringUtils.isEmpty(task.getPhotoUrl())) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<String> photoList = new ArrayList<>();
|
|
|
|
|
+ for (String url : task.getPhotoUrl().split(",")) {
|
|
|
|
|
+ if (StringUtils.isNotEmpty(url.trim())) {
|
|
|
|
|
+ photoList.add(url.trim());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ task.setPhotoList(photoList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 解析时间字符串,兼容 yyyy-MM-dd HH:mm:ss / yyyy-MM-dd HH:mm / yyyy-MM-dd
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param text 时间字符串
|
|
|
|
|
+ * @param isEnd 是否为结束时间(结束时间补齐到当日 23:59:59)
|
|
|
|
|
+ * @return 解析结果,为空或解析失败返回 null
|
|
|
|
|
+ */
|
|
|
|
|
+ private LocalDateTime parseDateTime(String text, boolean isEnd) {
|
|
|
|
|
+ if (StringUtils.isEmpty(text)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String value = text.trim();
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (value.length() == 19) {
|
|
|
|
|
+ return LocalDateTime.parse(value, DATE_TIME_FORMATTER);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value.length() == 16) {
|
|
|
|
|
+ return LocalDateTime.parse(value + ":00", DATE_TIME_FORMATTER);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value.length() == 10) {
|
|
|
|
|
+ return isEnd
|
|
|
|
|
+ ? LocalDateTime.parse(value + " 23:59:59", DATE_TIME_FORMATTER)
|
|
|
|
|
+ : LocalDateTime.parse(value + " 00:00:00", DATE_TIME_FORMATTER);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
|
|
+ log.warn("巡检任务时间参数解析失败:{}", value);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户所属部门及其全部子部门的ID集合(空集表示无部门范围)
|
|
|
|
|
+ * 超级管理员(userId=1)返回 null 表示不做数据限制
|
|
|
|
|
+ */
|
|
|
|
|
+ private Set<Long> getScopeDeptIds() {
|
|
|
|
|
+ Long currentUserId = currentUserId();
|
|
|
|
|
+ if (SecurityUtils.isAdmin(currentUserId)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ Long currentDeptId = currentDeptId();
|
|
|
|
|
+ Set<Long> allowed = new HashSet<>();
|
|
|
|
|
+ if (currentDeptId == null) {
|
|
|
|
|
+ return allowed;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<SysDept> allDepts = deptService.selectDeptList(new SysDept());
|
|
|
|
|
+ if (allDepts == null || allDepts.isEmpty()) {
|
|
|
|
|
+ return allowed;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (SysDept dept : allDepts) {
|
|
|
|
|
+ if (dept.getDeptId() == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dept.getDeptId().equals(currentDeptId) || ancestorsContain(dept.getAncestors(), currentDeptId)) {
|
|
|
|
|
+ allowed.add(dept.getDeptId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return allowed;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判断部门祖先链是否包含指定部门ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private boolean ancestorsContain(String ancestors, Long deptId) {
|
|
|
|
|
+ if (StringUtils.isEmpty(ancestors) || deptId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String part : ancestors.split(",")) {
|
|
|
|
|
+ if (StringUtils.isNotEmpty(part.trim()) && deptId.toString().equals(part.trim())) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户所属部门ID,未登录时返回 null
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long currentDeptId() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return SecurityUtils.getDeptId();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("获取当前登录用户部门ID失败", e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 校验指派巡检人是否处于当前部门或下属部门范围内,否则抛出异常
|
|
|
|
|
+ */
|
|
|
|
|
+ private void validateAssignUserInScope(Long assignUserId) {
|
|
|
|
|
+ if (assignUserId == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Long> scope = getScopeDeptIds();
|
|
|
|
|
+ if (scope == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (scope.isEmpty()) {
|
|
|
|
|
+ throw new ServiceException("无法校验指派人员部门范围,请联系管理员");
|
|
|
|
|
+ }
|
|
|
|
|
+ SysUser assignUser = userService.selectUserById(assignUserId);
|
|
|
|
|
+ if (assignUser == null || assignUser.getDeptId() == null) {
|
|
|
|
|
+ throw new ServiceException("指派巡检人不存在或部门信息缺失");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!scope.contains(assignUser.getDeptId())) {
|
|
|
|
|
+ throw new ServiceException("只能指派当前部门或下属部门的成员");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取可指派部门树:当前登录用户所属部门及其全部子部门
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<TreeSelect> assignableDeptTree() {
|
|
|
|
|
+ Set<Long> scope = getScopeDeptIds();
|
|
|
|
|
+ if (scope == null) {
|
|
|
|
|
+ List<SysDept> allDepts = deptService.selectDeptList(new SysDept());
|
|
|
|
|
+ if (allDepts == null || allDepts.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ return deptService.buildDeptTreeSelect(allDepts);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (scope.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ List<SysDept> allDepts = deptService.selectDeptList(new SysDept());
|
|
|
|
|
+ if (allDepts == null || allDepts.isEmpty()) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ List<SysDept> scoped = allDepts.stream()
|
|
|
|
|
+ .filter(dept -> dept.getDeptId() != null && scope.contains(dept.getDeptId()))
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ return deptService.buildDeptTreeSelect(scoped);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询可指派用户:仅返回指定部门(须在可指派部门范围内)的人员
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<SysUser> assignableUsers(Long deptId) {
|
|
|
|
|
+ Set<Long> scope = getScopeDeptIds();
|
|
|
|
|
+ if (scope == null) {
|
|
|
|
|
+ SysUser query = new SysUser();
|
|
|
|
|
+ if (deptId != null) {
|
|
|
|
|
+ query.setDeptId(deptId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return userService.selectUserList(query);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (scope.isEmpty()) {
|
|
|
|
|
+ throw new ServiceException("无法获取可指派部门范围,请联系管理员");
|
|
|
|
|
+ }
|
|
|
|
|
+ Long targetDeptId = (deptId == null || !scope.contains(deptId)) ? currentDeptId() : deptId;
|
|
|
|
|
+ if (targetDeptId == null || !scope.contains(targetDeptId)) {
|
|
|
|
|
+ throw new ServiceException("无权限获取该部门人员");
|
|
|
|
|
+ }
|
|
|
|
|
+ SysUser query = new SysUser();
|
|
|
|
|
+ query.setDeptId(targetDeptId);
|
|
|
|
|
+ return userService.selectUserList(query);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户ID,未登录时返回 null(避免鉴权异常掩盖业务错误)
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long currentUserId() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return SecurityUtils.getUserId();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("获取当前登录用户ID失败", e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|