From e55c8b0a92eb9715edd90c31dfd4de51a47b588b Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期五, 04 十一月 2022 17:40:08 +0800
Subject: [PATCH] changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java |  192 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 171 insertions(+), 21 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java
index f8bcee9..bcf3a7d 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java
@@ -1,60 +1,210 @@
 package com.sandu.ximon.admin.service;
 
+import com.alibaba.fastjson.JSON;
+import com.alipay.service.schema.util.StringUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.github.pagehelper.PageHelper;
 import com.sandu.common.execption.BusinessException;
+import com.sandu.common.file.FileUploadDto;
+import com.sandu.common.object.BaseConditionVO;
 import com.sandu.common.service.impl.BaseServiceImpl;
+import com.sandu.ximon.admin.minio.BroadcastFileUtils;
+import com.sandu.ximon.admin.minio.entity.FileSuffix;
+import com.sandu.ximon.admin.minio.entity.MinIoConstant;
+import com.sandu.ximon.admin.minio.utils.MinIoUtil;
 import com.sandu.ximon.admin.param.IpVolumeFileParam;
-import com.sandu.ximon.admin.param.ReceiveParam;
 import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
 import com.sandu.ximon.dao.domain.IpVolumeFile;
+import com.sandu.ximon.dao.enums.OrderByEnums;
 import com.sandu.ximon.dao.mapper.IpVolumeFileMapper;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
 
 /**
  * @Author liuhaonan
  * @Date 2021/12/16 10:48
  * @Version 1.0
  */
