package com.njcn.device.pq.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.dto.LineDataQualityDTO;
import com.njcn.device.pq.pojo.dto.MonitorIntegrityDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.param.LineBaseQueryParam;
import com.njcn.device.pq.pojo.param.LineDataQualityParam;
import com.njcn.device.pq.pojo.param.LineIntegrityDataParam;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;
import com.njcn.device.pq.pojo.vo.LineIntegrityDataVO;
import com.njcn.device.pq.pojo.vo.OnlineMonitorVo;
import com.njcn.device.pq.pojo.vo.common.DeviceOnlineRate;
import com.njcn.device.pq.service.IRStatIntegrityDService;
import com.njcn.device.pq.service.LineIntegrityDataService;
import com.njcn.harmonic.pojo.vo.IntegrityIconVO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * 监测点数据完整性列表
 * @author yzh
 * @date 2022/9/9
 */

@Slf4j
@Api(tags = "监测点数据完整性列表")
@RestController
@RequestMapping("/LineIntegrityData")
@RequiredArgsConstructor
public class LineIntegrityDataController extends BaseController {

    private final LineIntegrityDataService lineIntegrityDataService;

    private final IRStatIntegrityDService irStatIntegrityDService;


    /**
     * 监测点数据完整性
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineIntegrityData")
    @ApiOperation("监测点数据完整性")
    @ApiImplicitParam(name = "lineIntegrityDataParam", value = "参数实体", required = true)
    public HttpResult<List<LineIntegrityDataVO>> getIntegrityDataOfLine(@RequestBody LineIntegrityDataParam lineIntegrityDataParam) {
        String methodDescribe = getMethodDescribe("getIntegrityDataOfLine");
        List<LineIntegrityDataVO> integrityDataOfLine = lineIntegrityDataService.getIntegrityDataOfLine(lineIntegrityDataParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,integrityDataOfLine,methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getIntegrityIcon")
    @ApiOperation("数据完整性图表")
    @ApiImplicitParam(name = "integrityIconParam", value = "数据完整性参数", required = true)
    public HttpResult<List<IntegrityIconVO>> getIntegrityIcon(@RequestBody @Validated DeviceInfoParam.CompareBusinessParam integrityIconParam) {
        String methodDescribe = getMethodDescribe("getIntegrityIcon");
        List<IntegrityIconVO> integrityIconVO = lineIntegrityDataService.getIntegrityIcon(integrityIconParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, integrityIconVO, methodDescribe);
    }


    /**
     * 通过监测点集合查询监测点数据完整性
     * @author cdf
     * @date 2023/6/7
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getIntegrityByLineIds")
    @ApiOperation("通过监测点集合查询监测点数据完整性")
    @ApiIgnore
    public HttpResult<List<RStatIntegrityD>> getIntegrityByLineIds(@RequestBody List<String> lineIds, @RequestParam("startTime")String startTime, @RequestParam("endTime")String endTime) {
        String methodDescribe = getMethodDescribe("getIntegrityByLineIds");
        if(CollUtil.isNotEmpty(lineIds)){
            QueryWrapper<RStatIntegrityD> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("sum(real_time) as realTime,sum(due_time) as dueTime,avg(real_time*1.0/due_time) as integrityData","line_index").in("line_index",lineIds).between("time_id",startTime,endTime).groupBy("line_index");
            List<RStatIntegrityD> rStatIntegrityDList = irStatIntegrityDService.list(queryWrapper);
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rStatIntegrityDList, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, new ArrayList<>(), methodDescribe);
    }


    /**
     * 监测点数据完整性
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEnterpriseIntegrityData")
    @ApiOperation("企业级监测点数据完整性图")
    @ApiImplicitParam(name = "lineIntegrityDataParam", value = "参数实体", required = true)
    public HttpResult<List<LineIntegrityDataVO>> getEnterpriseIntegrityData(@RequestBody LineIntegrityDataParam lineIntegrityDataParam) {
        String methodDescribe = getMethodDescribe("getEnterpriseIntegrityData");
        List<LineIntegrityDataVO> integrityDataOfLine = lineIntegrityDataService.getEnterpriseIntegrityData(lineIntegrityDataParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,integrityDataOfLine,methodDescribe);
    }

    /**
     * @Description: 通过监测点集合查询总监测点数据完整性
     * @param param
     * @Author: wr
     * @Date: 2024/1/8 13:58
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTotalIntegrityByLineIds")
    @ApiOperation("通过监测点集合查询总监测点数据完整性")
    public HttpResult<Float> getTotalIntegrityByLineIds(@RequestBody LineBaseQueryParam param) {
        String methodDescribe = getMethodDescribe("getTotalIntegrityByLineIds");
        Float integrity = irStatIntegrityDService.getTotalIntegrityByLineIds(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, integrity, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/lineDataQuality")
    @ApiOperation("获取监测点的在线率和数据完成性")
    @ApiImplicitParam(name = "lineDataQualityParam", value = "参数实体", required = true)
    public HttpResult<List<LineDataQualityDTO>> getLineDataQuality(@RequestBody LineDataQualityParam lineDataQualityParam) {
        String methodDescribe = getMethodDescribe("getLineDataQuality");
        List<LineDataQualityDTO> result = irStatIntegrityDService.getLineDataQuality(lineDataQualityParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
    }

    /**
     * 保存前置推送完整性记录数据
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/saveOrUpdateData")
    @ApiOperation("完整性数据录入")
    @ApiImplicitParam(name = "monitorIntegrityDTOList", value = "参数实体", required = true)
    public HttpResult<Boolean> saveOrUpdateData(@RequestBody List<MonitorIntegrityDTO> monitorIntegrityDTOList) {
        String methodDescribe = getMethodDescribe("saveOrUpdateData");
        Boolean flag = irStatIntegrityDService.saveOrUpdateData(monitorIntegrityDTOList);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,flag,methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/data")
    @ApiOperation("监测点数据完整性(冀北)")
    @ApiImplicitParam(name = "param", value = "参数实体", required = true)
    public HttpResult<DeviceOnlineRate> getData(@RequestBody DeviceInfoParam.BusinessParam param) {
        String methodDescribe = getMethodDescribe("getData");
        DeviceOnlineRate rate = irStatIntegrityDService.getData(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rate, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/noData")
    @ApiOperation("监测点数据完整性为0的数据(冀北)")
    @ApiImplicitParam(name = "param", value = "参数实体", required = true)
    public HttpResult<List<OnlineMonitorVo>> getNoData(@RequestBody LineBaseQueryParam param) {
        String methodDescribe = getMethodDescribe("getNoData");
        List<OnlineMonitorVo> rate = irStatIntegrityDService.getNoData(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rate, methodDescribe);
    }

}
