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.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 = tabSystemMsgMapper.getTabAccessTableMsgList();
        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 = tabSystemMsgMapper.getTabAccessTableMsgList();
        list.forEach(tabSystemMsg1 -> {
            tabAccessTableMsg.forEach(tabAccessTableMsg1 -> {
                if (tabAccessTableMsg1.getTableName().equals(tabSystemMsg1.getTableName())) {
                    tabSystemMsg1.setCount(tabAccessTableMsg1.getCount());
                }
            });
        });

        return list;
    }

    @Override
    public List<Exchange> getExchangeCount(String dateTime) {
        return tabSystemMsgMapper.getExchangeCount(dateTime);
    }

    @Override
    public List<JkCloudCount> getJkcloudCount() {
        return tabSystemMsgMapper.getJkcloudCount();
    }

    @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<TabSystemMsgReport> list = tabSystemMsgMapper.weekReport();

        List<TabAccessTableMsg> tabAccessTableMsg = tabSystemMsgMapper.getTabAccessTableMsgList();

        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();
    }
}
