From 8458e64aab474c0fc2f49ae4ff22fb11ce5cf6e2 Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: 星期一, 11 十一月 2024 16:55:28 +0800
Subject: [PATCH] 批次新增学员查询条件,新增题目导入接口

---
 exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExResourceServiceImpl.java |   66 ++++++++++++++++++++++++--------
 1 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExResourceServiceImpl.java b/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExResourceServiceImpl.java
index 52ebb5b..200e266 100644
--- a/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExResourceServiceImpl.java
+++ b/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExResourceServiceImpl.java
@@ -2,20 +2,22 @@
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.gkhy.exam.common.api.CommonPage;
+import com.gkhy.exam.common.config.MinioConfig;
 import com.gkhy.exam.common.constant.UserConstant;
 import com.gkhy.exam.common.domain.entity.SysUser;
 import com.gkhy.exam.common.enums.PrivatizeEnum;
+import com.gkhy.exam.common.enums.ResourceTypeEnum;
 import com.gkhy.exam.common.enums.UserTypeEnum;
 import com.gkhy.exam.common.exception.ApiException;
 import com.gkhy.exam.common.utils.PageUtils;
 import com.gkhy.exam.common.utils.SecurityUtils;
 import com.gkhy.exam.system.domain.ExResource;
-import com.gkhy.exam.system.domain.vo.UploadObjectVO;
 import com.gkhy.exam.system.mapper.ExResourceMapper;
 import com.gkhy.exam.system.service.ExResourceService;
 import com.gkhy.exam.system.service.SysCommonService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 
@@ -31,6 +33,12 @@
 public class ExResourceServiceImpl extends ServiceImpl<ExResourceMapper, ExResource> implements ExResourceService {
     @Autowired
     private SysCommonService commonService;
+
+    @Autowired
+    private MinioConfig minioConfig;
+
+
+
     @Override
     public CommonPage selectResourseList(ExResource resource) {
         SysUser currentUser = SecurityUtils.getLoginUser().getUser();
@@ -39,6 +47,9 @@
         }
         PageUtils.startPage();
         List<ExResource> resourceList=baseMapper.selectResourceList(resource);
+        resourceList.forEach(item -> {
+            item.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+item.getResourcePath());
+        });
         return CommonPage.restPage(resourceList);
     }
 
@@ -48,6 +59,7 @@
         if(resource==null){
             return resource;
         }
+        resource.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+resource.getResourcePath());
         if(resource.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
             return resource;
         }
@@ -58,6 +70,7 @@
         if(!resource.getCompanyId().equals(currentUser.getCompanyId())){
             throw new ApiException("无权限查看其它企业资源");
         }
+
         return resource;
     }
 
@@ -67,6 +80,7 @@
         if(resource==null){
             return resource;
         }
+        resource.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+resource.getResourcePath());
         if(resource.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
             return resource;
         }
@@ -83,9 +97,14 @@
 
     @Override
     public int insertResource(ExResource resource) {
-        checkUserAllowed(resource);
-        if(!checkNameUnique(resource)){
-            throw new ApiException("资源名称已存在");
+        if(resource.getResourceType().equals(ResourceTypeEnum.VIDEO.getCode())||resource.getResourceType().equals(ResourceTypeEnum.AUDIO.getCode())){
+            if(resource.getResourceLength()==null){
+                throw new ApiException("视频或者音频时长不能为空");
+            }
+        }else{
+            if(resource.getDocPage()==null){
+                throw new ApiException("文档页数不能为空");
+            }
         }
         SysUser user=SecurityUtils.getLoginUser().getUser();
         if(user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
@@ -93,19 +112,13 @@
         }else{
             resource.setCompanyId(user.getCompanyId());
         }
-        UploadObjectVO uploadObjectVO =commonService.doUpload(resource.getFile());
-        resource.setResourceUri(uploadObjectVO.getPath());
-        resource.setVideoVid(uploadObjectVO.getFilename());
-        resource.setResourceSize(uploadObjectVO.getSize());
+        checkUserAllowed(resource);
+        if(!checkNameUnique(resource)){
+            throw new ApiException("资源名称已存在");
+        }
         resource.setCreateBy(SecurityUtils.getUsername());
         int row=baseMapper.insert(resource);
         if(row<1){
-            try {
-                //删除原文件
-                commonService.removeFile(uploadObjectVO.getPath());
-            }catch (Exception e){
-                log.error("新增资源,删除文件失败="+e.getMessage());
-            }
             throw new ApiException("新增资源失败");
         }
         return row;
@@ -113,9 +126,23 @@
 
     @Override
     public int updateResource(ExResource resource) {
+        if(resource.getResourceType().equals(ResourceTypeEnum.VIDEO.getCode())||resource.getResourceType().equals(ResourceTypeEnum.AUDIO.getCode())){
+            if(resource.getResourceLength()==null){
+                throw new ApiException("视频或者音频时长不能为空");
+            }
+        }else{
+            if(resource.getDocPage()==null){
+                throw new ApiException("文档页数不能为空");
+            }
+        }
         checkUserAllowed(resource);
         if(!checkNameUnique(resource)){
             throw new ApiException("资源名称已存在");
+        }
+        String resourcePath=resource.getResourcePath();
+        if(resourcePath.startsWith(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/")) {
+            resourcePath = resourcePath.replace(minioConfig.getEndpoint()+minioConfig.getBucketName() + "/", "");
+            resource.setResourcePath(resourcePath);
         }
         int row=baseMapper.updateById(resource);
         if(row<1){
@@ -132,22 +159,27 @@
         if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
             throw new ApiException("没有权限操作");
         }
-        if(!currentUser.getCompanyId().equals(resource.getCompanyId())){
+        if(resource.getCompanyId()!=null&&!currentUser.getCompanyId().equals(resource.getCompanyId())){
             throw new ApiException("没有权限操作其他企业资源");
         }
     }
 
     @Override
+    @Transactional(rollbackFor = RuntimeException.class)
     public int deleteResourceById(Long resourceId) {
-        //校验资源是否绑定
         checkUserAllowed(baseMapper.selectById(resourceId));
+        //校验资源是否绑定
+        int count= baseMapper.checkResourceAssign(resourceId);
+        if(count>0){
+            throw new ApiException("资源已跟课时关联,不能删除");
+        }
         ExResource resource=getById(resourceId);
         int row=baseMapper.deleteById(resourceId);
         if(row<1){
             throw new ApiException("删除资源失败");
         }
         //删除文件
-        commonService.removeFile(resource.getResourceUri());
+        commonService.removeMinioFile(resourceId,resource.getResourcePath());
         return row;
     }
 

--
Gitblit v1.9.2