package com.njcn.device.controller;

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.common.utils.LogUtil;
import com.njcn.device.mapper.DeviceMapper;
import com.njcn.device.mapper.LineMapper;
import com.njcn.device.pojo.dto.*;
import com.njcn.device.pojo.po.Device;
import com.njcn.device.pojo.po.Overlimit;
import com.njcn.device.pojo.vo.*;
import com.njcn.device.service.LineService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.*;
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.List;

/**
 * @author denghuajun
 * @date 2022/2/23
 * 监测点相关
 */
@Slf4j
@Api(tags = "监测点管理")
@RestController
@RequestMapping("/line")
@RequiredArgsConstructor
public class LineController extends BaseController {

    private final LineService lineService;

    private final DeviceMapper deviceMapper;

    private final LineMapper lineMapper;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineDetailData")
    @ApiOperation("根据监测点id获取监测点详情")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<LineDetailDataVO> getLineDetailData(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getLineDetailData");
        LineDetailDataVO result = lineService.getLineDetailData(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineOverLimitData")
    @ApiOperation("根据监测点id获取越限详情")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<LineOverLimitVO> getLineOverLimitData(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getLineOverLimitData");
        LineOverLimitVO result = lineService.getLineOverLimitData(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSubstationData")
    @ApiOperation("根据变电站id获取变电站详情")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<List<SubstationDetailVO>> getSubstationData(@RequestParam("id") List<String> id) {
        String methodDescribe = getMethodDescribe("getSubstationData");
        List<SubstationDetailVO> result = lineService.getSubstationData(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getComFlagInfoData")
    @ApiOperation("根据监测点Id终端状态信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "监测点id", required = true),
            @ApiImplicitParam(name = "searchBeginTime", value = "起始时间"),
            @ApiImplicitParam(name = "searchEndTime", value = "截止时间")
    })
    public HttpResult<CommunicateVO> getComFlagInfoData(@RequestParam(required = false) @ApiParam("id") String id,
                                                        @RequestParam("searchBeginTime") String searchBeginTime,
                                                        @RequestParam("searchEndTime") String searchEndTime) {
        String methodDescribe = getMethodDescribe("getComFlagInfoData");
        CommunicateVO result = lineService.getComFlagInfoData(id, searchBeginTime, searchEndTime);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineIdByDevId")
    @ApiOperation("根据监测点Id终端状态信息")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<String> getLineIdByDevId(@RequestParam(required = false) @ApiParam("id") String id) {
        String methodDescribe = getMethodDescribe("getComFlagInfoData");
        String result = lineService.getLineIdByDevId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOverLimitData")
    @ApiOperation("根据监测点id获取越限数值")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<Overlimit> getOverLimitData(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getLineOverLimitData");
        Overlimit result = lineService.getOverLimitData(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 获取投运在线的监测点的限值
     *
     * @author cdf
     * @date 2022/3/23
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllLineOverLimit")
    @ApiOperation("获取投运在线的监测点的限值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serverName", value = "服务名称", required = true),
            @ApiImplicitParam(name = "deptId", value = "部门id")
    })
    public HttpResult<List<Overlimit>> getAllLineOverLimit(@RequestParam("serverName") String serverName, @RequestParam(value = "deptId", required = false) String deptId) {
        String methodDescribe = getMethodDescribe("getAllLineOverLimit");
        List<Overlimit> result = lineService.getAllLineOverLimit(serverName, deptId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllLine")
    @ApiOperation("获取所有监测点")
    @ApiImplicitParam(name = "id", value = "监测点集合", required = true)
    public HttpResult<List<LineDeviceStateVO>> getAllLine(@RequestBody List<String> id) {
        String methodDescribe = getMethodDescribe("getAllLine");
        List<LineDeviceStateVO> result = lineMapper.getLineDeviceStateVO(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDeviceState")
    @ApiOperation("获取变电站详情(pq_line)")
    @ApiImplicitParam(name = "id", value = "变电站id", required = true)
    public HttpResult<Integer> getDeviceState(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getDeviceState");
        String result = lineService.getLineIdByDevId(id);
        Device device = deviceMapper.selectById(result);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, device.getComFlag(), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSubstationInfo")
    @ApiOperation("获取变电站信息")
    @ApiImplicitParam(name = "id", value = "变电站id", required = true)
    @ApiIgnore
    public HttpResult<PollutionSubstationDTO> getSubstationInfo(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getSubstationInfo");
        LogUtil.njcnDebug(log, "{}，变电站id：{}", methodDescribe, id);
        PollutionSubstationDTO pollutionSubstation = lineService.getSubstationInfo(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pollutionSubstation, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineInfo")
    @ApiOperation("获取监测点信息")
    @ApiImplicitParam(name = "pollutionParamDTO", value = "对象", required = true)
    @ApiIgnore
    public HttpResult<List<PollutionLineDTO>> getLineInfo(@RequestBody PollutionParamDTO pollutionParamDTO) {
        String methodDescribe = getMethodDescribe("getLineInfo");
        LogUtil.njcnDebug(log, "{}，对象：{}", methodDescribe, pollutionParamDTO);
        List<PollutionLineDTO> result = lineService.getLineInfo(pollutionParamDTO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineSubGdDetail")
    @ApiOperation("根据监测点id集合获取详细信息")
    @ApiImplicitParam(name = "id", value = "监测点集合", required = true)
    @ApiIgnore
    public HttpResult<LineDetailVO> getLineSubGdDetail(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getLineSubGdDetail");
        LineDetailVO result = lineMapper.getLineSubGdDetail(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getWarningSub")
    @ApiOperation("获取告警变电站信息")
    @ApiImplicitParam(name = "list", value = "变电站集合", required = true)
    @ApiIgnore
    public HttpResult<List<WarningSubstationDTO>> getWarningSub(@RequestParam("list") List<String> list) {
        String methodDescribe = getMethodDescribe("getWarningSub");
        LogUtil.njcnDebug(log, "{}，变电站集合：{}", methodDescribe, list);
        List<WarningSubstationDTO> result = lineMapper.getWarningSub(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOverLimitLineInfo")
    @ApiOperation("监测点稳态超标信息")
    @ApiImplicitParam(name = "pollutionParamDTO", value = "实体", required = true)
    public HttpResult<List<OverLimitLineDTO>> getOverLimitLineInfo(@RequestBody PollutionParamDTO pollutionParamDTO) {
        String methodDescribe = getMethodDescribe("getOverLimitLineInfo");
        LogUtil.njcnDebug(log, "{}，变电站集合：{}", methodDescribe, pollutionParamDTO);
        List<OverLimitLineDTO> result = lineMapper.getOverLimitLineInfo(pollutionParamDTO.getLineList());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineList")
    @ApiOperation("获取生成limitRate的监测点")
    @ApiIgnore
    public HttpResult<List<String>> getLineList() {
        String methodDescribe = getMethodDescribe("getLineList");
        List<String> result = lineMapper.getLineList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOverLimitByList")
    @ApiOperation("获取监测点限值")
    @ApiImplicitParam(name = "pollutionParamDTO", value = "实体", required = true)
    @ApiIgnore
    public HttpResult<List<Overlimit>> getOverLimitByList(@RequestBody PollutionParamDTO pollutionParamDTO) {
        String methodDescribe = getMethodDescribe("getOverLimitByList");
        LogUtil.njcnDebug(log, "{}，监测点集合：{}", methodDescribe, pollutionParamDTO);
        List<Overlimit> result = lineService.getOverLimitByList(pollutionParamDTO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
