package me.zhengjie.archives.controller; import java.io.File; import java.io.IOException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; <<<<<<< HEAD import org.springframework.security.core.userdetails.UserDetails; ======= >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3 import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.swagger.annotations.ApiOperation; import me.zhengjie.annotation.rest.AnonymousGetMapping; import me.zhengjie.annotation.rest.AnonymousPostMapping; import me.zhengjie.archives.entity.ArchivesEntity; import me.zhengjie.archives.entity.ChromeLogEntity; import me.zhengjie.archives.entity.FileInfoEntity; import me.zhengjie.archives.service.ArchivesService; import me.zhengjie.archives.service.ChromeLogService; import me.zhengjie.archives.service.FileInfoService; import me.zhengjie.archives.vo.ArchivesVo; import me.zhengjie.archives.vo.CaseNumVo; import me.zhengjie.archives.vo.CasePortraitVo; import me.zhengjie.base.ResponseDTO; import me.zhengjie.config.FileProperties; import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.mnt.service.dto.AppQueryCriteria; import me.zhengjie.modules.security.service.dto.JwtUserDto; import me.zhengjie.modules.system.service.DictDetailService; import me.zhengjie.modules.system.service.dto.RoleSmallDto; import me.zhengjie.modules.system.service.dto.UserLoginDto; import me.zhengjie.utils.SecurityUtils; /** *

* 前端控制器 *

* * @author Erwin Feng * @since 2023-04-18 */ @RestController @RequestMapping("/api/archives") public class ArchivesController { @Autowired DictDetailService dictDetailService; @Autowired ArchivesService archivesService; @Autowired FileInfoService fileInfoService; @Autowired ChromeLogService chromeLogService; @Autowired private FileProperties properties; /** * 文件和图片 * * @return * @throws Exception */ @AnonymousPostMapping("/mergeSubmit") public ResponseDTO> mergeSubmit(@RequestBody CasePortraitVo archives) throws Exception { vadateImage(archives); <<<<<<< HEAD ======= >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3 archivesService.mergeSubmit(archives); return ResponseDTO.success(); } public void vadateImage(@RequestBody CasePortraitVo archives) { List images = archives.getImageIds(); if (CollectionUtils.isEmpty(archives.getImageIds())) { throw new BadRequestException("当前图片为空!"); } // 判断图片是否含有null for (int j = 0; j < images.size(); j++) { if (StringUtils.isEmpty(images.get(j)) || images.get(j).equalsIgnoreCase("null") || images.get(j).equalsIgnoreCase("undefined")) { throw new BadRequestException("当前第" + j + "页码是空值!"); } } if (CollectionUtils.isEmpty(archives.getPages())) { throw new BadRequestException("当页码不能为空!"); } } /** * 文件和图片 * * @return * @throws Exception */ @AnonymousPostMapping("/stage") public ResponseDTO> stage(@RequestBody CasePortraitVo archives) throws Exception { vadateImage(archives); ArchivesEntity archiveEntity = new ArchivesEntity(); archiveEntity.setId(archives.getId()); <<<<<<< HEAD // 0是为提交,1是以提交,2是合并中,3是暂存 ======= //0是为提交,1是以提交,2是合并中,3是暂存 >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3 archiveEntity.setStatus("3"); archiveEntity.setImageJson(JSON.toJSONString(archives)); archivesService.updateById(archiveEntity); return ResponseDTO.success(); } /** * 文件和图片 * * @return * @throws Exception */ @AnonymousPostMapping("/reMergeSubmit") public ResponseDTO> reMergeSubmit(@RequestBody CasePortraitVo archives) throws Exception { vadateImage(archives); archives.setIsRedo("1"); archivesService.mergeSubmit(archives); return ResponseDTO.success(); } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/save") public ResponseDTO> save(@RequestBody ArchivesEntity archives) { archives.setCreateBy("1"); archives.setUpdateBy("1"); archives.setIsDelete("1"); archives.setStatus("0"); archives.setIsClean("0"); archives.setRedoCount(0); archives.setCleanCount(0); archives.setPdfPath(""); archives.setCreateTime(LocalDateTime.now()); archives.setUpdateTime(LocalDateTime.now()); archives.setImageJson(""); QueryWrapper qw = new QueryWrapper<>(); qw.eq("archives_num", archives.getArchivesNum()); long result = archivesService.count(qw); if (result >= 1) { // return ResponseDTO.error(ResultCode.ARCHIVES_NUM_ALREADY_EXISTS, // archives.getArchivesNum()); throw new BadRequestException(archives.getArchivesNum() + "档案号已存在!"); } validateCaseNum(archives); archives.setId(null); archivesService.save(archives); return ResponseDTO.success(); } private void validateCaseNum(ArchivesEntity archives) { String year = archives.getAnnual(); String catalogNum = archives.getCatalogNum(); QueryWrapper qw = new QueryWrapper<>(); qw.eq("annual", year); qw.eq("catalog_num", catalogNum); qw.select(" IFNULL(max(CAST(case_num as SIGNED)),0) caseNum"); <<<<<<< HEAD ======= >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3 Map map = archivesService.getMap(qw); Object caseNum = map.get("caseNum"); if (caseNum != null) { if (caseNum.equals("0")) { String catalogNumYear = "catalog_num_" + year; String value = dictDetailService.getLabelValue(catalogNumYear, catalogNum); if (value == null) { throw new BadRequestException("输入年份和目录号在字典不存在默认值。"); } caseNum = value; } int yearMaxCaseNum = Integer.parseInt(caseNum.toString()); int inputCaseNum = Integer.parseInt(archives.getCaseNum()); if (inputCaseNum < yearMaxCaseNum) { throw new BadRequestException("当前" + archives.getAnnual() + "年,最大的案卷号是“" + yearMaxCaseNum + "“"); } } } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/edit") public ResponseDTO edit(@RequestBody ArchivesEntity archives) { ArchivesEntity entity = archivesService.getById(archives.getId()); QueryWrapper qw = new QueryWrapper<>(); qw.ne("archives_num", entity.getArchivesNum()); qw.eq("archives_num", archives.getArchivesNum()); long result = archivesService.count(qw); // 过来掉自己 if (result >= 1) { // return ResponseDTO.error(ResultCode.ARCHIVES_NUM_ALREADY_EXISTS, // archives.getArchivesNum()); throw new BadRequestException(archives.getArchivesNum() + "档案号已存在!"); } // 验证案卷号是否正确 // validateCaseNum(archives); archives.setPdfPath(null); archivesService.updateById(archives); return ResponseDTO.success(); } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/redo") public ResponseDTO> redo(@RequestBody ArchivesEntity archives) { ArchivesEntity entity = archivesService.getById(archives.getId()); String imageJson = entity.getImageJson(); List jsonObjs = new ArrayList<>(); List pages = new ArrayList<>(); if (StringUtils.isNotBlank(imageJson)) { CasePortraitVo caseVo = JSONObject.parseObject(imageJson, CasePortraitVo.class); List imageIds = caseVo.getImageIds(); List fileInfo = fileInfoService.listByIds(imageIds); Map fileInfoMap = new HashMap<>(); for (FileInfoEntity file : fileInfo) { fileInfoMap.put(file.getId(), file); } String previewPath = dictDetailService.getValueByName("file_path").get("preview_path"); for (String imageId : imageIds) { JSONObject json = new JSONObject(); json.put("imageId", imageId); json.put("fileUrl", previewPath + fileInfoMap.get(imageId).getPath()); json.put("smallUrl", previewPath + fileInfoMap.get(imageId).getSmallPath()); jsonObjs.add(json); } pages = caseVo.getPages(); } Map result = new HashMap<>(); result.put("imageList", jsonObjs); result.put("pages", pages); return ResponseDTO.success(result); } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/caseNum") public ResponseDTO caseNum(@RequestBody CaseNumVo caseNumVo) { String year = caseNumVo.getYear(); String catalogNum = caseNumVo.getCatalogNum(); QueryWrapper qw = new QueryWrapper<>(); qw.eq("annual", year); qw.eq("catalog_num", catalogNum); qw.select(" IFNULL(max(CAST(case_num as SIGNED)),0) caseNum"); Map map = archivesService.getMap(qw); Object caseNum = map.get("caseNum"); if (map == null || caseNum.toString().equals("0")) { String catalogNumYear = "catalog_num_" + year; String value = dictDetailService.getLabelValue(catalogNumYear, catalogNum); if (value == null) { throw new BadRequestException("输入年份和目录号在字典不存在默认值。"); } return ResponseDTO.success(Integer.valueOf(value) + 1); } return ResponseDTO.success(Integer.valueOf(caseNum.toString()) + 1); } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/detail") public ResponseDTO detail(@RequestBody ArchivesVo archives) { ArchivesEntity entity = new ArchivesEntity(); entity = archivesService.getById(archives.getId()); <<<<<<< HEAD ======= >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3 String previewPath = dictDetailService.getValueByName("file_path").get("preview_path"); entity.setPdfPath(previewPath + entity.getPdfPath()); return ResponseDTO.success(entity); } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/delete") public ResponseDTO delete(@RequestBody List id) { // List list = new ArrayList(); // for (int i = 0; i < id.size(); i++) { // ArchivesEntity entity = new ArchivesEntity(); // entity.setId(Long.valueOf(id.get(i))); // entity.setIsDelete("0"); // list.add(entity); // } // 先删除图片, QueryWrapper qw = new QueryWrapper<>(); qw.in("loan_no", id); List fileInfos = fileInfoService.list(qw); List fileIds = new ArrayList<>(); List filePath = new ArrayList<>(); // FileUtils.forceDelete(arg0); String path = properties.getPath().getPath(); for (FileInfoEntity f : fileInfos) { fileIds.add(f.getId()); filePath.add(path + f.getPath()); filePath.add(path + f.getSmallPath()); } for (String imagePath : filePath) { FileUtils.deleteQuietly(new File(imagePath)); } fileInfoService.removeBatchByIds(fileIds); archivesService.removeByIds(id); // return ResponseDTO.success(); } /** * 文件和图片 * * @return */ @AnonymousGetMapping("/list") public ResponseEntity list(ArchivesVo archives, Pageable pageable) { UserDetails userDetail = SecurityUtils.getCurrentUser(); JwtUserDto jwtUser = (JwtUserDto) userDetail; //先判断是否是管理员 UserLoginDto user= jwtUser.getUser(); if(!user.getIsAdmin()) { //判断数据权限范围 Set roles=user.getRoles(); //如果选择全部则,查询所有,否则查询当个 boolean allScope=false; for (RoleSmallDto role:roles) { if(role.getDataScope().equals("全部")) { allScope=true; } } if(!allScope) { archives.setNotary(user.getNickName().trim()); } } archives.setIndex(pageable.getPageNumber()); archives.setSize(pageable.getPageSize()); Map map = archivesService.page(archives); return new ResponseEntity<>(map, HttpStatus.OK); } @AnonymousGetMapping(value = "/download") public void download(ArchivesVo archives, HttpServletResponse response) throws IOException { archivesService.download(archives, response); } /** * 文件和图片 * * @return */ @AnonymousPostMapping("/log") public ResponseDTO log(@RequestBody Map log) { ChromeLogEntity chrome = new ChromeLogEntity(); chrome.setVueLog(JSONObject.toJSONString(log)); chromeLogService.save(chrome); return ResponseDTO.success(); } }