“djh”
4 天以前 f99d902db3c31e57229439962abd2746bb06868d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.gkhy.exam.system.service.impl;
 
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.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;
 
/**
 * <p>
 * 课程资源表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-06 10:25:36
 */
@Service
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();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            resource.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<ExResource> resourceList=baseMapper.selectResourceList(resource);
        resourceList.forEach(item -> {
            item.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+item.getResourcePath());
        });
        return CommonPage.restPage(resourceList);
    }
 
    @Override
    public ExResource selectResourceById(Long resourceId) {
        ExResource resource= baseMapper.selectResourceById(resourceId);
        if(resource==null){
            return resource;
        }
        resource.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+resource.getResourcePath());
        if(resource.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
            return resource;
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return resource;
        }
        if(!resource.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业资源");
        }
 
        return resource;
    }
 
    @Override
    public ExResource selectResourceByPeriodId(Long periodId) {
        ExResource resource= baseMapper.selectResourceByPeriodId(periodId);
        if(resource==null){
            return resource;
        }
        resource.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+resource.getResourcePath());
        if(resource.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
            return resource;
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return resource;
        }
        if(!resource.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业资源");
        }
        return resource;
 
    }
 
    @Override
    public int insertResource(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("文档页数不能为空");
            }
        }
        SysUser user=SecurityUtils.getLoginUser().getUser();
        if(user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            resource.setPrivatize(PrivatizeEnum.PUBLIC.getCode());
        }else{
            resource.setCompanyId(user.getCompanyId());
        }
        checkUserAllowed(resource);
        if(!checkNameUnique(resource)){
            throw new ApiException("资源名称已存在");
        }
        resource.setCreateBy(SecurityUtils.getUsername());
        int row=baseMapper.insert(resource);
        if(row<1){
            throw new ApiException("新增资源失败");
        }
        return row;
    }
 
    @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){
            throw new ApiException("更新资源失败");
        }
        return row;
    }
 
    public void checkUserAllowed(ExResource resource) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return;
        }
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
        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.removeMinioFile(resourceId,resource.getResourcePath());
        return row;
    }
 
    @Override
    public boolean checkNameUnique(ExResource resource) {
        Long resourceId=resource.getId()==null?-1L:resource.getId();
        ExResource res= baseMapper.checkNameUnique(resource.getName(),resource.getCompanyId());
        if(res!=null&&res.getId().longValue()!=resourceId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
}