瀏覽代碼

fix: 批量添加材质,分享中心功能

liaoyitao 8 月之前
父節點
當前提交
5ec4422335

+ 3 - 3
src/main/java/com/lqkj/link/module/zone/controller/MaterialInfoController.java

@@ -66,12 +66,12 @@ public class MaterialInfoController {
66
 
66
 
67
     /**
67
     /**
68
      * 批量添加材质
68
      * 批量添加材质
69
-     * @param file
69
+     * @param compressFilePath
70
      * @return
70
      * @return
71
      */
71
      */
72
     @PostMapping("/batchAdd")
72
     @PostMapping("/batchAdd")
73
-    public MessageBean batchAdd(MultipartFile file){
74
-        materialInfoService.batchAdd(file);
73
+    public MessageBean batchAdd(@RequestParam("compressFilePath") String compressFilePath){
74
+        materialInfoService.batchAdd(compressFilePath);
75
         return MessageBean.ok(null, "批量添加材质成功");
75
         return MessageBean.ok(null, "批量添加材质成功");
76
     }
76
     }
77
 
77
 

+ 46 - 1
src/main/java/com/lqkj/link/module/zone/controller/ZoneInfoController.java

@@ -346,7 +346,6 @@ public class ZoneInfoController {
346
      *
346
      *
347
      * @param zoneId
347
      * @param zoneId
348
      * @param userId
348
      * @param userId
349
-     * @return
350
      */
349
      */
351
     @PostMapping("/checkShareStatus")
350
     @PostMapping("/checkShareStatus")
352
     public MessageBean save(@Parameter(name = "zoneId", description = "作品ID") Integer zoneId,
351
     public MessageBean save(@Parameter(name = "zoneId", description = "作品ID") Integer zoneId,
@@ -355,4 +354,50 @@ public class ZoneInfoController {
355
         return MessageBean.ok(result, "检查分享状态");
354
         return MessageBean.ok(result, "检查分享状态");
356
     }
355
     }
357
 
356
 
357
+
358
+    /**
359
+     * 获取分享中心数据
360
+     * @param name
361
+     * @param userId
362
+     * @param page
363
+     * @param pageSize
364
+     * @return
365
+     */
366
+    @PostMapping("/getShareCenter")
367
+    public MessageBean<Page<Map<String, Object>>> getShareCenter(@RequestParam(required = false, defaultValue = "") String name,
368
+                                                                     @RequestParam(required = false) Integer userId,
369
+                                                                     @RequestParam(required = false, defaultValue = "0") Integer page,
370
+                                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize){
371
+        return MessageBean.ok(zoneInfoService.shareCenter(name, userId, page, pageSize), "获取分享中心");
372
+
373
+    }
374
+
375
+    /**
376
+     * 分享中心自动补全接口
377
+     * @param keyword
378
+     * @param userId
379
+     * @return
380
+     */
381
+    @PostMapping("/autoCompleteShare")
382
+    public MessageListBean<String> autoCompleteShare(@RequestParam(required = false, defaultValue = "") String keyword,
383
+                                                                 @RequestParam(required = false) Integer userId){
384
+        return MessageListBean.ok(zoneInfoService.autoCompleteShare(keyword, userId), "获取分享中心");
385
+
386
+    }
387
+
388
+
389
+    /**
390
+     * 接受分享
391
+     * @param zoneId
392
+     * @param userId
393
+     * @return
394
+     */
395
+    @PostMapping("/acceptShare")
396
+    public MessageBean acceptShare(@RequestParam Integer zoneId,
397
+                                  @RequestParam Integer userId,
398
+                                   @RequestParam Boolean acceptStatus){
399
+        zoneInfoService.acceptShare(zoneId, userId, acceptStatus);
400
+        return MessageBean.ok(null, "接受分享");
401
+    }
402
+
358
 }
403
 }

+ 4 - 0
src/main/java/com/lqkj/link/module/zone/domain/MaterialInfo.java

@@ -40,6 +40,10 @@ public class MaterialInfo {
40
     @Schema(description = "材质图标")
40
     @Schema(description = "材质图标")
41
     private String materialIcon;
41
     private String materialIcon;
42
 
42
 
43
+    @Column(name = "json_file_name")
44
+    @Schema(description = "材质图标")
45
+    private String compressFileName;
46
+
43
     @Column(name = "update_time")
47
     @Column(name = "update_time")
44
     @JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
48
     @JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
45
     @Schema(description = "更新时间")
49
     @Schema(description = "更新时间")

+ 1 - 1
src/main/java/com/lqkj/link/module/zone/repository/MaterialInfoRepository.java

@@ -20,7 +20,7 @@ public interface MaterialInfoRepository extends JpaRepository<MaterialInfo, Inte
20
      * @return
20
      * @return
21
      */
21
      */
22
     @Query(nativeQuery = true,
22
     @Query(nativeQuery = true,
23
-            value = "select count(*) > 0 from material_info where material_name = :materialName and category_id != :materialId"
23
+            value = "select count(*) > 0 from material_info where material_name = :materialName and material_id != :materialId"
24
     )
24
     )
25
     boolean hasSameNameWithoutOne(@Param("materialId") Integer materialId,
25
     boolean hasSameNameWithoutOne(@Param("materialId") Integer materialId,
26
                                   @Param("materialName")String materialName);
26
                                   @Param("materialName")String materialName);

+ 27 - 0
src/main/java/com/lqkj/link/module/zone/repository/ZoneInfoRepository.java

@@ -192,4 +192,31 @@ public interface ZoneInfoRepository extends JpaRepository<ZoneInfo, Integer> {
192
     )
192
     )
193
     Boolean hasSameTemplateNameWithoutOne(@Param("zoneName") String zoneName,
193
     Boolean hasSameTemplateNameWithoutOne(@Param("zoneName") String zoneName,
194
                                           @Param("zoneId") Integer zoneId);
194
                                           @Param("zoneId") Integer zoneId);
195
+
196
+    @Query(nativeQuery = true,
197
+            value = "select zi.* from zone_info zi, share_info si " +
198
+                    "where zi.zone_id = si.zone_id and si.shared_user_id = :userId " +
199
+                    "and zi.zone_name like concat('%', :name, '%') " +
200
+                    "order by zi.update_time desc"
201
+    )
202
+    Page<Map<String, Object>> pageQueryshareCenter(@Param("name") String name,
203
+                                                   @Param("userId") Integer userId, Pageable pageable);
204
+
205
+
206
+    @Query(nativeQuery = true,
207
+            value = "select distinct(zone_name) as \"name\" from zone_info zi, share_info si " +
208
+                    "where where zi.zone_id = si.zone_id and si.shared_user_id = :userId and zone_name like concat('%', :keyword, '%') " +
209
+                    "order by zone_name"
210
+    )
211
+    List<String> autoCompleteShare(@Param("keyword") String keyword,
212
+                                   @Param("userId") Integer userId);
213
+
214
+
215
+    @Modifying
216
+    @Query(nativeQuery = true,
217
+            value = "update share_info set accept_status = :acceptStatus where zone_id = :zoneId and shared_user_id = :userId"
218
+    )
219
+    void acceptShare(@Param("zoneId") Integer zoneId,
220
+                     @Param("userId") Integer userId,
221
+                     @Param("acceptStatus") Boolean acceptStatus);
195
 }
