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.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; /** *

* 课程资源表 服务实现类 *

* * @author kzy * @since 2024-06-06 10:25:36 */ @Service public class ExResourceServiceImpl extends ServiceImpl 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 resourceList=baseMapper.selectResourceList(resource); return CommonPage.restPage(resourceList); } @Override public ExResource selectResourceById(Long resourceId) { ExResource resource= baseMapper.selectResourceById(resourceId); if(resource==null){ return resource; } 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; } 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("资源名称已存在"); } 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(!currentUser.getCompanyId().equals(resource.getCompanyId())){ throw new ApiException("没有权限操作其他企业资源"); } } @Override @Transactional(rollbackFor = RuntimeException.class) public int deleteResourceById(Long resourceId) { //校验资源是否绑定 checkUserAllowed(baseMapper.selectById(resourceId)); 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; } }