DocDirController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package com.doc.biz.controller;
  2. import com.doc.biz.domain.*;
  3. import com.doc.biz.service.*;
  4. import com.doc.biz.vo.MatchVO;
  5. import com.doc.biz.vo.TreeVO;
  6. import com.doc.common.annotation.Log;
  7. import com.doc.common.constant.Constants;
  8. import com.doc.common.core.controller.BaseController;
  9. import com.doc.common.core.domain.AjaxResult;
  10. import com.doc.common.core.domain.entity.SysRole;
  11. import com.doc.common.enums.BusinessType;
  12. import com.doc.common.enums.EventLevel;
  13. import com.doc.common.utils.DateUtils;
  14. import com.doc.common.utils.SecurityUtils;
  15. import com.doc.common.utils.poi.ExcelUtil;
  16. import io.swagger.annotations.*;
  17. import org.springframework.web.bind.annotation.*;
  18. import javax.annotation.Resource;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. /**
  23. * 文档目录管理Controller
  24. *
  25. * @author wukai
  26. * @date 2023-08-15
  27. */
  28. @Api(tags = "文档目录管理")
  29. @RestController
  30. @RequestMapping("/biz/dir")
  31. public class DocDirController extends BaseController {
  32. @Resource
  33. private IDocDirService docDirService;
  34. @Resource
  35. private IDocInfoService docInfoService;
  36. @Resource
  37. private IDocSpaceService spaceService;
  38. @Resource
  39. private IDocDirUserService dirUserService;
  40. @Resource
  41. private IDocFavoriteService favoriteService;
  42. @Resource
  43. private IDocRecentService recentService;
  44. /**
  45. * 获取顶层目录
  46. */
  47. @ApiOperation("获取顶层目录")
  48. @GetMapping(value = "/top-dir/{type}")
  49. public DocDir topDir(@ApiParam(value = "目录类型(1.公共 2.部门 3.个人)", required = true) @PathVariable("type") String type) {
  50. DocSpace space = spaceService.selectDocSpaceListByType(type);
  51. String dept = "2";
  52. if (space.getSpaceId() == null && dept.equals(type)) {
  53. spaceService.initDeptSpace(SecurityUtils.getLoginUser().getUser().getDept());
  54. space = spaceService.selectDocSpaceListByType(type);
  55. }
  56. DocDir dir = new DocDir();
  57. dir.setSpaceId(space.getSpaceId());
  58. dir.setParentId(0L);
  59. List<DocDir> dirList = docDirService.selectDocDirList(dir);
  60. if (dirList.size() > 0) {
  61. return dirList.get(0);
  62. } else {
  63. return null;
  64. }
  65. }
  66. /**
  67. * 获取目录树
  68. */
  69. @ApiOperation("目录树")
  70. @GetMapping(value = "/dir-tree/{type}")
  71. public TreeVO dirTree(@ApiParam(value = "目录类型(1.公共 2.部门 3.个人)", required = true) @PathVariable("type") String type) {
  72. return tree(type, dirTree);
  73. }
  74. /**
  75. * 文件树
  76. */
  77. private final String fileTree = "1";
  78. /**
  79. * 目录树
  80. */
  81. private final String dirTree = "2";
  82. /**
  83. * 图片树
  84. */
  85. private final String picTree = "3";
  86. /**
  87. * 获取文件树
  88. */
  89. @ApiOperation("文件树")
  90. @GetMapping(value = "/file-tree/{type}")
  91. public TreeVO fileTree(@ApiParam(value = "目录类型(1.公共 2.部门 3.个人)", required = true) @PathVariable("type") String type) {
  92. return tree(type, fileTree);
  93. }
  94. /**
  95. * 获取文件树
  96. */
  97. @ApiOperation("图片文件树")
  98. @GetMapping(value = "/pic-tree")
  99. public TreeVO picTree() {
  100. String type = "3";
  101. return tree(type, picTree);
  102. }
  103. /**
  104. * 获取树
  105. *
  106. * @param type 空间类型
  107. * @param st 1.文件树 2.目录树 3.图片树
  108. * @return
  109. */
  110. private TreeVO tree(String type, String st) {
  111. DocSpace space = spaceService.selectDocSpaceListByType(type);
  112. DocDir docDir = new DocDir();
  113. docDir.setSpaceId(space.getSpaceId());
  114. docDir.setParentId(0L);
  115. List<DocDir> list = docDirService.selectDocDirList(docDir);
  116. TreeVO vo = getChildren(list.get(0), st);
  117. return vo;
  118. }
  119. /**
  120. * 递归获取目录树
  121. *
  122. * @param dir 目录信息
  123. * @return
  124. */
  125. private TreeVO getChildren(DocDir dir, String st) {
  126. TreeVO vo = new TreeVO();
  127. vo.setId(dir.getDirId());
  128. vo.setLabel(dir.getDirName());
  129. List<TreeVO> children = new ArrayList<>();
  130. DocDir dir1 = new DocDir();
  131. dir1.setParentId(dir.getDirId());
  132. dir1.setSpaceId(dir.getSpaceId());
  133. docDirService.selectDocDirList(dir1).forEach(d -> {
  134. children.add(getChildren(d, st));
  135. });
  136. if (!dirTree.equals(st)) {
  137. //如果不是目录树
  138. vo.setDisabled(true);
  139. DocInfo info = new DocInfo();
  140. info.setDirId(dir.getDirId());
  141. docInfoService.selectDocInfoList(info).forEach(d -> {
  142. if (!picTree.equals(st) || Constants.IMAGE_EXTENSION.contains(d.getFileType())) {
  143. TreeVO childVO = new TreeVO();
  144. childVO.setId(d.getDocId());
  145. childVO.setLabel(d.getFileName());
  146. childVO.setRemark(d.getFileId());
  147. childVO.setDisabled(false);
  148. children.add(childVO);
  149. }
  150. });
  151. }
  152. vo.setChildren(children);
  153. return vo;
  154. }
  155. /**
  156. * 查询文档目录管理列表
  157. */
  158. @ApiOperation("查询目录列表")
  159. //@PreAuthorize("@ss.hasPermi('biz:dir:list')")
  160. @GetMapping("/list")
  161. public AjaxResult list(DocDir docDir) {
  162. List<DocDir> list = docDirService.selectDocDirList(docDir);
  163. DocDir dir = docDirService.selectDocDirByDirId(docDir.getParentId());
  164. if (dir.getParentId() != 0) {
  165. //如果不是顶层目录
  166. //插入最近访问记录
  167. DocRecent recent = new DocRecent();
  168. recent.setIsFolder("Y");
  169. recent.setOwner(SecurityUtils.getUserId());
  170. recent.setRelaId(dir.getDirId());
  171. recentService.insertDocRecent(recent);
  172. }
  173. return success(list);
  174. }
  175. /**
  176. * 查询文档目录管理列表
  177. */
  178. @ApiOperation("按空间类型查询目录列表")
  179. @GetMapping("/list/{type}")
  180. public AjaxResult listByType(@ApiParam(value = "目录类型(1.公共 2.部门 3.个人)", required = true) @PathVariable("type") String type, DocDir docDir) {
  181. String pub_t = "1";
  182. String dept_t = "2";
  183. String pub_k = "system";
  184. String dept_k = "dept";
  185. //如果不是个人空间,则需要按权限
  186. List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
  187. Set<String> roleKeys = roles.stream().map(SysRole::getRoleKey).collect(Collectors.toSet());
  188. if ((pub_t.equals(type) && !roleKeys.contains(pub_k)) || (dept_t.equals(type) && !roleKeys.contains(dept_k))) {
  189. //如果是公共空间,且不为系统管理员
  190. //如果是部门空间,且不为部门管理员
  191. //则需要判断组织目录是否有权限访问
  192. Map<String, Object> params = new HashMap<>(4);
  193. params.put("flag", 1);
  194. params.put("uid", SecurityUtils.getUserId());
  195. docDir.setParams(params);
  196. }
  197. return list(docDir);
  198. }
  199. /**
  200. * 导出文档目录管理列表
  201. */
  202. // @ApiOperation("导出文档目录管理列表")
  203. //@PreAuthorize("@ss.hasPermi('biz:dir:export')")
  204. @Log(title = "文档目录管理", businessType = BusinessType.EXPORT)
  205. @PostMapping("/export")
  206. public void export(HttpServletResponse response, DocDir docDir) {
  207. List<DocDir> list = docDirService.selectDocDirList(docDir);
  208. ExcelUtil<DocDir> util = new ExcelUtil<DocDir>(DocDir.class);
  209. util.exportExcel(response, list, "文档目录管理数据");
  210. }
  211. /**
  212. * 获取文档目录管理详细信息
  213. */
  214. @ApiOperation("获取文档目录管理详细信息")
  215. //@PreAuthorize("@ss.hasPermi('biz:dir:query')")
  216. @GetMapping(value = "/{dirId}")
  217. public AjaxResult getInfo(@PathVariable("dirId") Long dirId) {
  218. DocDir dir = docDirService.selectDocDirByDirId(dirId);
  219. DocDirUser dirUser = new DocDirUser();
  220. dirUser.setDirId(dirId);
  221. List<DocDirUser> users = dirUserService.selectDocDirUserList(dirUser);
  222. if (users.size() > 0) {
  223. dir.setUsers(users);
  224. }
  225. return success(dir);
  226. }
  227. /**
  228. * 新建目录
  229. */
  230. @ApiOperation("新建目录")
  231. //@PreAuthorize("@ss.hasPermi('biz:dir:add')")
  232. @Log(title = "文档目录管理", businessType = BusinessType.INSERT)
  233. @PostMapping
  234. public AjaxResult add(@RequestBody DocDir docDir) {
  235. checkDuplicateNames(docDir);
  236. int i = docDirService.insertDocDir(docDir);
  237. //如果是组织目录,则需要插入成员
  238. String dept = "2";
  239. if (dept.equals(docDir.getDirType())) {
  240. docDir.getUsers().forEach(u -> {
  241. u.setDirId(docDir.getDirId());
  242. dirUserService.insertDocDirUser(u);
  243. });
  244. }
  245. return toAjax(i);
  246. }
  247. /**
  248. * 检查重名
  249. *
  250. * @param docDir 目录信息
  251. */
  252. private void checkDuplicateNames(DocDir docDir) {
  253. DocDir query = new DocDir();
  254. query.setParentId(docDir.getParentId());
  255. query.setSearchValue(docDir.getDirName());
  256. List list = docDirService.selectDocDirList(query);
  257. if (list.size() > 0) {
  258. String dirName = docDir.getDirName();
  259. dirName = dirName + "-" + DateUtils.dateTimeNow();
  260. docDir.setDirName(dirName);
  261. }
  262. }
  263. /**
  264. * 新建目录
  265. */
  266. @ApiOperation("验证访问码")
  267. @Log(title = "文档目录管理", businessType = BusinessType.OTHER, eventLevel = EventLevel.HIGH)
  268. @PostMapping("/matches")
  269. public AjaxResult matches(@RequestBody MatchVO vo) {
  270. return success(docDirService.matchesCode(vo.getDirId(), vo.getCode()));
  271. }
  272. /**
  273. * 重命名
  274. */
  275. @ApiOperation("重命名")
  276. @Log(title = "文档目录管理", businessType = BusinessType.UPDATE)
  277. @GetMapping("/rename")
  278. @ApiImplicitParams({@ApiImplicitParam(name = "dirId", value = "目录ID", required = true), @ApiImplicitParam(name = "name", value = "新目录名", required = true)})
  279. public AjaxResult rename(Long dirId, String name) {
  280. DocDir docDir = docDirService.selectDocDirByDirId(dirId);
  281. docDir.setDirName(name);
  282. docDir.setUpdateBy(SecurityUtils.getUsername());
  283. checkDuplicateNames(docDir);
  284. return toAjax(docDirService.updateDocDir(docDir));
  285. }
  286. /**
  287. * 修改文档目录管理
  288. */
  289. @ApiOperation("修改目录信息")
  290. //@PreAuthorize("@ss.hasPermi('biz:dir:edit')")
  291. @Log(title = "文档目录管理", businessType = BusinessType.UPDATE)
  292. @PutMapping
  293. public AjaxResult edit(@RequestBody DocDir docDir) {
  294. //如果是部门目录,则需要插入成员
  295. String dept = "2";
  296. dirUserService.deleteDocDirUserByDirId(docDir.getDirId());
  297. if (dept.equals(docDir.getDirType()) && docDir.getUsers() != null) {
  298. docDir.getUsers().forEach(u -> {
  299. u.setDirId(docDir.getDirId());
  300. dirUserService.insertDocDirUser(u);
  301. });
  302. }
  303. return toAjax(docDirService.updateDocDir(docDir));
  304. }
  305. /**
  306. * 删除文档目录管理
  307. */
  308. @ApiOperation("删除目录")
  309. //@PreAuthorize("@ss.hasPermi('biz:dir:remove')")
  310. @Log(title = "文档目录管理", businessType = BusinessType.DELETE)
  311. @DeleteMapping("/{dirId}")
  312. public AjaxResult remove(@PathVariable Long dirId) {
  313. DocDir dir = new DocDir();
  314. dir.setParentId(dirId);
  315. List list = docDirService.selectDocDirList(dir);
  316. if (list.size() > 0) {
  317. return error("目录不为空,无法删除!");
  318. }
  319. DocInfo info = new DocInfo();
  320. info.setDirId(dirId);
  321. list = docInfoService.selectDocInfoList(info);
  322. if (list.size() > 0) {
  323. return error("目录不为空,无法删除!");
  324. }
  325. int i = docDirService.deleteDocDirByDirId(dirId);
  326. //删除收藏记录
  327. favoriteService.delete("Y", dirId);
  328. return toAjax(i);
  329. }
  330. }