ReportController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package jnpf.controller;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.http.Method;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.Parameter;
  6. import io.swagger.v3.oas.annotations.Parameters;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import jnpf.base.ActionResult;
  9. import jnpf.base.vo.DownloadVO;
  10. import jnpf.base.vo.ListVO;
  11. import jnpf.base.vo.PageListVO;
  12. import jnpf.base.vo.PaginationVO;
  13. import jnpf.constant.CodeConst;
  14. import jnpf.constant.FileTypeConstant;
  15. import jnpf.constant.GlobalConst;
  16. import jnpf.constant.MsgCode;
  17. import jnpf.consts.ApiConst;
  18. import jnpf.emnus.ModuleTypeEnum;
  19. import jnpf.entity.*;
  20. import jnpf.exception.DataException;
  21. import jnpf.model.data.DataSetInfo;
  22. import jnpf.model.data.MenuModel;
  23. import jnpf.model.data.ModuleNameVO;
  24. import jnpf.model.report.*;
  25. import jnpf.service.*;
  26. import jnpf.util.*;
  27. import jnpf.util.context.RequestContext;
  28. import lombok.extern.java.Log;
  29. import lombok.extern.log4j.Log4j;
  30. import lombok.extern.log4j.Log4j2;
  31. import org.apache.commons.collections4.CollectionUtils;
  32. import org.dromara.x.file.storage.core.FileInfo;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.http.MediaType;
  35. import org.springframework.web.bind.annotation.*;
  36. import org.springframework.web.multipart.MultipartFile;
  37. import java.util.*;
  38. import java.util.stream.Collectors;
  39. /**
  40. * @author :JNPF开发平台组
  41. * @version: V3.1.0
  42. * @copyright 引迈信息技术有限公司
  43. * @date :2024/5/11 下午4:35
  44. */
  45. @Tag(name = "报表信息", description = "Report")
  46. @RestController
  47. @RequestMapping("/api/Report")
  48. @Log4j2
  49. public class ReportController {
  50. @Autowired
  51. private ReportService reportService;
  52. @Autowired
  53. private UserService userService;
  54. @Autowired
  55. private DictionaryDataService dictionaryDataService;
  56. @Autowired
  57. private ReportVersionService versionService;
  58. @Autowired
  59. private SystemService systemService;
  60. @Autowired
  61. private CodeNumService codeNumService;
  62. @Operation(summary = "列表")
  63. @GetMapping
  64. public ActionResult<PageListVO<ReportListVO>> list(ReportPagination paginationPrint) {
  65. SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
  66. if (BeanUtil.isNotEmpty(infoByEnCode)) {
  67. paginationPrint.setSystemId(infoByEnCode.getId());
  68. }
  69. List<ReportEntity> list = reportService.getList(paginationPrint);
  70. List<String> userId = new ArrayList<>();
  71. userId.addAll(list.stream().map(ReportEntity::getCreatorUserId).filter(StringUtil::isNotEmpty).collect(Collectors.toList()));
  72. userId.addAll(list.stream().map(ReportEntity::getLastModifyUserId).filter(StringUtil::isNotEmpty).collect(Collectors.toList()));
  73. List<UserEntity> userList = userService.getUserName(userId);
  74. List<String> dictionary = list.stream().map(ReportEntity::getCategory).collect(Collectors.toList());
  75. List<DictionaryDataEntity> dictionList = dictionaryDataService.getDictionName(dictionary);
  76. List<ReportListVO> listVOS = new ArrayList<>();
  77. for (ReportEntity entity : list) {
  78. ReportListVO vo = JsonUtil.getJsonToBean(entity, ReportListVO.class);
  79. vo.setState(vo.getEnabledMark());
  80. DictionaryDataEntity dataEntity = dictionList.stream().filter(t -> t.getId().equals(entity.getCategory())).findFirst().orElse(null);
  81. vo.setCategory(dataEntity != null ? dataEntity.getFullName() : "");
  82. //创建者
  83. UserEntity creatorUser = userList.stream().filter(t -> t.getId().equals(entity.getCreatorUserId())).findFirst().orElse(null);
  84. vo.setCreatorUser(creatorUser != null ? creatorUser.getRealName() + "/" + creatorUser.getAccount() : entity.getCreatorUserId());
  85. //修改人
  86. UserEntity lastModifyUser = userList.stream().filter(t -> t.getId().equals(entity.getLastModifyUserId())).findFirst().orElse(null);
  87. vo.setLastModifyUser(lastModifyUser != null ? lastModifyUser.getRealName() + "/" + lastModifyUser.getAccount() : entity.getLastModifyUserId());
  88. listVOS.add(vo);
  89. }
  90. PaginationVO paginationVO = JsonUtil.getJsonToBean(paginationPrint, PaginationVO.class);
  91. return ActionResult.page(listVOS, paginationVO);
  92. }
  93. @PostMapping
  94. @Operation(summary = "新建")
  95. @Parameters({
  96. @Parameter(name = "form", description = "模型", required = true),
  97. })
  98. public ActionResult create(@RequestBody ReportCrForm form) {
  99. form.setAllowExport(1);
  100. form.setAllowPrint(1);
  101. SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
  102. if (BeanUtil.isNotEmpty(infoByEnCode)) {
  103. form.setSystemId(infoByEnCode.getId());
  104. }
  105. reportService.create(form);
  106. return ActionResult.success(MsgCode.SU001.get(), form.getId());
  107. }
  108. @Operation(summary = "详情")
  109. @Parameters({
  110. @Parameter(name = "id", description = "模板id")
  111. })
  112. @GetMapping("/{id}")
  113. public ActionResult<ReportInfoVO> info(@PathVariable("id") String id) {
  114. ReportEntity byId = reportService.getById(id);
  115. ReportInfoVO vo = JsonUtil.getJsonToBean(byId, ReportInfoVO.class);
  116. return ActionResult.success(vo);
  117. }
  118. @Operation(summary = "更新")
  119. @PutMapping("/{id}")
  120. @Parameters({
  121. @Parameter(name = "id", description = "主键", required = true),
  122. @Parameter(name = "form", description = "模型", required = true),
  123. })
  124. public ActionResult update(@PathVariable("id") String id, @RequestBody ReportUpForm form) {
  125. ReportEntity entity = JsonUtil.getJsonToBean(form, ReportEntity.class);
  126. SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
  127. if (BeanUtil.isNotEmpty(infoByEnCode)) {
  128. entity.setSystemId(infoByEnCode.getId());
  129. }
  130. reportService.update(id, entity);
  131. return ActionResult.success(MsgCode.SU004.get());
  132. }
  133. @Operation(summary = "删除")
  134. @Parameters({
  135. @Parameter(name = "id", description = "模板id", required = true)
  136. })
  137. @DeleteMapping("/{id}")
  138. public ActionResult delete(@PathVariable String id) {
  139. if (reportService.getById(id) != null) {
  140. reportService.delete(id);
  141. return ActionResult.success(MsgCode.SU003.get());
  142. } else {
  143. return ActionResult.fail(MsgCode.FA003.get());
  144. }
  145. }
  146. /*============版本增删改==============*/
  147. @Operation(summary = "版本详情")
  148. @Parameters({
  149. @Parameter(name = "versionId", description = "版本id", required = true)
  150. })
  151. @GetMapping("/Info/{versionId}")
  152. public ActionResult<ReportInfoVO> versionInfo(@PathVariable String versionId) {
  153. ReportInfoVO info = reportService.getVersionInfo(versionId);
  154. return ActionResult.success(info);
  155. }
  156. @Operation(summary = "版本新增")
  157. @Parameters({
  158. @Parameter(name = "versionId", description = "版本id", required = true)
  159. })
  160. @PostMapping("/Info/{versionId}")
  161. public ActionResult copyVersion(@PathVariable String versionId) {
  162. String newVersionId = versionService.copyVersion(versionId);
  163. return ActionResult.success(MsgCode.SU005.get(), newVersionId);
  164. }
  165. @Operation(summary = "版本删除")
  166. @Parameters({
  167. @Parameter(name = "versionId", description = "版本id", required = true)
  168. })
  169. @DeleteMapping("/Info/{versionId}")
  170. public ActionResult deleteVersion(@PathVariable String versionId) {
  171. ReportVersionEntity entity = versionService.getById(versionId);
  172. if (entity != null) {
  173. List<ReportVersionEntity> list = versionService.getList(entity.getTemplateId(), ReportVersionEntity::getId);
  174. if (list.size() == 1) {
  175. return ActionResult.fail(MsgCode.SYS043.get());
  176. }
  177. if (Objects.equals(entity.getState(), 1)) {
  178. return ActionResult.fail(MsgCode.SYS044.get());
  179. }
  180. if (Objects.equals(entity.getState(), 2)) {
  181. return ActionResult.fail(MsgCode.SYS045.get());
  182. }
  183. versionService.removeById(versionId);
  184. }
  185. return ActionResult.success(MsgCode.SU003.get());
  186. }
  187. @Operation(summary = "版本列表")
  188. @Parameters({
  189. @Parameter(name = "id", description = "模板id", required = true)
  190. })
  191. @GetMapping("/Version/{id}")
  192. public ActionResult<List<ReportVersionListVO>> versionList(@PathVariable String id) {
  193. List<ReportVersionEntity> list = versionService.getList(id, ReportVersionEntity::getId, ReportVersionEntity::getState, ReportVersionEntity::getVersion);
  194. List<ReportVersionListVO> listVO = new ArrayList<>();
  195. for (ReportVersionEntity jsonEntity : list) {
  196. ReportVersionListVO vo = JsonUtil.getJsonToBean(jsonEntity, ReportVersionListVO.class);
  197. vo.setFullName("报表版本V" + vo.getVersion());
  198. listVO.add(vo);
  199. }
  200. if (listVO.isEmpty()) {
  201. return ActionResult.fail(MsgCode.PRI008.get());
  202. }
  203. return ActionResult.success(listVO);
  204. }
  205. @Operation(summary = "保存或者发布")
  206. @PostMapping("/Save")
  207. public ActionResult saveOrRelease(@RequestBody ReportUpForm form) {
  208. reportService.saveOrRelease(form);
  209. if (Objects.equals(form.getType(), 1)) {
  210. return ActionResult.success(MsgCode.SU011.get());
  211. }
  212. return ActionResult.success(MsgCode.SU002.get());
  213. }
  214. @Operation(summary = "复制")
  215. @Parameters({
  216. @Parameter(name = "id", description = "模板id", required = true)
  217. })
  218. @PostMapping("/{id}/Actions/Copy")
  219. public ActionResult copy(@PathVariable String id) {
  220. ReportEntity entity = reportService.getById(id);
  221. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  222. String fullName = entity.getFullName() + ".副本" + copyNum;
  223. if (fullName.length() > 50) {
  224. return ActionResult.fail(MsgCode.PRI006.get());
  225. }
  226. List<ReportVersionEntity> list = versionService.getList(id, ReportVersionEntity::getId);
  227. ReportInfoVO info = new ReportInfoVO();
  228. List<DataSetInfo> listVO = new ArrayList<>();
  229. if (CollectionUtils.isNotEmpty(list)) {
  230. info = reportService.getVersionInfo(list.get(0).getId());
  231. List<DataSetInfo> dataSetList = info.getDataSetList() != null ? info.getDataSetList() : new ArrayList<>();
  232. for (DataSetInfo dataSetInfo : dataSetList) {
  233. dataSetInfo.setObjectId(null);
  234. dataSetInfo.setId(null);
  235. listVO.add(dataSetInfo);
  236. }
  237. }
  238. ReportCrForm form = JsonUtil.getJsonToBean(info, ReportCrForm.class);
  239. form.setFullName(fullName);
  240. form.setEnCode(entity.getEnCode() + copyNum);
  241. form.setCategory(entity.getCategory());
  242. form.setSortCode(entity.getSortCode());
  243. form.setDescription(entity.getDescription());
  244. form.setId(null);
  245. SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
  246. if (BeanUtil.isNotEmpty(infoByEnCode)) {
  247. form.setSystemId(infoByEnCode.getId());
  248. }
  249. reportService.create(form);
  250. return ActionResult.success(MsgCode.SU007.get());
  251. }
  252. @Operation(summary = "导出")
  253. @Parameters({
  254. @Parameter(name = "id", description = "模板id")
  255. })
  256. @GetMapping("/{id}/Actions/Export")
  257. public ActionResult<DownloadVO> export(@PathVariable String id) {
  258. DownloadVO vo = new DownloadVO();
  259. ReportEntity entity = reportService.getById(id);
  260. List<ReportVersionEntity> list = versionService.getList(id, ReportVersionEntity::getId);
  261. if (CollectionUtils.isEmpty(list)) {
  262. throw new DataException(MsgCode.FA001.get());
  263. }
  264. ReportInfoVO info = reportService.getVersionInfo(list.get(0).getId());
  265. if (StringUtil.isEmpty(info.getSystemId())){
  266. SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
  267. if (BeanUtil.isNotEmpty(infoByEnCode)) {
  268. info.setSystemId(infoByEnCode.getId());
  269. }
  270. }
  271. String json = JsonUtil.getObjectToString(info);
  272. String tableName = ModuleTypeEnum.REPORT_TEMPLATE.getTableName();
  273. String fileName = entity.getFullName() + "_" + DateUtil.dateFormatByPattern(new Date(), "yyyyMMddHHmmss") + "." + tableName;
  274. try {
  275. String url = "/api/Report/data/Download?name=" + fileName + "&encryption=";
  276. FileInfo fileInfo = FileUploadUtils.uploadFile(new FileParameter(FileTypeConstant.TEMPORARY, fileName), json.getBytes(GlobalConst.DEFAULT_CHARSET));
  277. vo.setName(fileInfo.getFilename());
  278. vo.setUrl(UploaderUtil.uploaderFile(url, fileInfo.getFilename() + "#" + FileTypeConstant.TEMPORARY));
  279. } catch (Exception e) {
  280. log.error(e.getMessage(), e);
  281. }
  282. return ActionResult.success(vo);
  283. }
  284. @Operation(summary = "导入")
  285. @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  286. public ActionResult importData(@RequestPart("file") MultipartFile multipartFile,
  287. @RequestParam("type") Integer type) throws DataException {
  288. //判断是否为.rp结尾
  289. if (FileUtil.existsSuffix(multipartFile, ModuleTypeEnum.REPORT_TEMPLATE.getTableName())) {
  290. return ActionResult.fail(MsgCode.IMP002.get());
  291. }
  292. SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
  293. //读取文件内容
  294. String fileContent = FileUtil.getFileContent(multipartFile);
  295. ReportInfoVO infVo = JsonUtil.getJsonToBean(fileContent, ReportInfoVO.class);
  296. //判断Id是否重复
  297. boolean idCheck = false;
  298. if (type==0&&(StringUtil.isEmpty(infVo.getSystemId())
  299. ||!infVo.getSystemId().equals(infoByEnCode.getId()))){
  300. infVo.setId(RandomUtil.uuId());
  301. infVo.setEnCode(codeNumService.getCodeFunction(() ->
  302. codeNumService.getCodeOnce(CodeConst.BB), encode -> reportService.isEncodeExist(encode)));
  303. }else {
  304. idCheck = true;
  305. }
  306. infVo.setSystemId(infoByEnCode.getId());
  307. String str = reportService.importData(infVo, type,idCheck);
  308. if (StringUtil.isNotEmpty(str)) {
  309. return ActionResult.fail(str);
  310. }
  311. return ActionResult.success(MsgCode.IMP001.get());
  312. }
  313. @Operation(summary = "下拉列表")
  314. @GetMapping("/Selector")
  315. public ActionResult<ListVO<ReportSelectVO>> selectorList() {
  316. List<ReportEntity> list = reportService.getTreeList();
  317. List<String> dictionary = list.stream().map(ReportEntity::getCategory).collect(Collectors.toList());
  318. List<DictionaryDataEntity> dictionList = dictionaryDataService.getDictionName(dictionary);
  319. Map<String, List<ReportEntity>> map = list.stream().collect(Collectors.groupingBy(ReportEntity::getCategory));
  320. List<ReportSelectVO> listVO = new ArrayList<>();
  321. for (DictionaryDataEntity entity : dictionList) {
  322. List<ReportEntity> entityList = map.get(entity.getId()) != null ? map.get(entity.getId()) : new ArrayList<>();
  323. if (CollectionUtils.isNotEmpty(entityList)) {
  324. ReportSelectVO vo = new ReportSelectVO();
  325. vo.setId(entity.getId());
  326. vo.setFullName(entity.getFullName());
  327. vo.setHasChildren(true);
  328. vo.setChildren(JsonUtil.getJsonToList(entityList, ReportSelectVO.class));
  329. listVO.add(vo);
  330. }
  331. }
  332. ListVO vo = new ListVO<>();
  333. vo.setList(listVO);
  334. return ActionResult.success(vo);
  335. }
  336. @Operation(summary = "报表发布菜单")
  337. @Parameters({
  338. @Parameter(name = "id", description = "模板id", required = true)
  339. })
  340. @PostMapping("/{id}/Actions/Module")
  341. public ActionResult module(@PathVariable String id, @RequestBody MenuModel model) {
  342. ReportEntity entity = reportService.getById(id);
  343. if (entity == null) {
  344. return ActionResult.fail(MsgCode.FA012.get());
  345. }
  346. model.setId(id);
  347. model.setFullName(entity.getFullName());
  348. model.setEnCode(entity.getEnCode());
  349. model.setType(10);
  350. entity.setPlatformRelease(model.getPlatformRelease());
  351. String json = ReportUtil.http(ApiConst.SAVE_MENU, Method.POST, JsonUtil.entityToMap(model));
  352. ActionResult result = JsonUtil.getJsonToBean(json, ActionResult.class);
  353. if (result == null) {
  354. return ActionResult.fail(MsgCode.FA101.get());
  355. }
  356. if (!Objects.equals(result.getCode(), 200)) {
  357. return ActionResult.fail(result.getMsg());
  358. }
  359. reportService.update(id, entity);
  360. return ActionResult.success(MsgCode.SU011.get());
  361. }
  362. @Operation(summary = "获取报表发布菜单")
  363. @Parameters({
  364. @Parameter(name = "id", description = "模板id", required = true)
  365. })
  366. @GetMapping("/{id}/getReleaseMenu")
  367. public ActionResult getReleaseMenu(@PathVariable String id) {
  368. ReportEntity entity = reportService.getById(id);
  369. if (entity == null) {
  370. return ActionResult.fail(MsgCode.FA012.get());
  371. }
  372. MenuModel model = new MenuModel();
  373. model.setId(id);
  374. String json = ReportUtil.http(ApiConst.GET_MENU, Method.POST, JsonUtil.entityToMap(model));
  375. ActionResult result = JsonUtil.getJsonToBean(json, ActionResult.class);
  376. ModuleNameVO moduleNameVO = new ModuleNameVO();
  377. if (Objects.equals(result.getCode(), 200)) {
  378. moduleNameVO = JsonUtil.getJsonToBean(result.getData(), ModuleNameVO.class);
  379. }
  380. ReportInfoVO vo = JsonUtil.getJsonToBean(entity, ReportInfoVO.class);
  381. vo.setAppIsRelease(0);
  382. vo.setPcIsRelease(0);
  383. if (moduleNameVO != null) {
  384. if (StringUtil.isNotEmpty(moduleNameVO.getPcNames())) {
  385. vo.setPcIsRelease(1);
  386. vo.setPcReleaseName(moduleNameVO.getPcNames());
  387. }
  388. if (StringUtil.isNotEmpty(moduleNameVO.getAppNames())) {
  389. vo.setAppIsRelease(1);
  390. vo.setAppReleaseName(moduleNameVO.getAppNames());
  391. }
  392. }
  393. return ActionResult.success(vo);
  394. }
  395. }