TargetManagementService.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. package thyyxxk.webserver.service.targetmanagement;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.jetbrains.annotations.NotNull;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import thyyxxk.webserver.config.exception.ExceptionEnum;
  7. import thyyxxk.webserver.dao.his.highreport.HighReportDao;
  8. import thyyxxk.webserver.dao.his.targetmanagement.TargetManagementDao;
  9. import thyyxxk.webserver.dao.his.targetmanagement.ZbZdBaseInfoDao;
  10. import thyyxxk.webserver.entity.ResultVo;
  11. import thyyxxk.webserver.entity.highreport.HighReportDto;
  12. import thyyxxk.webserver.entity.highreport.ReportBaseInfo;
  13. import thyyxxk.webserver.entity.highreport.ReportType;
  14. import thyyxxk.webserver.entity.highreport.TableGroupInfo;
  15. import thyyxxk.webserver.entity.highreport.ZbReportResult;
  16. import thyyxxk.webserver.entity.highreport.ZdReportType;
  17. import thyyxxk.webserver.entity.login.UserInfo;
  18. import thyyxxk.webserver.entity.targetmanagement.TargetDictTree;
  19. import thyyxxk.webserver.entity.targetmanagement.TargetManagement;
  20. import thyyxxk.webserver.entity.targetmanagement.ZbZdBaseInfo;
  21. import thyyxxk.webserver.service.redislike.RedisLikeService;
  22. import thyyxxk.webserver.utils.DateUtil;
  23. import thyyxxk.webserver.utils.DecimalUtil;
  24. import thyyxxk.webserver.utils.ReportUtil;
  25. import thyyxxk.webserver.utils.ResultVoUtil;
  26. import thyyxxk.webserver.utils.StringUtil;
  27. import thyyxxk.webserver.utils.TargetDictTreeUtil;
  28. import thyyxxk.webserver.utils.TokenUtil;
  29. import java.math.BigDecimal;
  30. import java.util.ArrayList;
  31. import java.util.Comparator;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.stream.Collectors;
  36. /**
  37. * @Description: 指标管理
  38. * @Author: hsh
  39. * @CreateTime: 2023-05-30 16:52
  40. * @Version: 1.0
  41. */
  42. @Service
  43. @Slf4j
  44. public class TargetManagementService {
  45. private final TargetManagementDao dao;
  46. private final ZbZdBaseInfoDao zbZdDao;
  47. private final HighReportDao reportDao;
  48. private final RedisLikeService redis;
  49. @Autowired
  50. public TargetManagementService(TargetManagementDao dao, ZbZdBaseInfoDao zbZdDao, HighReportDao reportDao, RedisLikeService redis) {
  51. this.dao = dao;
  52. this.zbZdDao = zbZdDao;
  53. this.reportDao = reportDao;
  54. this.redis = redis;
  55. }
  56. /**
  57. * @description: 查询指标字典基本信息
  58. * @author: hsh
  59. * @date: 2023/5/30 17:27
  60. * @param: [dto]
  61. * @return: ResultVo<List<ZbZdBaseInfo>>
  62. **/
  63. public ResultVo<List<ZbZdBaseInfo>> selectTargetDict(TargetManagement dto) {
  64. List<ZbZdBaseInfo> list = zbZdDao.selectZbZdBaseInfo(dto);
  65. return ResultVoUtil.success(list);
  66. }
  67. /**
  68. * @description: 根据id查询指标字典基本信息
  69. * @author: hsh
  70. * @date: 2023/7/5 10:58
  71. * @param: [dto]
  72. * @return: ResultVo<ZbZdBaseInfo>
  73. **/
  74. public ResultVo<ZbZdBaseInfo> selectTargetDictById(TargetManagement dto) {
  75. ZbZdBaseInfo info = zbZdDao.selectZbZdBaseInfoById(dto);
  76. return ResultVoUtil.success(info);
  77. }
  78. /**
  79. * @description: 查询指标树
  80. * @author: hsh
  81. * @date: 2023/6/2 11:11
  82. * @param: [dto]
  83. * @return: ResultVo<List<TargetDictTree>>
  84. **/
  85. public ResultVo<List<TargetDictTree>> selectTargetDictTree(TargetManagement dto) {
  86. List<TargetDictTree> zbList = zbZdDao.selectTargetDictTree(dto);
  87. // 组装指标树
  88. List<TargetDictTree> list = assemblyTargetDictTree(zbList);
  89. return ResultVoUtil.success(list);
  90. }
  91. /**
  92. * @description: 组装指标字典树
  93. * @author: hsh
  94. * @date: 2023/6/2 9:37
  95. * @param: [list, zbList]
  96. * @return: void
  97. **/
  98. private List<TargetDictTree> assemblyTargetDictTree(@NotNull List<TargetDictTree> zbList) {
  99. // 创建树形结构(数据集合作为参数)
  100. TargetDictTreeUtil treeBuild = new TargetDictTreeUtil(zbList);
  101. // 原查询结果转换树形结构
  102. return treeBuild.buildTree();
  103. }
  104. public ResultVo<Map<String, Object>> saveTargetDict(ZbZdBaseInfo dto) {
  105. // 验证sql是否有问题
  106. String msg = sqlCheckAndVerify(dto);
  107. // 补充责任科室以及责任人
  108. if(StringUtil.notBlank(msg)){
  109. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, msg);
  110. }
  111. TargetManagement target = new TargetManagement();
  112. target.setId(dto.getId());
  113. target.setPid(dto.getPid());
  114. ZbZdBaseInfo zdBaseInfo = zbZdDao.selectZbZdBaseInfoById(target);
  115. int num;
  116. if(zdBaseInfo != null){
  117. num = zbZdDao.updateZbZdBaseInfo(dto);
  118. } else {
  119. num = zbZdDao.saveZbZdBaseInfo(dto);
  120. }
  121. if(num > 0){
  122. Map<String, Object> m = new HashMap<>();
  123. m.put("1", "保存指标字典成功!");
  124. return ResultVoUtil.success(m);
  125. } else {
  126. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存指标字典失败!");
  127. }
  128. }
  129. /**
  130. * @description: 更新指标字典数据
  131. * @author: hsh
  132. * @date: 2023/6/2 9:29
  133. * @param: [dto]
  134. * @return: ResultVo<Map<String,Object>>
  135. **/
  136. public ResultVo<Map<String, Object>> updateTargetDict(ZbZdBaseInfo dto) {
  137. // 验证sql是否有问题
  138. String msg = sqlCheckAndVerify(dto);
  139. if(StringUtil.notBlank(msg)){
  140. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, msg);
  141. }
  142. int num = zbZdDao.updateZbZdBaseInfo(dto);
  143. if(num > 0){
  144. return ResultVoUtil.success(ExceptionEnum.SUCCESS, "更新指标字典成功!");
  145. } else {
  146. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新指标字典失败");
  147. }
  148. }
  149. /**
  150. * @description: 验证查询sql是否正确
  151. * @author: hsh
  152. * @date: 2023/5/31 10:11
  153. * @param: [dto]
  154. * @return: String 错误信息
  155. **/
  156. private String sqlCheckAndVerify(@NotNull ZbZdBaseInfo dto) {
  157. String sqlChild = dto.getCalcChild();
  158. String sqlMom = dto.getCalcMom();
  159. String message = "";
  160. if(StringUtil.isBlank(dto.getId())){
  161. message = "id不存在,请检查!";
  162. }
  163. if(StringUtil.isBlank(dto.getName())){
  164. message = "指标名称不存在,请检查!";
  165. }
  166. if(StringUtil.isBlank(dto.getPid())){
  167. message = "父id不存在,请检查!";
  168. }
  169. if(dto.getScore() == null || dto.getScore().doubleValue() < 0){
  170. message = "指标总分不存在或者小于0,请检查!";
  171. }
  172. String isLeaf = dto.getIsLeaf();
  173. if("1".equals(isLeaf)){
  174. // 叶子节点要求填写责任科室,责任人
  175. if(StringUtil.isBlank(dto.getDeptCode())){
  176. message = "责任科室未选择,请检查!";
  177. }
  178. if(StringUtil.isBlank(dto.getOpId())){
  179. message = "责任人未选择,请检查!";
  180. }
  181. if(StringUtil.isBlank(dto.getDefinition())){
  182. message = "指标定义不存在,请检查!";
  183. }
  184. if(StringUtil.isBlank(dto.getMethod())){
  185. message = "指标评审方法不存在,请检查!";
  186. }
  187. if(StringUtil.isBlank(dto.getScoreRule())){
  188. message = "指标计分细则不存在,请检查!";
  189. }
  190. if(StringUtil.notBlank(sqlChild)){
  191. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild))){
  192. message = "保存指标字典失败,分子" + ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild);
  193. }
  194. }
  195. if(StringUtil.notBlank(sqlMom)){
  196. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(sqlMom))){
  197. message = "保存指标字典失败,分母" + ReportUtil.sqlCheckAndVerifyByKeyword(sqlMom);
  198. }
  199. }
  200. }
  201. return message;
  202. }
  203. /**
  204. * @description: 查询分子分母指标报表结果
  205. * @author: hsh
  206. * @date: 2023/7/6 9:31
  207. * @param: [dto]
  208. * @return: ResultVo<Map<String,Object>>
  209. **/
  210. public ResultVo<Map<String, Object>> targetSqlExecute(TargetManagement dto) {
  211. String sqlChild = dto.getCalcChild();
  212. String sqlMom = dto.getCalcMom();
  213. if(StringUtil.isBlank(sqlChild)){
  214. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "计算-分子sql不存在,请检查!");
  215. } else {
  216. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild))){
  217. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "分子" + ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild));
  218. }
  219. }
  220. if(StringUtil.notBlank(sqlMom)){
  221. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(sqlMom))){
  222. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "分母" + ReportUtil.sqlCheckAndVerifyByKeyword(sqlMom));
  223. }
  224. }
  225. // 转换sql的查询条件
  226. Map<String, ReportBaseInfo> map = ReportUtil.assertReportForTarget(dto);
  227. ReportBaseInfo r1 = map.get("calcChild");
  228. ReportBaseInfo r2 = map.get("calcMom");
  229. // 转换查询dto
  230. HighReportDto d = ReportUtil.TargetManagementConvertHighReportDto(dto);
  231. // 计算结果
  232. Map<String, Object> resultMap = new HashMap<>();
  233. String child = null;
  234. String mom = null;
  235. if(r1 != null){
  236. String childSql = ReportUtil.callSqlFormat(d, r1.getBaseSql(), r1.getGatherSql(), null, null, null);
  237. child = dao.targetSqlExecute(childSql);
  238. }
  239. if(r2 != null){
  240. String momSql = ReportUtil.callSqlFormat(d, r2.getBaseSql(), r2.getGatherSql(), null, null, null);
  241. mom = dao.targetSqlExecute(momSql);
  242. }
  243. if (StringUtil.notBlank(child)) {
  244. resultMap.put("r1", child);
  245. } else {
  246. resultMap.put("r1", "无");
  247. }
  248. if (StringUtil.notBlank(mom)) {
  249. resultMap.put("r2", mom);
  250. if(StringUtil.notBlank(child)){
  251. resultMap.put("r3", DecimalUtil.divide(new BigDecimal(child), new BigDecimal(mom), 4));
  252. } else {
  253. resultMap.put("r3", "无");
  254. }
  255. } else {
  256. resultMap.put("r2", "无");
  257. if (StringUtil.notBlank(child)) {
  258. resultMap.put("r3", child);
  259. } else {
  260. resultMap.put("r3", "无");
  261. }
  262. }
  263. return ResultVoUtil.success(resultMap);
  264. }
  265. /**
  266. * @description: 根据id查询分子分母指标报表结果
  267. * @author: hsh
  268. * @date: 2023/7/6 9:30
  269. * @param: [dto]
  270. * @return: ResultVo<Map<String,Object>>
  271. **/
  272. public ResultVo<List<Map<String, Object>>> targetSqlExecuteById(TargetManagement dto) {
  273. ReportBaseInfo reportChild = dao.selectReportBaseInfoByCalcId(dto.getChildId());
  274. ReportBaseInfo reportMom = dao.selectReportBaseInfoByCalcId(dto.getMomId());
  275. if(null == reportChild && null == reportMom){
  276. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, dto.getName() + "计算-分子或分母指标都不存在,请联系管理员!");
  277. }
  278. if(null == dto.getStartTime() || null == dto.getEndTime()){
  279. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, dto.getName() + "查询时间不存在,请检查!");
  280. }
  281. // 处理时间(1.时间跨度需要一年一年查询结果; 2.查询时间格式为年月日时分秒: yyyy-MM-DD 24H:mm:ss)
  282. int start = Integer.parseInt(dto.getStartTime());
  283. int end = Integer.parseInt(dto.getEndTime());
  284. // 超过4年只选择4年数据查询
  285. int endIndex = start + 3;
  286. List<Map<String, Object>> list = new ArrayList<>();
  287. for(int i = start; i <= end; i++){
  288. Map<String, Object> resultMap = new HashMap<>();
  289. if(i > endIndex){
  290. break;
  291. }
  292. int s = i - start + 1;
  293. String ks = i + "-01-01 00:00:00";
  294. String js = i + "-12-31 23:59:59";
  295. dto.setStartTime(ks);
  296. dto.setEndTime(js);
  297. HighReportDto d = ReportUtil.TargetManagementConvertHighReportDto(dto);
  298. // 计算结果
  299. String child = null;
  300. String mom = null;
  301. if(reportChild != null){
  302. String childSql = ReportUtil.callSqlFormat(d, reportChild.getBaseSql(), reportChild.getGatherSql(), null, null, null);
  303. child = dao.targetSqlExecute(childSql);
  304. }
  305. if(reportMom != null){
  306. String momSql = ReportUtil.callSqlFormat(d, reportMom.getBaseSql(), reportMom.getGatherSql(), null, null, null);
  307. mom = dao.targetSqlExecute(momSql);
  308. }
  309. resultMap.put("id", dto.getId());
  310. resultMap.put("pid", dto.getPid());
  311. resultMap.put("index", s);
  312. // 数据统计来源(1:sql统计;2:手动填写输入。)
  313. resultMap.put("dataType", "1");
  314. // 统计年份
  315. resultMap.put("year", i);
  316. resultMap.put("childLabel", i + "年分子结果");
  317. resultMap.put("momLabel", i + "年分母结果");
  318. resultMap.put("calcLabel", i + "年分子/分母的结果");
  319. if (StringUtil.notBlank(child)) {
  320. resultMap.put("childResult", child);
  321. } else {
  322. resultMap.put("childResult", "无");
  323. }
  324. if (StringUtil.notBlank(mom)) {
  325. resultMap.put("momResult", mom);
  326. if(StringUtil.notBlank(child)){
  327. resultMap.put("calcResult", DecimalUtil.divide(new BigDecimal(child), new BigDecimal(mom), 2));
  328. } else {
  329. resultMap.put("calcResult", "无");
  330. }
  331. } else {
  332. resultMap.put("momResult", "无");
  333. if (StringUtil.notBlank(child)) {
  334. resultMap.put("calcResult", child);
  335. } else {
  336. resultMap.put("calcResult", "无");
  337. }
  338. }
  339. list.add(resultMap);
  340. }
  341. return ResultVoUtil.success(list);
  342. }
  343. public ResultVo<Map<String, Object>> targetSqlSave(TargetManagement dto) {
  344. String sqlChild = dto.getCalcChild();
  345. String sqlMom = dto.getCalcMom();
  346. if(StringUtil.notBlank(sqlChild)) {
  347. if (!sqlChild.contains("/") && !(sqlChild.contains("select") || sqlChild.contains("SELECT"))) {
  348. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "分子sql语句不合法,请检查!");
  349. }
  350. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild))){
  351. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "分子" + ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild));
  352. }
  353. }
  354. if(StringUtil.notBlank(sqlMom)){
  355. if (!sqlMom.contains("/") && !(sqlMom.contains("select") || sqlMom.contains("SELECT"))) {
  356. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "分母sql语句不合法,请检查!");
  357. }
  358. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild))){
  359. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "分母" + ReportUtil.sqlCheckAndVerifyByKeyword(sqlChild));
  360. }
  361. }
  362. try {
  363. int num = 0;
  364. // 获取指标字典sql信息
  365. Map<String, ReportBaseInfo> map = ReportUtil.assertReportForTarget(dto);
  366. // 查询指标字典是否以及存在分子分母的sql指标报表信息
  367. ZbZdBaseInfo info = zbZdDao.selectZbZdBaseInfoById(dto);
  368. if(null != map.get("calcChild")){
  369. // 已经配置分子sql指标报表信息
  370. ReportBaseInfo r1 = map.get("calcChild");
  371. // 更新分子指标报表的sql信息
  372. if(StringUtil.notBlank(info.getChildId())){
  373. dao.deleteReportBaseInfoById(r1.getReportId());
  374. } else {
  375. // 保存分子指标报表的id
  376. zbZdDao.updateZbZdBaseInfoCalcId(dto.getId(), dto.getPid() ,r1.getReportId(), null);
  377. }
  378. num += dao.insert(r1);
  379. }
  380. if(null != map.get("calcMom")){
  381. ReportBaseInfo r2 = map.get("calcMom");
  382. if(StringUtil.notBlank(info.getMomId())){
  383. dao.deleteReportBaseInfoById(r2.getReportId());
  384. } else {
  385. // 保存分母指标报表的id
  386. zbZdDao.updateZbZdBaseInfoCalcId(dto.getId(), dto.getPid(), null, r2.getReportId());
  387. }
  388. num += dao.insert(r2);
  389. }
  390. Map<String, Object> resultMap = new HashMap<>();
  391. if(num == 0){
  392. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存指标sql以及报表失败!");
  393. }
  394. resultMap.put("cg", "保存指标sql以及报表成功!");
  395. return ResultVoUtil.success(resultMap);
  396. } catch (Exception e) {
  397. e.printStackTrace();
  398. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存指标sql以及报表失败!");
  399. }
  400. }
  401. /**
  402. * @description: 根据id和父id删除指标字典
  403. * @author: hsh
  404. * @date: 2023/6/21 11:05
  405. * @param: [dto]
  406. * @return: ResultVo<Map<String,Object>>
  407. **/
  408. public ResultVo<Map<String, Object>> deleteTargetDictById(TargetManagement dto) {
  409. if(StringUtil.isBlank(dto.getId())){
  410. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "id不存在,请检查!");
  411. }
  412. if(StringUtil.isBlank(dto.getPid())){
  413. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "父id不存在,请检查!");
  414. }
  415. Map<String, Object> resultMap = new HashMap<>();
  416. int num = zbZdDao.deleteTargetDictById(dto.getId(), dto.getPid());
  417. if(num == 0){
  418. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除指标字典失败!");
  419. }
  420. resultMap.put("cg", "删除指标字典成功!");
  421. return ResultVoUtil.success(resultMap);
  422. }
  423. /**
  424. * @description: 查询指标字典的分子分母sql
  425. * @author: hsh
  426. * @date: 2023/6/30 15:19
  427. * @param: [dto]
  428. * @return: ResultVo<Map<String,Object>>
  429. **/
  430. public ResultVo<Map<String, Object>> selectTargetSql(TargetManagement dto) {
  431. if (StringUtil.isBlank(dto.getId())) {
  432. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "id不存在,请检查!");
  433. }
  434. if (StringUtil.isBlank(dto.getPid())) {
  435. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "父id不存在,请检查!");
  436. }
  437. Map<String, Object> resultMap = new HashMap<>();
  438. ReportBaseInfo reportChild = dao.selectReportBaseInfoByCalcId(dto.getChildId());
  439. ReportBaseInfo reportMom = dao.selectReportBaseInfoByCalcId(dto.getMomId());
  440. resultMap.put("reportChild", reportChild);
  441. resultMap.put("reportMom", reportMom);
  442. return ResultVoUtil.success(resultMap);
  443. }
  444. /**
  445. * @description: 查询指标字典的分子分母sql的详细内容
  446. * @author: hsh
  447. * @date: 2023/6/30 15:20
  448. * @param: [dto]
  449. * @return: ResultVo<Map<String,Object>>
  450. **/
  451. public ResultVo<Map<String, Object>> selectTargetSqlDetail(TargetManagement dto) {
  452. if (StringUtil.isBlank(dto.getId())) {
  453. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "id不存在,请检查!");
  454. }
  455. if (StringUtil.isBlank(dto.getPid())) {
  456. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "父id不存在,请检查!");
  457. }
  458. Map<String, Object> resultMap = new HashMap<>();
  459. ReportBaseInfo reportBaseInfo = dao.selectReportBaseInfoById(dto.getId(), dto.getPid());
  460. if (reportBaseInfo == null) {
  461. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "查询指标不存在!");
  462. }
  463. resultMap.put("report", reportBaseInfo);
  464. return ResultVoUtil.success(resultMap);
  465. }
  466. /**
  467. * @description: 查询指标报表数据
  468. * @author: hsh
  469. * @date: 2023/7/7 10:19
  470. * @param: [dto]
  471. * @return: ResultVo<List<Map<String, Object>>>
  472. **/
  473. public ResultVo<List<Map<String, Object>>> selectReportDictTree(HighReportDto dto){
  474. // 查询所有使用的指标报表数据
  475. List<ReportBaseInfo> list = reportDao.selectReportDictTree();
  476. // 查询指标类型
  477. List<ZdReportType> reportTypes = reportDao.selectReportType();
  478. Map<String, ZdReportType> typeMap = new HashMap<>();
  479. for (ZdReportType zdReport : reportTypes) {
  480. typeMap.put(zdReport.getReportType(), zdReport);
  481. }
  482. Map<String, List<ReportBaseInfo>> reportMap = list.stream().collect(Collectors.groupingBy(ReportBaseInfo::getReportType));
  483. List<Map<String, Object>> resultList = new ArrayList<>();
  484. Map<String, Object> rootMap = new HashMap<>();
  485. rootMap.put("id", "zbJc");
  486. rootMap.put("pid", "0");
  487. rootMap.put("label", "指标监测");
  488. List<Map<String, Object>> rootChildList = new ArrayList<>();
  489. for(Map.Entry<String, List<ReportBaseInfo>> entry: reportMap.entrySet()){
  490. Map<String, Object> map = new HashMap<>();
  491. List<ReportBaseInfo> l = entry.getValue();
  492. String type = entry.getKey();
  493. String reportName = typeMap.get(type) == null ? "" : typeMap.get(type).getReportLabel();
  494. if(StringUtil.notBlank(reportName)){
  495. map.put("id", type);
  496. map.put("pid", "zbJc");
  497. map.put("label", reportName);
  498. map.put("sort", typeMap.get(type).getSort());
  499. } else {
  500. map.put("id", type);
  501. map.put("label", ReportType.QT.getName());
  502. map.put("sort", "999");
  503. }
  504. map.put("pid", "zbJc");
  505. List<Map<String, Object>> child = AssertReportTree(l, type);
  506. map.put("children", child);
  507. rootChildList.add(map);
  508. }
  509. rootMap.put("children", rootChildList.stream().sorted(Comparator.comparingInt(e -> Integer.parseInt(String.valueOf(e.get("sort"))))).collect(Collectors.toList()));
  510. resultList.add(rootMap);
  511. return ResultVoUtil.success(resultList);
  512. }
  513. /**
  514. * @description: 组装指标报表树数据
  515. * @author: hsh
  516. * @date: 2023/7/7 11:34
  517. * @param: [list, type]
  518. * @return: List<Map<String, Object>>
  519. **/
  520. private List<Map<String, Object>> AssertReportTree(List<ReportBaseInfo> list, String type) {
  521. List<Map<String, Object>> resultList = new ArrayList<>();
  522. for(ReportBaseInfo info : list){
  523. Map<String, Object> map = new HashMap<>();
  524. map.put("id", info.getReportId());
  525. map.put("pid", type);
  526. map.put("label", info.getReportName());
  527. resultList.add(map);
  528. }
  529. return resultList;
  530. }
  531. /**
  532. * @description: 根据报表id作废指标报表
  533. * @author: hsh
  534. * @date: 2023/7/7 16:13
  535. * @param: [dto]
  536. * @return: ResultVo<Map<String,Object>>
  537. **/
  538. public ResultVo<Map<String, Object>> deleteReportDictById(TargetManagement dto) {
  539. if (StringUtil.isBlank(dto.getId())) {
  540. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "id不存在,请检查!");
  541. }
  542. Map<String, Object> resultMap = new HashMap<>();
  543. int num = reportDao.updateReportDictById(dto.getId());
  544. if (num == 0) {
  545. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "作废指标报表失败!");
  546. }
  547. resultMap.put("cg", "作废指标报表成功!");
  548. return ResultVoUtil.success(resultMap);
  549. }
  550. /**
  551. * @description: 查询指标报表信息
  552. * @author: hsh
  553. * @date: 2023/7/7 16:52
  554. * @param: [dto]
  555. * @return: ResultVo<List<ReportBaseInfo>>
  556. **/
  557. public ResultVo<List<ReportBaseInfo>> selectReportDict(HighReportDto dto) {
  558. List<ReportBaseInfo> list = reportDao.selectReportDict(dto);
  559. return ResultVoUtil.success(list);
  560. }
  561. /**
  562. * @description: 查询指标报表类型字典信息
  563. * @author: hsh
  564. * @date: 2023/7/11 16:26
  565. * @param: []
  566. * @return: ResultVo<List<ZdReportType>>
  567. **/
  568. public ResultVo<List<ZdReportType>> selectReportType() {
  569. List<ZdReportType> list = reportDao.selectReportType();
  570. return ResultVoUtil.success(list);
  571. }
  572. /**
  573. * @description: 查询报表sql统计总计结果,用来验证报表sql是否编写正确
  574. * @author: hsh
  575. * @date: 2023/7/12 16:52
  576. * @param: [dto]
  577. * @return: ResultVo<Map<String,Object>>
  578. **/
  579. public ResultVo<Map<String, Object>> reportSqlExecute(TargetManagement dto){
  580. if(StringUtil.isBlank(dto.getBaseSql())){
  581. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "基础sql不存在,请检查!");
  582. }
  583. if(StringUtil.isBlank(dto.getGatherSql())){
  584. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "聚合sql不存在,请检查!");
  585. }
  586. if(StringUtil.notBlank(ReportUtil.sqlCheckAndVerifyByKeyword(dto.getBaseSql()))){
  587. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, ReportUtil.sqlCheckAndVerifyByKeyword(dto.getBaseSql()));
  588. }
  589. Map<String, Object> resultMap = new HashMap<>();
  590. HighReportDto d = ReportUtil.TargetManagementConvertHighReportDto(dto);
  591. String calcSql = ReportUtil.callSqlFormat(d, dto.getBaseSql(), dto.getGatherSql(), null, null, null);
  592. String result = dao.targetSqlExecute(calcSql);
  593. if(StringUtil.notBlank(result)){
  594. resultMap.put("r1", result);
  595. } else {
  596. resultMap.put("r1", "无");
  597. }
  598. return ResultVoUtil.success(resultMap);
  599. }
  600. /**
  601. * @description: 查询报表分组类型
  602. * @author: hsh
  603. * @date: 2023/7/19 15:25
  604. * @param: [dto]
  605. * @return: ResultVo<TableGroupInfo>
  606. **/
  607. public ResultVo<TableGroupInfo> selectReportLevel(HighReportDto dto){
  608. TableGroupInfo info = reportDao.selectTableGroupInfoByLevel(dto.getReportId(), dto.getLevel());
  609. if(info == null){
  610. // 查不到给他默认报表分组类型
  611. info = reportDao.selectTableGroupInfoByLevel(ReportType.GROUP_KS.getCode(), dto.getLevel());
  612. }
  613. return ResultVoUtil.success(info);
  614. }
  615. /**
  616. * @description: 保存指标结果
  617. * @author: hsh
  618. * @date: 2023/7/19 15:27
  619. * @param: [list]
  620. * @return: ResultVo<Map<String,Object>>
  621. **/
  622. public ResultVo<Map<String, Object>> saveTargetReportResult(List<ZbReportResult> list){
  623. Map<String, Object> resultMap = new HashMap<>();
  624. if(list == null || list.size() == 0){
  625. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "指标结果记录不存在,请检查!");
  626. }
  627. UserInfo user = redis.getUserInfoByCode(TokenUtil.getTokenUserId());
  628. for (ZbReportResult result : list) {
  629. result.setOp(user.getName() != null ? user.getName().trim() : user.getName());
  630. result.setOpId(user.getCodeRs());
  631. result.setOpTime(DateUtil.now());
  632. // 入库或更新操作
  633. int n = updateAndSaveTargetReportResult(result);
  634. if (n <= 0) {
  635. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存指标结果失败!");
  636. }
  637. }
  638. resultMap.put("cg", "保存指标结果成功");
  639. return ResultVoUtil.success(resultMap);
  640. }
  641. /**
  642. * @description: 先删后保存数据
  643. * @author: hsh
  644. * @date: 2023/7/21 10:06
  645. * @param: [result]
  646. * @return: int
  647. **/
  648. private int updateAndSaveTargetReportResult(ZbReportResult result) {
  649. if(result == null){
  650. return 0;
  651. }
  652. ZbReportResult r = reportDao.selectReportResultByYear(result.getId(), result.getPid(), result.getYear());
  653. if(r != null){
  654. reportDao.deleteReportResultById(result.getId(), result.getPid(), result.getYear());
  655. }
  656. return reportDao.saveTargetReportResult(result);
  657. }
  658. /**
  659. * @description: 查询指标报表结果
  660. * @author: hsh
  661. * @date: 2023/7/21 11:21
  662. * @param: [dto]
  663. * @return: ResultVo<List<ZbReportResult>>
  664. **/
  665. public ResultVo<List<ZbReportResult>> selectTargetReportResult(TargetManagement dto) {
  666. List<ZbReportResult> list = reportDao.selectReportResultById(dto.getId(), dto.getPid(), dto.getStartTime(), dto.getEndTime());
  667. return ResultVoUtil.success(list);
  668. }
  669. /**
  670. * @description: 保存/更新指标报表基本信息
  671. * @author: hsh
  672. * @date: 2023/7/27 9:55
  673. * @param: [info]
  674. * @return: ResultVo<Map<String,Object>>
  675. **/
  676. public ResultVo<Map<String, Object>> saveReportBaseInfo(ReportBaseInfo info) {
  677. Map<String, Object> resultMap = new HashMap<>();
  678. if(info == null || StringUtil.isBlank(info.getReportId())){
  679. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "报表详情为空,保存指标详情失败!");
  680. }
  681. ReportBaseInfo report = reportDao.selectReportPortalMenu(info.getReportId(), null, null);
  682. if (report != null) {
  683. reportDao.deleteReportBaseInfoById(info.getReportId());
  684. }
  685. int n = dao.insert(info);
  686. if (n <= 0) {
  687. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存指标详情失败!");
  688. }
  689. resultMap.put("cg", "保存指标详情成功!");
  690. return ResultVoUtil.success(resultMap);
  691. }
  692. }