package com.rehome.mqttclienttemperature.service.impl;


import com.google.gson.Gson;
import com.rehome.mqttclienttemperature.datasource.DataSource;
import com.rehome.mqttclienttemperature.dto.ResponseDto;
import com.rehome.mqttclienttemperature.entity.Temperature;
import com.rehome.mqttclienttemperature.service.KingbaseTemperatureService;
import com.rehome.mqttclienttemperature.service.OracleTemperatureService;
import com.rehome.mqttclienttemperature.service.PostgresSqlTemperatureService;
import com.rehome.mqttclienttemperature.service.SqlServerTemperatureService;
import com.rehome.mqttclienttemperature.utils.JdbcUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
public class KingbaseTemperatureServiceImpl implements KingbaseTemperatureService {
    public static DataSource ds = new DataSource();
    static {
        //配置数据源
        ds.setId("202");
        ds.setName("kingBase_3_25");
        ds.setUrl("jdbc:kingbase8://192.168.3.25:54321/appserver?useUnicode=true");
        ds.setUsername("system");
        ds.setPassword("452131wW");
        ds.setDriver("com.kingbase8.Driver");
    }

    @Override
    public ResponseDto saveTemperature(Temperature temperature) {
        // 自定义一个线程池,内部包含8个线程
        ExecutorService customPool = Executors.newFixedThreadPool(2);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            //SYSTIMESTAMP
            //CURRENT_TIMESTAMP
            //HIBERNATE_SEQUENCE
            //temperature_seq_appserver
            //pg
            //String sql = "insert into temperature(temperature, humidity, data_date, data_hour, data_minute, topic, location_desc, create_date) values(?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)";
            //sqlserver 和 pg 相同
            String sql = "insert into temperature(temperature, humidity, data_date, data_hour, data_minute, topic, location_desc, create_date) values(?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)";
            List<Object> jdbcParamValues = new ArrayList<>();

            jdbcParamValues.add(temperature.getTemperature());
            jdbcParamValues.add(temperature.getHumidity());
            jdbcParamValues.add(temperature.getDataDate());
            jdbcParamValues.add(temperature.getDataHour());
            jdbcParamValues.add(temperature.getDataMinute());
            jdbcParamValues.add(temperature.getTopic());
            jdbcParamValues.add(temperature.getLocationDesc());
            //jdbcParamValues.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(temperature.getCreateDate()));

            Gson gson = new Gson();
            ResponseDto responseDto = JdbcUtil.executeSql(ds, sql, jdbcParamValues);
            String dbQueryResult = gson.toJson(responseDto);
            log.info(dbQueryResult);
            jdbcParamValues.clear();

            if(responseDto.isSuccess()){
                return "数据库同步成功";
            }
            return "数据库同步失败";
        }, customPool);

        ResponseDto responseDto = future.thenApply(result -> {
            log.info("Result: " + result);
            if("数据库同步成功".equals(result)){
                return ResponseDto.successWithMsg(result);
            }
            return ResponseDto.fail(result);
        }).join();
        return responseDto;
    }
}
