package com.qxueyou.scc.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.util.ClientUtils; import com.qxueyou.scc.base.util.CollectionUtils; import com.qxueyou.scc.base.util.QBeanUtils; import com.qxueyou.scc.teach.res.model.Res; import com.qxueyou.scc.teach.res.model.ResDatas; import com.qxueyou.scc.teach.res.model.ResDir; import com.qxueyou.scc.teach.res.service.IResService; import io.swagger.annotations.Api; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Map; /** * 资源管理控制器 * * @author chenjunliang */ @Api(tags = "资源管理接口") @RestController @RequestMapping(value = "/teach/res") public class ResController { @Autowired IResService resService; /** * 资源库层级管理Tree */ @GetMapping(value = "categoryLevel") public Result categoryLevel(String parentId) { List dirs = resService.doGetMyLibChildDirs(parentId); return new Result(true, "success", QBeanUtils.listBean2ListMap(dirs, CollectionUtils.newStringMap("name", "name", "dirId", "id"))); } /** * 资源库根节点 * * @param type 资源库类型 * user,个人资源库 * class,班级资源库 * @param ownerId 资源库所属主体id,个人资源库传userId,班级资源库传classId */ @GetMapping(value = "rootDir") public Result getRoot(String type, String ownerId) { if ("user".equals(type) && StringUtils.isEmpty(ownerId)) { ownerId = ClientUtils.getUserId(); // System.out.println(ownerId); } ResDir root = resService.doGetRootDir(type, ownerId); // System.out.println(root); return new Result(true, "success", CollectionUtils.newList( QBeanUtils.bean2Map(root, CollectionUtils.newStringMap("name", "name", "dirId", "id")))); } /** * 资源管理获取 资源列表 * * @param dirId 目录id * @param keyword 搜索关键字 * @param limit 每页显示几条 * @param pageNum 页码 * @param type 类型(0,视频。1,音频。2,文档。3,练习。) * @return 资源列表数据 */ @GetMapping(value = "lstRes") public Result lstRes(String dirId, String keyword, Integer limit, Integer pageNum, String type) { List resLst = resService.listRes(dirId, keyword, limit, pageNum, type); List> lst = QBeanUtils.listBean2ListMap(resLst, CollectionUtils.newStringMap("resId", "id", "name", "name", "type", "type", "coverPageUrl", "imgPath", "updateTime", "updateTime", "remark", "size", "status", "status")); return new Result(true, "success", CollectionUtils.newObjectMap("resLst", lst, "resCount", resService.listResCount(dirId, keyword, type))); } /** * 资源管理删除 资源 * * @param id 资源id,英文逗号区分,可传多个 */ @GetMapping(value = "deleteFile") public Result deleteRes(String fileId) { return resService.delete(fileId.split(",")); } /** * 资源管理 复制资源 * * @param resId 资源id * @param dirId 目录id * @param type 类型 */ @GetMapping(value = "copyRes") public Result copyRes(String resId, String dirId, String type) { return resService.doCopy(resId, dirId, null); } /** * 资源管理 资源的移动 * * @param resId 要移动的资源 * @param type 目录类型 * @param dirId 目录id */ @GetMapping(value = "moveRes") public Result moveRes(String resId, String type, String dirId) { return resService.doMove(resId, dirId, null); } /** * 资源管理新增or编辑目录 * * @param dirId 目录id * @param name 目录名 * @param type (add新增 and edit修改) * @param childFlag 是否操作下级 */ @GetMapping(value = "addOrUpdateDir") public Result addOrUpdateDir(String type, String name, String dirId, boolean childFlag) { ResDir dir = resService.readDir(dirId); String parentDirId = childFlag ? dirId : dir.getParentDirId(); if ("add".equals(type)) { return resService.addDir(parentDirId, name); } else { return resService.updateDir(dirId, name); } } /** * 资源管理 删除目录 * * @param dirId 目录id */ @GetMapping(value = "deleteDir") public Result deleteDir(String dirId) { return resService.deleteDir(dirId.split(",")); } /** * 添加/更新(视频,讲义,音频) * * @param dirId 资源夹id * @param name 名称 * @param coverUrl 封面Url * @param remark:

* 视频啊啊啊 *

* 介绍 * @param fileId 资源id * @param id 资源id * @param type * @return 状态说明(0, 视频 。 1, 音频 。 2, 文档 。 3, 练习 。) */ @PostMapping(value = "addOrUpdateRes") public Result addOrUpdateRes(String dirId, String id, String remark, String name, String coverUrl, String type, String fileId) { if (StringUtils.isEmpty(id)) { return resService.add(dirId, fileId, name, remark, type, coverUrl); } else { return resService.update(id, name, remark, coverUrl); } } /** * 批量添加资源 * * @param dirId 节点id * @param fileId 文件id * @param fileName 文件名称 * @param fileType 资源类型 */ @PostMapping(value = "addOfBatch") public Result addOfBatch(String dirId, String type, @RequestParam(value = "datas") String datas) { // 转换json到对象 ObjectMapper mapper = new ObjectMapper(); try { ResDatas resDatas = mapper.readValue(datas, ResDatas.class); return resService.addBatchRes(dirId, type, resDatas.getItems()); } catch (IOException e) { e.printStackTrace(); } return new Result(false); } /** * 编辑 获取内容 * * @param id 资源id */ @GetMapping(value = "getResDetail") public Result getResDetail(String id,HttpServletResponse response) { Res res = resService.read(id); Result pathResult = resService.readAccessPath(id, null); return new Result(true, "success", CollectionUtils.newObjectMap("type", res.getType(), "name", res.getName(), "coverUrl", res.getCoverPageUrl(), "remark", res.getRemark(), "fullPath", pathResult.getDataT("path"))); } }