package com.njcn.device.pq.controller;

import cn.hutool.core.collection.CollectionUtil;
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.common.service.GeneralDeviceService;
import com.njcn.device.common.service.TerminalBaseService;
import com.njcn.device.pq.mapper.RStatOnlinerateDMapper;
import com.njcn.device.pq.pojo.bo.BaseLineInfo;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.device.pq.pojo.po.TopMsgPO;
import com.njcn.device.pq.pojo.vo.*;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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 java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年02月11日 09:28
 */
@Slf4j
@Api(tags = "获取终端信息")
@RestController
@RequestMapping("/deviceInfo")
@RequiredArgsConstructor
public class GeneralDeviceInfoController extends BaseController {


    private final GeneralDeviceService generalDeviceService;

    private final TerminalBaseService terminalBaseService;

    private final RStatOnlinerateDMapper onlineRateMapper;
    /**
     * 获取按部门分类的实际运行终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalRunDeviceInfoAsDept")
    @ApiOperation("获取按部门分类的实际运行终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<List<GeneralDeviceDTO>> getPracticalRunDeviceInfoAsDept(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getPracticalRunDeviceInfoAsDept");
        List<GeneralDeviceDTO> deptDeviceInfos = generalDeviceService.getDeviceInfoAsDept(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(deptDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceInfos, methodDescribe);
        }
    }

    /**
     * 获取按部门分类的实际便携式终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOfflineRunDeviceInfoAsDept")
    @ApiOperation("获取按部门分类的实际便携式终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<List<GeneralDeviceDTO>> getOfflineRunDeviceInfoAsDept(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getOfflineRunDeviceInfoAsDept");
        List<GeneralDeviceDTO> deptDeviceInfos = generalDeviceService.getDeviceInfoAsDept(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(2).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(deptDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceInfos, methodDescribe);
        }
    }


    /**
     * 获取按部门分类的实际所有终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalAllDeviceInfoAsDept")
    @ApiOperation("获取按部门分类的实际所有终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<List<GeneralDeviceDTO>> getPracticalAllDeviceInfoAsDept(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getPracticalAllDeviceInfoAsDept");
        List<GeneralDeviceDTO> deptDeviceInfos = generalDeviceService.getDeviceInfoAsDept(deviceInfoParam, null, Stream.of(1).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(deptDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceInfos, methodDescribe);
        }
    }



    /**
     * 按变电站分类的实际终端综合信息
     *
     * 1、取变电站索引集合&排重
     * 2、根据变电站索引获取出名称+索引信息
     * 3、取所有监测点索引&排重
     * 4、获取所有检测的pids
     * 5、清洗数据
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalRunDeviceInfoAsSubstation")
    @ApiOperation("获取按变电站分类的实际运行终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    public HttpResult<List<GeneralDeviceDTO>> getPracticalRunDeviceInfoAsSubstation(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getPracticalRunDeviceInfoAsSubstation");
        List<GeneralDeviceDTO> substationDeviceInfos = generalDeviceService.getDeviceInfoAsSubstation(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(substationDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, new ArrayList<>(), methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, substationDeviceInfos, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalAllDeviceInfoAsSubstation")
    @ApiOperation("获取按变电站分类的所有终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    public HttpResult<List<GeneralDeviceDTO>> getPracticalAllDeviceInfoAsSubstation(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getPracticalAllDeviceInfoAsSubstation");
        List<GeneralDeviceDTO> substationDeviceInfos = generalDeviceService.getDeviceInfoAsSubstation(deviceInfoParam, null, Stream.of(1).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(substationDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, new ArrayList<>(), methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, substationDeviceInfos, methodDescribe);
        }
    }

    /**
     * 获取实际运行终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalRunDeviceInfo")
    @ApiOperation("获取实际运行终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    public HttpResult<List<GeneralDeviceDTO>> getPracticalRunDeviceInfo(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getPracticalRunDeviceInfo");
        List<GeneralDeviceDTO> deptDeviceInfos = generalDeviceService.getDeviceInfo(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(deptDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceInfos, methodDescribe);
        }
    }

    /**
     * 获取实际便携式终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOfflineRunDeviceInfo")
    @ApiOperation("获取实际便携式终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<List<GeneralDeviceDTO>> getOfflineRunDeviceInfo(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getOfflineRunDeviceInfo");
        List<GeneralDeviceDTO> deptDeviceInfos = generalDeviceService.getDeviceInfo(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(2).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(deptDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceInfos, methodDescribe);
        }
    }

    /**
     * 获取实际所有终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalAllDeviceInfo")
    @ApiOperation("获取实际所有终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    public HttpResult<List<GeneralDeviceDTO>> getPracticalAllDeviceInfo(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getPracticalAllDeviceInfo");
        List<GeneralDeviceDTO> deptDeviceInfos = generalDeviceService.getDeviceInfo(deviceInfoParam, null, Stream.of(1).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(deptDeviceInfos)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceInfos, methodDescribe);
        }
    }

    /**
     * 获取监测点及以上层的基础信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getBaseLineInfo")
    @ApiOperation("获取监测点及以上层的基础信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndex", value = "监测点集合", required = true)
    })
    public HttpResult<List<BaseLineInfo>> getBaseLineInfo(@RequestBody List<String> lineIndex) {
        String methodDescribe = getMethodDescribe("getBaseLineInfo");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, terminalBaseService.getBaseLineInfo(lineIndex), methodDescribe);
    }


    /**
     * 按部门分类获取通讯异常设备警告
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getExceptionDeviceInfoAsDept")
    @ApiOperation("按部门分类获取通讯异常设备警告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<List<ExceptionDeviceInfoVO>> getExceptionDeviceInfoAsDept(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getExceptionDeviceInfoAsDept");
        List<ExceptionDeviceInfoVO> exceptionDeviceInfoAsDept = generalDeviceService.getExceptionDeviceInfoAsDept(deviceInfoParam);
        if (CollectionUtil.isEmpty(exceptionDeviceInfoAsDept)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, exceptionDeviceInfoAsDept, methodDescribe);
        }
    }

    /**
     * 按部门获取终端设备总数，在线数，在线率
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDeptDeviceDetail")
    @ApiOperation("按部门获取终端设备总数，在线数，在线率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<DeptDeviceDetailVO> getDeptDeviceDetail(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getDeptDeviceDetail");
        DeptDeviceDetailVO deptDeviceDetail = generalDeviceService.getDeptDeviceDetail(deviceInfoParam);

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptDeviceDetail, methodDescribe);

    }

    /**
     * 按部门获变电站数，变电站在线数，在线率
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDeptSubstationDetail")
    @ApiOperation("按部门获变电站数，变电站在线数，在线率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    @Deprecated
    public HttpResult<DeptSubstationDetailVO> getDeptSubstationDetail(@RequestBody @Validated DeviceInfoParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getDeptSubstationDetail");
        DeptSubstationDetailVO deptSubstationDetail = generalDeviceService.getDeptSubstationDetail(deviceInfoParam);

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, deptSubstationDetail, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOnlineRateByDevIds")
    @ApiOperation("终端在线率（谐波专用）")
    @ApiImplicitParam(name = "param", value = "参数实体", required = true)
    public HttpResult<List<RStatOnlineRateVO>> getOnlineRateByDevIds(@RequestBody OnlineRateParam param) {
        String methodDescribe = getMethodDescribe("getOnlineRateByDevIds");
        List<RStatOnlineRateVO> onlineRateByDevIds = onlineRateMapper.getOnlineRateByDevIds(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,onlineRateByDevIds,methodDescribe);
    }




    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deptGetRunLine")
    @ApiOperation("根据单位获取投运监测点")
    @ApiImplicitParam(name = "deptId", value = "单位部门索引", required = true)
    public HttpResult<List<String>> deptGetRunLine(@RequestParam("deptId")String deptId) {
        String methodDescribe = getMethodDescribe("deptGetRunLine");
        List<String> runLineIds = generalDeviceService.deptGetRunLine(deptId,Stream.of(0).collect(Collectors.toList()),Stream.of(0,1,2).collect(Collectors.toList()));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,runLineIds,methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/deptGetRunLineEvent")
    @ApiOperation("根据单位获取暂态系统投运监测点")
    @ApiImplicitParam(name = "deptId", value = "单位部门索引", required = true)
    public HttpResult<List<String>> deptGetRunLineEvent(@RequestParam("deptId")String deptId) {
        String methodDescribe = getMethodDescribe("deptGetRunLineEvent");
        List<String> runLineIds = generalDeviceService.deptGetRunLine(deptId,Stream.of(0).collect(Collectors.toList()),Stream.of(0,2).collect(Collectors.toList()));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,runLineIds,methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deptGetRunDev")
    @ApiOperation("根据单位获取终端id")
    @ApiImplicitParam(name = "deptId", value = "单位部门索引", required = true)
    public HttpResult<List<String>> deptGetRunDev(@RequestParam("deptId")String deptId) {
        String methodDescribe = getMethodDescribe("deptGetRunDev");
        List<String> runLineIds = generalDeviceService.deptGetRunDev(deptId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,runLineIds,methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDevInfoByIds")
    @ApiOperation("根据终端id集合获取终端信息")
    @ApiImplicitParam(name = "devIds", value = "单位部门索引", required = true)
    public HttpResult<List<DevDetail>> getDevInfoByIds(@RequestBody List<String> devIds) {
        String methodDescribe = getMethodDescribe("getDevInfoByIds");
        List<DevDetail> runLineIds = generalDeviceService.getDevInfoByIds(devIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,runLineIds,methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTopMsgInfoByIds")
    @ApiOperation("根据id集合获取终端消息信息")
    public HttpResult<List<TopMsgPO>> getTopMsgInfoByIds(@RequestBody OnlineRateParam param) {
        String methodDescribe = getMethodDescribe("getTopMsgInfoByIds");
        List<TopMsgPO> runLineIds = generalDeviceService.getTopMsgInfoByIds(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,runLineIds,methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTotalOnlineRates")
    @ApiOperation("根据终端id集合获取总终端在线率")
    public HttpResult<Float> getTotalOnlineRates(@RequestBody OnlineRateParam param) {
        String methodDescribe = getMethodDescribe("getTotalOnlineRates");
        Float totalOnlineRates = onlineRateMapper.getTotalOnlineRates(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,totalOnlineRates,methodDescribe);
    }
}
