Browse Source

feat: 投诉接口

xdd 3 tuần trước cách đây
mục cha
commit
382c352659

+ 205 - 0
fs-common/src/main/java/com/fs/common/utils/FileNameExtractor.java

@@ -0,0 +1,205 @@
+package com.fs.common.utils;
+
+import org.apache.commons.io.FilenameUtils;
+import java.net.URL;
+import java.net.MalformedURLException;
+
+public class FileNameExtractor {
+
+    /**
+     * 使用Apache Commons IO获取文件名
+     * @param urlString URL字符串
+     * @return 文件名
+     */
+    public static String getFileNameFromUrl(String urlString) {
+        if (urlString == null || urlString.trim().isEmpty()) {
+            return null;
+        }
+
+        try {
+            URL url = new URL(urlString);
+            String path = url.getPath();
+            return FilenameUtils.getName(path);
+        } catch (MalformedURLException e) {
+            // 作为普通路径处理
+            return FilenameUtils.getName(urlString);
+        }
+    }
+
+    /**
+     * 获取不带扩展名的文件名
+     * @param urlString URL字符串
+     * @return 不带扩展名的文件名
+     */
+    public static String getBaseNameFromUrl(String urlString) {
+        if (urlString == null || urlString.trim().isEmpty()) {
+            return null;
+        }
+
+        try {
+            URL url = new URL(urlString);
+            String path = url.getPath();
+            return FilenameUtils.getBaseName(path);
+        } catch (MalformedURLException e) {
+            return FilenameUtils.getBaseName(urlString);
+        }
+    }
+
+    /**
+     * 获取文件扩展名
+     * @param urlString URL字符串
+     * @return 文件扩展名
+     */
+    public static String getExtensionFromUrl(String urlString) {
+        if (urlString == null || urlString.trim().isEmpty()) {
+            return null;
+        }
+
+        try {
+            URL url = new URL(urlString);
+            String path = url.getPath();
+            return FilenameUtils.getExtension(path);
+        } catch (MalformedURLException e) {
+            return FilenameUtils.getExtension(urlString);
+        }
+    }
+
+    /**
+     * 从URL或文件路径中获取扩展名
+     * @param urlOrPath URL字符串或文件路径
+     * @return 扩展名(不包含点号),如果没有扩展名则返回null
+     */
+    public static String getExtension(String urlOrPath) {
+        if (urlOrPath == null || urlOrPath.trim().isEmpty()) {
+            return null;
+        }
+
+        String fileName = getFileNameFromUrlOrPath(urlOrPath);
+        if (fileName == null || fileName.isEmpty()) {
+            return null;
+        }
+
+        // 查找最后一个点的位置
+        int lastDotIndex = fileName.lastIndexOf('.');
+
+        // 如果没有点,或者点在开头(隐藏文件),或者点在结尾,则没有扩展名
+        if (lastDotIndex <= 0 || lastDotIndex == fileName.length() - 1) {
+            return null;
+        }
+
+        return fileName.substring(lastDotIndex + 1).toLowerCase();
+    }
+
+    /**
+     * 从URL或文件路径中获取扩展名(包含点号)
+     * @param urlOrPath URL字符串或文件路径
+     * @return 扩展名(包含点号),如果没有扩展名则返回null
+     */
+    public static String getExtensionWithDot(String urlOrPath) {
+        String extension = getExtension(urlOrPath);
+        return extension != null ? "." + extension : null;
+    }
+
+    /**
+     * 检查文件是否具有指定的扩展名
+     * @param urlOrPath URL字符串或文件路径
+     * @param expectedExtension 期望的扩展名(可以带点号也可以不带)
+     * @return 是否匹配
+     */
+    public static boolean hasExtension(String urlOrPath, String expectedExtension) {
+        if (expectedExtension == null) {
+            return false;
+        }
+
+        String actualExtension = getExtension(urlOrPath);
+        if (actualExtension == null) {
+            return false;
+        }
+
+        // 处理期望扩展名可能带点号的情况
+        String cleanExpectedExtension = expectedExtension.startsWith(".") ?
+                expectedExtension.substring(1) : expectedExtension;
+
+        return actualExtension.equalsIgnoreCase(cleanExpectedExtension);
+    }
+
+    /**
+     * 检查文件是否是图片类型
+     * @param urlOrPath URL字符串或文件路径
+     * @return 是否是图片文件
+     */
+    public static boolean isImageFile(String urlOrPath) {
+        String extension = getExtension(urlOrPath);
+        if (extension == null) {
+            return false;
+        }
+
+        String[] imageExtensions = {"jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "ico"};
+        for (String imgExt : imageExtensions) {
+            if (extension.equalsIgnoreCase(imgExt)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 检查文件是否是文档类型
+     * @param urlOrPath URL字符串或文件路径
+     * @return 是否是文档文件
+     */
+    public static boolean isDocumentFile(String urlOrPath) {
+        String extension = getExtension(urlOrPath);
+        if (extension == null) {
+            return false;
+        }
+
+        String[] docExtensions = {"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "rtf"};
+        for (String docExt : docExtensions) {
+            if (extension.equalsIgnoreCase(docExt)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 从URL或路径中提取文件名的辅助方法
+     */
+    private static String getFileNameFromUrlOrPath(String urlOrPath) {
+        try {
+            java.net.URL url = new java.net.URL(urlOrPath);
+            String path = url.getPath();
+
+            // 处理查询参数,移除?后面的内容
+            int queryIndex = path.indexOf('?');
+            if (queryIndex > 0) {
+                path = path.substring(0, queryIndex);
+            }
+
+            if (path.endsWith("/")) {
+                path = path.substring(0, path.length() - 1);
+            }
+
+            int lastSlashIndex = path.lastIndexOf('/');
+            return lastSlashIndex >= 0 ? path.substring(lastSlashIndex + 1) : path;
+
+        } catch (java.net.MalformedURLException e) {
+            // 作为普通路径处理
+            String path = urlOrPath.trim();
+
+            // 处理查询参数
+            int queryIndex = path.indexOf('?');
+            if (queryIndex > 0) {
+                path = path.substring(0, queryIndex);
+            }
+
+            if (path.endsWith("/") || path.endsWith("\\")) {
+                path = path.substring(0, path.length() - 1);
+            }
+
+            int lastSlashIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
+            return lastSlashIndex >= 0 ? path.substring(lastSlashIndex + 1) : path;
+        }
+    }
+}

+ 1 - 1
fs-service/src/main/java/com/fs/complaint/dto/SubmitComplaintDTO.java

@@ -10,7 +10,7 @@ public class SubmitComplaintDTO implements Serializable {
     /**
      * 投诉类型
      */
-    private List<Long> type;
+    private Long type;
 
     /**
      * 投诉类型

+ 14 - 0
fs-service/src/main/java/com/fs/complaint/mapper/FsComplaintAttachmentMapper.java

@@ -42,4 +42,18 @@ public interface FsComplaintAttachmentMapper {
      */
     @Update("DELETE FROM fs_complaint_attachment WHERE complaint_id = #{complaintId}")
     int deleteByComplaintId(Long complaintId);
+
+
+    /**
+     * 批量插入附件信息
+     */
+    @Insert("<script>" +
+            "INSERT INTO fs_complaint_attachment(complaint_id, file_name, file_path, file_size, file_type) VALUES " +
+            "<foreach collection='list' item='item' separator=','>" +
+            "(#{item.complaintId}, #{item.fileName}, #{item.filePath}, #{item.fileSize}, #{item.fileType})" +
+            "</foreach>" +
+            "</script>")
+    @Options(useGeneratedKeys = true, keyProperty = "id")
+    int batchInsert(@Param("list") List<FsComplaintAttachment> attachmentList);
+
 }

+ 44 - 0
fs-service/src/main/java/com/fs/complaint/service/impl/FsComplaintServiceImpl.java

@@ -1,17 +1,61 @@
 package com.fs.complaint.service.impl;
 
+import cn.hutool.core.lang.Snowflake;
+import cn.hutool.core.util.IdUtil;
+import com.fs.common.utils.FileNameExtractor;
+import com.fs.complaint.domain.FsComplaint;
+import com.fs.complaint.domain.FsComplaintAttachment;
 import com.fs.complaint.dto.SubmitComplaintDTO;
+import com.fs.complaint.mapper.FsComplaintAttachmentMapper;
 import com.fs.complaint.mapper.FsComplaintMapper;
 import com.fs.complaint.service.FsComplaintService;
+import org.bouncycastle.oer.its.etsi102941.Url;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
 
 @Service
 public class FsComplaintServiceImpl implements FsComplaintService {
     @Autowired
     private FsComplaintMapper fsComplaintMapper;
+    @Autowired
+    private FsComplaintAttachmentMapper fsComplaintAttachmentMapper;
+
     @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
     public void submitComplaint(SubmitComplaintDTO dto) {
+        FsComplaint fsComplaint = new FsComplaint();
+        String complaintNo = IdUtil.getSnowflake(0, 0).nextIdStr();
+
+        fsComplaint.setComplaintNo(complaintNo);
+        fsComplaint.setCategoryId(dto.getType());
+        fsComplaint.setContent(dto.getContent());
+        fsComplaint.setContactPhone(dto.getContact());
+        fsComplaint.setCreateTime(LocalDateTime.now());
+        fsComplaint.setUpdateTime(LocalDateTime.now());
+        fsComplaint.setStatus(1);
+
+        fsComplaintMapper.insert(fsComplaint);
+        List<String> urls = dto.getUrl();
+
+        List<FsComplaintAttachment> attachments = new ArrayList<>();
+        for (String url : urls) {
+            FsComplaintAttachment attachment = new FsComplaintAttachment();
+            attachment.setComplaintId(fsComplaint.getId());
+            attachment.setCreateTime(LocalDateTime.now());
 
+            String fileNameFromUrl = FileNameExtractor.getFileNameFromUrl(url);
+            attachment.setFileName(fileNameFromUrl);
+            attachment.setFilePath(url);
+            attachment.setFileType(FileNameExtractor.getExtensionFromUrl(url));
+            attachment.setCreateTime(LocalDateTime.now());
+            attachments.add(attachment);
+        }
+        fsComplaintAttachmentMapper.batchInsert(attachments);
     }
 }

+ 0 - 1
fs-user-app/src/main/java/com/fs/app/controller/FsComplaintController.java

@@ -44,5 +44,4 @@ public class FsComplaintController {
         return R.ok();
     }
 
-
 }