| | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | import java.util.stream.Collectors; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | |
| | | import com.ruoyi.system.utils.TaskCodeGenerator; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import com.ruoyi.system.domain.SysTaskLog; |
| | | import com.ruoyi.system.domain.SysTaskEmergency; |
| | | import com.ruoyi.system.domain.SysTaskWelfare; |
| | | import com.ruoyi.system.domain.SysTaskAssignee; |
| | | import com.ruoyi.system.domain.vo.TaskQueryVO; |
| | | import com.ruoyi.system.domain.vo.TaskCreateVO; |
| | | import com.ruoyi.system.domain.vo.TaskUpdateVO; |
| | |
| | | import com.ruoyi.system.mapper.SysTaskLogMapper; |
| | | import com.ruoyi.system.mapper.SysTaskEmergencyMapper; |
| | | import com.ruoyi.system.mapper.SysTaskWelfareMapper; |
| | | import com.ruoyi.system.mapper.SysTaskAssigneeMapper; |
| | | import com.ruoyi.system.mapper.VehicleInfoMapper; |
| | | import com.ruoyi.system.domain.VehicleInfo; |
| | | import com.ruoyi.system.service.ISysTaskService; |
| | | import com.ruoyi.system.service.ILegacySystemSyncService; |
| | | import com.ruoyi.system.event.TaskCreatedEvent; |
| | | import com.ruoyi.system.event.TaskAssignedEvent; |
| | | import com.ruoyi.system.event.TaskStatusChangedEvent; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.ApplicationEventPublisher; |
| | | |
| | | /** |
| | | * 任务管理Service业务层处理 |
| | |
| | | private SysTaskWelfareMapper sysTaskWelfareMapper; |
| | | |
| | | @Autowired |
| | | private SysTaskAssigneeMapper sysTaskAssigneeMapper; |
| | | |
| | | @Autowired |
| | | private VehicleInfoMapper vehicleInfoMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private ILegacySystemSyncService legacySystemSyncService; |
| | | |
| | | @Autowired |
| | | private ApplicationEventPublisher eventPublisher; |
| | | |
| | | /** |
| | | * 查询任务管理 |
| | |
| | | task.setPlannedEndTime(createVO.getPlannedEndTime()); |
| | | task.setAssigneeId(createVO.getAssigneeId()); |
| | | task.setCreatorId(SecurityUtils.getUserId()); |
| | | task.setDeptId(SecurityUtils.getDeptId()); |
| | | // 优先使用前端传入的部门ID,如果没有则使用当前用户的部门ID |
| | | task.setDeptId(createVO.getDeptId() != null ? createVO.getDeptId() : SecurityUtils.getDeptId()); |
| | | task.setCreateBy(SecurityUtils.getUsername()); |
| | | task.setCreateTime(DateUtils.getNowDate()); |
| | | task.setUpdateBy(SecurityUtils.getUsername()); |
| | |
| | | } |
| | | } |
| | | |
| | | // 保存执行人员信息(包含角色类型) |
| | | if (result > 0 && createVO.getAssignees() != null && !createVO.getAssignees().isEmpty()) { |
| | | saveTaskAssignees(task.getTaskId(), createVO.getAssignees()); |
| | | } |
| | | |
| | | // 保存急救转运扩展信息 |
| | | if (result > 0 && "EMERGENCY_TRANSFER".equals(createVO.getTaskType())) { |
| | | saveEmergencyInfo(task.getTaskId(), createVO); |
| | |
| | | if (result > 0) { |
| | | recordTaskLog(task.getTaskId(), "CREATE", "创建任务", null, |
| | | "任务类型:" + createVO.getTaskType(), SecurityUtils.getUserId(), SecurityUtils.getUsername()); |
| | | } |
| | | |
| | | // 发布任务创建事件 |
| | | if (result > 0) { |
| | | eventPublisher.publishEvent(new TaskCreatedEvent( |
| | | this, |
| | | task.getTaskId(), |
| | | task.getTaskCode(), |
| | | task.getTaskType(), |
| | | task.getCreatorId(), |
| | | SecurityUtils.getUsername() |
| | | )); |
| | | } |
| | | |
| | | // 发布任务分配事件 |
| | | if (result > 0 && createVO.getAssignees() != null && !createVO.getAssignees().isEmpty()) { |
| | | List<Long> assigneeIds = createVO.getAssignees().stream() |
| | | .map(assignee -> assignee.getUserId()) |
| | | .collect(Collectors.toList()); |
| | | List<String> assigneeNames = createVO.getAssignees().stream() |
| | | .map(assignee -> assignee.getUserName()) |
| | | .collect(Collectors.toList()); |
| | | |
| | | eventPublisher.publishEvent(new TaskAssignedEvent( |
| | | this, |
| | | task.getTaskId(), |
| | | task.getTaskCode(), |
| | | assigneeIds, |
| | | assigneeNames, |
| | | SecurityUtils.getUserId(), |
| | | SecurityUtils.getUsername() |
| | | )); |
| | | } |
| | | |
| | | // 异步同步急救转运任务到旧系统 |
| | | if (result > 0 && "EMERGENCY_TRANSFER".equals(createVO.getTaskType()) && legacySystemSyncService != null) { |
| | | final Long finalTaskId = task.getTaskId(); |
| | | new Thread(() -> { |
| | | try { |
| | | Thread.sleep(2000); // 等待2秒,确保事务已提交 |
| | | legacySystemSyncService.syncEmergencyTaskToLegacy(finalTaskId); |
| | | } catch (Exception e) { |
| | | // 同步失败不影响主流程,仅记录日志 |
| | | } |
| | | }).start(); |
| | | } |
| | | |
| | | return result; |
| | |
| | | @Override |
| | | @Transactional |
| | | public int assignTask(Long taskId, Long assigneeId, String remark) { |
| | | SysTask task = new SysTask(); |
| | | task.setTaskId(taskId); |
| | | task.setAssigneeId(assigneeId); |
| | | task.setUpdateBy(SecurityUtils.getUsername()); |
| | | task.setUpdateTime(DateUtils.getNowDate()); |
| | | SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId); |
| | | if (task == null) { |
| | | throw new RuntimeException("任务不存在"); |
| | | } |
| | | |
| | | int result = sysTaskMapper.assignTask(task); |
| | | SysTask updateTask = new SysTask(); |
| | | updateTask.setTaskId(taskId); |
| | | updateTask.setAssigneeId(assigneeId); |
| | | updateTask.setUpdateBy(SecurityUtils.getUsername()); |
| | | updateTask.setUpdateTime(DateUtils.getNowDate()); |
| | | |
| | | int result = sysTaskMapper.assignTask(updateTask); |
| | | |
| | | // 记录操作日志 |
| | | if (result > 0) { |
| | | recordTaskLog(taskId, "ASSIGN", "分配任务", null, |
| | | "分配给用户ID:" + assigneeId + ",备注:" + remark, |
| | | SecurityUtils.getUserId(), SecurityUtils.getUsername()); |
| | | } |
| | | |
| | | // 发布任务分配事件 |
| | | if (result > 0) { |
| | | List<Long> assigneeIds = new ArrayList<>(); |
| | | assigneeIds.add(assigneeId); |
| | | |
| | | eventPublisher.publishEvent(new TaskAssignedEvent( |
| | | this, |
| | | task.getTaskId(), |
| | | task.getTaskCode(), |
| | | assigneeIds, |
| | | null, // 姓名列表在监听器中查询 |
| | | SecurityUtils.getUserId(), |
| | | SecurityUtils.getUsername() |
| | | )); |
| | | } |
| | | |
| | | return result; |
| | |
| | | locationLog); |
| | | } |
| | | |
| | | // 发布任务状态变更事件 |
| | | if (result > 0) { |
| | | // 查询任务的所有执行人 |
| | | List<SysTaskAssignee> assignees = sysTaskAssigneeMapper.selectSysTaskAssigneeByTaskId(taskId); |
| | | List<Long> assigneeIds = null; |
| | | if (assignees != null && !assignees.isEmpty()) { |
| | | assigneeIds = assignees.stream() |
| | | .map(SysTaskAssignee::getUserId) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | eventPublisher.publishEvent(new TaskStatusChangedEvent( |
| | | this, |
| | | oldTask.getTaskId(), |
| | | oldTask.getTaskCode(), |
| | | oldTaskStatus.getCode(), |
| | | newStatus.getCode(), |
| | | oldTaskStatus.getInfo(), |
| | | newStatus.getInfo(), |
| | | assigneeIds, |
| | | oldTask.getCreatorId() |
| | | )); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | |
| | | * |
| | | * @param taskId 任务ID |
| | | * @param file 文件 |
| | | * @param category 附件分类 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int uploadAttachment(Long taskId, MultipartFile file) { |
| | | public int uploadAttachment(Long taskId, MultipartFile file, String category) { |
| | | try { |
| | | // 上传文件 |
| | | String fileName = FileUploadUtils.upload("/task", file); |
| | |
| | | attachment.setFilePath(filePath); |
| | | attachment.setFileSize(file.getSize()); |
| | | attachment.setFileType(getFileType(file.getOriginalFilename())); |
| | | attachment.setAttachmentCategory(category); |
| | | attachment.setUploadTime(DateUtils.getNowDate()); |
| | | attachment.setUploadBy(SecurityUtils.getUsername()); |
| | | |
| | |
| | | |
| | | // 记录操作日志 |
| | | if (result > 0) { |
| | | String categoryDesc = getCategoryDesc(category); |
| | | recordTaskLog(taskId, "UPDATE", "上传附件", null, |
| | | "上传文件:" + file.getOriginalFilename(), |
| | | "上传文件:" + file.getOriginalFilename() + "(分类:" + categoryDesc + ")", |
| | | SecurityUtils.getUserId(), SecurityUtils.getUsername()); |
| | | } |
| | | |
| | | return result; |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("文件上传失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从微信mediaId上传任务附件 |
| | | * |
| | | * @param taskId 任务ID |
| | | * @param accessToken 微信AccessToken |
| | | * @param mediaId 微信mediaId |
| | | * @param category 附件分类 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int uploadAttachmentFromWechat(Long taskId, String accessToken, String mediaId, String category) { |
| | | try { |
| | | // 从微信服务器下载文件 |
| | | String wechatUrl = String.format( |
| | | "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s", |
| | | accessToken, mediaId |
| | | ); |
| | | |
| | | byte[] fileBytes = downloadFromUrl(wechatUrl); |
| | | if (fileBytes == null || fileBytes.length == 0) { |
| | | throw new RuntimeException("从微信下载文件失败"); |
| | | } |
| | | |
| | | // 生成文件名(使用mediaId作为文件名的一部分) |
| | | String fileName = "wx_" + mediaId.substring(0, Math.min(20, mediaId.length())) + "_" + System.currentTimeMillis() + ".jpg"; |
| | | |
| | | // 保存到本地 |
| | | String baseDir = FileUploadUtils.getDefaultBaseDir(); |
| | | String datePath = DateUtils.datePath(); |
| | | String uploadDir = baseDir + "/task/" + datePath; |
| | | |
| | | // 创建目录 |
| | | File uploadPath = new File(uploadDir); |
| | | if (!uploadPath.exists()) { |
| | | uploadPath.mkdirs(); |
| | | } |
| | | |
| | | // 保存文件 |
| | | String filePath = uploadDir + "/" + fileName; |
| | | File file = new File(filePath); |
| | | try (FileOutputStream fos = new FileOutputStream(file)) { |
| | | fos.write(fileBytes); |
| | | } |
| | | |
| | | // 保存附件记录 |
| | | SysTaskAttachment attachment = new SysTaskAttachment(); |
| | | attachment.setTaskId(taskId); |
| | | attachment.setFileName(fileName); |
| | | attachment.setFilePath(filePath); |
| | | attachment.setFileSize((long) fileBytes.length); |
| | | attachment.setFileType("jpg"); |
| | | attachment.setAttachmentCategory(category); |
| | | attachment.setUploadTime(DateUtils.getNowDate()); |
| | | attachment.setUploadBy(SecurityUtils.getUsername()); |
| | | |
| | | int result = sysTaskAttachmentMapper.insertSysTaskAttachment(attachment); |
| | | |
| | | // 记录操作日志 |
| | | if (result > 0) { |
| | | String categoryDesc = getCategoryDesc(category); |
| | | recordTaskLog(taskId, "UPDATE", "上传附件", null, |
| | | "通过微信上传文件:" + fileName + "(分类:" + categoryDesc + ")", |
| | | SecurityUtils.getUserId(), SecurityUtils.getUsername()); |
| | | } |
| | | |
| | | return result; |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("从微信上传文件失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从 URL 下载文件 |
| | | */ |
| | | private byte[] downloadFromUrl(String fileUrl) throws IOException { |
| | | URL url = new URL(fileUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("GET"); |
| | | connection.setConnectTimeout(10000); |
| | | connection.setReadTimeout(30000); |
| | | |
| | | try (InputStream inputStream = connection.getInputStream()) { |
| | | byte[] buffer = new byte[4096]; |
| | | int bytesRead; |
| | | java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream(); |
| | | |
| | | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | |
| | | return outputStream.toByteArray(); |
| | | } finally { |
| | | connection.disconnect(); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 根据ID获取附件详情 |
| | | * |
| | | * @param attachmentId 附件ID |
| | | * @return 附件详情 |
| | | */ |
| | | @Override |
| | | public SysTaskAttachment getAttachmentById(Long attachmentId) { |
| | | return sysTaskAttachmentMapper.selectSysTaskAttachmentByAttachmentId(attachmentId); |
| | | } |
| | | |
| | | /** |
| | |
| | | VehicleInfo queryParam = new VehicleInfo(); |
| | | queryParam.setStatus("0"); // 0表示正常状态 |
| | | // 不设置deptId,查询所有部门的车辆 |
| | | queryParam.setDeptId(deptId); |
| | | |
| | | List<VehicleInfo> vehicles = vehicleInfoMapper.selectVehicleInfoList(queryParam); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 保存任务执行人员信息(包含角色类型) |
| | | * |
| | | * @param taskId 任务ID |
| | | * @param assignees 执行人员信息列表 |
| | | */ |
| | | private void saveTaskAssignees(Long taskId, java.util.List<TaskCreateVO.AssigneeInfo> assignees) { |
| | | if (assignees == null || assignees.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | java.util.List<SysTaskAssignee> taskAssignees = new java.util.ArrayList<>(); |
| | | Date now = DateUtils.getNowDate(); |
| | | String currentUser = SecurityUtils.getUsername(); |
| | | |
| | | for (int i = 0; i < assignees.size(); i++) { |
| | | TaskCreateVO.AssigneeInfo assigneeInfo = assignees.get(i); |
| | | |
| | | SysTaskAssignee taskAssignee = new SysTaskAssignee(); |
| | | taskAssignee.setTaskId(taskId); |
| | | taskAssignee.setUserId(assigneeInfo.getUserId()); |
| | | taskAssignee.setUserName(assigneeInfo.getUserName()); |
| | | taskAssignee.setUserType(assigneeInfo.getUserType()); |
| | | // 第一个执行人员为主要执行人 |
| | | taskAssignee.setIsPrimary(i == 0 ? "1" : "0"); |
| | | taskAssignee.setSortOrder(i); |
| | | taskAssignee.setCreateTime(now); |
| | | taskAssignee.setCreateBy(currentUser); |
| | | taskAssignee.setUpdateTime(now); |
| | | taskAssignee.setUpdateBy(currentUser); |
| | | |
| | | taskAssignees.add(taskAssignee); |
| | | } |
| | | |
| | | // 批量保存 |
| | | if (!taskAssignees.isEmpty()) { |
| | | sysTaskAssigneeMapper.batchInsertSysTaskAssignee(taskAssignees); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存急救转运任务扩展信息 |
| | | * |
| | | * @param taskId 任务ID |
| | |
| | | |
| | | // 设置转出医院信息 |
| | | if (createVO.getHospitalOut() != null) { |
| | | emergencyInfo.setHospitalOutId(createVO.getHospitalOut().getId()); |
| | | emergencyInfo.setHospitalOutName(createVO.getHospitalOut().getName()); |
| | | emergencyInfo.setHospitalOutDepartment(createVO.getHospitalOut().getDepartment()); |
| | | emergencyInfo.setHospitalOutDepartmentId(createVO.getHospitalOut().getDepartmentId()); |
| | | emergencyInfo.setHospitalOutBedNumber(createVO.getHospitalOut().getBedNumber()); |
| | | emergencyInfo.setHospitalOutAddress(createVO.getHospitalOut().getAddress()); |
| | | emergencyInfo.setHospitalOutLongitude(createVO.getHospitalOut().getLongitude()); |
| | |
| | | |
| | | // 设置转入医院信息 |
| | | if (createVO.getHospitalIn() != null) { |
| | | emergencyInfo.setHospitalInId(createVO.getHospitalIn().getId()); |
| | | emergencyInfo.setHospitalInName(createVO.getHospitalIn().getName()); |
| | | emergencyInfo.setHospitalInDepartment(createVO.getHospitalIn().getDepartment()); |
| | | emergencyInfo.setHospitalInDepartmentId(createVO.getHospitalIn().getDepartmentId()); |
| | | emergencyInfo.setHospitalInBedNumber(createVO.getHospitalIn().getBedNumber()); |
| | | emergencyInfo.setHospitalInAddress(createVO.getHospitalIn().getAddress()); |
| | | emergencyInfo.setHospitalInLongitude(createVO.getHospitalIn().getLongitude()); |
| | |
| | | // 设置费用信息 |
| | | emergencyInfo.setTransferDistance(createVO.getTransferDistance()); |
| | | emergencyInfo.setTransferPrice(createVO.getPrice()); |
| | | |
| | | // 设置单据类型ID |
| | | emergencyInfo.setDocumentTypeId(createVO.getDocumentTypeId()); |
| | | |
| | | // 设置任务类型ID |
| | | emergencyInfo.setTaskTypeId(createVO.getTaskTypeId()); |
| | | |
| | | // 设置病情ID列表(将List<Long>转换为逗号分隔的字符串) |
| | | if (createVO.getDiseaseIds() != null && !createVO.getDiseaseIds().isEmpty()) { |
| | | String diseaseIdsStr = createVO.getDiseaseIds().stream() |
| | | .map(String::valueOf) |
| | | .collect(Collectors.joining(",")); |
| | | emergencyInfo.setDiseaseIds(diseaseIdsStr); |
| | | } |
| | | |
| | | // 系统字段 |
| | | emergencyInfo.setCreateTime(DateUtils.getNowDate()); |
| | |
| | | |
| | | sysTaskWelfareMapper.insertSysTaskWelfare(welfareInfo); |
| | | } |
| | | |
| | | /** |
| | | * 获取附件分类描述 |
| | | * |
| | | * @param category 附件分类代码 |
| | | * @return 分类描述 |
| | | */ |
| | | private String getCategoryDesc(String category) { |
| | | if (category == null || category.isEmpty()) { |
| | | return "未分类"; |
| | | } |
| | | switch (category) { |
| | | case "1": return "知情同意书"; |
| | | case "2": return "病人资料"; |
| | | case "3": return "操作记录"; |
| | | case "4": return "出车前"; |
| | | case "5": return "出车后"; |
| | | case "6": return "系安全带"; |
| | | default: return "其他"; |
| | | } |
| | | } |
| | | } |