package com.njcn.harmonic.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.param.StatisticsBizBaseParam;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.harmonic.pojo.param.HistoryHarmParam;
import com.njcn.harmonic.pojo.param.HistoryParam;
import com.njcn.harmonic.pojo.param.NormHistoryParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.vo.AssessEvaluation;
import com.njcn.harmonic.pojo.vo.HistoryDataResultVO;
import com.njcn.harmonic.pojo.vo.StatHarmonicOrgVO;
import com.njcn.harmonic.service.HistoryResultService;
import com.njcn.harmonic.service.IRStatLimitTargetDService;
import com.njcn.harmonic.service.OracleResultService;
import com.njcn.influx.pojo.dto.HarmHistoryDataDTO;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
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.List;

/**
 * @author denghuajun
 * @date 2022/3/14
 * 趋势图
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/harmonic")
@Api(tags = "稳态数据分析")
@RequiredArgsConstructor
public class HistoryResultController extends BaseController {

    private final HistoryResultService historyResultService;
    private final IRStatLimitTargetDService limitTargetDService;
    private final OracleResultService oracleResultService;

    @Value("${History.DataSource:0}")
    private Integer HistoryDataSource;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getHistoryResult")
    @ApiOperation("稳态数据分析")
    @ApiImplicitParam(name = "historyParam", value = "稳态数据分析参数", required = true)
    public HttpResult<List<HistoryDataResultVO>> getHistoryResult(@RequestBody @Validated HistoryParam historyParam) {
        String methodDescribe = getMethodDescribe("getHistoryResult");
        List<HistoryDataResultVO> list;
        //针对河北现场特殊处理，pmswifi_开头监测点为无线装置，需要查询influxdb,其他测点查询oracle
        if (HistoryDataSource == 1 && !historyParam.getLineId()[0].contains("pmswifi_")) {
            list = oracleResultService.getHistoryResult(historyParam);
        } else {
            list = historyResultService.getHistoryResult(historyParam);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getHistoryLineData")
    @ApiOperation("异常数据趋势图")
    @ApiImplicitParam(name = "normHistoryParam", value = "异常数据趋势参数", required = true)
    public HttpResult<List<HistoryDataResultVO>> getHistoryLineData(@RequestBody @Validated NormHistoryParam normHistoryParam) {
        String methodDescribe = getMethodDescribe("getHistoryLineData");
        List<HistoryDataResultVO> list = historyResultService.getHistoryLineData(normHistoryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getHarmonicProportion")
    @ApiOperation("获取谐波越限监测点占比")
    @ApiImplicitParam(name = "statisticsBizBaseParam", value = "谐波越限监测点参数", required = true)
    public HttpResult<List<StatHarmonicOrgVO>> getHistoryLineData(@RequestBody @Validated StatisticsBizBaseParam statisticsBizBaseParam) {
        String methodDescribe = getMethodDescribe("getHarmonicProportion");
        List<StatHarmonicOrgVO> list = historyResultService.getHarmonicProportion(statisticsBizBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getHistoryHarmData")
    @ApiOperation("获取谐波历史数据")
    @ApiImplicitParam(name = "historyHarmParam", value = "谐波历史数据请求参数", required = true)
    public HttpResult<HarmHistoryDataDTO> getHistoryHarmData(@RequestBody @Validated HistoryHarmParam historyHarmParam) {
        String methodDescribe = getMethodDescribe("getHistoryHarmData");
        HarmHistoryDataDTO harmHistoryDataDTO = historyResultService.getHistoryHarmData(historyHarmParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, harmHistoryDataDTO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLinesTarget")
    @ApiOperation("获取越线监测点")
    @ApiImplicitParam(name = "param", value = "越线监测点请求参数", required = true)
    public HttpResult<List<String>> getLinesTarget(@RequestBody StatSubstationBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getLinesTarget");
        List<String> linesTarget = limitTargetDService.getLinesTarget(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, linesTarget, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getHarmIAndHarmVData")
    @ApiOperation("评估结果获取谐波电压和谐波电流含有率、三项不平横度和负序电流")
    @ApiImplicitParam(name = "param", value = "获取越线监测点越线次数请求参数", required = true)
    public HttpResult<AssessEvaluation> getHarmIAndHarmVData(@RequestBody StatisticsBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getHarmIAndHarmVData");
        AssessEvaluation harmIAndHarmVData = historyResultService.getHarmIAndHarmVData(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, harmIAndHarmVData, methodDescribe);
    }
}