222
 }

+ 2 - 2
src/main/java/com/lqkj/link/module/zone/service/MaterialInfoService.java

@@ -35,7 +35,7 @@ public interface MaterialInfoService {
35
 
35
 
36
     /**
36
     /**
37
      * 批量添加材质
37
      * 批量添加材质
38
-     * @param file
38
+     * @param compressFilePath
39
      */
39
      */
40
-    void batchAdd(MultipartFile file);
40
+    void batchAdd(String compressFilePath);
41
 }
41
 }

+ 37 - 1
src/main/java/com/lqkj/link/module/zone/service/ZoneInfoService.java

@@ -285,8 +285,9 @@ public class ZoneInfoService {
285
     @Transactional
285
     @Transactional
286
     public String share(Integer zoneId, String userCode, String shareUserCode) {
286
     public String share(Integer zoneId, String userCode, String shareUserCode) {
287
         UserInfo shareUserInfo = userInfoRepository.findByUserCode(shareUserCode);
287
         UserInfo shareUserInfo = userInfoRepository.findByUserCode(shareUserCode);
288
-        if (Objects.isNull(shareUserInfo)) throw new RuntimeException("分享用户不存在!请核验手机号");
288
+        if (Objects.isNull(shareUserInfo)) throw new RuntimeException("未能找到与该手机号码相关的账户,请确认号码无误");
289
         String shareCode = UUID.randomUUID().toString();
289
         String shareCode = UUID.randomUUID().toString();
290
+        if (shareInfoRepository.findByZoneIdAndSharedUserId(zoneId, shareUserInfo.getUserId()).size() > 0) throw new RuntimeException("该账户已分享过该作品");
290
         zoneInfoRepository.share(zoneId, userCode, shareCode, shareUserInfo.getUserId());
291
         zoneInfoRepository.share(zoneId, userCode, shareCode, shareUserInfo.getUserId());
291
         UserInfo userInfo = userInfoRepository.findByUserCode(userCode);
292
         UserInfo userInfo = userInfoRepository.findByUserCode(userCode);
292
         NoticeInfo noticeInfo = new NoticeInfo();
293
         NoticeInfo noticeInfo = new NoticeInfo();
@@ -400,4 +401,39 @@ public class ZoneInfoService {
400
         ShareInfo shareInfo = shareInfos.get(0);
401
         ShareInfo shareInfo = shareInfos.get(0);
401
         return (shareInfo.getShareTime().getTime() + (shareInfo.getCanUse() * 1000 * 60 * 60)) > System.currentTimeMillis();
402
         return (shareInfo.getShareTime().getTime() + (shareInfo.getCanUse() * 1000 * 60 * 60)) > System.currentTimeMillis();
402
     }
403
     }
404
+
405
+
406
+    /**
407
+     * 获取分享中心数据
408
+     * @param name
409
+     * @param userId
410
+     * @param page
411
+     * @param pageSize
412
+     * @return
413
+     */
414
+    public Page<Map<String, Object>> shareCenter(String name, Integer userId, Integer page, Integer pageSize) {
415
+        Pageable pageable = PageRequest.of(page, pageSize);
416
+        return zoneInfoRepository.pageQueryshareCenter(name, userId, pageable);
417
+    }
418
+
419
+
420
+    /**
421
+     * 分享中心自动补全接口
422
+     * @param keyword
423
+     * @param userId
424
+     * @return
425
+     */
426
+    public List<String> autoCompleteShare(String keyword, Integer userId) {
427
+        return zoneInfoRepository.autoCompleteShare(keyword, userId);
428
+    }
429
+
430
+    /**
431
+     * 接受分享
432
+     * @param zoneId
433
+     * @param userId
434
+     */
435
+    @Transactional
436
+    public void acceptShare(Integer zoneId, Integer userId, Boolean acceptStatus) {
437
+        zoneInfoRepository.acceptShare(zoneId, userId, acceptStatus);
438
+    }
403
 }
439
 }

