910e4390690d1419a2be3d783094722dd8c7ea79.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package org.jeecg.modules.online.cgreport.controller;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import com.alibaba.fastjson.JSONObject;
  13. import org.apache.commons.collections.CollectionUtils;
  14. import org.apache.commons.lang.ArrayUtils;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.jeecg.common.api.vo.Result;
  17. import org.jeecg.common.aspect.annotation.AutoLog;
  18. import org.jeecg.common.system.base.controller.JeecgController;
  19. import org.jeecg.common.system.query.QueryGenerator;
  20. import org.jeecg.modules.online.cgreport.entity.ChartsData;
  21. import org.jeecg.modules.online.cgreport.entity.CommonConstants;
  22. import org.jeecg.modules.online.cgreport.entity.DiagramCombination;
  23. import org.jeecg.modules.online.cgreport.entity.DiagramCombinationDetail;
  24. import org.jeecg.modules.online.cgreport.entity.DiagramCombinationQueryConfig;
  25. import org.jeecg.modules.online.cgreport.entity.DiagramConfiguration;
  26. import org.jeecg.modules.online.cgreport.entity.DiagramFieldConfiguration;
  27. import org.jeecg.modules.online.cgreport.entity.GroupData;
  28. import org.jeecg.modules.online.cgreport.entity.TempletData;
  29. import org.jeecg.modules.online.cgreport.service.IDiagramCombinationDetailService;
  30. import org.jeecg.modules.online.cgreport.service.IDiagramCombinationQueryConfigService;
  31. import org.jeecg.modules.online.cgreport.service.IDiagramCombinationService;
  32. import org.jeecg.modules.online.cgreport.service.IDiagramConfigurationService;
  33. import org.jeecg.modules.online.cgreport.service.IDiagramFieldConfigurationService;
  34. import org.jeecg.modules.system.util.OnlineUtil;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import org.springframework.web.bind.annotation.PostMapping;
  38. import org.springframework.web.bind.annotation.GetMapping;
  39. import org.springframework.web.bind.annotation.PostMapping;
  40. import org.springframework.web.bind.annotation.PostMapping;
  41. import org.springframework.web.bind.annotation.RequestBody;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RequestMethod;
  44. import org.springframework.web.bind.annotation.RequestParam;
  45. import org.springframework.web.bind.annotation.RestController;
  46. import org.springframework.web.servlet.ModelAndView;
  47. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  48. import com.baomidou.mybatisplus.core.metadata.IPage;
  49. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  50. import com.googlecode.aviator.AviatorEvaluator;
  51. import com.googlecode.aviator.Expression;
  52. import io.swagger.annotations.Api;
  53. import io.swagger.annotations.ApiOperation;
  54. import lombok.extern.slf4j.Slf4j;
  55. /**
  56. * @Description: 图表配置
  57. */
  58. @Slf4j
  59. @Api(tags = "图表组合")
  60. @RestController
  61. @RequestMapping("/diagram/diagramCombination")
  62. public class DiagramCombinationController extends JeecgController<DiagramCombination, IDiagramCombinationService>{
  63. @Autowired
  64. private IDiagramCombinationService diagramCombinationService;
  65. @Autowired
  66. private IDiagramCombinationDetailService diagramCombinationDetailService;
  67. @Autowired
  68. private IDiagramCombinationQueryConfigService diagramCombinationQueryConfigService;
  69. @Autowired
  70. private IDiagramConfigurationService diagramConfigurationService;
  71. @Autowired
  72. private IDiagramFieldConfigurationService diagramFieldConfigurationService;
  73. /**
  74. * 分页列表查询
  75. *
  76. * @param diagramCombination
  77. * @param pageNo
  78. * @param pageSize
  79. * @param req
  80. * @return
  81. */
  82. @AutoLog(value = "图表组合配置-分页列表查询")
  83. @ApiOperation(value = "图表组合配置-分页列表查询", notes = "图表组合配置-分页列表查询")
  84. @GetMapping(value = "/list")
  85. public Result<?> queryPageList(DiagramCombination diagramCombination,
  86. @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
  87. @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
  88. QueryWrapper<DiagramCombination> queryWrapper = QueryGenerator.initQueryWrapper(diagramCombination, req.getParameterMap());
  89. Page<DiagramCombination> page = new Page<DiagramCombination>(pageNo, pageSize);
  90. IPage<DiagramCombination> pageList = diagramCombinationService.page(page, queryWrapper);
  91. List<DiagramCombination> records = pageList.getRecords();
  92. if (records != null && !records.isEmpty()) {
  93. for (DiagramCombination record : records) {
  94. QueryWrapper<DiagramCombinationDetail> qw = new QueryWrapper<DiagramCombinationDetail>();
  95. qw.eq("GRAPHREPORT_TEMPLET_ID", record.getId()).orderByAsc("ORDER_NUM");
  96. List<DiagramCombinationDetail> diagramCombinationDetails = this.diagramCombinationDetailService
  97. .list(qw);
  98. record.setDiagramCombinationDetails(diagramCombinationDetails);
  99. QueryWrapper<DiagramCombinationQueryConfig> dcqc = new QueryWrapper<DiagramCombinationQueryConfig>();
  100. dcqc.eq("GRAPHREPORT_TEMPLET_ID", record.getId()).orderByAsc("ORDER_NUM");
  101. List<DiagramCombinationQueryConfig> diagramCombinationQueryConfigs = this.diagramCombinationQueryConfigService
  102. .list(dcqc);
  103. record.setDiagramCombinationQueryConfigs(diagramCombinationQueryConfigs);
  104. }
  105. }
  106. return Result.OK(pageList);
  107. }
  108. /**
  109. * 添加
  110. *
  111. * @param diagramCombination
  112. * @return
  113. */
  114. @AutoLog(value = "图表组合配置-添加")
  115. @ApiOperation(value = "图表组合配置-添加", notes = "图表组合配置-添加")
  116. @PostMapping(value = "/add")
  117. public Result<?> add(@RequestBody DiagramCombination diagramCombination) {
  118. // 校验code是否重复
  119. Result<?> result = this.duplicateCheck(diagramCombination.getTempletCode());
  120. if (result != null) {
  121. return result;
  122. }
  123. // 保存主表/子表信息
  124. try {
  125. this.diagramCombinationService.add(diagramCombination);
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. log.error(CommonConstants.MSG_ERROR_OPERATE, e);
  129. return Result.error(e.getMessage());
  130. }
  131. // 渲染客户端
  132. return Result.OK(CommonConstants.MSG_SUCCESS_ADD,null);
  133. }
  134. /**
  135. * 编辑
  136. *
  137. * @param diagramCombination
  138. * @return
  139. */
  140. @AutoLog(value = "图表组合配置-编辑")
  141. @ApiOperation(value = "图表组合配置-编辑", notes = "图表组合配置-编辑")
  142. @PostMapping(value = "/edit")
  143. public Result<?> edit(@RequestBody DiagramCombination diagramCombination) {
  144. try {
  145. this.diagramCombinationService.edit(diagramCombination);
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. log.error(CommonConstants.MSG_ERROR_OPERATE, e);
  149. return Result.error(e.getMessage());
  150. }
  151. return Result.OK(CommonConstants.MSG_SUCCESS_EDIT,null);
  152. }
  153. /**
  154. * 通过id删除
  155. *
  156. * @param id
  157. * @return
  158. */
  159. @AutoLog(value = "图表组合配置-通过id删除")
  160. @ApiOperation(value = "图表组合配置-通过id删除", notes = "图表组合配置-通过id删除")
  161. @PostMapping(value = "/delete")
  162. public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
  163. try {
  164. this.diagramCombinationService.deleteById(id);
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. log.error(CommonConstants.MSG_ERROR_OPERATE, e);
  168. return Result.error(e.getMessage());
  169. }
  170. return Result.OK(CommonConstants.MSG_SUCCESS_DELTET,null);
  171. }
  172. /**
  173. * 批量删除
  174. *
  175. * @param ids
  176. * @return
  177. */
  178. @AutoLog(value = "图表组合配置-批量删除")
  179. @ApiOperation(value = "图表组合配置-批量删除", notes = "图表组合配置-批量删除")
  180. @PostMapping(value = "/deleteBatch")
  181. public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
  182. try {
  183. this.diagramCombinationService.deleteBatch(ids);
  184. } catch (Exception e) {
  185. e.printStackTrace();
  186. log.error(CommonConstants.MSG_ERROR_OPERATE, e);
  187. return Result.error(e.getMessage());
  188. }
  189. return Result.OK(CommonConstants.MSG_SUCCESS_DELTET_BATCH,null);
  190. }
  191. /**
  192. * 通过id查询
  193. *
  194. * @param id
  195. * @return
  196. */
  197. @AutoLog(value = "图表组合配置-通过id查询")
  198. @ApiOperation(value = "图表组合配置-通过id查询", notes = "图表组合配置-通过id查询")
  199. @GetMapping(value = "/queryById")
  200. public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
  201. DiagramCombination diagramCombination = diagramCombinationService.getById(id);
  202. if (diagramCombination == null) {
  203. return Result.error(CommonConstants.MSG_ERROR_NO_DATA_FOUND + "[ " + id + " ]");
  204. }
  205. return Result.OK(diagramCombination);
  206. }
  207. /**
  208. * 导出excel
  209. *
  210. * @param request
  211. */
  212. @RequestMapping(value = "/exportXls")
  213. public ModelAndView exportXls(HttpServletRequest request, DiagramCombination diagramCombination) {
  214. return super.exportXls(request, diagramCombination, DiagramCombination.class, "图表组合配置");
  215. }
  216. /**
  217. * 通过excel导入数据
  218. *
  219. * @param request
  220. * @param response
  221. * @return
  222. */
  223. @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  224. public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
  225. return super.importExcel(request, response, DiagramCombination.class);
  226. }
  227. //@AutoLog(value = "图表组合配置-获取图表数据")
  228. //@ApiOperation(value="图表组合配置-获取图表数据", notes="图表组合配置-获取图表数据")
  229. @RequestMapping(value = "/getTempletData", method = RequestMethod.GET)
  230. @Transactional(readOnly = true)
  231. public Result<?> getTempletData(@RequestParam Map<String, Object> params) throws CloneNotSupportedException {
  232. JSONObject jsonObject = JSONObject.parseObject(params.get("params").toString());
  233. String templetCode = jsonObject.getString("templetCode");
  234. // 查询主表信息
  235. QueryWrapper<DiagramCombination> diagramCombinationQueryWrapper = new QueryWrapper<>();
  236. diagramCombinationQueryWrapper.eq("templet_code", templetCode);
  237. DiagramCombination diagramCombination = this.diagramCombinationService
  238. .getOne(diagramCombinationQueryWrapper);
  239. if (diagramCombination == null) {
  240. return Result.error(CommonConstants.MSG_ERROR_NO_DATA_FOUND + "[ " + templetCode + " ]");
  241. }
  242. TempletData<DiagramCombination,Object, DiagramConfiguration, DiagramFieldConfiguration, DiagramCombinationQueryConfig> templetData = new TempletData<>();
  243. List<GroupData<ChartsData<Object, DiagramConfiguration, DiagramFieldConfiguration>>> templetDataGroup = new ArrayList<>();
  244. templetData.setGroups(templetDataGroup);
  245. templetData.setTemplet(diagramCombination);
  246. // 获取查询配置数据
  247. QueryWrapper<DiagramCombinationQueryConfig> dcqc = new QueryWrapper<>();
  248. dcqc.eq("graphreport_templet_id", diagramCombination.getId()).orderByAsc("ORDER_NUM");
  249. List<DiagramCombinationQueryConfig> diagramCombinationQueryConfigs = this.diagramCombinationQueryConfigService
  250. .list(dcqc);
  251. templetData.setQueryConfigList(diagramCombinationQueryConfigs);
  252. // 获取组合报表明细
  253. QueryWrapper<DiagramCombinationDetail> qw = new QueryWrapper<DiagramCombinationDetail>();
  254. qw.eq("GRAPHREPORT_TEMPLET_ID", diagramCombination.getId()).orderByAsc("GROUP_NUM","ORDER_NUM");
  255. List<DiagramCombinationDetail> diagramCombinationDetails = this.diagramCombinationDetailService
  256. .list(qw);
  257. Integer currentGroupNum = null;
  258. GroupData<ChartsData<Object, DiagramConfiguration, DiagramFieldConfiguration>> currentGroupData = new GroupData<>();;
  259. if(CollectionUtils.isNotEmpty(diagramCombinationDetails)){
  260. for(DiagramCombinationDetail diagramCombinationDetail : diagramCombinationDetails){
  261. if(currentGroupNum != diagramCombinationDetail.getGroupNum()){
  262. currentGroupNum = diagramCombinationDetail.getGroupNum();
  263. if(CollectionUtils.isNotEmpty(currentGroupData.getCharts())){
  264. templetData.getGroups().add(currentGroupData);
  265. currentGroupData = new GroupData<>();
  266. }else{
  267. currentGroupData = new GroupData<>();
  268. }
  269. //初始化分组数据
  270. currentGroupData.setGroupNum(currentGroupNum);
  271. currentGroupData.setGroupStyle(diagramCombinationDetail.getGroupStyle());
  272. currentGroupData.setGroupTxt(diagramCombinationDetail.getGroupTxt());
  273. currentGroupData.setTempletGrid(diagramCombinationDetail.getTempletGrid());
  274. currentGroupData.setCharts( new ArrayList<ChartsData<Object, DiagramConfiguration, DiagramFieldConfiguration>>());
  275. }
  276. QueryWrapper<DiagramConfiguration> dcQw = new QueryWrapper<>();
  277. dcQw.eq("code", diagramCombinationDetail.getGraphreportCode());
  278. DiagramConfiguration diagramConfiguration = (DiagramConfiguration) diagramConfigurationService
  279. .getOne(dcQw).clone();
  280. if (diagramConfiguration == null) {
  281. return Result.error(CommonConstants.MSG_ERROR_NO_DATA_FOUND + "[ " + diagramCombinationDetail.getGraphreportCode() + " ]");
  282. }
  283. if(StringUtils.isNotBlank(diagramCombinationDetail.getGraphreportType())){
  284. diagramConfiguration.setGraphType(diagramCombinationDetail.getGraphreportType());
  285. }
  286. // 根据sql查询数据
  287. List dataList = Collections.emptyList();
  288. if (CommonConstants.DATA_TYPE_SQL.equals(diagramConfiguration.getDataType())) {
  289. if (jsonObject.size() == 1) {
  290. dataList = this.diagramConfigurationService.selectBySql(diagramConfiguration.getCgrSql());
  291. } else {
  292. // 查询图表字段
  293. QueryWrapper<DiagramFieldConfiguration> fieldQuery = new QueryWrapper<>();
  294. fieldQuery.eq("diagram_code", diagramConfiguration.getCode());
  295. List<DiagramFieldConfiguration> list = diagramFieldConfigurationService.list(fieldQuery);
  296. String cgrSql = diagramConfiguration.getCgrSql();
  297. // 封装查询sql
  298. String tempSql = diagramConfigurationService.handelQuerySql(cgrSql, jsonObject, list);
  299. dataList = this.diagramConfigurationService.selectBySql(tempSql);
  300. }
  301. }
  302. // 获取子表信息
  303. QueryWrapper<DiagramFieldConfiguration> diagramFieldConfigurationQueryWrapper = new QueryWrapper<>();
  304. diagramFieldConfigurationQueryWrapper.eq("DIAGRAM_CODE", diagramCombinationDetail.getGraphreportCode());
  305. List<DiagramFieldConfiguration> diagramFieldConfigurationList = this.diagramFieldConfigurationService
  306. .list(diagramFieldConfigurationQueryWrapper);
  307. Map<String, Expression> expressions = new HashMap<>();
  308. Map<String, String> aggregates = new HashMap<>();
  309. Map<Object,Map<String, Object>> aggregateDataMap = new HashMap<Object,Map<String, Object>>();
  310. String xField = diagramConfiguration.getXaxisField();
  311. String[] groupFields = diagramConfiguration.getGroupField().split(",");
  312. for(DiagramFieldConfiguration dfc : diagramFieldConfigurationList){
  313. if(StringUtils.isNotEmpty(dfc.getFunc())){
  314. String expression = dfc.getFunc();
  315. Expression compiledExp = AviatorEvaluator.compile(expression);
  316. expressions.put(dfc.getFieldName(), compiledExp);
  317. }
  318. /* if(StringUtils.isNotEmpty(dfc.getAggregateType())){
  319. aggregates.put(dfc.getFieldName(), dfc.getAggregateType());
  320. }*/
  321. if(ArrayUtils.contains(groupFields, dfc.getFieldName())){
  322. aggregates.put(dfc.getFieldName(), dfc.getAggregateType());
  323. }
  324. }
  325. if(expressions.size()>0 || diagramConfiguration.getAggregate()){
  326. for(Object data : dataList){
  327. if(data instanceof Map){
  328. for(Entry<String, Expression> entry : expressions.entrySet()){
  329. Map<String,Object> env = new HashMap<String,Object>();
  330. env.putAll((Map<String,Object>) data);
  331. ((Map<String,Object>) data).put(entry.getKey(), entry.getValue().execute(env));
  332. }
  333. if(diagramConfiguration.getAggregate()){
  334. Object xValue = ((Map) data).get(xField);
  335. if(aggregateDataMap.get(xValue) == null){
  336. Map<String, Object> aggregateData = new HashMap<>();
  337. aggregateData.put(xField, xValue);
  338. for(Entry<String, String> yField : aggregates.entrySet()){
  339. aggregateData.put(yField.getKey(),BigDecimal.ZERO);
  340. }
  341. aggregateDataMap.put(xValue, aggregateData);
  342. }
  343. Map<String, Object> aggregateData = aggregateDataMap.get(xValue);
  344. for(Entry<String, String> yField : aggregates.entrySet()){
  345. aggregateData.put(yField.getKey(), OnlineUtil.calculation(aggregateData.get(yField.getKey()), ((Map) data).get(yField.getKey()), yField.getValue()));
  346. }
  347. aggregateDataMap.put(xValue, aggregateData);
  348. }
  349. }
  350. }
  351. }
  352. // 处理树形表格数据
  353. if (diagramConfiguration.getGraphType().equals("treeTable")) {
  354. String unfoldFieldName = diagramFieldConfigurationList.get(0).getFieldName();
  355. dataList = this.diagramConfigurationService.handelTreeTableData(dataList, unfoldFieldName, diagramConfiguration.getPid());
  356. }
  357. Map<String,Collection> result = new HashMap<String, Collection>();
  358. result.put("data", dataList);
  359. result.put("aggregate", aggregateDataMap.values());
  360. // 封装数据
  361. ChartsData<Object, DiagramConfiguration, DiagramFieldConfiguration> chartsData = new ChartsData<>();
  362. chartsData.setData(result);
  363. chartsData.setDictOptions(null);
  364. chartsData.setParam(diagramCombinationDetail);
  365. chartsData.setHead(diagramConfiguration);
  366. chartsData.setItems(diagramFieldConfigurationList);
  367. currentGroupData.getCharts().add(chartsData);
  368. }
  369. if(CollectionUtils.isNotEmpty(currentGroupData.getCharts())){
  370. templetData.getGroups().add(currentGroupData);
  371. }
  372. }
  373. return Result.OK(templetData);
  374. }
  375. /**
  376. * 远程code重复校验
  377. *
  378. * @return
  379. */
  380. @AutoLog(value = "图表组合配置-code重复校验")
  381. @ApiOperation(value = "图表组合配置-code重复校验", notes = "图表组合配置-code重复校验")
  382. @RequestMapping(value = "/codeCheck", method = RequestMethod.GET)
  383. public Result<?> codeCheck(@RequestParam(name = "templetCode", required = true) String templetCode) {
  384. Result<?> result = this.duplicateCheck(templetCode);
  385. if (result != null) {
  386. return result;
  387. }
  388. return Result.OK();
  389. }
  390. /**
  391. * code重复校验
  392. *
  393. * @return
  394. */
  395. private Result<?> duplicateCheck(String templetCode) {
  396. QueryWrapper<DiagramCombination> queryWrapper = new QueryWrapper<>();
  397. queryWrapper.eq("templet_code", templetCode);
  398. List<DiagramCombination> diagramCombinationList = this.diagramCombinationService.list(queryWrapper);
  399. if (diagramCombinationList != null && !diagramCombinationList.isEmpty()) {
  400. return Result.error(CommonConstants.MSG_EXIST + "[ " + templetCode + " ]");
  401. }
  402. return null;
  403. }
  404. }