package com.njcn.device.pq.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.pq.pojo.param.dataClean.MonitorBaseParam;

import com.njcn.device.pq.pojo.vo.dataClean.DetailAbnormalVO;
import com.njcn.device.pq.pojo.vo.dataClean.PowerQualityIndicatorsVO;
import com.njcn.device.pq.pojo.vo.dataClean.VerifyMonitorVO;
import com.njcn.device.pq.service.IDataVerifyService;
import com.njcn.device.pq.service.IPqDataVerifyBakService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 异常数据功能
 * </p>
 *
 * @author cdf
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/dataVerify")
@RequiredArgsConstructor
@Api(tags = "异常数据功能")
public class DataVerifyController extends BaseController {

    private final IDataVerifyService iDataVerifyService;

    private final IPqDataVerifyBakService iPqDataVerifyBakService;


    /**
     * 获取异常监测点相关信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorVerifyData")
    @ApiOperation("异常-获取异常数据主页面")
    public HttpResult<VerifyMonitorVO> getMonitorVerifyData(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("getMonitorVerifyData");
        VerifyMonitorVO verifyMonitorVO = iPqDataVerifyBakService.getMonitorVerifyData(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, verifyMonitorVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorVerifyDay")
    @ApiOperation("异常-更新按钮-获取异常数据列表")
    public HttpResult<List<PowerQualityIndicatorsVO>> getMonitorVerifyDay(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("getMonitorVerifyDay");
        List<PowerQualityIndicatorsVO> list = iPqDataVerifyBakService.getMonitorVerifyDay(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 弹框-获取异常监测点列表
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/monitorAbnormalTable")
    @ApiOperation("异常-弹框-获取异常监测点列表")
    public HttpResult<List<DetailAbnormalVO>> monitorAbnormalTable(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("monitorAbnormalDetail");
        List<DetailAbnormalVO> page = iPqDataVerifyBakService.monitorAbnormalTable(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }


    /**
     * 获取稳态告警监测点相关信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/monitorAbnormalTableDetail")
    @ApiOperation("弹框-获取异常监测点列表详情")
    public HttpResult<DetailAbnormalVO.DetailAbnormalCountVO> monitorAbnormalTableDetail(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("monitorAbnormalTableDetail");
        DetailAbnormalVO.DetailAbnormalCountVO list = iPqDataVerifyBakService.monitorAbnormalTableDetail(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 获取稳态告警数据
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorLimitData")
    @ApiOperation("获取稳态告警数据")
    public HttpResult<VerifyMonitorVO> getMonitorLimitData(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("getMonitorLimitData");
        VerifyMonitorVO verifyMonitorVO = iDataVerifyService.getMonitorLimitData(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, verifyMonitorVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorLimitDataDay")
    @ApiOperation("更新按钮-获取稳态告警数据")
    public HttpResult<List<PowerQualityIndicatorsVO>> getMonitorLimitDataDay(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("getMonitorLimitDataDay");
        List<PowerQualityIndicatorsVO> verifyMonitorVO = iDataVerifyService.getMonitorLimitDataDay(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, verifyMonitorVO, methodDescribe);
    }

    /**
     * 弹框-获取异常监测点列表
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/monitorLimitTable")
    @ApiOperation("弹框-获取稳态告警监测点列表")
    public HttpResult<List<DetailAbnormalVO>> monitorLimitTable(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("monitorLimitTable");
        List<DetailAbnormalVO> list = iDataVerifyService.monitorLimitTable(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 获取稳态告警监测点相关信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/monitorLimitTableDetail")
    @ApiOperation("弹框-获取稳态告警监测点列表详情")
    public HttpResult<DetailAbnormalVO.DetailLimitCountVO> monitorLimitTableDetail(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("monitorLimitTableDetail");
        DetailAbnormalVO.DetailLimitCountVO list = iDataVerifyService.monitorLimitTableDetail(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/limitTableDetail")
    @ApiOperation("监测点评价")
    public HttpResult<DetailAbnormalVO.Assess> limitTableDetail(@RequestBody MonitorBaseParam monitorBaseParam) {
        String methodDescribe = getMethodDescribe("limitTableDetail");
        DetailAbnormalVO.Assess list = iDataVerifyService.limitTableDetail(monitorBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/dataVerifyExcel")
    @ApiOperation(value = "导出异常监测点列表", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void dataVerifyExcel(HttpServletResponse response, MonitorBaseParam monitorBaseParam) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        if(StrUtil.isNotBlank(monitorBaseParam.getSearchBeginTime())&&StrUtil.isNotBlank(monitorBaseParam.getSearchEndTime())){
            monitorBaseParam.setSearchBeginTime(DateUtil.beginOfDay(DateUtil.parse(monitorBaseParam.getSearchBeginTime())).toString());
            monitorBaseParam.setSearchEndTime(DateUtil.endOfDay(DateUtil.parse(monitorBaseParam.getSearchEndTime())).toString());
        }
        iPqDataVerifyBakService.dataVerifyExcel(response, monitorBaseParam);

    }

}

