kongzy
2024-06-24 4910e41f81a85c03a9dfc83f8ec9c1e71c123d49
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
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.constant.UserConstant;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.enums.PrivatizeEnum;
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 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;
    @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);
        return CommonPage.restPage(resourceList);
    }
 
    @Override
    public ExResource selectResourceById(Long resourceId) {
        return baseMapper.selectResourceById(resourceId);
    }
 
    @Override
    public ExResource selectResourceByPeriodId(Long periodId) {
        return baseMapper.selectResourceByPeriodId(periodId);
    }
 
    @Override
    public int insertResource(ExResource resource) {
        if(!checkNameUnique(resource)){
            throw new ApiException("资源名称已存在");
        }
        SysUser user=SecurityUtils.getLoginUser().getUser();
        if(user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            resource.setPrivatize(PrivatizeEnum.PUBLIC.getCode());
        }else{
            if(user.getCompanyId()==null){
                throw new ApiException("获取用户公司id失败");
            }
            resource.setCompanyId(user.getCompanyId());
            resource.setPrivatize(PrivatizeEnum.PRIVATE.getCode());
        }
        UploadObjectVO uploadObjectVO =commonService.doUpload(resource.getFile());
        resource.setResourceUri(uploadObjectVO.getPath());
        resource.setVideoVid(uploadObjectVO.getFilename());
        resource.setResourceSize(uploadObjectVO.getSize());
        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;
    }
 
    @Override
    public int updateResource(ExResource resource) {
        if(!checkNameUnique(resource)){
            throw new ApiException("资源名称已存在");
        }
        int row=baseMapper.updateById(resource);
        if(row<1){
            throw new ApiException("更新资源失败");
        }
        return row;
    }
 
    @Override
    public int deleteResourceById(Long resourceId) {
        //校验资源是否绑定
        ExResource resource=getById(resourceId);
        int row=baseMapper.deleteById(resourceId);
        if(row<1){
            throw new ApiException("删除资源失败");
        }
        //删除文件
        commonService.removeFile(resource.getResourceUri());
        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;
    }
}