#include "DaoCharge.h"

#include "app/Global.h"
#include "common/spdlogger.h"
#include "common/TimeUtils.h"
#include "common/Snowflake.h"
#include "database/DaoEntity.h"
#include "database/dao/DaoDevice.h"
#include "Device.h"

bool DaoCharge::create_charge_record(std::string biz_id, int device_id)
{
    DataFields fields;
    fields.set(DMChargeRecord::ID, biz_id);
    fields.set(DMChargeRecord::DEVICE_ID, device_id);
    DaoEntity dao(DMChargeRecord::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoCharge::update_charge_record(std::string biz_id, int device_id, DataFields fields)
{
    DaoEntity dao(DMChargeRecord::TABLENAME);
    std::vector<DataFields> result;
    dao.exec("select * from " + DMChargeRecord::TABLENAME + " WHERE id='" + biz_id + "';", result);
    if (result.size() == 0)
    {
        fields.set(DMChargeRecord::ID, biz_id);
        fields.set(DMChargeRecord::DEVICE_ID, device_id);
        dao.insert_fields(fields);
    }
    fields.remove(DMChargeRecord::ID);
    fields.remove(DMChargeRecord::DEVICE_ID);
    return dao.update_fields(fields, "where " + DMChargeRecord::ID + "='" + biz_id + "'");
}

void DaoCharge::query_charger(vector<DataFields>& result)
{
    DaoEntity dao(DMDevice::TABLENAME);
    string sql = " where device.type='智能充电设备';";
    dao.query_fields("*", sql, result);
}

void DaoCharge::query_charger_attr(vector<DataFields>& result)
{
    DaoEntity dao(DMDevice::TABLENAME);
    string sql = " left join  device_attr on device.id =device_attr.device_id where device.type='智能充电设备';";
    dao.query_fields("*", sql, result);
}
