| | |
| | | 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.service.IWechatAccessTokenService; |
| | | 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; |
| | |
| | | @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; |
| | | |
| | | @Autowired |
| | | private IWechatAccessTokenService wechatAccessTokenService; |
| | | /** |
| | | * 查询任务附件列表 |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('task:general:query')") |
| | | @GetMapping("/list/{taskId}") |
| | | public AjaxResult list(@PathVariable("taskId") Long taskId) { |
| | | SysTask task = sysTaskService.getTaskDetail(taskId); |
| | |
| | | /** |
| | | * 上传任务附件 |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('task:general:edit')") |
| | | |
| | | @Log(title = "任务附件", businessType = BusinessType.INSERT) |
| | | @PostMapping("/upload/{taskId}") |
| | | public AjaxResult upload(@PathVariable("taskId") Long taskId, @RequestParam("file") MultipartFile file) { |
| | | public AjaxResult upload(@PathVariable("taskId") Long taskId, |
| | | @RequestParam("file") MultipartFile file, |
| | | @RequestParam(value = "category", required = true) String category) { |
| | | try { |
| | | int result = sysTaskService.uploadAttachment(taskId, file); |
| | | if (result > 0) { |
| | | Long attachmentId= sysTaskService.uploadAttachment(taskId, file, category); |
| | | if (attachmentId > 0) { |
| | | //在这里进行同步 |
| | | SyncAttachmentToImageData(taskId,sysTaskService.getTaskDetail(taskId).getCreatorId(),attachmentId); |
| | | return success("上传成功"); |
| | | } else { |
| | | return error("上传失败"); |
| | |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * 删除任务附件 |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('task:general:edit')") |
| | | @Log(title = "任务附件", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{attachmentId}") |
| | | public AjaxResult remove(@PathVariable("attachmentId") Long attachmentId) { |
| | |
| | | return error("删除失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从微信mediaId上传附件(微信小程序专用) |
| | | * 使用应用级缓存的AccessToken |
| | | */ |
| | | @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 = wechatAccessTokenService.getAppAccessToken(); |
| | | 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); |
| | | } |
| | | } |
| | | } |