|
@@ -4,12 +4,15 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.lang.Validator;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.github.pagehelper.Page;
|
|
|
import com.zhongzheng.common.exception.CustomException;
|
|
|
import com.zhongzheng.common.utils.DateUtils;
|
|
|
import com.zhongzheng.common.utils.ServletUtils;
|
|
|
+import com.zhongzheng.modules.alioss.bo.OssRequest;
|
|
|
+import com.zhongzheng.modules.alioss.service.OssService;
|
|
|
import com.zhongzheng.modules.course.bo.*;
|
|
|
import com.zhongzheng.modules.course.domain.CourseFile;
|
|
|
import com.zhongzheng.modules.course.domain.CourseHandouts;
|
|
@@ -18,14 +21,27 @@ import com.zhongzheng.modules.course.mapper.CourseHandoutsMapper;
|
|
|
import com.zhongzheng.modules.course.service.ICourseFileService;
|
|
|
import com.zhongzheng.modules.course.service.ICourseHandoutsBusinessService;
|
|
|
import com.zhongzheng.modules.course.service.ICourseHandoutsService;
|
|
|
+import com.zhongzheng.modules.course.vo.CourseFileVo;
|
|
|
import com.zhongzheng.modules.course.vo.CourseHandoutsBusinessVo;
|
|
|
import com.zhongzheng.modules.course.vo.CourseHandoutsVo;
|
|
|
import com.zhongzheng.modules.goods.domain.Goods;
|
|
|
import com.zhongzheng.modules.goods.service.IGoodsService;
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
@@ -47,12 +63,14 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
private IGoodsService iGoodsService;
|
|
|
@Autowired
|
|
|
private ICourseFileService iCourseFileService;
|
|
|
+ @Autowired
|
|
|
+ private OssService ossService;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public CourseHandoutsVo queryById(Long handoutsId){
|
|
|
+ public CourseHandoutsVo queryById(Long handoutsId) {
|
|
|
CourseHandouts db = this.baseMapper.selectById(handoutsId);
|
|
|
- if (Validator.isEmpty(db)){
|
|
|
+ if (Validator.isEmpty(db)) {
|
|
|
throw new CustomException("查无数据");
|
|
|
}
|
|
|
//获得讲义业务层
|
|
@@ -62,13 +80,36 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
|
|
|
CourseFileQueryBo queryBo = new CourseFileQueryBo();
|
|
|
queryBo.setHandoutsId(courseHandoutsVo.getHandoutsId());
|
|
|
- courseHandoutsVo.setFileList(iCourseFileService.queryList(queryBo));
|
|
|
+ List<CourseFileVo> courseFileVos = iCourseFileService.queryList(queryBo);
|
|
|
+ if (CollectionUtils.isEmpty(courseFileVos)){
|
|
|
+ return courseHandoutsVo;
|
|
|
+ }
|
|
|
+ List<CourseFileVo> fileVos = courseFileVos.stream().filter(item -> item.getParentId() == 0L).collect(Collectors.toList());
|
|
|
+ if (!CollectionUtils.isEmpty(fileVos)){
|
|
|
+ fileVos.forEach(file -> {
|
|
|
+ assembleFile(file,courseFileVos);
|
|
|
+ });
|
|
|
+ courseHandoutsVo.setFileList(fileVos);
|
|
|
+ }
|
|
|
return courseHandoutsVo;
|
|
|
}
|
|
|
|
|
|
+ private void assembleFile(CourseFileVo parent,List<CourseFileVo> childs){
|
|
|
+ if (CollectionUtils.isEmpty(childs)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<CourseFileVo> collect = childs.stream().filter(x -> x.getParentId().equals(parent.getFileId())).collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isEmpty(collect)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ parent.setChildren(collect);
|
|
|
+ collect.forEach(item -> {
|
|
|
+ assembleFile(item,childs);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<CourseHandoutsVo> queryList(CourseHandoutsQueryBo bo) {
|
|
|
-
|
|
|
return entity2Vo(baseMapper.queryList(bo));
|
|
|
}
|
|
|
|
|
@@ -78,19 +119,19 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 实体类转化成视图对象
|
|
|
- *
|
|
|
- * @param collection 实体类集合
|
|
|
- * @return
|
|
|
- */
|
|
|
+ * 实体类转化成视图对象
|
|
|
+ *
|
|
|
+ * @param collection 实体类集合
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private List<CourseHandoutsVo> entity2Vo(Collection<CourseHandouts> collection) {
|
|
|
List<CourseHandoutsVo> voList = collection.stream()
|
|
|
.map(any -> BeanUtil.toBean(any, CourseHandoutsVo.class))
|
|
|
.collect(Collectors.toList());
|
|
|
if (collection instanceof Page) {
|
|
|
- Page<CourseHandouts> page = (Page<CourseHandouts>)collection;
|
|
|
+ Page<CourseHandouts> page = (Page<CourseHandouts>) collection;
|
|
|
Page<CourseHandoutsVo> pageVo = new Page<>();
|
|
|
- BeanUtil.copyProperties(page,pageVo);
|
|
|
+ BeanUtil.copyProperties(page, pageVo);
|
|
|
pageVo.addAll(voList);
|
|
|
voList = pageVo;
|
|
|
}
|
|
@@ -143,7 +184,7 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
validEntityBeforeSave(update);
|
|
|
update.setUpdateTime(DateUtils.getNowTime());
|
|
|
LambdaQueryWrapper<CourseHandoutsBusiness> lqw = Wrappers.lambdaQuery();
|
|
|
- lqw.eq(CourseHandoutsBusiness::getHandoutsId,bo.getHandoutsId());
|
|
|
+ lqw.eq(CourseHandoutsBusiness::getHandoutsId, bo.getHandoutsId());
|
|
|
courseHandoutsBusinessService.remove(lqw);
|
|
|
//添加讲义绑定的业务层次
|
|
|
if (!CollectionUtils.isEmpty(bo.getCourseHandoutsBusinessAddBos())) {
|
|
@@ -176,23 +217,22 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 保存前的数据校验
|
|
|
*
|
|
|
* @param entity 实体类数据
|
|
|
*/
|
|
|
- private void validEntityBeforeSave(CourseHandouts entity){
|
|
|
+ private void validEntityBeforeSave(CourseHandouts entity) {
|
|
|
//TODO 做一些数据校验,如唯一约束
|
|
|
- if(Validator.isNotEmpty(entity.getHandoutsId())){
|
|
|
- if(entity.getStatus()==0){
|
|
|
- CourseHandouts courseHandouts =this.baseMapper.selectById(entity.getHandoutsId());
|
|
|
- if(courseHandouts.getStatus()==1){
|
|
|
+ if (Validator.isNotEmpty(entity.getHandoutsId())) {
|
|
|
+ if (entity.getStatus() == 0) {
|
|
|
+ CourseHandouts courseHandouts = this.baseMapper.selectById(entity.getHandoutsId());
|
|
|
+ if (courseHandouts.getStatus() == 1) {
|
|
|
//从发布改为未发布
|
|
|
LambdaQueryWrapper<Goods> lqw = Wrappers.lambdaQuery();
|
|
|
- lqw.eq( Goods::getHandoutsId, entity.getHandoutsId());
|
|
|
+ lqw.eq(Goods::getHandoutsId, entity.getHandoutsId());
|
|
|
Goods goods = iGoodsService.getOne(lqw.last("limit 1"));
|
|
|
- if(Validator.isNotEmpty(goods)){
|
|
|
+ if (Validator.isNotEmpty(goods)) {
|
|
|
throw new CustomException("该讲义已被关联,禁止改为未发布");
|
|
|
}
|
|
|
|
|
@@ -203,7 +243,7 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
|
|
|
@Override
|
|
|
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
- if(isValid){
|
|
|
+ if (isValid) {
|
|
|
//TODO 做一些业务上的校验,判断是否需要校验
|
|
|
}
|
|
|
return this.removeByIds(ids);
|
|
@@ -211,6 +251,237 @@ public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper,
|
|
|
|
|
|
@Override
|
|
|
public CourseHandouts getHandoutsByTenant(String encoder, Long newTenantId) {
|
|
|
- return baseMapper.getHandoutsByTenant(encoder,newTenantId);
|
|
|
+ return baseMapper.getHandoutsByTenant(encoder, newTenantId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean addHandouts(MultipartFile[] files, CourseHandoutsAddBo bo) {
|
|
|
+ CourseHandouts add = BeanUtil.toBean(bo, CourseHandouts.class);
|
|
|
+ validEntityBeforeSave(add);
|
|
|
+ add.setCreateTime(DateUtils.getNowTime());
|
|
|
+ add.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ add.setEncoder(ServletUtils.getEncoded("JYLB"));
|
|
|
+ boolean save = this.save(add);
|
|
|
+ //添加讲义绑定的业务层次
|
|
|
+ for (CourseHandoutsBusinessAddBo courseHandoutsBusinessAddBo : bo.getCourseHandoutsBusinessAddBos()) {
|
|
|
+ CourseHandoutsBusiness addBusiness = BeanUtil.toBean(courseHandoutsBusinessAddBo, CourseHandoutsBusiness.class);
|
|
|
+ addBusiness.setHandoutsId(add.getHandoutsId());
|
|
|
+ addBusiness.setCreateTime(DateUtils.getNowTime());
|
|
|
+ addBusiness.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ courseHandoutsBusinessService.save(addBusiness);
|
|
|
+ }
|
|
|
+ for (MultipartFile multipartFile : files) {
|
|
|
+ if (multipartFile.getOriginalFilename().contains(".zip")) {
|
|
|
+ try {
|
|
|
+ matchingFile(multipartFile.getOriginalFilename(),add.getHandoutsId(),0L);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException("解压/上传zip包失败");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ try {
|
|
|
+ //文件
|
|
|
+ CourseFile courseFile = new CourseFile();
|
|
|
+ //上传oss
|
|
|
+ String ossPath = ossService.uploadInputStream(multipartFile.getInputStream(), 17);
|
|
|
+ courseFile.setUrlName(multipartFile.getOriginalFilename());
|
|
|
+ courseFile.setUrl(ossPath);
|
|
|
+ courseFile.setStatus(1);
|
|
|
+ courseFile.setSort(1);
|
|
|
+ courseFile.setCreateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setHandoutsId(add.getHandoutsId());
|
|
|
+ courseFile.setType(1);//文件
|
|
|
+ courseFile.setParentId(0L);
|
|
|
+ iCourseFileService.save(courseFile);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException("上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean updateHandouts(MultipartFile[] files, CourseHandoutsEditBo bo) {
|
|
|
+ CourseHandouts update = BeanUtil.toBean(bo, CourseHandouts.class);
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ update.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ LambdaQueryWrapper<CourseHandoutsBusiness> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(CourseHandoutsBusiness::getHandoutsId, bo.getHandoutsId());
|
|
|
+ courseHandoutsBusinessService.remove(lqw);
|
|
|
+ //添加讲义绑定的业务层次
|
|
|
+ if (!CollectionUtils.isEmpty(bo.getCourseHandoutsBusinessAddBos())) {
|
|
|
+ for (CourseHandoutsBusinessAddBo courseHandoutsBusinessAddBo : bo.getCourseHandoutsBusinessAddBos()) {
|
|
|
+ CourseHandoutsBusiness addBusiness = BeanUtil.toBean(courseHandoutsBusinessAddBo, CourseHandoutsBusiness.class);
|
|
|
+ addBusiness.setHandoutsId(bo.getHandoutsId());
|
|
|
+ addBusiness.setCreateTime(DateUtils.getNowTime());
|
|
|
+ addBusiness.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ courseHandoutsBusinessService.save(addBusiness);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除原有的关联重新绑定
|
|
|
+ iCourseFileService.remove(new LambdaQueryWrapper<CourseFile>().eq(CourseFile::getHandoutsId, update.getHandoutsId()));
|
|
|
+ if (!CollectionUtils.isEmpty(bo.getFileList())){
|
|
|
+ handleFile(bo.getFileList(),update.getHandoutsId(),files,0L);
|
|
|
+ }
|
|
|
+ //修改完成
|
|
|
+ update.setUpdateStatus(1);
|
|
|
+ return this.updateById(update);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void handleFile(List<CourseFileAddBo> fileList,Long handoutsId,MultipartFile[] files,Long parentId){
|
|
|
+ fileList.forEach(bo -> {
|
|
|
+ Long fileId = 0L;
|
|
|
+ if (ObjectUtils.isNull(bo.getFileId())){
|
|
|
+ //新增讲义
|
|
|
+ fileId = handleAddFile(files,bo.getUrlName(),parentId,handoutsId);
|
|
|
+ }else {
|
|
|
+ bo.setFileId(null);
|
|
|
+ bo.setParentId(parentId);
|
|
|
+ bo.setHandoutsId(handoutsId);
|
|
|
+ CourseFile courseFile = BeanUtil.toBean(bo, CourseFile.class);
|
|
|
+ courseFile.setCreateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ iCourseFileService.save(courseFile);
|
|
|
+ fileId = courseFile.getFileId();
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(bo.getChildren())){
|
|
|
+ handleFile(bo.getChildren(),handoutsId,files,fileId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long handleAddFile(MultipartFile[] files, String addFileName, Long fileId, Long handoutsId) {
|
|
|
+ //新增文件资源
|
|
|
+ MultipartFile multipartFile = Arrays.stream(files).filter(item -> item.getOriginalFilename().equals(addFileName)).findFirst().orElse(null);
|
|
|
+ if (ObjectUtils.isNotNull(multipartFile)){
|
|
|
+ if (multipartFile.getOriginalFilename().contains(".zip")) {
|
|
|
+ try {
|
|
|
+ return matchingFile(addFileName,handoutsId,fileId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException("解压/上传zip包失败");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ try {
|
|
|
+ //文件
|
|
|
+ CourseFile file = new CourseFile();
|
|
|
+
|
|
|
+ //上传阿里云
|
|
|
+ OssRequest ossRequest = new OssRequest();
|
|
|
+ ossRequest.setFile(multipartFile);
|
|
|
+ ossRequest.setImageStatus(17);
|
|
|
+ String upload = ossService.upload(ossRequest);
|
|
|
+// String ossPath = ossService.uploadInputStream(multipartFile.getInputStream(), 17);
|
|
|
+ file.setUrlName(multipartFile.getOriginalFilename());
|
|
|
+ file.setUrl(upload);
|
|
|
+ file.setStatus(1);
|
|
|
+ file.setSort(1);
|
|
|
+ file.setCreateTime(DateUtils.getNowTime());
|
|
|
+ file.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ file.setHandoutsId(handoutsId);
|
|
|
+ file.setType(1);//文件
|
|
|
+ file.setParentId(fileId);
|
|
|
+ iCourseFileService.save(file);
|
|
|
+ return file.getFileId();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException("上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long matchingFile(String addFileName, Long handoutsId, Long fileId) throws Exception {
|
|
|
+ String zhiyuan = System.getProperty("user.dir");
|
|
|
+ String destDirPath = zhiyuan+"/zhongzheng-admin/src/main/resources/zhiyuan";
|
|
|
+ String substring = addFileName.substring(0, addFileName.indexOf("."));
|
|
|
+ String path = destDirPath+"/"+ substring;
|
|
|
+ //文件夹
|
|
|
+ CourseFile courseFile = new CourseFile();
|
|
|
+ courseFile.setUrlName(substring);
|
|
|
+ courseFile.setStatus(1);
|
|
|
+ courseFile.setSort(1);
|
|
|
+ courseFile.setCreateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setHandoutsId(handoutsId);
|
|
|
+ courseFile.setType(2);//文件夹
|
|
|
+ courseFile.setParentId(fileId);
|
|
|
+ iCourseFileService.save(courseFile);
|
|
|
+ //处理讲义文件
|
|
|
+ getFileNames(new File(path), courseFile.getFileId(), handoutsId);
|
|
|
+ return courseFile.getFileId();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到文件名称
|
|
|
+ *
|
|
|
+ * @param file 文件
|
|
|
+ * @param parentId 父ID
|
|
|
+ * @return {@link List}<{@link String}>
|
|
|
+ */
|
|
|
+ private void getFileNames(File file, Long parentId, Long handoutsId) throws Exception {
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File f : files) {
|
|
|
+ if (f.isDirectory()) {
|
|
|
+ //文件夹
|
|
|
+ CourseFile courseFile = new CourseFile();
|
|
|
+ courseFile.setUrlName(f.getName());
|
|
|
+ courseFile.setStatus(1);
|
|
|
+ courseFile.setSort(1);
|
|
|
+ courseFile.setCreateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setHandoutsId(handoutsId);
|
|
|
+ courseFile.setType(2);//文件夹
|
|
|
+ courseFile.setParentId(parentId);
|
|
|
+ iCourseFileService.save(courseFile);
|
|
|
+ getFileNames(f, courseFile.getFileId(), handoutsId);
|
|
|
+ } else {
|
|
|
+ //文件
|
|
|
+ CourseFile courseFile = new CourseFile();
|
|
|
+ //上传阿里云
|
|
|
+ OssRequest ossRequest = new OssRequest();
|
|
|
+ ossRequest.setFile(getMultipartFile(f));
|
|
|
+ ossRequest.setImageStatus(17);
|
|
|
+ String upload = ossService.upload(ossRequest);
|
|
|
+// //上传oss
|
|
|
+// String ossPath = ossService.uploadInputStream(new FileInputStream(f), 17);
|
|
|
+ courseFile.setUrlName(f.getName());
|
|
|
+ courseFile.setUrl(upload);
|
|
|
+ courseFile.setStatus(1);
|
|
|
+ courseFile.setSort(1);
|
|
|
+ courseFile.setCreateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ courseFile.setHandoutsId(handoutsId);
|
|
|
+ courseFile.setType(1);//文件
|
|
|
+ courseFile.setParentId(parentId);
|
|
|
+ iCourseFileService.save(courseFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ public static MultipartFile getMultipartFile(File file) {
|
|
|
+ FileItem item = new DiskFileItemFactory().createItem("file"
|
|
|
+ , MediaType.MULTIPART_FORM_DATA_VALUE
|
|
|
+ , true
|
|
|
+ , file.getName());
|
|
|
+ try (InputStream input = new FileInputStream(file);
|
|
|
+ OutputStream os = item.getOutputStream()) {
|
|
|
+ // 流转移
|
|
|
+ IOUtils.copy(input, os);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IllegalArgumentException("Invalid file: " + e, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new CommonsMultipartFile(item);
|
|
|
+ }
|
|
|
+
|
|
|
}
|