package com.njcn.device.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.pojo.bo.BaseLineInfo;
import com.njcn.device.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pojo.param.DeviceInfoParam;
import com.njcn.device.service.TerminalBaseService;
import com.njcn.device.service.impl.GeneralDeviceService;
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.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;
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;

    /**
     * 获取按部门分类的实际运行终端综合信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPracticalRunDeviceInfoAsDept")
    @ApiOperation("获取按部门分类的实际运行终端综合信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceInfoParam", value = "查询终端条件", required = true)
    })
    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)
    })
    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)
    })
    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.NO_DATA, null, 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)
    })
    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);
    }



}
