From f67945d53b20f6a45ae50b27d74c966eb1355bb4 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 16 十一月 2025 22:53:54 +0800
Subject: [PATCH] feat: 增加分段GPS计算行程距离

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java |  274 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 255 insertions(+), 19 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
index ac521d2..796b509 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
@@ -1,14 +1,19 @@
 package com.ruoyi.system.service.impl;
 
+import java.io.*;
 import java.math.BigDecimal;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.stream.Collectors;
-import java.io.File;
-import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
 
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.system.mapper.*;
 import com.ruoyi.system.utils.TaskCodeGenerator;
+import com.ruoyi.common.config.ImageUrlConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -31,20 +36,15 @@
 import com.ruoyi.system.domain.vo.TaskUpdateVO;
 import com.ruoyi.system.domain.vo.TaskStatisticsVO;
 import com.ruoyi.system.domain.enums.TaskStatus;
-import com.ruoyi.system.mapper.SysTaskMapper;
-import com.ruoyi.system.mapper.SysTaskVehicleMapper;
-import com.ruoyi.system.mapper.SysTaskAttachmentMapper;
-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.service.ITaskAttachmentSyncService;
 import com.ruoyi.system.event.TaskCreatedEvent;
 import com.ruoyi.system.event.TaskAssignedEvent;
 import com.ruoyi.system.event.TaskStatusChangedEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationEventPublisher;
 
