package com.sckw.slope.detection.controller;

import com.alibaba.fastjson2.JSONObject;
import com.sckw.core.annotation.Log;
import com.sckw.core.model.vo.BaseList;
import com.sckw.core.model.vo.PublicBaseList;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.annotation.ExcelContext;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.log.TraceLog.TraceLog;
import com.sckw.slope.detection.model.param.AlarmLogThresholdExport;
import com.sckw.slope.detection.model.param.AlarmLogThresholdQuery;
import com.sckw.slope.detection.model.param.AlarmStatisticsQuery;
import com.sckw.slope.detection.model.vo.ThresholdLogDetailVO;
import com.sckw.slope.detection.model.vo.excel.KwsAlarmExportVO;
import com.sckw.slope.detection.model.vo.excel.ThresholdRecordDetailExportVO;
import com.sckw.slope.detection.service.KwsAlarmService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lfdc
 * @description 报警Controller
 * @date 2023-10-30 17:10:29
 */
@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmController {

    @Autowired
    KwsAlarmService kwsAlarmService;

    @Log(description = "告警日志-系统日志")
    @RequestMapping(name = "告警日志-系统日志", value = "/logSelectAll", method = RequestMethod.POST)
    public HttpResult logSelectAll(@Valid @RequestBody AlarmLogThresholdQuery query) {
        log.info("告警日志-系统日志 logSelectAll param {}", JSONObject.toJSONString(query));
        return HttpResult.ok(kwsAlarmService.logSelectAll(query));
    }

    @Log(description = "告警查询-分页")
    @RequestMapping(name = "告警查询-分页", value = "/select", method = RequestMethod.POST)
    public HttpResult select(@Valid @RequestBody AlarmLogThresholdQuery query, HttpServletRequest request) {
        log.info("告警查询-分页 select param {}", JSONObject.toJSONString(query));
        return HttpResult.ok(kwsAlarmService.select(query, request));
    }

    @Log(description = "告警已读")
    @RequestMapping(name = "告警已读", value = "/read", method = RequestMethod.POST)
    public HttpResult read(@Valid @RequestBody BaseList baseList, HttpServletRequest request) {
        log.info("告警已读 read param {}", JSONObject.toJSONString(baseList));
        return kwsAlarmService.read(baseList, request);
    }


    @TraceLog(description = "告警导出")
    @Log(description = "告警导出")
    @RequestMapping(name = "告警导出", value = "/export", method = RequestMethod.POST)
    public HttpResult export(@Valid @RequestBody AlarmLogThresholdExport query, HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("告警导出 export param {}", JSONObject.toJSONString(query));
        List<KwsAlarmExportVO> export = kwsAlarmService.export(query, request, response);
        if (org.springframework.util.CollectionUtils.isEmpty(export)) {
            return HttpResult.ok(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelContext excelContext = KwsAlarmExportVO.class.getAnnotation(ExcelContext.class);
        String filePath = ExcelUtil.exportByExcelUploadingToOss(response, export, excelContext.fileName() + ".xlsx", KwsAlarmExportVO.class);
        Map<String, String> filePathMap = new HashMap<>();
        filePathMap.put("filePath", filePath);
        return HttpResult.ok(HttpStatus.SUCCESS_CODE, "导出成功", filePathMap);
    }

    @Log(description = "告警通知-详情/告警日志-告警明细")
    @RequestMapping(name = "告警通知-详情", value = "/thresholdDetail", method = RequestMethod.POST)
    public HttpResult thresholdDetail(@Valid @RequestBody PublicBaseList baseList, HttpServletRequest request) {
        log.info("告警通知-详情 threshold_detail param {}", baseList);
        return HttpResult.ok(kwsAlarmService.thresholdDetail(baseList, request));
    }

    @TraceLog(description = "告警记录-导出详情明细")
    @Log(description = "告警记录-导出详情明细")
    @RequestMapping(name = "告警记录-导出详情明细", value = "/exportDetail", method = RequestMethod.POST)
    public HttpResult exportDetail(@Valid @RequestBody BaseList baseList, HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("告警记录-导出详情明细 exportDetail param {}", baseList);
        List<ThresholdRecordDetailExportVO> list = kwsAlarmService.exportDetail(baseList, request);
        if (org.springframework.util.CollectionUtils.isEmpty(list)) {
            return HttpResult.ok(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelContext excelContext = KwsAlarmExportVO.class.getAnnotation(ExcelContext.class);
        String filePath = ExcelUtil.exportByExcelUploadingToOss(response, list, excelContext.fileName() + ".xlsx", ThresholdRecordDetailExportVO.class);
        Map<String, String> filePathMap = new HashMap<>();
        filePathMap.put("filePath", filePath);
        return HttpResult.ok(filePathMap);
    }

    @Log(description = "告警记录-告警明细")
    @RequestMapping(name = "告警记录-告警明细", value = "/detail", method = RequestMethod.GET)
    public HttpResult detail(@RequestParam("id") String id, HttpServletRequest request) {
        log.info("告警记录-告警明细 detail param {}", id);
        return kwsAlarmService.detail(id, request);
    }

    @Log(description = "告警记录-告警明细")
    @RequestMapping(name = "告警记录-告警明细", value = "/exportDetailById", method = RequestMethod.GET)
    public HttpResult exportDetailById(@RequestParam("id") String id, HttpServletRequest request,HttpServletResponse response) throws IOException {
        log.info("告警记录-告警明细 detail param {}", id);
        List<ThresholdLogDetailVO> thresholdLogDetailVOS = kwsAlarmService.exportDetailById(id, request);
        if (org.springframework.util.CollectionUtils.isEmpty(thresholdLogDetailVOS)) {
            return HttpResult.ok(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelContext excelContext = KwsAlarmExportVO.class.getAnnotation(ExcelContext.class);
        String filePath = ExcelUtil.exportByExcelUploadingToOss(response, thresholdLogDetailVOS, excelContext.fileName() + ".xlsx", KwsAlarmExportVO.class);
        Map<String, String> filePathMap = new HashMap<>();
        filePathMap.put("filePath", filePath);
        return HttpResult.ok(filePath);
    }

    @Log(description = "数据告警-图-项目/项目告警-图")
    @RequestMapping(name = "项目告警-图", value = "/projectAlarm", method = RequestMethod.POST)
    public HttpResult projectAlarm(@Valid @RequestBody AlarmStatisticsQuery query, HttpServletRequest request) {
        log.info("数据告警-图-项目/项目告警-图 projectAlarm param {}", query);
        return kwsAlarmService.projectAlarm(query, request);
    }

    @Log(description = "数据告警-图")
    @RequestMapping(name = "数据告警-图", value = "/dataAlarm", method = RequestMethod.POST)
    public HttpResult dataAlarm(@Valid @RequestBody AlarmStatisticsQuery query, HttpServletRequest request) {
        log.info("数据告警-图 dataAlarm param {}", query);
            return kwsAlarmService.dataAlarm(query, request);
    }

    @Log(description = "设备告警-图")
    @RequestMapping(name = "设备告警-图", value = "/deviceAlarm", method = RequestMethod.POST)
    public HttpResult deviceAlarm(@Valid @RequestBody AlarmStatisticsQuery query, HttpServletRequest request) {
        log.info("设备告警-图 deviceAlarm param {}", query);
        return kwsAlarmService.deviceAlarm(query, request);
    }

    @Log(description = "设备告警-项目")
    @RequestMapping(name = "设备告警-项目", value = "/deviceAlarmProject", method = RequestMethod.POST)
    public HttpResult deviceAlarmProject(@Valid @RequestBody AlarmStatisticsQuery query, HttpServletRequest request) {
        log.info("设备告警-项目 deviceAlarmProject param {}", query);
        return kwsAlarmService.deviceAlarmProject(query, request);
    }
}
