package com.njcn.device.pq.controller;

import cn.hutool.core.date.TimeInterval;
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.exception.BusinessException;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.biz.pojo.dto.*;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.device.biz.pojo.param.SubstationParam;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import com.njcn.device.line.mapper.LineMapper;
import com.njcn.device.line.service.LineService;
import com.njcn.device.line.mapper.LineDetailMapper;
import com.njcn.device.biz.pojo.dto.MonitorCommLedgerInfoDTO;
import com.njcn.device.pq.service.CommTerminalService;
import com.njcn.device.pq.service.IPqsDeviceUnitService;
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;
import java.util.Map;
import java.util.Objects;

/**
 * pqs
 *
 * @author cdf
 * @date 2023/4/24
 */
@RestController
@RequestMapping("commTerminal")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "通用台账查询")
public class CommTerminalController extends BaseController {

    private final CommTerminalService commTerminalService;

    private final LineService lineService;

    private final LineMapper lineMapper;

    private final LineDetailMapper lineDetailMapper;

    private final IPqsDeviceUnitService iPqsDeviceUnitService;


    /**
     * 通过部门获取所有子集部门所拥有的监测点基本信息
     *
     * @author cdf
     * @date 2023/4/24
     */
    @PostMapping("deptGetLineIds")
    @ApiOperation("通过部门获取所有子集部门所拥有的监测点")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetChildrenDTO>> deptGetLineList(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        TimeInterval timer = new TimeInterval();
        String methodDescribe = getMethodDescribe("deptGetLineList");
        List<DeptGetChildrenDTO> result = commTerminalService.deptGetLineList(deptGetLineParam);
        log.info("运行时长" + timer.intervalRestart());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据单位获取所有子单位信息
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("getDeptChildrenByParent")
    @ApiOperation("根据单位获取所有子单位信息")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetBase>> getDeptChildrenByParent(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        TimeInterval timer = new TimeInterval();
        String methodDescribe = getMethodDescribe("getDeptChildrenByParent");
        List<DeptGetBase> result = commTerminalService.getDeptChildrenByParent(deptGetLineParam);
        log.info("运行时长" + timer.intervalSecond());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据单位获取监测点详细信息
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("deptGetLine")
    @ApiOperation("根据单位获取监测点信息")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetChildrenMoreDTO>> deptGetLine(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        TimeInterval timer = new TimeInterval();
        String methodDescribe = getMethodDescribe("deptGetLine");
        List<DeptGetChildrenMoreDTO> result = commTerminalService.deptGetLine(deptGetLineParam);
        log.info("运行时长" + timer.intervalSecond());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 根据部门id获取部门监测点台账信息
     * @param deptGetLineParam
     * @return
     */
    @PostMapping("deptGetLineInfo")
    @ApiOperation("根据部门id获取部门监测点台账信息")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<MonitorCommLedgerInfoDTO>> deptGetLineInfo(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        String methodDescribe = getMethodDescribe("deptGetLineInfo");
        List<MonitorCommLedgerInfoDTO> result = commTerminalService.deptGetLineInfo(deptGetLineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @PostMapping("deptGetAllLine")
    @ApiOperation("根据单位获取监测点信息(all line)")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetChildrenMoreDTO>> deptGetAllLine(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        TimeInterval timer = new TimeInterval();
        String methodDescribe = getMethodDescribe("deptGetLine");
        List<DeptGetChildrenMoreDTO> result = commTerminalService.deptGetAllLine(deptGetLineParam);
        log.info("运行时长" + timer.intervalSecond());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据单位获取所有变电站
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("deptGetSubStation")
    @ApiOperation("根据单位获取所有变电站")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetSubStationDTO>> deptSubStation(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        TimeInterval timer = new TimeInterval();
        String methodDescribe = getMethodDescribe("deptSubStation");
        List<DeptGetSubStationDTO> result = commTerminalService.deptSubStation(deptGetLineParam);
        log.info("运行时长" + timer.intervalSecond());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据单位获取所有变电站详细信息
     * @param deptGetLineParam
     * @return
     */
    @PostMapping("/deptGetSubStationInfo")
    @ApiOperation("根据单位获取所有变电站详细信息")
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetSubStationDTO.Info>> deptGetSubStationInfo(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        String methodDescribe = getMethodDescribe("deptGetSubStationInfo");
        List<DeptGetSubStationDTO.Info> result = commTerminalService.deptSubStationInfo(deptGetLineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 根据单位获取所有变电站
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("deptGetBusBar")
    @ApiOperation("根据单位获取所有母线")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetBusBarDTO>> deptBusBar(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        String methodDescribe = getMethodDescribe("deptBusBar");
        List<DeptGetBusBarDTO> result = commTerminalService.deptBusBar(deptGetLineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }



    /**
     * 根据单位获取所有装置
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("deptGetDevice")
    @ApiOperation("根据单位获取所有装置")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetDeviceDTO>> deptGetDevice(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        String methodDescribe = getMethodDescribe("deptGetDevice");
        List<DeptGetDeviceDTO> result = commTerminalService.deptGetDevice(deptGetLineParam,3);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据单位获取单位下的装置以及装置下的监测点
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("deptGetDeviceAndMonitor")
    @ApiOperation("根据单位获取装置以及监测点")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "deptGetLineParam", value = "请求体", required = true)
    public HttpResult<List<DeptGetDeviceDTO>> deptGetDeviceAndMonitor(@RequestBody @Validated DeptGetLineParam deptGetLineParam) {
        String methodDescribe = getMethodDescribe("deptGetDeviceAndMonitor");
        List<DeptGetDeviceDTO> result = commTerminalService.deptGetDevice(deptGetLineParam,1);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据单站id获取监测点信息
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("substationGetLine")
    @ApiOperation("根据电站获取所有监测点")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "substationId", value = "请求体", required = true)
    public HttpResult<LineDevGetBandDTO> substationGetLine(@RequestParam("substationId")String substationId) {
        String methodDescribe = getMethodDescribe("substationGetLine");
        LineDevGetBandDTO result = commTerminalService.substationGetLine(substationId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 根据母线id获取监测点信息
     * @author cdf
     * @date 2023/5/10
     */
    @PostMapping("busBarGetLine")
    @ApiOperation("根据母线id获取监测点信息")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiImplicitParam(name = "busBarId", value = "请求体", required = true)
    public HttpResult<LineDevGetBandDTO> busBarGetLine(@RequestParam("busBarId")String busBarId) {
        String methodDescribe = getMethodDescribe("busBarGetLine");
        LineDevGetBandDTO result = commTerminalService.substationGetLine(busBarId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 用于返回pq 还是pms系统
     * @author cdf
     * @date 2023/8/3
     */
    @GetMapping("isPqOrPms")
    @ApiOperation("用于返回pq 还是pms系统")
    @ApiIgnore
    public HttpResult<String> isPqOrPms() {
        String methodDescribe = getMethodDescribe("isPqOrPms");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "pq", methodDescribe);
    }



    @PostMapping("/getLineBySubstationRelation")
    @ApiOperation("获取变电站和监测点的关系")
    @ApiImplicitParam(name = "devDataType", value = "数据类型（0：暂态系统；1：稳态系统；）", required = true)
    public HttpResult<Map<String,List<String>>> getLineBySubstationRelation(@RequestParam("devDataType") Integer devDataType) {
        String methodDescribe = getMethodDescribe("getLineBySubstationRelation");
        Map<String,List<String>> map= lineService.getLineBySubstationRelation(devDataType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, map, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getRunMonitorIds")
    @ApiOperation("获取投运谐波系统所有监测点")
    public HttpResult<List<String>> getRunMonitorIds() {
        String methodDescribe = getMethodDescribe("getRunMonitorIds");
        List<String> result = lineMapper.getLineList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }



    /**
     * 根据监测点集合获取监测点详情
     * @author cdf
     * @date 2023/9/21
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorDetailList")
    @ApiOperation("根据监测点集合获取监测点详情")
    @ApiImplicitParam(name = "list", value = "监测点集合")
    public HttpResult<List<LineDevGetDTO>> getMonitorDetailList(@RequestBody List<String> list) {
        String methodDescribe = getMethodDescribe("getMonitorDetailList");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, lineMapper.getMonitorListDetail(list), methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/lineUnitDetail")
    @ApiOperation("根据监测点id获取数据单位")
    @ApiImplicitParam(name = "lineId", value = "实体", required = true)
    public HttpResult<PqsDeviceUnit> lineUnitDetail(@RequestParam("lineId") String lineId) {
        String methodDescribe = getMethodDescribe("lineUnitDetail");
        PqsDeviceUnit pqsDeviceUnit = iPqsDeviceUnitService.lineUnitDetail(lineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqsDeviceUnit, methodDescribe);
    }


    /**
     * 根据监测点id获取监测点详情
     * @author cdf
     * @date 2023/9/21
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getMonitorDetail")
    @ApiOperation("根据监测点id获取监测点详情")
    @ApiImplicitParam(name = "lineId", value = "监测点id", required = true)
    public HttpResult<LineDevGetDTO> getMonitorDetail(@RequestParam("lineId") String lineId) {
        String methodDescribe = getMethodDescribe("getMonitorDetail");
        LineDevGetDTO monitor = lineDetailMapper.getMonitorDetail(lineId);
        if(Objects.isNull(monitor)){
            throw new BusinessException("未查询到监测点信息");
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getStatisticsMonitor")
    @ApiOperation("专项分析台账统计")
    @ApiImplicitParam(name = "param", value = "实体", required = true)
    public HttpResult<List<StatisticsMonitor>> getStatisticsMonitor(@RequestBody StatisticsBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getStatisticsMonitor");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, new ArrayList<>(), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/tagOrIdGetSub")
    @ApiOperation("获取变电站信息")
    public HttpResult<List<SubGetBase>> tagOrIdGetSub(@RequestBody SubstationParam substationParam) {
        String methodDescribe = getMethodDescribe("tagOrIdGetSub");
        List<SubGetBase> subGetBases = commTerminalService.tagOrIdGetSub(substationParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, subGetBases, methodDescribe);
    }
    /**
     * 获取监测点的台账详细信息
     * @author cdf
     * @date 2023/10/23
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getCustomDetailByLineId")
    @ApiOperation("获取监测点的台账详细信息(自定义报表用)")
    @ApiImplicitParam(name = "id", value = "监测点ID", required = true)
    public HttpResult<Map<String,String>> getCustomDetailByLineId(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getCustomDetailByLineId");
        Map<String,String> result = commTerminalService.getCustomDetailByLineId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
