#include "cworkerdriveruser.h"

CWorkerDriverUser::CWorkerDriverUser(const QString &name, int32_t id)
    :CWorkerDriver(name, id)
{
	m_ModelWorker = nullptr;
}

int32_t CWorkerDriverUser::init_driver()
{
    int32_t ret = CWorkerDriver::init_driver();

    m_ModelWorker = new QSqlTableModel(nullptr, db);
    m_ModelWorker->setTable("worker");
    m_ModelWorker->setEditStrategy(QSqlTableModel::OnManualSubmit);


    /*m_pmodelLog = new QSqlTableModel(nullptr, db);
    m_pmodelLog->setTable("Log");
    m_pmodelLog->setEditStrategy(QSqlTableModel::OnManualSubmit);*/

//    ExistUser("Factory");

    return ret;
}

int32_t CWorkerDriverUser::exit_driver()
{
    if(nullptr != m_ModelWorker){
        delete m_ModelWorker;
        m_ModelWorker = nullptr;
    }

    return CWorkerDriver::exit_driver();
}

bool CWorkerDriverUser::ExistUser(const QString &userName)
{
    QSqlQuery query(GetDatabase());
    query.prepare("select * from worker where Name=:name");
    query.bindValue(":name", userName);
    query.exec();

    if (query.next()) {
        return true;
    }

    return false;
}

bool CWorkerDriverUser::AddUser(const QString &userName, int levelId, const QString& team, const QString& password, QString& error)
{
	QSqlQuery query(GetDatabase());
	if (!query.exec(QString("insert into worker (Name, Password, Level, Team) \
				  select '%1', '%2', %3, '%4' where not exists\
				  (select 1 from worker where Name='%1')").arg(userName).arg(password).arg(levelId).arg(team)))
	{
		error = query.lastError().text();

		LOG_ERR("error:{0} sql:{1}", query.lastQuery().toStdString(), error.toStdString());

		return false;
	}

	return true;
}

bool CWorkerDriverUser::DeleteUser(const QString &userName, QString& error)
{
	QSqlQuery query(GetDatabase());
	query.prepare("delete from worker where Name=:name");
	query.bindValue(":name", userName);

	if (!query.exec())
	{
		error = query.lastError().text();

		LOG_ERR("error:{0} sql:{1}", query.lastQuery().toStdString(), error.toStdString());
		return false;
	}

	return query.numRowsAffected() > 0;
}

bool CWorkerDriverUser::ModifyUser(int userId, const QString &userName, int levelId, const QString &team, QString& error)
{
	QSqlQuery query(GetDatabase());
	query.prepare("update worker set Name=:name,Level=:level,Team=:team where SerialNum=:id");
	query.bindValue(":name", userName);
	query.bindValue(":level", levelId);
	query.bindValue(":team", team);
	query.bindValue(":id", userId);

	if (!query.exec())
	{
		error = query.lastError().text();

		LOG_ERR("error:{0} sql:{1}", query.lastQuery().toStdString(), error.toStdString());
		return false;
	}

	return true;
}

bool CWorkerDriverUser::ModifyUser(int userId, const QString &userName, int levelId, const QString &team, const QString& password, QString& error)
{
	QSqlQuery query(GetDatabase());
	query.prepare("update worker set Name=:name,Password=:pwd,Level=:level,Team=:team where SerialNum=:id");
	query.bindValue(":name", userName);
	query.bindValue(":pwd", password);
	query.bindValue(":level", levelId);
	query.bindValue(":team", team);
	query.bindValue(":id", userId);

	if (!query.exec())
	{
		error = query.lastError().text();

		LOG_ERR("error:{0} sql:{1}", query.lastQuery().toStdString(), error.toStdString());

		return false;
	}

	return true;
}

bool CWorkerDriverUser::Commit()
{
    return m_ModelWorker->submitAll();
}
