ArchivesController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package me.zhengjie.archives.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.time.LocalDateTime;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.commons.collections4.CollectionUtils;
  12. import org.apache.commons.io.FileUtils;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.data.domain.Pageable;
  16. import org.springframework.http.HttpStatus;
  17. import org.springframework.http.ResponseEntity;
  18. <<<<<<< HEAD
  19. import org.springframework.security.core.userdetails.UserDetails;
  20. =======
  21. >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3
  22. import org.springframework.web.bind.annotation.RequestBody;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import com.alibaba.fastjson.JSON;
  26. import com.alibaba.fastjson.JSONObject;
  27. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  28. import io.swagger.annotations.ApiOperation;
  29. import me.zhengjie.annotation.rest.AnonymousGetMapping;
  30. import me.zhengjie.annotation.rest.AnonymousPostMapping;
  31. import me.zhengjie.archives.entity.ArchivesEntity;
  32. import me.zhengjie.archives.entity.ChromeLogEntity;
  33. import me.zhengjie.archives.entity.FileInfoEntity;
  34. import me.zhengjie.archives.service.ArchivesService;
  35. import me.zhengjie.archives.service.ChromeLogService;
  36. import me.zhengjie.archives.service.FileInfoService;
  37. import me.zhengjie.archives.vo.ArchivesVo;
  38. import me.zhengjie.archives.vo.CaseNumVo;
  39. import me.zhengjie.archives.vo.CasePortraitVo;
  40. import me.zhengjie.base.ResponseDTO;
  41. import me.zhengjie.config.FileProperties;
  42. import me.zhengjie.exception.BadRequestException;
  43. import me.zhengjie.modules.mnt.service.dto.AppQueryCriteria;
  44. import me.zhengjie.modules.security.service.dto.JwtUserDto;
  45. import me.zhengjie.modules.system.service.DictDetailService;
  46. import me.zhengjie.modules.system.service.dto.RoleSmallDto;
  47. import me.zhengjie.modules.system.service.dto.UserLoginDto;
  48. import me.zhengjie.utils.SecurityUtils;
  49. /**
  50. * <p>
  51. * 前端控制器
  52. * </p>
  53. *
  54. * @author <a href="https://www.fengwenyi.com?code">Erwin Feng</a>
  55. * @since 2023-04-18
  56. */
  57. @RestController
  58. @RequestMapping("/api/archives")
  59. public class ArchivesController {
  60. @Autowired
  61. DictDetailService dictDetailService;
  62. @Autowired
  63. ArchivesService archivesService;
  64. @Autowired
  65. FileInfoService fileInfoService;
  66. @Autowired
  67. ChromeLogService chromeLogService;
  68. @Autowired
  69. private FileProperties properties;
  70. /**
  71. * 文件和图片
  72. *
  73. * @return
  74. * @throws Exception
  75. */
  76. @AnonymousPostMapping("/mergeSubmit")
  77. public ResponseDTO<Map<String, String>> mergeSubmit(@RequestBody CasePortraitVo archives) throws Exception {
  78. vadateImage(archives);
  79. <<<<<<< HEAD
  80. =======
  81. >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3
  82. archivesService.mergeSubmit(archives);
  83. return ResponseDTO.success();
  84. }
  85. public void vadateImage(@RequestBody CasePortraitVo archives) {
  86. List<String> images = archives.getImageIds();
  87. if (CollectionUtils.isEmpty(archives.getImageIds())) {
  88. throw new BadRequestException("当前图片为空!");
  89. }
  90. // 判断图片是否含有null
  91. for (int j = 0; j < images.size(); j++) {
  92. if (StringUtils.isEmpty(images.get(j)) || images.get(j).equalsIgnoreCase("null")
  93. || images.get(j).equalsIgnoreCase("undefined")) {
  94. throw new BadRequestException("当前第" + j + "页码是空值!");
  95. }
  96. }
  97. if (CollectionUtils.isEmpty(archives.getPages())) {
  98. throw new BadRequestException("当页码不能为空!");
  99. }
  100. }
  101. /**
  102. * 文件和图片
  103. *
  104. * @return
  105. * @throws Exception
  106. */
  107. @AnonymousPostMapping("/stage")
  108. public ResponseDTO<Map<String, String>> stage(@RequestBody CasePortraitVo archives) throws Exception {
  109. vadateImage(archives);
  110. ArchivesEntity archiveEntity = new ArchivesEntity();
  111. archiveEntity.setId(archives.getId());
  112. <<<<<<< HEAD
  113. // 0是为提交,1是以提交,2是合并中,3是暂存
  114. =======
  115. //0是为提交,1是以提交,2是合并中,3是暂存
  116. >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3
  117. archiveEntity.setStatus("3");
  118. archiveEntity.setImageJson(JSON.toJSONString(archives));
  119. archivesService.updateById(archiveEntity);
  120. return ResponseDTO.success();
  121. }
  122. /**
  123. * 文件和图片
  124. *
  125. * @return
  126. * @throws Exception
  127. */
  128. @AnonymousPostMapping("/reMergeSubmit")
  129. public ResponseDTO<Map<String, String>> reMergeSubmit(@RequestBody CasePortraitVo archives) throws Exception {
  130. vadateImage(archives);
  131. archives.setIsRedo("1");
  132. archivesService.mergeSubmit(archives);
  133. return ResponseDTO.success();
  134. }
  135. /**
  136. * 文件和图片
  137. *
  138. * @return
  139. */
  140. @AnonymousPostMapping("/save")
  141. public ResponseDTO<Map<String, String>> save(@RequestBody ArchivesEntity archives) {
  142. archives.setCreateBy("1");
  143. archives.setUpdateBy("1");
  144. archives.setIsDelete("1");
  145. archives.setStatus("0");
  146. archives.setIsClean("0");
  147. archives.setRedoCount(0);
  148. archives.setCleanCount(0);
  149. archives.setPdfPath("");
  150. archives.setCreateTime(LocalDateTime.now());
  151. archives.setUpdateTime(LocalDateTime.now());
  152. archives.setImageJson("");
  153. QueryWrapper<ArchivesEntity> qw = new QueryWrapper<>();
  154. qw.eq("archives_num", archives.getArchivesNum());
  155. long result = archivesService.count(qw);
  156. if (result >= 1) {
  157. // return ResponseDTO.error(ResultCode.ARCHIVES_NUM_ALREADY_EXISTS,
  158. // archives.getArchivesNum());
  159. throw new BadRequestException(archives.getArchivesNum() + "档案号已存在!");
  160. }
  161. validateCaseNum(archives);
  162. archives.setId(null);
  163. archivesService.save(archives);
  164. return ResponseDTO.success();
  165. }
  166. private void validateCaseNum(ArchivesEntity archives) {
  167. String year = archives.getAnnual();
  168. String catalogNum = archives.getCatalogNum();
  169. QueryWrapper<ArchivesEntity> qw = new QueryWrapper<>();
  170. qw.eq("annual", year);
  171. qw.eq("catalog_num", catalogNum);
  172. qw.select(" IFNULL(max(CAST(case_num as SIGNED)),0) caseNum");
  173. <<<<<<< HEAD
  174. =======
  175. >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3
  176. Map<String, Object> map = archivesService.getMap(qw);
  177. Object caseNum = map.get("caseNum");
  178. if (caseNum != null) {
  179. if (caseNum.equals("0")) {
  180. String catalogNumYear = "catalog_num_" + year;
  181. String value = dictDetailService.getLabelValue(catalogNumYear, catalogNum);
  182. if (value == null) {
  183. throw new BadRequestException("输入年份和目录号在字典不存在默认值。");
  184. }
  185. caseNum = value;
  186. }
  187. int yearMaxCaseNum = Integer.parseInt(caseNum.toString());
  188. int inputCaseNum = Integer.parseInt(archives.getCaseNum());
  189. if (inputCaseNum < yearMaxCaseNum) {
  190. throw new BadRequestException("当前" + archives.getAnnual() + "年,最大的案卷号是“" + yearMaxCaseNum + "“");
  191. }
  192. }
  193. }
  194. /**
  195. * 文件和图片
  196. *
  197. * @return
  198. */
  199. @AnonymousPostMapping("/edit")
  200. public ResponseDTO<ArchivesEntity> edit(@RequestBody ArchivesEntity archives) {
  201. ArchivesEntity entity = archivesService.getById(archives.getId());
  202. QueryWrapper<ArchivesEntity> qw = new QueryWrapper<>();
  203. qw.ne("archives_num", entity.getArchivesNum());
  204. qw.eq("archives_num", archives.getArchivesNum());
  205. long result = archivesService.count(qw);
  206. // 过来掉自己
  207. if (result >= 1) {
  208. // return ResponseDTO.error(ResultCode.ARCHIVES_NUM_ALREADY_EXISTS,
  209. // archives.getArchivesNum());
  210. throw new BadRequestException(archives.getArchivesNum() + "档案号已存在!");
  211. }
  212. // 验证案卷号是否正确
  213. // validateCaseNum(archives);
  214. archives.setPdfPath(null);
  215. archivesService.updateById(archives);
  216. return ResponseDTO.success();
  217. }
  218. /**
  219. * 文件和图片
  220. *
  221. * @return
  222. */
  223. @AnonymousPostMapping("/redo")
  224. public ResponseDTO<Map<String, Object>> redo(@RequestBody ArchivesEntity archives) {
  225. ArchivesEntity entity = archivesService.getById(archives.getId());
  226. String imageJson = entity.getImageJson();
  227. List<JSONObject> jsonObjs = new ArrayList<>();
  228. List<String> pages = new ArrayList<>();
  229. if (StringUtils.isNotBlank(imageJson)) {
  230. CasePortraitVo caseVo = JSONObject.parseObject(imageJson, CasePortraitVo.class);
  231. List<String> imageIds = caseVo.getImageIds();
  232. List<FileInfoEntity> fileInfo = fileInfoService.listByIds(imageIds);
  233. Map<String, FileInfoEntity> fileInfoMap = new HashMap<>();
  234. for (FileInfoEntity file : fileInfo) {
  235. fileInfoMap.put(file.getId(), file);
  236. }
  237. String previewPath = dictDetailService.getValueByName("file_path").get("preview_path");
  238. for (String imageId : imageIds) {
  239. JSONObject json = new JSONObject();
  240. json.put("imageId", imageId);
  241. json.put("fileUrl", previewPath + fileInfoMap.get(imageId).getPath());
  242. json.put("smallUrl", previewPath + fileInfoMap.get(imageId).getSmallPath());
  243. jsonObjs.add(json);
  244. }
  245. pages = caseVo.getPages();
  246. }
  247. Map<String, Object> result = new HashMap<>();
  248. result.put("imageList", jsonObjs);
  249. result.put("pages", pages);
  250. return ResponseDTO.success(result);
  251. }
  252. /**
  253. * 文件和图片
  254. *
  255. * @return
  256. */
  257. @AnonymousPostMapping("/caseNum")
  258. public ResponseDTO<Object> caseNum(@RequestBody CaseNumVo caseNumVo) {
  259. String year = caseNumVo.getYear();
  260. String catalogNum = caseNumVo.getCatalogNum();
  261. QueryWrapper<ArchivesEntity> qw = new QueryWrapper<>();
  262. qw.eq("annual", year);
  263. qw.eq("catalog_num", catalogNum);
  264. qw.select(" IFNULL(max(CAST(case_num as SIGNED)),0) caseNum");
  265. Map<String, Object> map = archivesService.getMap(qw);
  266. Object caseNum = map.get("caseNum");
  267. if (map == null || caseNum.toString().equals("0")) {
  268. String catalogNumYear = "catalog_num_" + year;
  269. String value = dictDetailService.getLabelValue(catalogNumYear, catalogNum);
  270. if (value == null) {
  271. throw new BadRequestException("输入年份和目录号在字典不存在默认值。");
  272. }
  273. return ResponseDTO.success(Integer.valueOf(value) + 1);
  274. }
  275. return ResponseDTO.success(Integer.valueOf(caseNum.toString()) + 1);
  276. }
  277. /**
  278. * 文件和图片
  279. *
  280. * @return
  281. */
  282. @AnonymousPostMapping("/detail")
  283. public ResponseDTO<ArchivesEntity> detail(@RequestBody ArchivesVo archives) {
  284. ArchivesEntity entity = new ArchivesEntity();
  285. entity = archivesService.getById(archives.getId());
  286. <<<<<<< HEAD
  287. =======
  288. >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3
  289. String previewPath = dictDetailService.getValueByName("file_path").get("preview_path");
  290. entity.setPdfPath(previewPath + entity.getPdfPath());
  291. return ResponseDTO.success(entity);
  292. }
  293. /**
  294. * 文件和图片
  295. *
  296. * @return
  297. */
  298. @AnonymousPostMapping("/delete")
  299. public ResponseDTO<ArchivesEntity> delete(@RequestBody List<String> id) {
  300. // List<ArchivesEntity> list = new ArrayList<ArchivesEntity>();
  301. // for (int i = 0; i < id.size(); i++) {
  302. // ArchivesEntity entity = new ArchivesEntity();
  303. // entity.setId(Long.valueOf(id.get(i)));
  304. // entity.setIsDelete("0");
  305. // list.add(entity);
  306. // }
  307. // 先删除图片,
  308. QueryWrapper<FileInfoEntity> qw = new QueryWrapper<>();
  309. qw.in("loan_no", id);
  310. List<FileInfoEntity> fileInfos = fileInfoService.list(qw);
  311. List<String> fileIds = new ArrayList<>();
  312. List<String> filePath = new ArrayList<>();
  313. // FileUtils.forceDelete(arg0);
  314. String path = properties.getPath().getPath();
  315. for (FileInfoEntity f : fileInfos) {
  316. fileIds.add(f.getId());
  317. filePath.add(path + f.getPath());
  318. filePath.add(path + f.getSmallPath());
  319. }
  320. for (String imagePath : filePath) {
  321. FileUtils.deleteQuietly(new File(imagePath));
  322. }
  323. fileInfoService.removeBatchByIds(fileIds);
  324. archivesService.removeByIds(id);
  325. //
  326. return ResponseDTO.success();
  327. }
  328. /**
  329. * 文件和图片
  330. *
  331. * @return
  332. */
  333. @AnonymousGetMapping("/list")
  334. public ResponseEntity<Object> list(ArchivesVo archives, Pageable pageable) {
  335. UserDetails userDetail = SecurityUtils.getCurrentUser();
  336. JwtUserDto jwtUser = (JwtUserDto) userDetail;
  337. //先判断是否是管理员
  338. UserLoginDto user= jwtUser.getUser();
  339. if(!user.getIsAdmin()) {
  340. //判断数据权限范围
  341. Set<RoleSmallDto> roles=user.getRoles();
  342. //如果选择全部则,查询所有,否则查询当个
  343. boolean allScope=false;
  344. for (RoleSmallDto role:roles) {
  345. if(role.getDataScope().equals("全部")) {
  346. allScope=true;
  347. }
  348. }
  349. if(!allScope) {
  350. archives.setNotary(user.getNickName().trim());
  351. }
  352. }
  353. archives.setIndex(pageable.getPageNumber());
  354. archives.setSize(pageable.getPageSize());
  355. Map<String, Object> map = archivesService.page(archives);
  356. return new ResponseEntity<>(map, HttpStatus.OK);
  357. }
  358. @AnonymousGetMapping(value = "/download")
  359. public void download(ArchivesVo archives, HttpServletResponse response) throws IOException {
  360. archivesService.download(archives, response);
  361. }
  362. /**
  363. * 文件和图片
  364. *
  365. * @return
  366. */
  367. @AnonymousPostMapping("/log")
  368. public ResponseDTO<?> log(@RequestBody Map<String, Object> log) {
  369. ChromeLogEntity chrome = new ChromeLogEntity();
  370. chrome.setVueLog(JSONObject.toJSONString(log));
  371. chromeLogService.save(chrome);
  372. return ResponseDTO.success();
  373. }
  374. }