package com.usky.demo.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.BeanMapperUtils;
import com.usky.common.core.util.FileUtils;
import com.usky.common.core.util.GlobalUtils;
import com.usky.demo.RemoteTsdbProxyService;
import com.usky.demo.domain.*;
import com.usky.demo.service.QueryInfluxdbDataService;
import com.usky.demo.service.QueryTdengineDataService;
import com.usky.demo.service.SysUserService;
import com.usky.system.RemoteUserService;
import com.usky.system.domain.SysUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@Slf4j
public class DataTsdbProxyControllerApi implements RemoteTsdbProxyService {
    @Value("${spring.sourcetype}")
    private String sourcetype;

    @Autowired
    private QueryInfluxdbDataService queryInfluxdbDataService;
    @Autowired
    private QueryTdengineDataService queryTdengineDataService;

//    /**
//     * 获取设备实时数据(对内)
//     * @param requestVO
//     * @return
//     */
//    @Override
//    public List<LastInnerResultVO> last(LastInnerQueryVO requestVO){
//        return queryInfluxdbDataService.last(requestVO);
//    }

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

//    /**
//     * 单个设备实时数据查询
//     * @param deviceUUId
//     * @return
//     */
//    @Override
//    public LastResultVO queryLastDeviceData(String deviceUUId){
//        return queryInfluxdbDataService.queryLastDeviceData(deviceUUId);
//    }

    /**
     * 批量设备实时数据查询
     * @param requestVO
     * @return
     */
    @Override
    public ApiResult<List<LastInnerResultVO>> queryLastDeviceData(LastInnerQueryVO requestVO){
        if(sourcetype.equals("taos")){
            return ApiResult.success(queryTdengineDataService.tdengineLast(requestVO));
        }else{
            return ApiResult.success(queryInfluxdbDataService.last(requestVO));
        }
    }

//    /**
//     * 单个设备历史数据查询
//     * @param deviceUUId
//     * @param startTime
//     * @param endTime
//     * @return
//     */
//    @Override
//    public HistoryResultVO queryHistoryDeviceData(String deviceUUId,
//                                                             String startTime,
//                                                             String endTime){
//        return queryInfluxdbDataService.queryHistoryDeviceData(deviceUUId,startTime,endTime);
//    }

    /**
     * 批量设备历史数据查询
     * @param requestVO
     * @return
     */
    @Override
    public ApiResult<List<HistorysInnerResultVO>> queryHistoryDeviceData(HistorysInnerRequestVO requestVO){
        if(sourcetype.equals("taos")) {
            return ApiResult.success(queryTdengineDataService.tdengineHistory(requestVO));
        }else {
            return ApiResult.success(queryInfluxdbDataService.history(requestVO));
        }
    }


    /**
     * 创建超级表和列
     * @param superTableDTO 超级表信息
     * @return
     */
    @Override
    public Void createSuperTableAndColumn(@RequestBody SuperTableDTO superTableDTO){
        if(sourcetype.equals("taos")){
            queryTdengineDataService.createSuperTableAndColumn(superTableDTO);
        }

        return null;
    }

    /**
     * 超级表新增字段
     *
     * @param superTableDTO 数据信息
     * @return 执行结果
     */
    @Override
    public Void addSuperTableColumn(@RequestBody SuperTableDTO superTableDTO) {
        if(sourcetype.equals("taos")){
            queryTdengineDataService.addSuperTableColumn(superTableDTO.getSuperTableName(), superTableDTO.getFields());
        }

        return null;
    }

    /**
     * 超级表删除字段
     *
     * @param superTableDTO 数据信息
     * @return 执行结果
     */
    @Override
    public Void dropSuperTableColumn(@RequestBody SuperTableDTO superTableDTO) {
        if(sourcetype.equals("taos")){
            queryTdengineDataService.dropSuperTableColumn(superTableDTO.getSuperTableName(), superTableDTO.getFields());
        }

        return null;
    }

    /**
     * 能耗分项汇总（对外 Feign）
     */
    @Override
    public EnergyItemSumResultVO sumEnergyItem(@RequestBody EnergyItemSumQueryVO requestVO) {
        if (!"taos".equals(sourcetype)) {
            throw new BusinessException("当前数据源不支持能耗分项汇总查询");
        }
        return queryTdengineDataService.sumEnergyItemDiff(requestVO);
    }

    @Override
    public EnergyItemTrendResultVO sumEnergyItemTrend(@RequestBody EnergyItemTrendQueryVO requestVO) {
        if (!"taos".equals(sourcetype)) {
            throw new BusinessException("当前数据源不支持能耗分项趋势查询");
        }
        return queryTdengineDataService.sumEnergyItemTrend(requestVO);
    }

    @Override
    @PostMapping(value = "/importDeviceCsv", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ApiResult<DeviceCsvImportResultVO> importDeviceCsv(@RequestPart("deviceUuid") String deviceUuid,
                                                              @RequestPart("file") MultipartFile file) {
        if (!"taos".equals(sourcetype)) {
            log.error("当前数据源不支持 TDengine CSV 导入, sourcetype={}", sourcetype);
            return ApiResult.error("当前数据源不支持 TDengine CSV 导入");
        }
        return queryTdengineDataService.importDeviceCsv(deviceUuid, file);
    }

}
