123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- #include "UserService.h"
- #include "SqlDBHelper.h"
- #include <QDebug>
- UserService::UserService(QObject *parent) { }
- //////////////////////用户信息表-start////////////////////
- int UserService::AddUserInfo(const QFUser &userInfo)
- {
- int returnId = -1;
- try {
- // returnId = DBServiceSet::getNextId("t_user_info");
- Transaction t(SqlDBHelper::getDatabase());
- InsertQuery query = t.insertInto("t_user_info (user_name, user_no, password, role, post, "
- "major,work_position,education_degree,phone,project_id,write_time,remark)");
- NonQueryResult result =
- query.values(userInfo.userName, userInfo.userNo, userInfo.password, userInfo.role, userInfo.post,
- userInfo.major, userInfo.workPosition, userInfo.educationDegree, userInfo.phone,
- userInfo.projectId, userInfo.writeTime, userInfo.remark)
- .exec();
- t.commit();
- returnId = result.lastInsertId().toInt();
- } catch (const DBException &ex) {
- qDebug() << "AddUserInfo" << ex.lastError.text();
- returnId = -1;
- }
- return returnId;
- }
- /*暂时用id修改*/
- bool UserService::UpdateUserInfo(const QFUser &userInfo)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.update("t_user_info")
- .set("user_name", userInfo.userName)
- .set("user_no", userInfo.userNo)
- .set("password", userInfo.password)
- .set("role", userInfo.role)
- .set("post", userInfo.post)
- .set("major", userInfo.major)
- .set("work_position", userInfo.workPosition)
- .set("education_degree", userInfo.educationDegree)
- .set("phone", userInfo.phone)
- .set("remark", userInfo.remark)
- .set("project_id", userInfo.projectId)
- .where("id=?", userInfo.id);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool UserService::UpdateUserPassword(QString userNo, QString password)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.update("t_user_info").set("password", password).where("user_no=?", userNo);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool UserService::UpdateUserById(QString userNo, QString password, int id)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.update("t_user_info").set("password", password).set("user_no", userNo).where("id=?", id);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool UserService::QueryUserInfoById(QFUser *userInfo, int userId)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time from "
- "t_user_info WHERE id = %1")
- .arg(userId);
- if (query.exec(selectSql)) {
- if (query.next()) {
- if (query.isNull(0) == false) {
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserInfoByUnserInfo(const QFUser ¶mInfo, QFUser &queryInfo)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,project_id,write_time from "
- "t_user_info WHERE 1 = 1 ");
- if (!paramInfo.userName.isNull() && !paramInfo.userName.isEmpty()) {
- selectSql += " and user_name = '" + paramInfo.userName + "'";
- }
- if (!paramInfo.userNo.isNull() && !paramInfo.userNo.isEmpty()) {
- selectSql += " and user_no = '" + paramInfo.userNo + "'";
- }
- if (!paramInfo.post.isNull() && !paramInfo.post.isEmpty()) {
- selectSql += " and post = '" + paramInfo.post + "'";
- }
- if (!paramInfo.major.isNull() && !paramInfo.major.isEmpty()) {
- selectSql += " and major = '" + paramInfo.major + "'";
- }
- if (!paramInfo.workPosition.isNull() && !paramInfo.workPosition.isEmpty()) {
- selectSql += " and work_position = '" + paramInfo.workPosition + "'";
- }
- if (!paramInfo.educationDegree.isNull() && !paramInfo.educationDegree.isEmpty()) {
- selectSql += " and education_degree = '" + paramInfo.educationDegree + "'";
- }
- if (!paramInfo.phone.isNull() && !paramInfo.phone.isEmpty()) {
- selectSql += " and phone = '" + paramInfo.phone + "'";
- }
- if (!paramInfo.projectId.isNull() && !paramInfo.projectId.isEmpty()) {
- selectSql += " and project_id = '" + paramInfo.projectId + "'";
- }
- int role = paramInfo.role;
- if (role == 0) {
- selectSql += " and role != '" + QString::number(role) + "'";
- } else {
- selectSql += " and role = '" + QString::number(role) + "'";
- }
- if (query.exec(selectSql)) {
- if (query.next()) {
- queryInfo = paramInfo;
- queryInfo.id = query.value(0).toInt();
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- /*获取到用户列表*/
- bool UserService::QueryUserListByEngineerId(QList<QFUser> &userInfoList, int engineerId)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time "
- "from t_user_info"
- " WHERE id in (select uc.user_id from t_user_config uc where "
- "uc.engineer_id = %1 and uc.is_valid =1 )")
- .arg(engineerId);
- // qDebug() << selectSql;
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- QFUser userInfo;
- userInfo.id = query.value(0).toInt();
- userInfo.userName = query.value(1).toString();
- userInfo.userNo = query.value(2).toString();
- userInfo.password = query.value(3).toString();
- userInfo.role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo.post = query.value(5).toString();
- userInfo.major = query.value(6).toString();
- userInfo.workPosition = query.value(7).toString();
- userInfo.educationDegree = query.value(8).toString();
- userInfo.phone = query.value(9).toString();
- userInfo.remark = query.value(10).toString();
- userInfo.projectId = query.value(11).toString();
- userInfo.writeTime = query.value(12).toString();
- userInfoList.append(userInfo);
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserListByColumnAndColumnValue(QList<QFUser *> *userInfoList, QString columnName,
- QString columnValue)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time "
- "from t_user_info"
- " WHERE %1 = '%2'")
- .arg(columnName)
- .arg(columnValue);
- // qDebug() << selectSql;
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- QFUser *userInfo = new QFUser();
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- userInfoList->append(userInfo);
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserList(QList<QFUser *> *userInfoList)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time from t_user_info");
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- QFUser *userInfo = new QFUser();
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- userInfoList->append(userInfo);
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserListNotAdmin(QList<QFUser *> *userInfoList)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- try {
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time from t_user_info where role !=0");
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- QFUser *userInfo = new QFUser();
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- userInfoList->append(userInfo);
- }
- }
- } else {
- qDebug() << query.lastError();
- }
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool UserService::DeleteUserById(int Id)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString deleteSql = QString("DELETE FROM t_user_info WHERE id = %1").arg(Id);
- if (query.exec(deleteSql)) {
- ret = true;
- qDebug() << "deleteSql success!";
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- //删除除管理管外的所有用户
- bool UserService::DeleteUserByRole(int roleType)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString deleteSql = QString("DELETE FROM t_user_info WHERE role != %1").arg(roleType);
- if (query.exec(deleteSql)) {
- ret = true;
- qDebug() << "deleteSql success!";
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserByNoAndPassword(QFUser *userInfo, QString userNo, QString password)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time from "
- "t_user_info WHERE user_no = %1 and password = %2")
- .arg("'" + userNo + "'")
- .arg("'" + password + "'");
- qDebug() << "QueryUserByNoAndPassword---" << selectSql;
- if (query.exec(selectSql)) {
- if (query.next()) {
- if (query.isNull(0) == false) {
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserByNo(QFUser *userInfo, QString userNo)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time from "
- "t_user_info WHERE user_no = %1 ")
- .arg("'" + userNo + "'");
- qDebug() << selectSql << userNo;
- if (query.exec(selectSql)) {
- if (query.next()) {
- if (query.isNull(0) == false) {
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::QueryUserInfoById2(QFUser *userInfo, int userId)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase2();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
- "major,work_position,education_degree,phone,remark,project_id,"
- "write_time from "
- "t_user_info WHERE id = %1")
- .arg(userId);
- qDebug() << selectSql;
- if (query.exec(selectSql)) {
- if (query.next()) {
- if (query.isNull(0) == false) {
- userInfo->id = query.value(0).toInt();
- userInfo->userName = query.value(1).toString();
- userInfo->userNo = query.value(2).toString();
- userInfo->password = query.value(3).toString();
- userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
- userInfo->post = query.value(5).toString();
- userInfo->major = query.value(6).toString();
- userInfo->workPosition = query.value(7).toString();
- userInfo->educationDegree = query.value(8).toString();
- userInfo->phone = query.value(9).toString();
- userInfo->remark = query.value(10).toString();
- userInfo->projectId = query.value(11).toString();
- userInfo->writeTime = query.value(12).toString();
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool UserService::UpdateUserInfo2(const QFUser &userInfo)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase2());
- t.update("t_user_info")
- .set("user_name", userInfo.userName)
- .set("user_no", userInfo.userNo)
- .set("password", userInfo.password)
- .set("role", userInfo.role)
- .set("post", userInfo.post)
- .set("major", userInfo.major)
- .set("work_position", userInfo.workPosition)
- .set("education_degree", userInfo.educationDegree)
- .set("phone", userInfo.phone)
- .set("remark", userInfo.remark)
- .set("project_id", userInfo.projectId)
- .where("id=?", userInfo.id);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- int UserService::AddUserInfo2(const QFUser &userInfo)
- {
- int returnId = -1;
- try {
- Transaction t(SqlDBHelper::getDatabase2());
- InsertQuery query = t.insertInto("t_user_info (id,user_name, user_no, password, role, post, "
- "major,work_position,education_degree,phone,project_id,write_time)");
- NonQueryResult result =
- query.values(userInfo.id, userInfo.userName, userInfo.userNo, userInfo.password, userInfo.role,
- userInfo.post, userInfo.major, userInfo.workPosition, userInfo.educationDegree,
- userInfo.phone, userInfo.projectId, userInfo.writeTime)
- .exec();
- t.commit();
- returnId = result.lastInsertId().toInt();
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- returnId = -1;
- }
- return returnId;
- }
- //删除除管理管外的所有用户
- bool UserService::DeleteAllUser()
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString deleteSql = QString("DELETE FROM t_user_info ");
- if (query.exec(deleteSql)) {
- ret = true;
- qDebug() << "deleteSql success!";
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- //////////////////////用户信息表-end////////////////////
|