package com.ruoyi.web.controller.task; import java.util.Date; import java.util.List; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import com.ruoyi.common.utils.WechatUtils; import com.ruoyi.common.config.WechatConfig; import com.ruoyi.system.domain.SysTask; import com.ruoyi.system.domain.SysTaskEmergency; import com.ruoyi.system.service.ISysTaskEmergencyService; import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.ITaskAttachmentSyncService; import com.ruoyi.system.task.ITaskAttachmentService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.SysTaskAttachment; import com.ruoyi.system.service.ISysTaskService; /** * 任务附件Controller * * @author ruoyi * @date 2024-01-15 */ @RestController @RequestMapping("/task/attachment") public class SysTaskAttachmentController extends BaseController { @Autowired private ISysTaskService sysTaskService; @Autowired private ISysUserService sysUserService; @Autowired private ITaskAttachmentSyncService taskAttachmentSyncService; @Autowired private ITaskAttachmentService taskAttachmentService; @Autowired private WechatConfig wechatConfig; @Autowired private ISysTaskEmergencyService sysTaskEmergencyService; @Autowired private ISysUserService userService; /** * 查询任务附件列表 */ @GetMapping("/list/{taskId}") public AjaxResult list(@PathVariable("taskId") Long taskId) { SysTask task = sysTaskService.getTaskDetail(taskId); return success(task.getAttachments()); } /** * 上传任务附件 */ @Log(title = "任务附件", businessType = BusinessType.INSERT) @PostMapping("/upload/{taskId}") public AjaxResult upload(@PathVariable("taskId") Long taskId, @RequestParam("file") MultipartFile file, @RequestParam(value = "category", required = true) String category) { try { Long attachmentId= sysTaskService.uploadAttachment(taskId, file, category); if (attachmentId > 0) { //在这里进行同步 SyncAttachmentToImageData(taskId,sysTaskService.getTaskDetail(taskId).getCreatorId(),attachmentId); return success("上传成功"); } else { return error("上传失败"); } } catch (Exception e) { return error("上传失败:" + e.getMessage()); } } private void SyncAttachmentToImageData(Long taskId,Long taskCreatorId, Long attachmentId) { Integer oaUserId= sysUserService.selectUserById(taskCreatorId).getOaUserId(); SysTaskEmergency taskEmergency= sysTaskEmergencyService.selectSysTaskEmergencyByTaskId(taskId); if(taskEmergency!=null ){ Long serviceOrderId= taskEmergency.getLegacyServiceOrdId(); Long dispathOrdId=taskEmergency.getLegacyDispatchOrdId(); if(serviceOrderId!=null && dispathOrdId!=null){ SysTaskAttachment attachment=sysTaskService.getAttachmentById(attachmentId); Long imageDataId= taskAttachmentSyncService.syncAttachmentToImageData(attachment, serviceOrderId, dispathOrdId,oaUserId); //同步成功 if(imageDataId>0){ attachment.setSyncedToImageData(1); attachment.setSyncTime(new Date()); attachment.setImageDataId(imageDataId); taskAttachmentService.updateAttachment(attachment); } } } } /** * 删除任务附件 */ @Log(title = "任务附件", businessType = BusinessType.DELETE) @DeleteMapping("/{attachmentId}") public AjaxResult remove(@PathVariable("attachmentId") Long attachmentId) { try { int result = sysTaskService.deleteAttachment(attachmentId); if (result > 0) { return success("删除成功"); } else { return error("删除失败"); } } catch (Exception e) { return error("删除失败:" + e.getMessage()); } } /** * 从微信mediaId上传附件(微信小程序专用) */ @Log(title = "任务附件", businessType = BusinessType.INSERT) @PostMapping("/uploadFromWechat/{taskId}") public AjaxResult uploadFromWechat(@PathVariable("taskId") Long taskId, @RequestParam("mediaId") String mediaId, @RequestParam(value = "category", required = false) String category) { try { // 获取微信AccessToken String accessToken = WechatUtils.getAccessToken( wechatConfig.getAppId(), wechatConfig.getAppSecret() ); if (accessToken == null || accessToken.isEmpty()) { return error("获取微信AccessToken失败"); } // 通过mediaId上传附件 Long attachmentId = sysTaskService.uploadAttachmentFromWechat(taskId, accessToken, mediaId, category); SysTask task=sysTaskService.getTaskDetail(taskId); if (attachmentId > 0) { this.SyncAttachmentToImageData(taskId,task.getCreatorId(),attachmentId); return success("上传成功"); } else { return error("上传失败"); } } catch (Exception e) { return error("上传失败:" + e.getMessage()); } } /** * 下载附件 */ @GetMapping("/download/{attachmentId}") public void downloadAttachment(@PathVariable("attachmentId") Long attachmentId, HttpServletResponse response) { try { SysTaskAttachment attachment = sysTaskService.getAttachmentById(attachmentId); if (attachment == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } File file = new File(attachment.getFilePath()); if (!file.exists()) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } // 设置响应头 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + attachment.getFileName()); response.setContentLengthLong(file.length()); // 读取文件并输出 try (FileInputStream fis = new FileInputStream(file); OutputStream os = response.getOutputStream()) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.flush(); } } catch (Exception e) { logger.error("下载附件失败", e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } }