#include "DaoCar.h"

#include "database/DaoEntity.h"
#include "database/dao/DaoDevice.h"


bool DaoCar::query_car_page(PageInfo& pageinfo, vector<DataFields>& result)
{
    // 查询数据库
    DaoEntity dao(DMCar::TABLENAME);
    return dao.query_fields("*", "", pageinfo, result);
}

bool DaoCar::query_car_by_userid(string userid, vector<DataFields>& result)
{
    string sql = "select * from car where user_id='" + userid + "';";
    return DaoEntity::exec_once(sql, result);
}

bool DaoCar::query_car_by_carnum(string carnum, vector<DataFields>& result)
{
    string sql = "select * from car where car_no='" + carnum + "';";
    return DaoEntity::exec_once(sql, result);
}

bool DaoCar::insert_car(DataFields& fields)
{

    fields.check(DMCar::FID_RANGE, "", "0");
    fields.check(DMCar::FID_PRODUCT_DATE, "", "NULL");
    fields.check(DMCar::FID_BUY_DATE, "", "NULL");
    fields.remove(DMCar::FID_CREATE_TIME);

    DaoEntity dao(DMCar::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoCar::update_car(DataFields& fields)
{
    fields.check(DMCar::FID_RANGE, "", "0");
    fields.check(DMCar::FID_PRODUCT_DATE, "", "NULL");
    fields.check(DMCar::FID_BUY_DATE, "", "NULL");
    fields.remove(DMCar::FID_CREATE_TIME);

    DaoEntity dao(DMCar::TABLENAME);
    string id = fields.get_str(DMCar::FID_ID);
    fields.remove(DMDevice::ID);
    return dao.update_fields(fields, "where id='" + id + "'");
}