@@ -56,6 +56,8 @@
  */
 @Service
 public class SysTaskServiceImpl implements ISysTaskService {
+    
+    private static final Logger log = LoggerFactory.getLogger(SysTaskServiceImpl.class);
     
     @Autowired
     private SysTaskMapper sysTaskMapper;
@@ -86,6 +88,15 @@
     
     @Autowired
     private ApplicationEventPublisher eventPublisher;
+    
+    @Autowired
+    private ImageUrlConfig imageUrlConfig;
+    
+    @Autowired(required = false)
+    private ITaskAttachmentSyncService taskAttachmentSyncService;
+
+    @Autowired
+    private SysUserMapper sysUserMapper;
 
     /**
      * 鏌ヨ浠诲姟绠$悊
@@ -496,37 +507,183 @@
      * 
      * @param taskId 浠诲姟ID
      * @param file 鏂囦欢
+     * @param category 闄勪欢鍒嗙被
      * @return 缁撴灉
      */
     @Override
     @Transactional
-    public int uploadAttachment(Long taskId, MultipartFile file) {
+    public Long uploadAttachment(Long taskId, MultipartFile file, String category) {
         try {
-            // 涓婁紶鏂囦欢
-            String fileName = FileUploadUtils.upload("/task", file);
-            String filePath = FileUploadUtils.getDefaultBaseDir() + fileName;
+            // 涓婁紶鏂囦欢锛岃繑鍥炵浉瀵硅矾寰勶紙濡傦細/task/2025/01/15/xxx.jpg锛�
+            String fileName = category+"_"+System.currentTimeMillis()+"_"+file.getOriginalFilename();
+
+            fileName=saveLocalPath(fileName,file.getInputStream());
             
             SysTaskAttachment attachment = new SysTaskAttachment();
             attachment.setTaskId(taskId);
             attachment.setFileName(file.getOriginalFilename());
-            attachment.setFilePath(filePath);
+            // 淇濆瓨鐩稿璺緞锛屼笉鍖呭惈 baseDir
+            attachment.setFilePath(fileName);
             attachment.setFileSize(file.getSize());
             attachment.setFileType(getFileType(file.getOriginalFilename()));
+            attachment.setAttachmentCategory(category);
             attachment.setUploadTime(DateUtils.getNowDate());
             attachment.setUploadBy(SecurityUtils.getUsername());
             
-            int result = sysTaskAttachmentMapper.insertSysTaskAttachment(attachment);
+            Long result = sysTaskAttachmentMapper.insertSysTaskAttachment(attachment);
             
             // 璁板綍鎿嶄綔鏃ュ織
             if (result > 0) {
+                String categoryDesc = getCategoryDesc(category);
                 recordTaskLog(taskId, "UPDATE", "涓婁紶闄勪欢", null, 
-                             "涓婁紶鏂囦欢锛�" + file.getOriginalFilename(), 
+                             "涓婁紶鏂囦欢锛�" + file.getOriginalFilename() + "(鍒嗙被锛�" + categoryDesc + ")", 
                              SecurityUtils.getUserId(), SecurityUtils.getUsername());
+                
+
             }
             
-            return result;
+            return attachment.getAttachmentId();
         } catch (IOException e) {
             throw new RuntimeException("鏂囦欢涓婁紶澶辫触锛�" + e.getMessage());
+        }
+    }
+    
+    /**
+     * 浠庡井淇ediaId涓婁紶浠诲姟闄勪欢
+     * 
+     * @param taskId 浠诲姟ID
+     * @param accessToken 寰俊AccessToken
+     * @param mediaId 寰俊mediaId
+     * @param category 闄勪欢鍒嗙被
+     * @return 杩斿洖闄勪欢ID
+     */
+    @Override
+    @Transactional
+    public Long 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 relativeFilePath = saveLocalPath(fileName, fileBytes);
+
+            // 淇濆瓨闄勪欢璁板綍
+            SysTaskAttachment attachment = new SysTaskAttachment();
+            attachment.setTaskId(taskId);
+            attachment.setFileName(fileName);
+            // 淇濆瓨鐩稿璺緞
+            attachment.setFilePath(relativeFilePath);
+            attachment.setFileSize((long) fileBytes.length);
+            attachment.setFileType("jpg");
+            attachment.setAttachmentCategory(category);
+            attachment.setUploadTime(DateUtils.getNowDate());
+            attachment.setUploadBy(SecurityUtils.getUsername());
+            
+            Long result = sysTaskAttachmentMapper.insertSysTaskAttachment(attachment);
+            
+            // 璁板綍鎿嶄綔鏃ュ織
+            if (result > 0) {
+                String categoryDesc = getCategoryDesc(category);
+                recordTaskLog(taskId, "UPDATE", "涓婁紶闄勪欢", null, 
+                             "閫氳繃寰俊涓婁紶鏂囦欢锛�" + fileName + "(鍒嗙被锛�" + categoryDesc + ")", 
+                             SecurityUtils.getUserId(), SecurityUtils.getUsername());
+                
+
+            }
+            
+            return attachment.getAttachmentId();
+        } catch (Exception e) {
+            throw new RuntimeException("浠庡井淇′笂浼犳枃浠跺け璐ワ細" + e.getMessage());
+        }
+    }
+
+    private static String saveLocalPath(String fileName, byte[] fileBytes) throws IOException {
+        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);
+        }
+
+        // 鐢熸垚鐩稿璺緞锛堜笉鍖呭惈baseDir锛�
+        String relativeFilePath = "/task/" + datePath + "/" + fileName;
+        return relativeFilePath;
+    }
+
+    private String saveLocalPath(String fileName,InputStream stream){
+        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;
+       //灏唅nputstream鍐欏叆鏂囦欢
+        try (OutputStream os = new FileOutputStream(filePath)) {
+            byte[] buffer = new byte[1024]; // 缂撳啿鍖猴紝鍑忓皯 IO 娆℃暟
+            int bytesRead;
+            // 寰幆璇诲彇杈撳叆娴佷腑鐨勬暟鎹紝鍐欏叆杈撳嚭娴�
+            while ((bytesRead = stream.read(buffer)) != -1) {
+                os.write(buffer, 0, bytesRead);
+            }
+            os.flush(); // 寮哄埗鍒锋柊缂撳啿鍖猴紝纭繚鏁版嵁鍐欏叆鏂囦欢
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        // 鐢熸垚鐩稿璺緞锛堜笉鍖呭惈baseDir锛�
+        String relativeFilePath = "/task/" + datePath + "/" + fileName;
+        return relativeFilePath;
+    }
+
+    /**
+     * 浠� 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();
         }
     }
 
@@ -561,6 +718,27 @@
         }
         
         return result;
+    }
+    
+    /**
+     * 鏍规嵁ID鑾峰彇闄勪欢璇︽儏
+     * 
+     * @param attachmentId 闄勪欢ID
+     * @return 闄勪欢璇︽儏
+     */
+    @Override
+    public SysTaskAttachment getAttachmentById(Long attachmentId) {
+        SysTaskAttachment attachment = sysTaskAttachmentMapper.selectSysTaskAttachmentByAttachmentId(attachmentId);
+        if (attachment != null) {
+            // 鎷兼帴瀹屾暣URL
+            buildAttachmentUrl(attachment);
+        }
+        return attachment;
+    }
+
+    @Override
+    public List<SysTaskAttachment> getAttachmentsByTaskId(Long taskId) {
+        return sysTaskAttachmentMapper.selectSysTaskAttachmentByTaskId(taskId);
     }
 
     /**
@@ -761,7 +939,12 @@
             // 鏌ヨ鍏宠仈杞﹁締
             task.setAssignedVehicles(sysTaskVehicleMapper.selectSysTaskVehicleByTaskId(taskId));
             // 鏌ヨ闄勪欢
-            task.setAttachments(sysTaskAttachmentMapper.selectSysTaskAttachmentByTaskId(taskId));
+            List<SysTaskAttachment> attachments = sysTaskAttachmentMapper.selectSysTaskAttachmentByTaskId(taskId);
+            // 涓烘瘡涓檮浠舵嫾鎺ュ畬鏁碪RL
+            if (attachments != null && !attachments.isEmpty()) {
+                attachments.forEach(this::buildAttachmentUrl);
+            }
+            task.setAttachments(attachments);
             // 鏌ヨ鎿嶄綔鏃ュ織
             task.setOperationLogs(sysTaskLogMapper.selectSysTaskLogByTaskId(taskId));
             // 鍔犺浇鎬ユ晳杞繍鎵╁睍淇℃伅
@@ -776,6 +959,18 @@
             }
         }
         return task;
+    }
+
+    /**
+     * 妫�鏌ヨ溅杈嗘槸鍚︽湁姝e湪杩涜涓殑浠诲姟
+     * 姝e湪杩涜涓殑浠诲姟鏄寚鐘舵�佷笉涓猴細PENDING锛堝緟澶勭悊锛夈�丆OMPLETED锛堝凡瀹屾垚锛夈�丆ANCELLED锛堝凡鍙栨秷锛夌殑浠诲姟
+     * 
+     * @param vehicleId 杞﹁締ID
+     * @return 姝e湪杩涜涓殑浠诲姟鍒楄〃
+     */
+    @Override
+    public List<SysTask> checkVehicleActiveTasks(Long vehicleId) {
+        return sysTaskMapper.selectActiveTasksByVehicleId(vehicleId);
     }
 
     @Autowired
@@ -1055,4 +1250,45 @@
         
         sysTaskWelfareMapper.insertSysTaskWelfare(welfareInfo);
     }
+    
+    /**
+     * 鑾峰彇闄勪欢鍒嗙被鎻忚堪
+     * 
+     * @param category 闄勪欢鍒嗙被浠g爜
+     * @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 "鍏朵粬";
+        }
+    }
+    
+    /**
+     * 鏋勫缓闄勪欢鐨勫畬鏁碪RL
+     * 
+     * @param attachment 闄勪欢瀵硅薄
+     */
+    private void buildAttachmentUrl(SysTaskAttachment attachment) {
+        if (attachment != null && StringUtils.isNotEmpty(attachment.getFilePath())) {
+            String imageUrl = imageUrlConfig.getImageUrl();
+            if (StringUtils.isNotEmpty(imageUrl)) {
+                // 鎷兼帴瀹屾暣URL锛氬煙鍚� + 鐩稿璺緞
+                attachment.setFileUrl(imageUrl + attachment.getFilePath());
+            } else {
+                // 濡傛灉鏈厤缃煙鍚嶏紝鐩存帴浣跨敤鐩稿璺緞
+                attachment.setFileUrl(attachment.getFilePath());
+            }
+        }
+    }
+    
+   
 }

--
Gitblit v1.9.1