123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package com.citygis.web.service.impl;
- import com.alibaba.excel.EasyExcel;
- import com.alibaba.excel.util.MapUtils;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.citygis.common.annotation.DataSource;
- import com.citygis.common.enums.DataSourceType;
- import com.citygis.framework.datasource.DynamicDataSourceContextHolder;
- import com.citygis.web.domain.*;
- import com.citygis.web.mapper.TabSystemMsgMapper;
- import com.citygis.web.service.ITabFileInfoService;
- import com.citygis.web.service.ITabSystemMsgService;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.time.DayOfWeek;
- import java.time.LocalDate;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author citygis
- * @since 2025-01-09
- */
- @Service
- @DataSource(DataSourceType.SLAVE)
- public class TabSystemMsgServiceImpl extends ServiceImpl<TabSystemMsgMapper, TabSystemMsg> implements ITabSystemMsgService {
- @Value("${weekReport.filepath}")
- private String weekReportFilePath;
- @Value("${weekReport.templateFilePath}")
- private String templateFilePath;
- @Resource
- TabSystemMsgMapper tabSystemMsgMapper;
- @Resource
- ITabFileInfoService tabFileInfoService;
- @Override
- public List<TabSystemMsg> getStatistics(TabSystemMsg tabSystemMsg) {
- List<TabSystemMsg> list = tabSystemMsgMapper.getTabSystemMsgList(tabSystemMsg);
- List<TabAccessTableMsg> tabAccessTableMsg = new ArrayList<>();
- tabAccessTableMsg.addAll(getJkcloudCount());
- tabAccessTableMsg.addAll(tabSystemMsgMapper.getTabAccessTableMsgList(null, null));
- list.forEach(tabSystemMsg1 -> {
- tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
- if (tabAccessTableMsg1.getTableName().equals(tabSystemMsg1.getTableName())) {
- tabSystemMsg1.setCount(tabAccessTableMsg1.getCount());
- }
- });
- });
- return list;
- }
- @Override
- public List<TabSystemMsg> getTabSystemMsgList(TabSystemMsg tabSystemMsg) {
- List<TabSystemMsg> list = tabSystemMsgMapper.getTabSystemMsgList(tabSystemMsg);
- List<TabAccessTableMsg> tabAccessTableMsg = new ArrayList<>();
- tabAccessTableMsg.addAll(getJkcloudCount());
- tabAccessTableMsg.addAll(tabSystemMsgMapper.getTabAccessTableMsgList(null, null));
- list.forEach(tabSystemMsg1 -> {
- tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
- if (tabAccessTableMsg1.getTableName().equals(tabSystemMsg1.getTableName())) {
- tabSystemMsg1.setCount(tabAccessTableMsg1.getCount());
- }
- });
- });
- return list;
- }
- public List<TabAccessTableMsg> getJkcloudCount() {
- DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE_TWO.name());
- List<TabAccessTableMsg> jkcloudCount = tabSystemMsgMapper.getJkcloudCount(null, null);
- DynamicDataSourceContextHolder.clearDataSourceType();
- return jkcloudCount;
- }
- @Override
- public List<Exchange> getExchangeCount(String dateTime) {
- return tabSystemMsgMapper.getExchangeCount(dateTime);
- }
- @Override
- public List<JkCloudCount> getJkcloudNewCount() {
- return tabSystemMsgMapper.getJkcloudNewCount();
- }
- @Override
- public List<UrlDayCount> getUrlDayCount() {
- return tabSystemMsgMapper.getUrlDayCount();
- }
- @Override
- public List<TopicCount> getTopicCountYesterday() {
- return tabSystemMsgMapper.getTopicCountYesterday();
- }
- @Override
- public List<NewCount> getNew() {
- return tabSystemMsgMapper.getNew();
- }
- @DataSource(DataSourceType.SLAVE)
- public Boolean insertTabSystemMsg(TabSystemMsg tabSystemMsg) {
- return this.save(tabSystemMsg);
- }
- @Override
- public void weekReport() {
- List<TabSystemMsg> list = tabSystemMsgMapper.getTabSystemMsgList(new TabSystemMsg());
- List<TabAccessTableMsg> tabAccessTableMsg = tabSystemMsgMapper.getTabAccessTableMsgList(null, null);
- Map<String, Object> map = MapUtils.newHashMap();
- list.forEach(tabSystemMsgReport -> {
- tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
- if (tabAccessTableMsg1.getTableName().equals(tabSystemMsgReport.getTableEnglishName())) {
- map.put(tabAccessTableMsg1.getTableName(), tabAccessTableMsg1.getCount());
- }
- });
- });
- // 获取当前日期
- LocalDate today = LocalDate.now();
- // 获取本周周一的日期
- LocalDate monday = today.with(DayOfWeek.MONDAY);
- // 获取本周周日的日期
- LocalDate sunday = today.with(DayOfWeek.SUNDAY);
- // 设置文件保存路径
- String filePath = weekReportFilePath + "疾控周报" + monday + "-" + sunday + ".xlsx";
- EasyExcel.write(filePath).withTemplate(templateFilePath).sheet().doFill(map);
- saveTabFileInfo(filePath);
- }
- private void saveTabFileInfo(String filePath) {
- //保存到主库,手动切换数据源
- DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
- TabFileInfo tabFileInfo = new TabFileInfo();
- tabFileInfo.setFileName(filePath.substring(filePath.lastIndexOf("\\") + 1));
- tabFileInfo.setFilePath(filePath);
- tabFileInfo.setCreateTime(new Date());
- tabFileInfo.setCreateBy("system");
- tabFileInfo.setSystemType("疾控周报");
- tabFileInfoService.save(tabFileInfo);
- DynamicDataSourceContextHolder.clearDataSourceType();
- }
- }
|