#include "DaoDevice.h"

#include "database/DaoEntity.h"

void DaoDevice::insert_device(DataFields& fields)
{
    fields.remove("id");
    fields.check("open_time", "", "NULL");
    fields.check("product_time", "", "NULL");
    DaoEntity dao(DMDevice::TABLENAME);
    dao.insert_fields(fields);
}



void DaoDevice::query_device_page(PageInfo& pageinfo, vector<DataFields>& result)
{
    DaoEntity dao(DMDevice::TABLENAME);
    dao.query_fields("*", "", pageinfo, result);
}

void DaoDevice::query_device(string sql_c, vector<DataFields>& result)
{
    DaoEntity dao(DMDevice::TABLENAME);
    dao.query_fields("*", sql_c, result);
}

void DaoDevice::query_device_attr(string sql_c, vector<DataFields>& result)
{
    DaoEntity dao(DMDeviceAttr::TABLENAME);
    string c = " left join device on device_attr.device_id=device.id " + sql_c + ";";
    dao.query_fields("*", c, result);
}

int DaoDevice::update_device(DataFields& fields)
{
    string id = fields.get_str(DMDevice::ID);
    string sql_c = "WHERE id='" + id + "'";
    fields.remove(DMDevice::ID);
    fields.check(DMDevice::PRODUCT_DATE, "", "NULL");
    fields.check(DMDevice::OPEN_TIME, "", "NULL");
    DaoEntity dao(DMDevice::TABLENAME);
    return dao.update_fields(fields, sql_c);
}

void  DaoDevice::update_device_attr(vector<DataFields> vec_d)
{
    DaoEntity dao(DMDeviceAttr::TABLENAME);
    vector<string> v_keys = {DMDeviceAttr::ATTR_VAL};
    for (auto& fields : vec_d)
    {
        dao.duplicate_update(fields, v_keys);
    }
}

void DaoDevice::query_attr(int device_id, vector<DataFields>& result)
{
    DaoEntity dao(DMDeviceAttr::TABLENAME);
    string sql = " where device_id='" + to_string(device_id) + "'";
    dao.query_fields("*", sql, result);
}