package com.usky.demo.controller.web;


import cn.hutool.core.util.StrUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.domain.R;
import com.usky.common.core.exception.BusinessException;
import com.usky.demo.constant.TdsConstants;
import com.usky.demo.domain.*;
import com.usky.demo.service.QueryInfluxdbDataService;
import com.usky.demo.service.QueryTdengineDataService;
import com.usky.demo.service.vo.SuperTableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ya
 * @since 2024-07-29
 */
@RestController
@Api(tags = "数据统一查询")
@RequestMapping("/dataQuery")
public class QueryInfluxdbDataController {
    @Autowired
    private QueryInfluxdbDataService queryInfluxdbDataService;
    @Autowired
    private QueryTdengineDataService queryTdengineDataService;

    @Value("${spring.sourcetype}")
    private String sourcetype;

    /**
     * 获取设备实时数据(对内)
     * @param requestVO
     * @return
     */
    @PostMapping("/last")
    public ApiResult<List<LastInnerResultVO>> last(@RequestBody LastInnerQueryVO requestVO){
        if(sourcetype.equals("taos")){
            return ApiResult.success(queryTdengineDataService.tdengineLast(requestVO));
        }else{
            return ApiResult.success(queryInfluxdbDataService.last(requestVO));
        }
    }

    /**
     * 获取设备历史数据(对内)
     * @param requestVO
     * @return
     */
    @PostMapping("/history")
    public ApiResult<List<HistorysInnerResultVO>> history(@RequestBody HistorysInnerRequestVO requestVO){
        if(sourcetype.equals("taos")) {
            return ApiResult.success(queryTdengineDataService.tdengineHistory(requestVO));
        }else {
            return ApiResult.success(queryInfluxdbDataService.history(requestVO));
        }
    }

    /**
     * 查询influxdb所有表实时数据
     * @return
     */
    @PostMapping("getAllDeviceRealTime")
    public ApiResult<List<Map<String, Object>>> getAllDeviceRealTime(){
        if(sourcetype.equals("taos")){
            return ApiResult.success(queryTdengineDataService.getAllDeviceRealTime());
        }else{
            return ApiResult.success(queryInfluxdbDataService.getAllDeviceRealTime());
        }
    }

    /**
     * 创建超级表和列
     * @param superTableDTO 超级表信息
     * @return
     */
    @PostMapping("/createSuperTableAndColumn")
    public ApiResult<Void> createSuperTableAndColumn(@RequestBody SuperTableDTO superTableDTO){
        queryTdengineDataService.createSuperTableAndColumn(superTableDTO);
        return ApiResult.success();
    }

    /**
     * 超级表新增字段
     *
     * @param superTableDTO 数据信息
     * @return 执行结果
     */
    @PostMapping("/addSuperTableColumn")
    public ApiResult<Void> addSuperTableColumn(@RequestBody SuperTableDTO superTableDTO) {
        queryTdengineDataService.addSuperTableColumn(superTableDTO.getSuperTableName(), superTableDTO.getFields());
        return ApiResult.success();
    }

    /**
     * 超级表删除字段
     *
     * @param superTableDTO 数据信息
     * @return 执行结果
     */
    @PostMapping("/dropSuperTableColumn")
    public ApiResult<Void> dropSuperTableColumn(@RequestBody SuperTableDTO superTableDTO) {
        queryTdengineDataService.dropSuperTableColumn(superTableDTO.getSuperTableName(), superTableDTO.getFields());
        return ApiResult.success();
    }

    /**
     * 超级表新增标签
     *
     * @param superTableDTO 数据信息
     * @return 执行结果
     */
    @PostMapping("/addSuperTableTag")
    public ApiResult<Void> addSuperTableTag(@RequestBody SuperTableDTO superTableDTO) {
        queryTdengineDataService.addSuperTableTag(superTableDTO.getSuperTableName(), superTableDTO.getFields());
        return ApiResult.success();
    }

    /**
     * 超级表删除标签
     *
     * @param superTableDTO 数据信息
     * @return 执行结果
     */
    @PostMapping("/dropSuperTableTag")
    public ApiResult<Void> dropSuperTableTag(@RequestBody SuperTableDTO superTableDTO) {
        queryTdengineDataService.dropSuperTableTag(superTableDTO.getSuperTableName(), superTableDTO.getFields());
        return ApiResult.success();
    }

    /**
     * 查询超级表、子表结构
     *
     * @param tableName
     * @return
     */
    @GetMapping("/describeSuperOrSubTable")
    public ApiResult<List<SuperTableDescribeVO>> describeSuperOrSubTable(@RequestParam(value = "tableName") String tableName) {
        List<SuperTableDescribeVO> superTableDescribeVOS = queryTdengineDataService.describeSuperOrSubTable( tableName);
        return ApiResult.success(superTableDescribeVOS);
    }

    /**
     * 能耗分项汇总（按超级表、设备维度 LAST-FIRST 后求和）
     */
    @ApiOperation("能耗分项汇总查询")
    @PostMapping("/energy-item/sum")
    public ApiResult<EnergyItemSumResultVO> sumEnergyItem(@RequestBody EnergyItemSumQueryVO requestVO) {
        if (!"taos".equals(sourcetype)) {
            throw new BusinessException("当前数据源不支持能耗分项汇总查询");
        }
        return ApiResult.success(queryTdengineDataService.sumEnergyItemDiff(requestVO));
    }

    /**
     * 能耗分项汇总（GET，便于调试）
     */
    @ApiOperation("能耗分项汇总查询(GET)")
    @GetMapping("/energy-item/sum")
    public ApiResult<EnergyItemSumResultVO> sumEnergyItemGet(
            @RequestParam String identifier,
            @RequestParam("super_table") String superTable,
            @RequestParam String startTime,
            @RequestParam String endTime) {
        if (!"taos".equals(sourcetype)) {
            throw new BusinessException("当前数据源不支持能耗分项汇总查询");
        }
        EnergyItemSumQueryVO requestVO = new EnergyItemSumQueryVO();
        requestVO.setIdentifier(identifier);
        requestVO.setSuperTable(superTable);
        requestVO.setStartTime(startTime);
        requestVO.setEndTime(endTime);
        return ApiResult.success(queryTdengineDataService.sumEnergyItemDiff(requestVO));
    }

}