+ 59 - 10
src/main/java/com/lqkj/link/module/zone/service/impl/MaterialInfoServiceImpl.java

@@ -1,11 +1,14 @@
1
 package com.lqkj.link.module.zone.service.impl;
1
 package com.lqkj.link.module.zone.service.impl;
2
 
2
 
3
+import cn.hutool.core.util.RandomUtil;
3
 import com.lqkj.link.message.MessageBean;
4
 import com.lqkj.link.message.MessageBean;
4
 import com.lqkj.link.module.authority.repository.UserInfoRepository;
5
 import com.lqkj.link.module.authority.repository.UserInfoRepository;
5
 import com.lqkj.link.module.zone.domain.MaterialInfo;
6
 import com.lqkj.link.module.zone.domain.MaterialInfo;
6
 import com.lqkj.link.module.zone.domain.ModelCategory;
7
 import com.lqkj.link.module.zone.domain.ModelCategory;
8
+import com.lqkj.link.module.zone.domain.ModelInfo;
7
 import com.lqkj.link.module.zone.repository.MaterialInfoRepository;
9
 import com.lqkj.link.module.zone.repository.MaterialInfoRepository;
8
 import com.lqkj.link.module.zone.service.MaterialInfoService;
10
 import com.lqkj.link.module.zone.service.MaterialInfoService;
11
+import com.lqkj.link.util.Unzipper;
9
 import org.checkerframework.checker.units.qual.A;
12
 import org.checkerframework.checker.units.qual.A;
10
 import org.springframework.beans.factory.annotation.Autowired;
13
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.data.domain.Page;
14
 import org.springframework.data.domain.Page;
@@ -15,10 +18,10 @@ import org.springframework.stereotype.Service;
15
 import org.springframework.transaction.annotation.Transactional;
18
 import org.springframework.transaction.annotation.Transactional;
16
 import org.springframework.web.multipart.MultipartFile;
19
 import org.springframework.web.multipart.MultipartFile;