+
 @Service
 @AllArgsConstructor
 public class IpVolumeFileService extends BaseServiceImpl<IpVolumeFileMapper, IpVolumeFile> {
     private final IpVolumeFileMapper ipVolumeFileMapper;
     private final ClientService clientService;
+    private MinIoUtil minIoUtil;
 
 
     public boolean addFile(IpVolumeFileParam fileParam) {
         IpVolumeFile file = new IpVolumeFile();
-        file.setUserId(SecurityUtils.getClientId());
-        if(SecurityUtils.getClientId()!=null){
-            file.setUserName(SecurityUtils.getUsername());
-        }
 
-        if(clientService.getClientId()!=null){
-            file.setClientId(clientService.getClientId());
-        }
+        file.setUserName(SecurityUtils.getUsername());
+        file.setUserId(SecurityUtils.getClientId());
+        file.setClientId(clientService.getClientId());
+
         file.setFileName(fileParam.getFileName());
-        file.setFileSize(fileParam.getFileSizeIp());
+        file.setFileUrl(fileParam.getFileUrl());
         file.setOriginSize(fileParam.getOriginSize());
+
+        /**
+         * 闊虫煴鏂囦欢鏂板 鏃ュ織璁板綍寮�濮�
+         */
+        String content = "闊虫煴鏂囦欢鍚嶇О锛�" + fileParam.getFileName() + "锛屾枃浠跺ぇ灏忥細" + fileParam.getOriginSize() + "锛屾枃浠惰矾寰勶細" + fileParam.getFileUrl();
+        StoreOperationRecordsUtils.storeOperationData(null, null, "闊虫煴鏂囦欢鏂板", content);
+        /**
+         * 闊虫煴鏂囦欢鏂板 鏃ュ織璁板綍缁撴潫
+         */
         return save(file);
     }
 
-    public boolean deleteFile(Long fileId) {
-        IpVolumeFile byId = getById(fileId);
-        if (byId == null) {
-            throw new BusinessException("鎵句笉鍒板搴旀枃浠�");
-        }
-        return removeById(fileId);
+    public boolean deleteFile(List<Long> fileIds) {
+        fileIds.forEach(fileId -> {
+            IpVolumeFile byId = getById(fileId);
+            String bucketName;
+            if (SecurityUtils.getClientId() == null) {
+                bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName();
+            } else {
+                bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId();
+            }
+            if (byId == null) {
+                throw new BusinessException("鎵句笉鍒板搴旀枃浠�");
+            }
+            boolean b = minIoUtil.deleteFile(bucketName, byId.getFileName());
+//        if (!b) {
+//            throw new BusinessException("鏈嶅姟鍣ㄥ垹闄ゅけ璐�");
+//        }
+            /**
+             * 闊虫煴鏂囦欢鍒犻櫎 鏃ュ織璁板綍寮�濮�
+             */
+            String content = "鍒犻櫎鏂囦欢id:" + fileId + "闊虫煴鏂囦欢鍚嶇О锛�" + byId.getFileName() + "锛屾枃浠跺ぇ灏忥細" + byId.getOriginSize() + "锛屾枃浠惰矾寰勶細" + byId.getFileUrl();
+            StoreOperationRecordsUtils.storeOperationData(null, null, "闊虫煴鏂囦欢鍒犻櫎", content);
+            /**
+             * 闊虫煴鏂囦欢鍒犻櫎 鏃ュ織璁板綍缁撴潫
+             */
+
+        });
+        return removeByIds(fileIds);
     }
 
-    public LambdaQueryWrapper<IpVolumeFile> listFile(ReceiveParam receiveParam) {
-        LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class).eq(IpVolumeFile::getClientId, clientService.getClientId());
-        if (receiveParam.getKeyWord() == null) {
-            return wrapper;
-        } else {
-          return   wrapper.like(IpVolumeFile::getFileName,receiveParam.getKeyWord());
+    public List<IpVolumeFile> ipVolumeFileList(BaseConditionVO baseConditionVO, String keyword, Integer order, Integer seq) {
+        LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class);
+        //涓嶆槸瓒呯
+        if (SecurityUtils.getClientId() != null) {
+            wrapper = wrapper.eq(IpVolumeFile::getClientId, SecurityUtils.getUserId())
+                    .or(file -> {
+                        file.eq(IpVolumeFile::getUserId, SecurityUtils.getUserId());
+                    });
         }
+
+        if (keyword != null && !keyword.isEmpty()) {
+            wrapper.like(IpVolumeFile::getFileName, keyword);
+        }
+
+        //鎺掑簭瀛楁
+        String orderByResult = "id";
+        //姝e簭銆佸�掑彊
+        String orderBySeq = OrderByEnums.ASC.getCode();
+        if (order != null) {
+            switch (order) {
+                case 1:
+                    orderByResult = OrderByEnums.IP_VOLUME_FILE_CREATE_TIME.getCode();
+                    break;
+                default:
+            }
+        }
+        if (seq != null) {
+            switch (seq) {
+                case 1:
+                    orderBySeq = OrderByEnums.ASC.getCode();
+                    break;
+                case 2:
+                    orderBySeq = OrderByEnums.DESC.getCode();
+                    break;
+                default:
+                    break;
+            }
+        }
+        //鎺掑簭鏂瑰紡
+        String orderBy = orderByResult + " " + orderBySeq;
+
+        if (baseConditionVO != null) {
+            PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize(), orderBy);
+        }
+        List<IpVolumeFile> list = list(wrapper);
+
+        return list;
+    }
+
+    public List<IpVolumeFile> listFiles(String keyword) {
+        LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class);
+
+        //涓嶆槸瓒呯
+        if (SecurityUtils.getClientId() != null) {
+            wrapper = wrapper.eq(IpVolumeFile::getClientId, SecurityUtils.getUserId())
+                    .or(file -> {
+                        file.eq(IpVolumeFile::getUserId, SecurityUtils.getUserId());
+                    });
+        }
+        if (keyword != null && !StringUtil.isEmpty(keyword)) {
+            wrapper.like(IpVolumeFile::getFileName, keyword);
+        }
+        return list(wrapper);
+    }
+
+
+    /**
+     * 鏂囦欢涓婁紶
+     *
+     * @param file
+     * @param
+     * @return
+     */
+    public FileUploadDto upload(MultipartFile file) {
+        log.error("闊虫煴鏂囦欢涓婁紶锛�(filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+        //  鏍¢獙鏄惁涓簃p3鏂囦欢
+        if (!BroadcastFileUtils.isMp3File(file.getOriginalFilename(), FileSuffix.MP3)) {
+            log.error("闊虫煴鏂囦欢涓婁紶锛�(涓婁紶澶辫触, 闈濵P3鏂囦欢) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+            throw new BusinessException("涓婁紶澶辫触, 闈濵P3鏂囦欢");
+        }
+        //  鍒ゆ柇璇ユ枃浠舵槸鍚﹀瓨鍦�
+        String bucketName;
+        if (SecurityUtils.getClientId() == null) {
+            bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName();
+        } else {
+            bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId();
+        }
+        if (minIoUtil.objectExists(bucketName, file.getOriginalFilename())) {
+            log.error("闊虫煴鏂囦欢涓婁紶锛�(涓婁紶澶辫触, 鍚屽悕鏂囦欢宸插瓨鍦�) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+            throw new BusinessException("涓婁紶澶辫触, 鍚屽悕鏂囦欢宸插瓨鍦�");
+        }
+
+
+        String upload = minIoUtil.upload(bucketName, file);
+
+        if (null == upload) {
+            log.error("闊虫煴鏂囦欢涓婁紶锛�(涓婁紶澶辫触, 鏂囦欢鏈嶅姟鍣ㄥ嚭閿�) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+            throw new BusinessException("涓婁紶澶辫触, 鏂囦欢鏈嶅姟鍣ㄥ嚭閿�");
+        }
+        FileUploadDto fileUploadDto = new FileUploadDto();
+        fileUploadDto.setFileUrl(upload);
+        fileUploadDto.setFileSize(file.getSize());
+        fileUploadDto.setFileName(file.getOriginalFilename());
+        fileUploadDto.setFileType(file.getContentType());
+
+        /**
+         * 闊虫煴鏂囦欢涓婁紶 鏃ュ織璁板綍寮�濮�
+         */
+        String content = "鏂囦欢鍐呭:" + JSON.toJSONString(fileUploadDto);
+        StoreOperationRecordsUtils.storeOperationData(null, null, "闊虫煴鏂囦欢涓婁紶", content);
+        /**
+         * 闊虫煴鏂囦欢涓婁紶 鏃ュ織璁板綍缁撴潫
+         */
+
+        return fileUploadDto;
     }
 
 }

--
Gitblit v1.9.3