TabSystemMsgServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.citygis.web.service.impl;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.util.MapUtils;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.citygis.common.annotation.DataSource;
  6. import com.citygis.common.enums.DataSourceType;
  7. import com.citygis.framework.datasource.DynamicDataSourceContextHolder;
  8. import com.citygis.web.domain.*;
  9. import com.citygis.web.mapper.TabSystemMsgMapper;
  10. import com.citygis.web.service.ITabFileInfoService;
  11. import com.citygis.web.service.ITabSystemMsgService;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import java.time.DayOfWeek;
  16. import java.time.LocalDate;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * <p>
  23. * 服务实现类
  24. * </p>
  25. *
  26. * @author citygis
  27. * @since 2025-01-09
  28. */
  29. @Service
  30. @DataSource(DataSourceType.SLAVE)
  31. public class TabSystemMsgServiceImpl extends ServiceImpl<TabSystemMsgMapper, TabSystemMsg> implements ITabSystemMsgService {
  32. @Value("${weekReport.filepath}")
  33. private String weekReportFilePath;
  34. @Value("${weekReport.templateFilePath}")
  35. private String templateFilePath;
  36. @Resource
  37. TabSystemMsgMapper tabSystemMsgMapper;
  38. @Resource
  39. ITabFileInfoService tabFileInfoService;
  40. @Override
  41. public List<TabSystemMsg> getStatistics(TabSystemMsg tabSystemMsg) {
  42. List<TabSystemMsg> list = tabSystemMsgMapper.getTabSystemMsgList(tabSystemMsg);
  43. List<TabAccessTableMsg> tabAccessTableMsg = new ArrayList<>();
  44. tabAccessTableMsg.addAll(getJkcloudCount());
  45. tabAccessTableMsg.addAll(tabSystemMsgMapper.getTabAccessTableMsgList(null, null));
  46. list.forEach(tabSystemMsg1 -> {
  47. tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
  48. if (tabAccessTableMsg1.getTableName().equals(tabSystemMsg1.getTableName())) {
  49. tabSystemMsg1.setCount(tabAccessTableMsg1.getCount());
  50. }
  51. });
  52. });
  53. return list;
  54. }
  55. @Override
  56. public List<TabSystemMsg> getTabSystemMsgList(TabSystemMsg tabSystemMsg) {
  57. List<TabSystemMsg> list = tabSystemMsgMapper.getTabSystemMsgList(tabSystemMsg);
  58. List<TabAccessTableMsg> tabAccessTableMsg = new ArrayList<>();
  59. tabAccessTableMsg.addAll(getJkcloudCount());
  60. tabAccessTableMsg.addAll(tabSystemMsgMapper.getTabAccessTableMsgList(null, null));
  61. list.forEach(tabSystemMsg1 -> {
  62. tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
  63. if (tabAccessTableMsg1.getTableName().equals(tabSystemMsg1.getTableName())) {
  64. tabSystemMsg1.setCount(tabAccessTableMsg1.getCount());
  65. }
  66. });
  67. });
  68. return list;
  69. }
  70. public List<TabAccessTableMsg> getJkcloudCount() {
  71. DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE_TWO.name());
  72. List<TabAccessTableMsg> jkcloudCount = tabSystemMsgMapper.getJkcloudCount(null, null);
  73. DynamicDataSourceContextHolder.clearDataSourceType();
  74. return jkcloudCount;
  75. }
  76. @Override
  77. public List<Exchange> getExchangeCount(String dateTime) {
  78. return tabSystemMsgMapper.getExchangeCount(dateTime);
  79. }
  80. @Override
  81. public List<JkCloudCount> getJkcloudNewCount() {
  82. return tabSystemMsgMapper.getJkcloudNewCount();
  83. }
  84. @Override
  85. public List<UrlDayCount> getUrlDayCount() {
  86. return tabSystemMsgMapper.getUrlDayCount();
  87. }
  88. @Override
  89. public List<TopicCount> getTopicCountYesterday() {
  90. return tabSystemMsgMapper.getTopicCountYesterday();
  91. }
  92. @Override
  93. public List<NewCount> getNew() {
  94. return tabSystemMsgMapper.getNew();
  95. }
  96. @DataSource(DataSourceType.SLAVE)
  97. public Boolean insertTabSystemMsg(TabSystemMsg tabSystemMsg) {
  98. return this.save(tabSystemMsg);
  99. }
  100. @Override
  101. public void weekReport() {
  102. List<TabSystemMsg> list = tabSystemMsgMapper.getTabSystemMsgList(new TabSystemMsg());
  103. List<TabAccessTableMsg> tabAccessTableMsg = tabSystemMsgMapper.getTabAccessTableMsgList(null, null);
  104. Map<String, Object> map = MapUtils.newHashMap();
  105. list.forEach(tabSystemMsgReport -> {
  106. tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
  107. if (tabAccessTableMsg1.getTableName().equals(tabSystemMsgReport.getTableEnglishName())) {
  108. map.put(tabAccessTableMsg1.getTableName(), tabAccessTableMsg1.getCount());
  109. }
  110. });
  111. });
  112. // 获取当前日期
  113. LocalDate today = LocalDate.now();
  114. // 获取本周周一的日期
  115. LocalDate monday = today.with(DayOfWeek.MONDAY);
  116. // 获取本周周日的日期
  117. LocalDate sunday = today.with(DayOfWeek.SUNDAY);
  118. // 设置文件保存路径
  119. String filePath = weekReportFilePath + "疾控周报" + monday + "-" + sunday + ".xlsx";
  120. EasyExcel.write(filePath).withTemplate(templateFilePath).sheet().doFill(map);
  121. saveTabFileInfo(filePath);
  122. }
  123. private void saveTabFileInfo(String filePath) {
  124. //保存到主库,手动切换数据源
  125. DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
  126. TabFileInfo tabFileInfo = new TabFileInfo();
  127. tabFileInfo.setFileName(filePath.substring(filePath.lastIndexOf("\\") + 1));
  128. tabFileInfo.setFilePath(filePath);
  129. tabFileInfo.setCreateTime(new Date());
  130. tabFileInfo.setCreateBy("system");
  131. tabFileInfo.setSystemType("疾控周报");
  132. tabFileInfoService.save(tabFileInfo);
  133. DynamicDataSourceContextHolder.clearDataSourceType();
  134. }
  135. }