package org.jeecg.modules.stm.metric.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;



import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.stm.metric.entity.MetricMonitorSet;
import org.jeecg.modules.stm.metric.entity.MetricReport;
import org.jeecg.modules.stm.metric.service.IMetricAppSystemReportService;
import org.jeecg.modules.stm.metric.service.IMetricAppSystemService;
import org.jeecg.modules.stm.metric.service.IMetricMonitorSetService;
import org.jeecg.modules.stm.metric.service.IMetricReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description: 监测方案
 * @Author: jeecg-boot
 * @Date:   2023-07-15
 * @Version: V1.0
 */

@RestController
@RequestMapping("/metric/formulaCalculat")
@Slf4j
public class MetricFormulaCalculatController extends JeecgController<MetricMonitorSet, IMetricMonitorSetService> {
	@Autowired
	private IMetricReportService metricReportService;
	@Autowired
	private IMetricAppSystemReportService metricAppSystemReportService;
	@Autowired
	private IMetricAppSystemService metricAppSystemService;
	@Autowired
	private IMetricMonitorSetService metricMonitorSetService;

	@PostMapping(value = "/exeCaluculat")
	public Result<String> exeCaluculat(@RequestBody MetricMonitorSet metricMonitorSet) {
		int ctp = metricMonitorSet.getMtrcCtp();

		if (ctp == 1) {
			return Result.OK("该指标是基本指标，无需计算");
		}

		String mtrcNo = metricMonitorSet.getMtrcNo();
		switch (mtrcNo) {
			case "IT-KRI-OA-R01":
				metricAppSystemReportService.caluculatAppSystems(metricMonitorSet);
				break;
			case "IT-KRI-OA-R03":
				metricAppSystemReportService.caluculatAppEventCount(metricMonitorSet);
				break;
			case "IT-KRI-OP-R03":
				metricAppSystemReportService.caluculatAppUrgentCount(metricMonitorSet);
				break;
			case "IT-KRI-OP-R02":
				metricAppSystemReportService.caluculatAppFaultCount(metricMonitorSet);
				break;
			default:
				cleanAndCalculate(metricMonitorSet);
				break;
		}

		return Result.OK("成功！");
	}

	/**
	 * 计算所有指标
	 */
	@PostMapping(value = "/caluculatAllMetric")
	public Result<String> caluculatAllMetric() {
		QueryWrapper<MetricMonitorSet> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("mtrc_ctp", 2);
		List<MetricMonitorSet> list = metricMonitorSetService.list(queryWrapper);
		list.forEach(this::processMetricMonitorSet);
		return Result.OK("成功！");
	}

	private void processMetricMonitorSet(MetricMonitorSet metricMonitorSet) {
		if (metricMonitorSet.getFormulas() != null && !metricMonitorSet.getFormulas().isEmpty()) {
			try {
				cleanAndCalculate(metricMonitorSet);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void cleanAndCalculate(MetricMonitorSet metricMonitorSet) {
		QueryWrapper<MetricReport> queryReportWrapper = new QueryWrapper<>();
		queryReportWrapper.eq("mtrc_no", metricMonitorSet.getMtrcNo());
		metricReportService.remove(queryReportWrapper);
		metricReportService.caluculatFormula(metricMonitorSet);
	}

}
