#include "DaoStat.h"
#include "database/DaoEntity.h"
#include "database/dao/DaoCharge.h"
#include "database/dao/DaoDevice.h"
#include "TimeUtils.h"

#include "database/SQL.h"

bool DaoStat::insert_charge_curve(DataFields& fields)
{
    DaoEntity dao(DMChargeDataRecord::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoStat::query_charge_curve_by_time(string start_time, string end_time, std::vector<DataFields>& result)
{
    string sql = "select * from " + DMChargeDataRecord::TABLENAME + " where " + DMChargeDataRecord::COLLECT_TIME + ">='" + start_time
        + "' and " + DMChargeDataRecord::COLLECT_TIME + "<='" + end_time + "';";
    return DaoEntity::exec_once(sql, result);
}

bool DaoStat::query_station_power_by_time(string start_time, string end_time, std::vector<DataFields>& result)
{
    string sql = "select * from " + DMDeviceDataRecord::TABLENAME + " where " + DMDeviceDataRecord::COLLECT_TIME + ">='" + start_time
        + "' and " + DMDeviceDataRecord::COLLECT_TIME + "<='" + end_time + "';";
    return DaoEntity::exec_once(sql, result);
}

#include "Device.h"
void DaoStat::insert_charger_data(int t_interval)
{
    // 存储时间间隔60
    // 记录上一次存储的时间点，用于计算防止重复存储
    static uint64_t t_last = 0;

    // 存储时间间隔： 从0点开始计算，每间隔t_interval存储一次数据
    uint64_t t_start = TimeUtils::datetime2ts(TimeUtils::now_date() + " 00:00:00");
    uint64_t t_now = TimeUtils::now();

    uint64_t a = (t_now - t_start) / t_interval;
    uint64_t b = (t_now - t_start) % t_interval;

    uint64_t t = t_start + a * t_interval;
    if (t != t_last)
    {
        t_last = t;
        std::string t_str = TimeUtils::ts2datetime(t);
        DaoEntity dao(DMChargeDataRecord::TABLENAME);
        for (int i = 1; i <= 6; i++)
        {
            std::string device_code = std::to_string(i);
            auto charger = Device::get_charger(device_code);
            if (charger)
            {
                float v = charger->bms.voltage + charger->bms.current + charger->bms.power;
                if (v != 0.0f)
                {
                    DataFields d;
                    d.set(DMChargeDataRecord::DEVICE_ID, charger->device_id);
                    d.set(DMChargeDataRecord::DEVICE_CODE, device_code);
                    d.set(DMChargeDataRecord::COLLECT_TIME, t_str);
                    d.set(DMChargeDataRecord::VOLTAGE, charger->bms.voltage);
                    d.set(DMChargeDataRecord::CURRENT, charger->bms.current);
                    d.set(DMChargeDataRecord::POWER, charger->bms.power);
                    dao.insert_fields({d});
                }
            }
        }
    }
}

bool DaoStat::query_charge_data(std::string dt, vector<DataFields>& result)
{
    // 存储查询结果集
    vector<DataFields> vec_result;

    // 查询设备数据记录表
    std::string sql_c = DMDeviceDataRecord::COLLECT_TIME + ">='" + dt + " 00:00:00"
        + "' AND " + DMDeviceDataRecord::COLLECT_TIME + "<='" + dt + " 23:59:59" + "'";
    std::string sql = SQL(SQL::TYPE::select).table(DMDeviceDataRecord::TABLENAME).select("*").where(sql_c).str();

    // 执行数据库查询
    return DaoEntity::exec_once(sql, vec_result);
}