package com.njcn.energy.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
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.common.utils.PubUtils;
import com.njcn.energy.pojo.param.*;
import com.njcn.energy.pojo.po.*;
import com.njcn.energy.pojo.vo.*;
import com.njcn.energy.service.ILogicDeviceLineService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * pqs
 * 逻辑设备监测点控制器
 *
 * @author cdf
 * @date 2022/2/24
 */
@Slf4j
@RestController
@RequestMapping("/logic")
@RequiredArgsConstructor
@Api(tags = "逻辑设备控制器")
public class LogicDeviceLineController extends BaseController {

    private final ILogicDeviceLineService iLogicDeviceLineService;

    /**
     * 获取网关设备树
     *
     * @author cdf
     * @date 2022/3/1
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getEnergyTree")
    @ApiOperation("获取网关设备树")
    public HttpResult<List<EnergyDeviceTreeVO>> getEnergyTree() {
        String methodDescribe = getMethodDescribe("getEnergyTree");
        List<EnergyDeviceTreeVO> list = iLogicDeviceLineService.getEnergyTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 获取设备详情
     *
     * @author cdf
     * @date 2022/3/2
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getEnergyDevInfoById")
    @ApiOperation("获取设备详情")
    @ApiImplicitParam(name = "id", value = "设备的id", required = true)
    public HttpResult<LogicDeviceLineInfoVO> getEnergyDevInfoById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getEnergyDevInfoById");
        LogicDeviceLineInfoVO logicDeviceLineInfoVO = iLogicDeviceLineService.getEnergyDevInfoById(id);
        if (Objects.nonNull(logicDeviceLineInfoVO)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, logicDeviceLineInfoVO, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 通过监测点id获取展示模板
     *
     * @author cdf
     * @date 2022/3/2
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getEnergyTemplateById")
    @ApiOperation("获取设备实时数据模板详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备的id", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面尺寸", required = true)
    })
    public HttpResult<IPage<EnergyTemplateVO>> getEnergyTemplateById(@RequestParam("id") String id, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        String methodDescribe = getMethodDescribe("getEnergyTemplateById");
        IPage<EnergyTemplateVO> page = iLogicDeviceLineService.getEnergyTemplateById(id, pageNum, pageSize);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }


    /**
     * 通过监测点id获取展示分组模板
     *
     * @author cdf
     * @date 2022/3/2
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getRealGroupTemplateById")
    @ApiOperation("获取设备实时数据模板详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备的id", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面尺寸", required = true)
    })
    public HttpResult<IPage<DataGroupTemplateVO>> getRealGroupTemplateById(@RequestParam("id") String id, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        String methodDescribe = getMethodDescribe("getRealGroupTemplateById");
        IPage<DataGroupTemplateVO> page = iLogicDeviceLineService.getRealGroupTemplateById(id, pageNum, pageSize);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    /**
     * 通过监测点id获取展示模板
     *
     * @author cdf
     * @date 2022/3/2
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getHistoryTemplateById")
    @ApiOperation("获取历史设备数据模板详情")
    @ApiImplicitParam(name = "energyBaseParam",required = true)
    public HttpResult<IPage<DataGroupTemplateVO>> getHistoryTemplateById(@RequestBody EnergyBaseParam energyBaseParam) {
        String methodDescribe = getMethodDescribe("getHistoryTemplateById");
        IPage<DataGroupTemplateVO> page = iLogicDeviceLineService.getHistoryTemplateById(energyBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    /**
     * 通过监测点id获取展示模板统计数据
     *
     * @author cdf
     * @date 2022/3/2
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getStatisTemplateById")
    @ApiOperation("获取监测点统计数据模板和数据详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "tab选项id", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面尺寸", required = true),
            @ApiImplicitParam(name = "lineId", value = "监测点id", required = true)
    })
    public HttpResult<IPage<DataGroupTemplateVO>> getStatisTemplateById(@RequestParam("id") String id, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestParam(value = "lineId")String lineId) {

        String methodDescribe = getMethodDescribe("getStatisTemplateById");
        IPage<DataGroupTemplateVO> page = iLogicDeviceLineService.getStatisTemplateById(id, pageNum, pageSize,lineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }


    /**
     * 通过监测点id获取展示模板
     *
     * @author cdf
     * @date 2022/3/2
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getTabTemplateById")
    @ApiOperation("获取设备数据模板详情")
    @ApiImplicitParam(name = "id", value = "设备的id", required = true)
    public HttpResult<List<DataSet>> getTabTemplateById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getTabTemplateById");
        List<DataSet> list = iLogicDeviceLineService.getTabTemplateById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 获取当前数据集的分组情况
     *
     * @author cdf
     * @date 2022/3/16
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getGroupDetailList")
    @ApiOperation("获取当前数据集的分组情况")
    @ApiImplicitParam(name = "id", value = "数据集id", required = true)
    public HttpResult<List<DataGroupTemplateVO>> getGroupDetailList(@RequestParam("id") String setId) {
        String methodDescribe = getMethodDescribe("getGroupDetailList");
        List<DataGroupTemplateVO> list = iLogicDeviceLineService.getGroupDetailList(setId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 重新分组
     * @author cdf
     * @date 2022/3/16
     */
    @PostMapping("/addGroupArr")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("新增重新分组")
    @ApiImplicitParam(name = "groupArrParam", value = "重新分组信息", required = true)
    public HttpResult<LogicDev> addGroupArr(@RequestBody GroupArrParam groupArrParam) {
        String methodDescribe = getMethodDescribe("addGroupArr");
        LogUtil.njcnDebug(log, "{}，分组信息为：{}", methodDescribe, groupArrParam);
        boolean res =iLogicDeviceLineService.addGroupArr(groupArrParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 获取解析模板
     * @author cdf
     * @date 2022/3/18
     */
    @GetMapping("/getParseTemplate")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("获取解析模板")
    @ApiImplicitParam(name = "setId", value = "数据集id", required = true)
    public HttpResult<List<String>> getParseTemplate(@RequestParam("setId") String setId){
        String methodDescribe = getMethodDescribe("getParseTemplate");
        List<String> list = iLogicDeviceLineService.getParseTemplate(setId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @PostMapping("/add")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.ADD)
    @ApiOperation("新增逻辑设备")
    @ApiImplicitParam(name = "devCfgParam", value = "逻辑设备信息", required = true)
    public HttpResult<LogicDev> add(@RequestBody DevCfgParam devCfgParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，逻辑设备信息为：{}", methodDescribe, devCfgParam);
        LogicDev logicDev = iLogicDeviceLineService.add(devCfgParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @GetMapping("/getLogicDevByDid")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询逻辑设备")
    @ApiImplicitParam(name = "did", value = "设备的did", required = true)
    public HttpResult<LogicDev> getLogicDevByDid(@RequestParam String did){
        String methodDescribe = getMethodDescribe("getLogicDevByDid");
        LogUtil.njcnDebug(log, "{}，设备的did：{}", methodDescribe, did);
        LogicDev logicDev = iLogicDeviceLineService.getLogicDev(did);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, logicDev, methodDescribe);
    }

    @GetMapping("/getLogicDevByList")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("根据网关查询逻辑设备")
    @ApiImplicitParam(name = "list", value = "网关集合", required = true)
    public HttpResult<List<LogicDev>> getLogicDevByPid(@RequestParam List<String> list){
        String methodDescribe = getMethodDescribe("getLogicDevByList");
        LogUtil.njcnDebug(log, "{}，网关集合：{}", methodDescribe, list);
        List<LogicDev> result = iLogicDeviceLineService.getLogicDevByPid(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @DeleteMapping("/deleteByVersionId")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.DELETE)
    @ApiOperation("根据版本id删除逻辑设备")
    @ApiImplicitParam(name = "id", value = "逻辑设备版本id", required = true)
    public HttpResult<Boolean> deleteByVersionId(@RequestParam String id){
        String methodDescribe = getMethodDescribe("deleteByVersionId");
        LogUtil.njcnDebug(log, "{}，设备的did：{}", methodDescribe, id);
        boolean result = iLogicDeviceLineService.deleteByVersionId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @GetMapping("/getLogicDevByVersionId")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("根据版本id查询逻辑设备")
    @ApiImplicitParam(name = "id", value = "逻辑设备版本id", required = true)
    public HttpResult<List<LogicDev>> getLogicDevByVersionId(@RequestParam String id){
        String methodDescribe = getMethodDescribe("getLogicDevByVersionId");
        LogUtil.njcnDebug(log, "{}，设备的did：{}", methodDescribe, id);
        List<LogicDev> list = iLogicDeviceLineService.getLogicDevByVersionId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @PostMapping("/getHisData")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("获取历史数据")
    @ApiImplicitParam(name = "dataHisParam", value = "参数", required = true)
    public HttpResult<List<DataHisVO>> getHisData(@RequestBody DataHisParam dataHisParam){
        String methodDescribe = getMethodDescribe("getHisData");
        List<DataHisVO> list = iLogicDeviceLineService.getHisData(dataHisParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

}
