Преглед изворни кода

试玩模块创建和优化5

zhaoxian пре 4 година
родитељ
комит
439eb2ec17

+ 3 - 8
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/controller/ShiwanFileUploadController.java

@@ -105,15 +105,10 @@ public class ShiwanFileUploadController {
 
 
     @PostMapping("/createNewZipFile")
-    public Result<Object> createNewZipFile(@RequestBody MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType) {
+    public Result<Object> createNewZipFile(@RequestBody MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType, String isCreate, String newFileName) {
         Result<Object> result = new Result<>();
         try {
-            ShiwanFileUpload entity = shiwanFileUploadService.createNewZipFile(file, id, updateName, userId, accountId, fileType);
-            if (Check.isNull(entity)) {
-                result = Result.error("上传失败");
-            }
-            Object obj = entity;
-            result = Result.ok(obj);
+            return shiwanFileUploadService.createNewZipFile(file, id, updateName, userId, accountId, fileType, isCreate, newFileName);
         } catch (Exception e) {
             log.error("生成新的试玩素材ZIP包异常", e);
             result = Result.error(-1, "上传失败");
@@ -123,7 +118,7 @@ public class ShiwanFileUploadController {
 
     @GetMapping("/selectListByParentId")
     public Result<List<ShiwanFileUpload>> selectListByParentId(String id) {
-        Result<List<ShiwanFileUpload>> result = new Result<List<ShiwanFileUpload>>();
+        Result<List<ShiwanFileUpload>> result = new Result<>();
         ShiwanFileUpload shiwanFileUploadEntity = shiwanFileUploadService.getById(id);
         if (shiwanFileUploadEntity == null) {
             result.error500("未找到对应实体");

+ 1 - 0
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/mapper/xml/ShiwanFileUploadMapper.xml

@@ -26,6 +26,7 @@
         <if test="signature != null and signature != '' ">
             and signature = #{signature}
         </if>
+        LIMIT 1
     </select>
     <select id="selectListByParentId" resultType="cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload">
         SELECT

+ 47 - 18
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/service/ShiwanFileUploadServiceImpl.java

@@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.xxl.job.core.enums.NoEn;
 import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -120,7 +121,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
         InputStream inputStream = null;
         try {
             inputStream = file.getInputStream();
-            resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, existFiles.getCfileName(), existFiles.getFileSuffix(), file.getSize(), url);
+            resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, existFiles.getFileName(), existFiles.getFileSuffix(), file.getSize(), url);
         } catch (IOException e) {
             log.error("更新文件,上传COS失败", e);
         } finally {
@@ -132,6 +133,8 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
         queryWrapper.eq("id", existFiles.getId());
         existFiles.setFileSize(resultFile.getFileSize() + "B");
         existFiles.setUploadTime(new Date());
+        existFiles.setFileUrl(resultFile.getFileUrl());
+        existFiles.setCfileName(resultFile.getCFileName());
         shiwanFileUploadMapper.update(existFiles, queryWrapper);
         shiwanFileUploadMapper.deleteByParentId(existFiles.getId());
         return existFiles;
@@ -187,14 +190,17 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
 
 
     @Override
-    public ShiwanFileUpload createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType) throws IOException {
-        ShiwanFileUpload fileUpload = shiwanFileUploadMapper.selectById(id);
+    public Result<Object> createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType, String isCreate, String newFileName) throws IOException {
+        ShiwanFileUpload oldFile = shiwanFileUploadMapper.selectById(id);
+        if (Check.isNull(oldFile)) {
+            return Result.error("查询原文件数据失败");
+        }
         StringBuffer path = new StringBuffer();
         String uuid = UUID.randomUUID().toString();
         path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(uuid).append("/");
         log.info("----------服务器下载地址:{}", path.toString());
         //解压zip文件
-        List<String> urlList = unzipFiles(fileUpload, path.toString(), null);
+        List<String> urlList = unzipFiles(oldFile, path.toString(), null);
         String newFilePath = "";
         if (null != urlList && urlList.size() > 0) {
             Iterator it = urlList.iterator();
@@ -210,12 +216,27 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
                 }
             }
         }
+        if (Check.isNull(newFilePath)) {
+            return Result.error("被替换文件名称有误");
+        }
         //添加上新的对象
         String newUrl = FileUtil.approvalFile(file, newFilePath);
         urlList.add(newUrl);
         //压缩zip文件
-        String newFileName = DateUtils.getNowDate(DateUtils.LONG_FORMAT).concat("_COPY-FROM_").concat(fileUpload.getFileName());
-        return createNewZipFiles(urlList, path.toString(), newFileName, updateName, userId, accountId, fileType);
+        if (Check.isNull(newFileName) && NoEn.NO1.valueStr().equals(isCreate)) {
+            newFileName = DateUtils.getNowDate(DateUtils.LONG_FORMAT).concat("_COPY-FROM_").concat(oldFile.getFileName());
+        }
+        ShiwanFileUpload newFile = new ShiwanFileUpload();
+        newFile.setUserId(userId);
+        newFile.setAccountId(accountId);
+        newFile.setFileType(fileType);
+        newFile.setFileName(newFileName);
+        oldFile.setUserId(userId);
+        oldFile.setAccountId(accountId);
+        oldFile.setFileType(fileType);
+        oldFile.setFileName(newFileName);
+        ShiwanFileUpload newZipFiles = createNewZipFiles(urlList, path.toString(), newFile, isCreate, oldFile);
+        return Result.ok(newZipFiles);
     }
 
     @Override
@@ -291,22 +312,30 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
      * @throws
      * @author ZHAOXA
      */
-    private ShiwanFileUpload createNewZipFiles(List<String> list, String path, String zipfilename, String updateName, String userId, Long accountId, String fileType) throws IOException {
-        String zipUrl = FileUtil.zipCompress(path.concat(zipfilename), new File(path.concat("innerFiles/")));
-        ShiwanFileUpload shiwanFileUpload = new ShiwanFileUpload();
-        shiwanFileUpload.setUserId(userId);
-        shiwanFileUpload.setAccountId(accountId);
-        shiwanFileUpload.setFileType(fileType);
+    private ShiwanFileUpload createNewZipFiles(List<String> list, String path, ShiwanFileUpload addFile, String isCreate, ShiwanFileUpload updateFile) throws IOException {
+        String zipUrl = FileUtil.zipCompress(path.concat(addFile.getFileName()), new File(path.concat("innerFiles/")));
         try {
-            shiwanFileUpload.setSignature(LoadFileUtil.getMD5(zipUrl));
+            String md5 = LoadFileUtil.getMD5(zipUrl);
+            addFile.setSignature(md5);
+            updateFile.setSignature(md5);
         } catch (IOException e) {
             log.error("获取MD5签名失败", e);
         }
-        //存储新ZIP包
-        ShiwanFileUpload newZipEntity = saveFile(FileUtil.getMulFileByPath(zipUrl), shiwanFileUpload, null);
-        deletelocalFiles(zipUrl);
-        if (!Check.isNull(newZipEntity)) {
-            saveInnerFiles(newZipEntity, list);
+        ShiwanFileUpload newZipEntity = null;
+        //判断是新增还是更新
+        if (NoEn.NO1.valueStr().equals(isCreate)) {
+            //存储新ZIP包
+            newZipEntity = saveFile(FileUtil.getMulFileByPath(zipUrl), addFile, null);
+            deletelocalFiles(zipUrl);
+            if (!Check.isNull(newZipEntity)) {
+                saveInnerFiles(newZipEntity, list);
+            }
+        } else {
+            newZipEntity = updateFile(FileUtil.getMulFileByPath(zipUrl), updateFile);
+            deletelocalFiles(zipUrl);
+            if (!Check.isNull(newZipEntity)) {
+                saveInnerFiles(newZipEntity, list);
+            }
         }
         return newZipEntity;
     }

+ 2 - 1
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/service/impl/IShiwanFileUploadService.java

@@ -2,6 +2,7 @@ package cn.com.ctop.shiwan.modules.service;
 
 import cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload;
 import com.baomidou.mybatisplus.extension.service.IService;
+import org.jeecg.common.api.vo.Result;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
@@ -33,7 +34,7 @@ public interface IShiwanFileUploadService extends IService<ShiwanFileUpload> {
      * @throws
      * @author ZHAOXA
      */
-    ShiwanFileUpload createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType) throws IOException;
+    Result<Object> createNewZipFile(MultipartFile file, String id, String updateName, String userId, Long accountId, String fileType, String isCreate, String newFileName) throws IOException;
 
     List<ShiwanFileUpload> selectListByParentId(String id);
 }