wlzboy
6 天以前 09e6dc3fb7266620fafb5e341808a8eb36e080a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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;
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;
 
    @Autowired
    private IWechatAccessTokenService wechatAccessTokenService;
    /**
     * 查询任务附件列表
     */
    @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上传附件(微信小程序专用)
     * 使用应用级缓存的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);
        }
    }
}