| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- 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;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author <a href="https://www.fengwenyi.com?code">Erwin Feng</a>
- * @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<Map<String, String>> mergeSubmit(@RequestBody CasePortraitVo archives) throws Exception {
- vadateImage(archives);
- <<<<<<< HEAD
- =======
-
- >>>>>>> 0c389bffe198548263c292beabdd50c2415fd8f3
- archivesService.mergeSubmit(archives);
- return ResponseDTO.success();
- }
- public void vadateImage(@RequestBody CasePortraitVo archives) {
- List<String> 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<Map<String, String>> 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<Map<String, String>> reMergeSubmit(@RequestBody CasePortraitVo archives) throws Exception {
- vadateImage(archives);
- archives.setIsRedo("1");
- archivesService.mergeSubmit(archives);
- return ResponseDTO.success();
- }
- /**
- * 文件和图片
- *
- * @return
- */
- @AnonymousPostMapping("/save")
- public ResponseDTO<Map<String, String>> 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<ArchivesEntity> 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<ArchivesEntity> 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<String, Object> 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<ArchivesEntity> edit(@RequestBody ArchivesEntity archives) {
- ArchivesEntity entity = archivesService.getById(archives.getId());
- QueryWrapper<ArchivesEntity> 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<Map<String, Object>> redo(@RequestBody ArchivesEntity archives) {
- ArchivesEntity entity = archivesService.getById(archives.getId());
- String imageJson = entity.getImageJson();
- List<JSONObject> jsonObjs = new ArrayList<>();
- List<String> pages = new ArrayList<>();
- if (StringUtils.isNotBlank(imageJson)) {
- CasePortraitVo caseVo = JSONObject.parseObject(imageJson, CasePortraitVo.class);
- List<String> imageIds = caseVo.getImageIds();
- List<FileInfoEntity> fileInfo = fileInfoService.listByIds(imageIds);
- Map<String, FileInfoEntity> 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<String, Object> result = new HashMap<>();
- result.put("imageList", jsonObjs);
- result.put("pages", pages);
- return ResponseDTO.success(result);
- }
- /**
- * 文件和图片
- *
- * @return
- */
- @AnonymousPostMapping("/caseNum")
- public ResponseDTO<Object> caseNum(@RequestBody CaseNumVo caseNumVo) {
- String year = caseNumVo.getYear();
- String catalogNum = caseNumVo.getCatalogNum();
- QueryWrapper<ArchivesEntity> qw = new QueryWrapper<>();
- qw.eq("annual", year);
- qw.eq("catalog_num", catalogNum);
- qw.select(" IFNULL(max(CAST(case_num as SIGNED)),0) caseNum");
- Map<String, Object> 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<ArchivesEntity> 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<ArchivesEntity> delete(@RequestBody List<String> id) {
- // List<ArchivesEntity> list = new ArrayList<ArchivesEntity>();
- // 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<FileInfoEntity> qw = new QueryWrapper<>();
- qw.in("loan_no", id);
- List<FileInfoEntity> fileInfos = fileInfoService.list(qw);
- List<String> fileIds = new ArrayList<>();
- List<String> 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<Object> list(ArchivesVo archives, Pageable pageable) {
- UserDetails userDetail = SecurityUtils.getCurrentUser();
- JwtUserDto jwtUser = (JwtUserDto) userDetail;
- //先判断是否是管理员
- UserLoginDto user= jwtUser.getUser();
- if(!user.getIsAdmin()) {
- //判断数据权限范围
- Set<RoleSmallDto> 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<String, Object> 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<String, Object> log) {
- ChromeLogEntity chrome = new ChromeLogEntity();
- chrome.setVueLog(JSONObject.toJSONString(log));
- chromeLogService.save(chrome);
- return ResponseDTO.success();
- }
- }
|