17
 
20
 
18
-import java.util.ArrayList;
19
-import java.util.Date;
20
-import java.util.List;
21
-import java.util.Objects;
21
+import java.io.File;
22
+import java.io.IOException;
23
+import java.util.*;
24
+import java.util.stream.Collectors;
22
 
25
 
23
 @Service
26
 @Service
24
 public class MaterialInfoServiceImpl implements MaterialInfoService {
27
 public class MaterialInfoServiceImpl implements MaterialInfoService {
@@ -56,14 +59,60 @@ public class MaterialInfoServiceImpl implements MaterialInfoService {
56
         return materialInfoRepository.pageQuery(name, pageable);
59
         return materialInfoRepository.pageQuery(name, pageable);
57
     }
60
     }
58
 
61
 
62
+    @Transactional
59
     @Override
63
     @Override
60
-    public void batchAdd(MultipartFile file) {
61
-        String fileName = file.getOriginalFilename();
62
-        String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
63
-        if (!suffix.equals("zip")) {
64
-            throw new RuntimeException("上传文件类型必须是zip格式的材质文件");
64
+    public void batchAdd(String compressFilePath) {
65
+
66
+        try {
67
+            String unzipPath = "./upload/resource/material" + RandomUtil.randomNumbers(5) + "/";
68
+            Unzipper.unZipFiles(new File("." + compressFilePath), unzipPath);
69
+            Map<String, MaterialInfo> materialMap = materialInfoRepository.findAll().stream().collect(Collectors.toMap(MaterialInfo::getMaterialName, materialInfo -> materialInfo));
70
+
71
+            String modelFolderPath = unzipPath + "model";
72
+            File materialModelDir = new File(modelFolderPath);
73
+            if (materialModelDir.isDirectory()){
74
+                List<MaterialInfo> materialInfoList = new ArrayList<>();
75
+                File[] jsonFiles = materialModelDir.listFiles();
76
+                assert jsonFiles != null;
77
+                for (File jsonFile : jsonFiles) {
78
+                    if (jsonFile.isFile() && jsonFile.getName().endsWith(".json")) {
79
+                        String modelFileName = jsonFile.getName();
80
+                        String modelPath = unzipPath + "model/";
81
+                        if (modelFileName.matches(".*[一-龥]+.*")) {
82
+                            // 如果包含中文,重命名
83
+                            String newFileName = UUID.randomUUID() + ".json";
84
+                            modelPath += newFileName;
85
+                            boolean rename = jsonFile.renameTo(new File(modelPath));
86
+                            System.out.println(rename);
87
+                            // 材质图标也要重命名
88
+                            File oldIconFile = new File(unzipPath + "icon/" + modelFileName.replace(".json", ".png"));
89
+                            if (oldIconFile.exists()) {
90
+                                File newIconFile = new File(unzipPath + "icon/" + newFileName.replace(".json", ".png"));
91
+                                rename = oldIconFile.renameTo(newIconFile);
92
+                                System.out.println(rename);
93
+                            }
94
+                        } else {
95
+                            modelPath += modelFileName;
96
+                        }
97
+                        if (!materialMap.containsKey(modelFileName.substring(0, modelFileName.lastIndexOf(".")))) {
98
+                            materialInfoList.add(new MaterialInfo(null, modelFileName.substring(0, modelFileName.lastIndexOf(".")), modelPath.substring(1),
99
+                                    modelPath.replace("model", "icon").replace("json", "png").substring(1), modelFileName, new Date()));
100
+                        } else {
101
+                            MaterialInfo materialInfo = materialMap.get(modelFileName.substring(0, modelFileName.lastIndexOf(".")));
102
+                            materialInfo.setMaterialIcon(modelPath.replace("model", "icon").replace("json", "png").substring(1));
103
+                            materialInfo.setCompressFileName(modelFileName.substring(1));
104
+                            materialInfo.setJsonPath(modelPath);
105
+                            materialInfo.setUpdateTime(new Date());
106
+                            materialInfoList.add(materialInfo);
107
+                        }
108
+                    }
109
+                }
110
+                materialInfoRepository.saveAll(materialInfoList);
111
+            }
112
+            userInfoRepository.updateRefreshStatus();
113
+        } catch (IOException e) {
114
+            throw new RuntimeException(e);
65
         }
115
         }
66
-        materialInfoRepository.saveAll(new ArrayList<>());
67
     }
116
     }
68
 
117
 
69
 
118