BaseScheduleService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package edp.davinci.service.impl;
  2. import edp.core.utils.CollectionUtils;
  3. import edp.core.utils.DateUtils;
  4. import edp.core.utils.ServerUtils;
  5. import edp.davinci.core.enums.LogNameEnum;
  6. import edp.davinci.dao.DashboardMapper;
  7. import edp.davinci.dao.DisplaySlideMapper;
  8. import edp.davinci.dto.cronJobDto.CronJobConfig;
  9. import edp.davinci.dto.cronJobDto.CronJobContent;
  10. import edp.davinci.dto.dashboardDto.DashboardTree;
  11. import edp.davinci.model.Dashboard;
  12. import edp.davinci.model.DisplaySlide;
  13. import edp.davinci.service.ShareService;
  14. import edp.davinci.service.screenshot.ImageContent;
  15. import edp.davinci.service.screenshot.ScreenshotUtil;
  16. import edp.davinci.service.share.ShareDataPermission;
  17. import edp.davinci.service.share.ShareFactor;
  18. import edp.davinci.service.share.ShareMode;
  19. import edp.davinci.service.share.ShareType;
  20. import org.apache.commons.lang.StringUtils;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26. import static edp.core.consts.Consts.AT_SYMBOL;
  27. public class BaseScheduleService {
  28. @Autowired
  29. private String TOKEN_SECRET;
  30. @Autowired
  31. protected DashboardMapper dashboardMapper;
  32. @Autowired
  33. protected ScreenshotUtil screenshotUtil;
  34. @Autowired
  35. protected DisplaySlideMapper displaySlideMapper;
  36. @Autowired
  37. private ShareService shareService;
  38. @Autowired
  39. private ServerUtils serverUtils;
  40. protected static final Logger scheduleLogger = LoggerFactory.getLogger(LogNameEnum.BUSINESS_SCHEDULE.getName());
  41. protected static final String PORTAL = "PORTAL";
  42. protected static final String DISPLAY = "DISPLAY";
  43. protected static final String DASHBOARD = "DASHBOARD";
  44. protected static final String WIDGET = "WIDGET";
  45. /**
  46. * 根据job配置截取图片
  47. *
  48. * @param jobId
  49. * @param cronJobConfig
  50. * @param userId
  51. * @return
  52. * @throws Exception
  53. */
  54. public List<ImageContent> generateImages(long jobId, CronJobConfig cronJobConfig, Long userId) throws Exception {
  55. scheduleLogger.info("CronJob({}) fetching images contents", jobId);
  56. List<ImageContent> imageContents = new ArrayList<>();
  57. Map<String, Integer> vizOrderMap = new HashMap<>();
  58. Map<Long, Map<Long, Integer>> displayPageMap = new HashMap<>();
  59. List<CronJobContent> jobContentList = getCronJobContents(cronJobConfig, vizOrderMap, displayPageMap);
  60. if (CollectionUtils.isEmpty(jobContentList)) {
  61. scheduleLogger.warn("CronJob({}) share entity is empty", jobId);
  62. return null;
  63. }
  64. for (CronJobContent cronJobContent : jobContentList) {
  65. int order = 0;
  66. if (cronJobContent.getContentType().equalsIgnoreCase(DISPLAY)) {
  67. if (vizOrderMap.containsKey(DISPLAY + AT_SYMBOL + cronJobContent.getId())) {
  68. order = vizOrderMap.get(DISPLAY + AT_SYMBOL + cronJobContent.getId());
  69. }
  70. if (!CollectionUtils.isEmpty(displayPageMap)) {
  71. Map<Long, Integer> slidePageMap = displayPageMap.get(cronJobContent.getId());
  72. if (CollectionUtils.isEmpty(cronJobContent.getItems())) {
  73. int finalOrder = order;
  74. slidePageMap.forEach((slide, page) -> {
  75. String url = getContentUrl(userId, cronJobContent.getContentType(), cronJobContent.getId(), page);
  76. imageContents.add(new ImageContent(finalOrder + page, cronJobContent.getId(), cronJobContent.getContentType(), url));
  77. });
  78. } else {
  79. for (Long slideId : cronJobContent.getItems()) {
  80. if (slidePageMap.containsKey(slideId)) {
  81. int page = slidePageMap.get(slideId);
  82. String url = getContentUrl(userId, cronJobContent.getContentType(), cronJobContent.getId(), page);
  83. imageContents.add(new ImageContent(order + page, cronJobContent.getId(), cronJobContent.getContentType(), url));
  84. }
  85. }
  86. }
  87. }
  88. } else {
  89. if (vizOrderMap.containsKey(DASHBOARD + AT_SYMBOL + cronJobContent.getId())) {
  90. order = vizOrderMap.get(DASHBOARD + AT_SYMBOL + cronJobContent.getId());
  91. }
  92. String url = getContentUrl(userId, cronJobContent.getContentType(), cronJobContent.getId(), -1);
  93. imageContents.add(new ImageContent(order, cronJobContent.getId(), cronJobContent.getContentType(), url));
  94. }
  95. }
  96. if (!CollectionUtils.isEmpty(imageContents)) {
  97. screenshotUtil.screenshot(jobId, imageContents, cronJobConfig.getImageWidth());
  98. }
  99. scheduleLogger.info("CronJob({}) fetched images contents, count:{}", jobId, imageContents.size());
  100. return imageContents;
  101. }
  102. protected List<CronJobContent> getCronJobContents(CronJobConfig cronJobConfig, Map<String, Integer> orderMap,
  103. Map<Long, Map<Long, Integer>> displayPageMap) {
  104. List<CronJobContent> jobContentList = new ArrayList<>();
  105. Map<String, Integer> orderWeightMap = new HashMap<>();
  106. Set<Long> dashboardIds = new HashSet<>();
  107. Set<Long> checkedPortalIds = new HashSet<>();
  108. Set<Long> refPortalIds = new HashSet<>();
  109. Set<Long> checkedDisplayIds = new HashSet<>();
  110. for (int i = 0; i < cronJobConfig.getContentList().size(); i++) {
  111. int orderWeight = i * 1000;
  112. CronJobContent cronJobContent = cronJobConfig.getContentList().get(i);
  113. if (cronJobContent.getContentType().equalsIgnoreCase(DISPLAY)) {
  114. checkedDisplayIds.add(cronJobContent.getId());
  115. orderWeightMap.put(DISPLAY + AT_SYMBOL + cronJobContent.getId(), orderWeight);
  116. jobContentList.add(cronJobContent);
  117. orderMap.put(DISPLAY + AT_SYMBOL + cronJobContent.getId(), orderWeight);
  118. } else {
  119. orderWeightMap.put(PORTAL + AT_SYMBOL + cronJobContent.getId(), orderWeight);
  120. if (CollectionUtils.isEmpty(cronJobContent.getItems())) {
  121. checkedPortalIds.add(cronJobContent.getId());
  122. } else {
  123. List<Long> items = cronJobContent.getItems();
  124. dashboardIds.addAll(items);
  125. }
  126. }
  127. }
  128. // dashboard
  129. Set<Dashboard> dashboards = new HashSet<>();
  130. if (!CollectionUtils.isEmpty(dashboardIds)) {
  131. Set<Dashboard> checkDashboards = dashboardMapper.queryDashboardsByIds(dashboardIds);
  132. if (!CollectionUtils.isEmpty(checkDashboards)) {
  133. dashboards.addAll(checkDashboards);
  134. }
  135. }
  136. if (!CollectionUtils.isEmpty(checkedPortalIds)) {
  137. Set<Dashboard> checkoutPortalDashboards = dashboardMapper.queryByPortals(checkedPortalIds);
  138. if (!CollectionUtils.isEmpty(checkoutPortalDashboards)) {
  139. dashboards.addAll(checkoutPortalDashboards);
  140. }
  141. }
  142. if (!CollectionUtils.isEmpty(dashboards)) {
  143. for (Dashboard dashboard : dashboards) {
  144. if (dashboard != null && dashboard.getType() == 1) {
  145. jobContentList.add(new CronJobContent(DASHBOARD, dashboard.getId()));
  146. refPortalIds.add(dashboard.getDashboardPortalId());
  147. }
  148. }
  149. }
  150. if (!CollectionUtils.isEmpty(refPortalIds)) {
  151. Set<Dashboard> refPortalAllDashboards = dashboardMapper.queryByPortals(refPortalIds);
  152. Map<Long, List<Dashboard>> portalDashboardsMap = refPortalAllDashboards.stream().collect(Collectors.groupingBy(Dashboard::getDashboardPortalId));
  153. portalDashboardsMap.forEach((pId, ds) -> {
  154. DashboardTree tree = new DashboardTree(pId, 0);
  155. buildDashboardTree(tree, ds);
  156. List<DashboardTree> list = tree.traversalLeaf();
  157. if (!CollectionUtils.isEmpty(list)) {
  158. for (int i = 0; i < list.size(); i++) {
  159. DashboardTree node = list.get(i);
  160. if (!orderMap.containsKey(DASHBOARD + AT_SYMBOL + node.getId())) {
  161. orderMap.put(DASHBOARD + AT_SYMBOL + node.getId(), i + orderWeightMap.get(PORTAL + AT_SYMBOL + pId));
  162. }
  163. }
  164. }
  165. });
  166. }
  167. //display
  168. List<DisplaySlide> displaySlides = displaySlideMapper.queryByDisplayIds(checkedDisplayIds);
  169. if (!CollectionUtils.isEmpty(displaySlides)) {
  170. Map<Long, List<DisplaySlide>> displaySlidesMap = displaySlides.stream().collect(Collectors.groupingBy(DisplaySlide::getDisplayId));
  171. displaySlidesMap.forEach((displayId, slides) -> {
  172. Map<Long, Integer> slidePageMap = new HashMap<>();
  173. slides.sort(Comparator.comparing(DisplaySlide::getIndex));
  174. for (int i = 0; i < slides.size(); i++) {
  175. slidePageMap.put(slides.get(i).getId(), i + 1);
  176. }
  177. displayPageMap.put(displayId, slidePageMap);
  178. });
  179. }
  180. return jobContentList;
  181. }
  182. private void buildDashboardTree(DashboardTree root, List<Dashboard> dashboards) {
  183. if (CollectionUtils.isEmpty(dashboards)) {
  184. return;
  185. }
  186. Map<Long, Set<Dashboard>> dashboardsMap = new HashMap<>();
  187. List<DashboardTree> rootChildren = new ArrayList<>();
  188. for (Dashboard dashboard : dashboards) {
  189. if (dashboard.getParentId() > 0L && !dashboard.getParentId().equals(dashboard.getId())) {
  190. Set<Dashboard> set;
  191. if (dashboardsMap.containsKey(dashboard.getParentId())) {
  192. set = dashboardsMap.get(dashboard.getParentId());
  193. } else {
  194. set = new HashSet<>();
  195. }
  196. set.add(dashboard);
  197. dashboardsMap.put(dashboard.getParentId(), set);
  198. } else {
  199. rootChildren.add(new DashboardTree(dashboard.getId(), dashboard.getIndex()));
  200. }
  201. }
  202. rootChildren.sort(Comparator.comparing(DashboardTree::getIndex));
  203. root.setChildren(rootChildren);
  204. for (DashboardTree child : rootChildren) {
  205. child.setChildren(getChildren(dashboardsMap, child));
  206. }
  207. }
  208. private List<DashboardTree> getChildren(Map<Long, Set<Dashboard>> dashboardsMap, DashboardTree node) {
  209. if (CollectionUtils.isEmpty(dashboardsMap)) {
  210. return null;
  211. }
  212. Set<Dashboard> children = dashboardsMap.get(node.getId());
  213. if (CollectionUtils.isEmpty(children)) {
  214. return null;
  215. }
  216. List<DashboardTree> list = new ArrayList<>();
  217. for (Dashboard dashboard : children) {
  218. DashboardTree treeNode = new DashboardTree(dashboard.getId(), dashboard.getIndex());
  219. treeNode.setChildren(getChildren(dashboardsMap, treeNode));
  220. list.add(treeNode);
  221. }
  222. list.sort(Comparator.comparing(DashboardTree::getIndex));
  223. return list;
  224. }
  225. private String getContentUrl(Long userId, String contentType, Long contentId, int index) {
  226. ShareFactor shareFactor = ShareFactor.Builder
  227. .shareFactor()
  228. .withMode(ShareMode.NORMAL)
  229. .withEntityId(contentId)
  230. .withSharerId(userId)
  231. .withExpired(DateUtils.add(DateUtils.currentDate(), Calendar.DATE, 1))
  232. .withPermission(ShareDataPermission.SHARER)
  233. .build();
  234. String page = null;
  235. switch (contentType.toUpperCase()) {
  236. case WIDGET:
  237. shareFactor.setType(ShareType.WIDGET);
  238. break;
  239. case DISPLAY:
  240. shareFactor.setType(ShareType.DISPLAY);
  241. page = "&p=" + index;
  242. break;
  243. default:
  244. shareFactor.setType(ShareType.DASHBOARD);
  245. break;
  246. }
  247. String shareToken = shareFactor.toShareResult(TOKEN_SECRET).getToken();
  248. StringBuilder sb = new StringBuilder();
  249. sb.append(serverUtils.getTempServerPath())
  250. .append("/share.html")
  251. .append("?shareToken=")
  252. .append(shareToken);
  253. sb.append(StringUtils.isEmpty(page) ? "" : page);
  254. sb.append("#/share/").append(WIDGET.equalsIgnoreCase(contentType) || PORTAL.equalsIgnoreCase(contentType) ? DASHBOARD.toLowerCase() : contentType);
  255. return sb.toString();
  256. }
  257. }