package com.citygis.web.service.impl;

import com.citygis.common.annotation.DataSource;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.common.enums.DataSourceType;
import com.citygis.framework.datasource.DynamicDataSourceContextHolder;
import com.citygis.web.domain.LookMonitor;
import com.citygis.web.mapper.LookMonitorMapper;
import com.citygis.web.service.LookMonitorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zyl
 * @CreateTime: 2025-01-23
 * @Description: 监控可视化
 * @Version: 1.0
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class LookMonitorServiceimpl implements LookMonitorService {

    @Resource
    LookMonitorMapper lookMonitorMapper;

    @Override
    public AjaxResult getLookMonitor() {

        // 获取今天的日期
        String today = String.valueOf(LocalDate.now());

        Map<String, Object> map = new HashMap<>();

        map.put("api", lookMonitorMapper.getLookMonitorByUrl(today));
        map.put("kb", lookMonitorMapper.getLookMonitorByKb(today));
        map.put("kafka", lookMonitorMapper.getLookMonitorByKafka(today));
        map.put("four", lookMonitorMapper.getUrlMsg(today));

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getLookMonitorByUrlAndStatus() {
        return AjaxResult.success(lookMonitorMapper.getLookMonitorByUrlAndStatus(String.valueOf(LocalDate.now())));
    }

    @Override
    public AjaxResult getDataMap(Integer type) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
        List<LookMonitor> dataMap = lookMonitorMapper.getDataMap(type);
        DynamicDataSourceContextHolder.clearDataSourceType();
        return AjaxResult.success(dataMap);
    }
}
