/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.sxkj.gd.workorder.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.StringUtil; import org.sxkj.common.constant.WordOrderConstant; import org.sxkj.common.utils.OrderNumUtils; import org.sxkj.gd.workorder.entity.GdClueEventEntity; import org.sxkj.gd.workorder.entity.GdTaskResultEntity; import org.sxkj.gd.workorder.enums.EventStatusEnum; import org.sxkj.gd.workorder.excel.GdClueEventExcel; import org.sxkj.gd.workorder.mapper.GdClueEventMapper; import org.sxkj.gd.workorder.param.GdClueEventDistributeParam; import org.sxkj.gd.workorder.param.GdClueEventRejectParam; import org.sxkj.gd.workorder.param.GdClueEventStatusUpdateParam; import org.sxkj.gd.workorder.service.IGdClueEventService; import org.sxkj.gd.workorder.service.IGdClueEventStatusFlowService; import org.sxkj.gd.workorder.service.IGdTaskResultService; import org.sxkj.gd.workorder.vo.GdClueEventCountVO; import org.sxkj.gd.workorder.vo.GdClueEventListVO; import org.sxkj.gd.workorder.vo.GdClueEventVO; import org.sxkj.gd.utils.GdGeoAddressUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springblade.core.mp.base.BaseServiceImpl; import java.util.Date; import java.util.List; /** * 事件表(线索事件) 服务实现类 * * @author lw * @since 2026-01-14 */ @Slf4j @Service @AllArgsConstructor public class GdClueEventServiceImpl extends BaseServiceImpl implements IGdClueEventService { private final IGdTaskResultService gdTaskResultService; private final IGdClueEventStatusFlowService gdClueEventStatusFlowService; @Override public IPage selectGdClueEventPage(IPage page, GdClueEventVO gdClueEvent) { return page.setRecords(baseMapper.selectGdClueEventPage(page, gdClueEvent)); } @Override public IPage selectGdClueEventList(IPage page, Wrapper queryWrapper) { return page.setRecords(baseMapper.selectGdClueEventList(page, queryWrapper)); } @Override public List listGdClueEventByDept(Integer onlyMine, String keyword) { // 1. 获取当前用户部门ID和用户ID Long deptId = null; Long userId = null; Integer mine = 0; List statusList; // 2. 判断是否为管理员 boolean isAdmin = AuthUtil.isAdministrator() || AuthUtil.isAdmin(); if (!isAdmin) { // 非管理员:获取部门ID和用户ID deptId = Long.valueOf(AuthUtil.getDeptId()); userId = AuthUtil.getUserId(); mine = onlyMine == null ? 0 : onlyMine; // 非管理员:排除已退回状态(状态2) statusList = java.util.Arrays.asList(0, 1, 3, 4); } else { // 管理员:可以查询所有状态 statusList = java.util.Arrays.asList(0, 1, 2, 3, 4); } // 3. 查询事件列表 return baseMapper.selectGdClueEventSimpleList(deptId, userId, mine, keyword, statusList); } /** * 列表查询(分页) *

* 步骤: * 1. 获取当前用户部门ID和用户ID * 2. 判断是否为管理员,设置状态列表 * 3. 根据includeReturned参数决定是否包含已退回状态 * 4. 根据事件状态参数调整状态列表 * 5. 查询分页数据 *

* * @param page 分页对象 * @param onlyMine 是否仅我的数据 * @param keyword 事件编号关键字 * @param eventStatus 事件状态 * @param startTime 开始时间 * @param endTime 结束时间 * @param includeReturned 是否包含已退回数据,1为是,0为否 * @return 分页数据 */ @Override public IPage pageGdClueEventByDept(IPage page, Integer onlyMine, String keyword, Integer eventStatus, String startTime, String endTime, Integer includeReturned) { // 1. 获取当前用户部门ID和用户ID Long deptId = null; Long userId = null; Integer mine = 0; List statusList; // 2. 判断是否为管理员 boolean isAdmin = AuthUtil.isAdministrator() || AuthUtil.isAdmin(); mine = onlyMine == null ? 0 : onlyMine; if (mine == 1) { // 管理员:仅查询当前用户数据 userId = AuthUtil.getUserId(); } // 3. 根据includeReturned参数构建状态列表 boolean shouldIncludeReturned = includeReturned != null && includeReturned == 1; if (!isAdmin) { // 非管理员:获取部门ID deptId = Long.valueOf(AuthUtil.getDeptId()); // 非管理员:根据includeReturned参数决定是否包含已退回状态(状态2) statusList = java.util.Arrays.asList(0, 1, 3, 4); } else { // 管理员:根据includeReturned参数决定是否包含已退回状态 if (shouldIncludeReturned) { statusList = java.util.Arrays.asList(0, 1, 2, 3, 4); } else { statusList = java.util.Arrays.asList(0, 1, 3, 4); } } // 4. 如果指定了事件状态,优先使用指定的状态 if (eventStatus != null) { // 检查指定的状态是否在允许的状态列表中 if (statusList.contains(eventStatus)) { statusList = java.util.Arrays.asList(eventStatus); } else { // 如果指定的状态不在允许列表中,返回空结果 statusList = java.util.Collections.emptyList(); } } // 5. 查询分页数据 return page.setRecords(baseMapper.selectGdClueEventSimpleList(page, deptId, userId, mine, keyword, statusList, startTime, endTime)); } @Override public GdClueEventVO getGdClueEventDetail(Long id) { if (id == null) { throw new RuntimeException("事件ID不能为空"); } Long deptId = null; //Long.valueOf(AuthUtil.getDeptId()); GdClueEventVO detail = baseMapper.selectGdClueEventDetailById(id, deptId); if (detail == null) { throw new RuntimeException("事件不存在"); } String address = GdGeoAddressUtil.getFormattedAddress(detail.getLongitude(), detail.getLatitude()); log.info("地址:{}", address); if (StringUtil.isNotBlank(address)) { detail.setEventLocation(address); } return detail; } @Override public GdClueEventCountVO getGdClueEventCount(String keyword) { // 1. 获取当前用户部门ID和用户ID List statusList; Long deptId = null; Long userId = null; // 2. 判断是否为管理员 boolean isAdmin = AuthUtil.isAdministrator() || AuthUtil.isAdmin(); if (isAdmin) { // 管理员:可以查询所有状态 userId = AuthUtil.getUserId(); statusList = java.util.Arrays.asList(0, 1, 3, 4); } else { deptId = Long.valueOf(AuthUtil.getDeptId()); userId = AuthUtil.getUserId(); // 非管理员:排除已退回状态(状态2) statusList = java.util.Arrays.asList(0, 1, 3, 4); } // 3. 查询统计数据 GdClueEventCountVO countVO = baseMapper.selectGdClueEventCount(deptId, userId, keyword, statusList); // 4. 处理空结果,返回默认值 if (countVO == null) { GdClueEventCountVO empty = new GdClueEventCountVO(); empty.setTotalCount(0L); empty.setMyCount(0L); empty.setDisposingCount(0L); empty.setDisposedCount(0L); empty.setCompletedCount(0L); return empty; } // 5. 返回统计结果 return countVO; } @Override @Transactional(rollbackFor = Exception.class) public boolean distributeClueEvent(GdClueEventDistributeParam distributeParam) { GdTaskResultEntity taskResult = gdTaskResultService.getById(distributeParam.getResultId()); if (taskResult == null || (taskResult.getIsDeleted() != null && taskResult.getIsDeleted() != 0)) { throw new RuntimeException("成果不存在"); } // 判断是否已分发,只有"已退回(2)"和"待确认(0)"状态才允许分发,其他状态均不能分发 if (!EventStatusEnum.RETURNED.getCode().equals(taskResult.getDistributeStatus()) && !EventStatusEnum.PENDING_CONFIRM.getCode().equals(taskResult.getDistributeStatus())) { throw new RuntimeException("成果已分发,无法再次分发"); } // 查看事件直接是否分发过 GdClueEventEntity clueEvent = getOne(Wrappers.query().lambda() .eq(GdClueEventEntity::getResultId, distributeParam.getResultId()) .eq(GdClueEventEntity::getIsDeleted, 0)); // 如果是空的则创建对象 if (clueEvent == null) { clueEvent = new GdClueEventEntity(); } clueEvent.setResultId(distributeParam.getResultId()); clueEvent.setWorkOrderId(taskResult.getPatrolTaskId()); clueEvent.setDisposeUser(distributeParam.getDisposeUser()); clueEvent.setDisposeDept(distributeParam.getDisposeDept()); clueEvent.setLongitude(distributeParam.getLongitude()); clueEvent.setLatitude(distributeParam.getLatitude()); clueEvent.setEventStatus(EventStatusEnum.PENDING_CONFIRM.getCode()); String times = OrderNumUtils.initOrderNum(WordOrderConstant.EVENT_NUM_KEY); String eventNum = WordOrderConstant.EVENT_NUM_PREFIX + times; clueEvent.setEventNum(eventNum); if (StringUtil.isBlank(distributeParam.getAreaCode())) { clueEvent.setAreaCode(taskResult.getAreaCode()); } else { clueEvent.setAreaCode(distributeParam.getAreaCode()); } clueEvent.setCreateUser(AuthUtil.getUserId()); clueEvent.setCreateDept(Long.valueOf(AuthUtil.getDeptId())); clueEvent.setCreateTime(new Date()); clueEvent.setEventName(distributeParam.getEventName()); if (!saveOrUpdate(clueEvent)) { throw new RuntimeException("事件分发失败"); } // 插入状态流转记录(从无到待确认) gdClueEventStatusFlowService.insertStatusFlow( clueEvent.getId(), null, EventStatusEnum.PENDING_CONFIRM.getCode(), "事件分发", new Date() ); taskResult.setDistributeStatus(1); taskResult.setUpdateUser(AuthUtil.getUserId()); taskResult.setUpdateTime(new Date()); if (!gdTaskResultService.updateTaskResultById(taskResult)) { throw new RuntimeException("成果状态更新失败"); } return true; } @Override @Transactional(rollbackFor = Exception.class) public boolean handleClueEvent(GdClueEventRejectParam rejectParam) { GdClueEventEntity clueEvent = getById(rejectParam.getEventId()); if (clueEvent == null || (clueEvent.getIsDeleted() != null && clueEvent.getIsDeleted() != 0)) { throw new RuntimeException("事件不存在"); } if (clueEvent.getEventStatus() == null || clueEvent.getEventStatus() != 1) { throw new RuntimeException("事件当前状态无法处理"); } Integer operateStatus = rejectParam.getEventStatus(); if (operateStatus == null || (operateStatus != 0 && operateStatus != 1)) { throw new RuntimeException("事件状态不正确"); } int targetStatus = operateStatus == 0 ? 2 : 3; clueEvent.setEventStatus(targetStatus); clueEvent.setUpdateUser(AuthUtil.getUserId()); clueEvent.setUpdateTime(new Date()); if (!updateById(clueEvent)) { throw new RuntimeException("事件处理失败"); } GdTaskResultEntity taskResult = gdTaskResultService.getById(clueEvent.getResultId()); if (taskResult == null || (taskResult.getIsDeleted() != null && taskResult.getIsDeleted() != 0)) { throw new RuntimeException("成果不存在"); } // 使用XML方式更新成果信息(更新所有字段) taskResult.setDistributeStatus(targetStatus); taskResult.setUpdateUser(AuthUtil.getUserId()); taskResult.setUpdateTime(new Date()); if (!gdTaskResultService.updateTaskResultById(taskResult)) { throw new RuntimeException("成果状态更新失败"); } return true; } @Override public List exportGdClueEvent(Wrapper queryWrapper) { List gdClueEventList = baseMapper.exportGdClueEvent(queryWrapper); //gdClueEventList.forEach(gdClueEvent -> { // gdClueEvent.setTypeName(DictCache.getValue(DictEnum.YES_NO, GdClueEvent.getType())); //}); return gdClueEventList; } /** * 对外接口新增或修改事件 *

* 步骤: * 1. 生成事件编号(使用SJ前缀) * 2. 设置事件编号到实体 * 3. 保存或更新事件信息 *

* * @param gdClueEvent 事件实体 * @return 是否操作成功 */ @Override public boolean saveOrUpdateExternal(GdClueEventEntity gdClueEvent) { // 1. 生成事件编号 String times = OrderNumUtils.initOrderNum(WordOrderConstant.EVENT_NUM_KEY); String eventNum = WordOrderConstant.SJ_EVENT_NUM_PREFIX + times; // 2. 设置事件编号 gdClueEvent.setEventNum(eventNum); gdClueEvent.setCreateTime(new Date()); gdClueEvent.setEventStatus(1); // 3. 保存或更新 return saveOrUpdate(gdClueEvent); } /** * 更新事件状态 *

* 步骤: * 1. 参数校验 * 2. 查询事件信息 * 3. 校验状态流转合法性 * 4. 更新事件状态 * 5. 插入状态流转记录 * 6. 退回时同步更新成果状态 *

* * @param updateParam 状态更新参数 * @return 是否操作成功 * @throws RuntimeException 当参数为空、事件不存在、状态流转非法或更新失败时抛出 */ @Override @Transactional(rollbackFor = Exception.class) public boolean updateEventStatus(GdClueEventStatusUpdateParam updateParam) { // 1. 参数校验 if (updateParam == null || updateParam.getEventId() == null) { throw new RuntimeException("参数不能为空"); } if (updateParam.getTargetStatus() == null) { throw new RuntimeException("目标状态不能为空"); } // 2. 查询事件信息 GdClueEventEntity eventEntity = getById(updateParam.getEventId()); if (eventEntity == null || (eventEntity.getIsDeleted() != null && eventEntity.getIsDeleted() != 0)) { throw new RuntimeException("事件不存在"); } Integer oldStatus = eventEntity.getEventStatus(); Integer targetStatus = updateParam.getTargetStatus(); // 3. 校验状态流转合法性 if (!EventStatusEnum.isValidCode(targetStatus)) { throw new RuntimeException("目标状态编码无效"); } if (!EventStatusEnum.isValidTransition(oldStatus, targetStatus)) { String oldStatusName = EventStatusEnum.getNameByCode(oldStatus); String newStatusName = EventStatusEnum.getNameByCode(targetStatus); throw new RuntimeException(String.format("状态流转非法:不允许从[%s]流转到[%s]", oldStatusName, newStatusName)); } // 4. 更新事件状态 eventEntity.setEventStatus(targetStatus); eventEntity.setUpdateUser(AuthUtil.getUserId()); eventEntity.setUpdateTime(new Date()); if (!updateById(eventEntity)) { throw new RuntimeException("更新事件状态失败"); } // 5. 插入状态流转记录 gdClueEventStatusFlowService.insertStatusFlow( updateParam.getEventId(), oldStatus, targetStatus, updateParam.getOperateContent(), updateParam.getOperateTime() != null ? updateParam.getOperateTime() : new Date() ); // 6. 退回时同步更新成果状态 if (targetStatus.equals(EventStatusEnum.RETURNED.getCode())) { // 查询成果信息 GdTaskResultEntity taskResult = gdTaskResultService.getById(eventEntity.getResultId()); if (taskResult != null && (taskResult.getIsDeleted() == null || taskResult.getIsDeleted() == 0)) { // 更新成果分发状态为已退回(状态2) taskResult.setDistributeStatus(EventStatusEnum.RETURNED.getCode()); taskResult.setUpdateUser(AuthUtil.getUserId()); taskResult.setUpdateTime(new Date()); if (!gdTaskResultService.updateTaskResultById(taskResult)) { throw new RuntimeException("成果状态更新失败"); } } } return true; } }