package org.stm.metric.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stm.metric.entity.MetricAppSystemReport;
import org.stm.metric.entity.MetricMonitorSet;
import org.stm.metric.entity.MetricReport;
import org.stm.metric.mapper.MetricAppSystemReportMapper;
import org.stm.metric.service.IMetricAppSystemReportService;
import org.stm.metric.service.IMetricReportService;
import org.stm.utils.CommonUtil;

import java.util.*;

/**
 * @Description: 应用系统运行记录表
 * @Author: jeecg-boot
 * @Date:   2024-03-23
 * @Version: V1.0
 */
@Service
public class MetricAppSystemReportServiceImpl extends ServiceImpl<MetricAppSystemReportMapper, MetricAppSystemReport> implements IMetricAppSystemReportService {

    @Autowired
    private IMetricReportService metricReportService;
    @Override
    public void caluculatAppSystems(MetricMonitorSet metricMonitorSet) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = this.baseMapper.caluculatAppSystems();
        List<MetricReport> reportList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            MetricReport metricReport = new MetricReport();
            Map<String,Object> data = list.get(i);
            Double avgVal = Double.parseDouble(data.get("avgval").toString());
            metricReport.setFillVals(CommonUtil.DoubleToStr(avgVal,6));
            metricReport.setReporter(sysUser.getId());
            metricReport.setMtrcNo(metricMonitorSet.getMtrcNo());
            metricReport.setRiskLevel(CommonUtil.computeRiskLevel(avgVal,metricMonitorSet));
            metricReport.setFillTime(getFillTime(data));
            reportList.add(metricReport);
        }
        QueryWrapper<MetricReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mtrc_no",metricMonitorSet.getMtrcNo());
        this.metricReportService.remove(queryWrapper);
        this.metricReportService.saveBatch(reportList);
    }


    @Override
    public void caluculatAppEventCount(MetricMonitorSet metricMonitorSet) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = this.baseMapper.caluculatAppEventCount();
        List<MetricReport> reportList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            MetricReport metricReport = new MetricReport();
            Map<String,Object> data = list.get(i);
            Double eventCount = Double.parseDouble(data.get("cnt").toString());
            metricReport.setFillVals(CommonUtil.DoubleToStr(eventCount,6));
            metricReport.setReporter(sysUser.getId());
            metricReport.setMtrcNo(metricMonitorSet.getMtrcNo());
            metricReport.setRiskLevel(CommonUtil.computeRiskLevel(eventCount,metricMonitorSet));
            metricReport.setFillTime(getFillTime(data));
            reportList.add(metricReport);
        }
        QueryWrapper<MetricReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mtrc_no",metricMonitorSet.getMtrcNo());
        this.metricReportService.remove(queryWrapper);
        this.metricReportService.saveBatch(reportList);
    }


    @Override
    public void caluculatAppUrgentCount(MetricMonitorSet metricMonitorSet) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = this.baseMapper.caluculatAppUrgentCount();
        List<MetricReport> reportList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            MetricReport metricReport = new MetricReport();
            Map<String,Object> data = list.get(i);
            Double eventCount = Double.parseDouble(data.get("cnt").toString());
            metricReport.setFillVals(CommonUtil.DoubleToStr(eventCount,4));
            metricReport.setReporter(sysUser.getId());
            metricReport.setMtrcNo(metricMonitorSet.getMtrcNo());
            metricReport.setRiskLevel(CommonUtil.computeRiskLevel(eventCount,metricMonitorSet));
            metricReport.setFillTime(getFillTime(data));
            reportList.add(metricReport);
        }
        QueryWrapper<MetricReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mtrc_no",metricMonitorSet.getMtrcNo());
        this.metricReportService.remove(queryWrapper);
        this.metricReportService.saveBatch(reportList);
    }


    @Override
    public void caluculatAppFaultCount(MetricMonitorSet metricMonitorSet) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = this.baseMapper.caluculatAppFaultCount();
        List<MetricReport> reportList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            MetricReport metricReport = new MetricReport();
            Map<String,Object> data = list.get(i);
            Double eventCount = Double.parseDouble(data.get("cnt").toString());
            metricReport.setFillVals(CommonUtil.DoubleToStr(eventCount,4));
            metricReport.setReporter(sysUser.getId());
            metricReport.setMtrcNo(metricMonitorSet.getMtrcNo());
            metricReport.setRiskLevel(CommonUtil.computeRiskLevel(eventCount,metricMonitorSet));
            metricReport.setFillTime(getFillTime(data));
            reportList.add(metricReport);
        }
        QueryWrapper<MetricReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mtrc_no",metricMonitorSet.getMtrcNo());
        this.metricReportService.remove(queryWrapper);
        this.metricReportService.saveBatch(reportList);
    }

    private Date getFillTime(Map<String,Object> data) {
        int year = Integer.parseInt(data.get("fill_year").toString());
        int quart = Integer.parseInt(data.get("fill_quarter").toString());
        Calendar calendar = Calendar.getInstance();
        switch (quart) {
            case 1:
                calendar.set(year, 1, 1);
                break;
            case 2:
                calendar.set(year, 4, 1);
                break;
            case 3:
                calendar.set(year, 7, 1);
                break;
            case 4:
                calendar.set(year, 10, 1);
                break;
        }
        return calendar.getTime();
    }
}
