开发工具集:
Qt5.12.4、VS2017、Sqlite3轻量级数据库(免除安装数据库的烦恼)
百度网盘链接:
链接:https://pan.baidu.com/s/1rNt6EI8uAAIuHCQeGXKinA
提取码:0mcn
效果图
1、加载数据库部分
void DatabaseInfo::loadDatabaseInfo()
{// 加载房间类型表loadTRoomType();// 加载房间状态列表loadTRoomState();// 加载房号列表loadRoomNumber();
}void DatabaseInfo::loadTRoomType()
{m_mapRoomType.clear();QSqlQuery query;QString strSql = "";strSql = QString("SELECT * FROM T_ROOMTYPE");query.exec(strSql);while (query.next()){TRoomType data;data.roomId = query.value("roomId").toInt();data.roomType = query.value("roomType").toString();data.roomIntroduce = query.value("roomIntroduce").toString();data.roomPrice = query.value("roomPrice").toInt();data.roomPixmap = query.value("roomPixmap").toByteArray();m_mapRoomType[data.roomId] = data;}
}void DatabaseInfo::loadTRoomState()
{m_mapRoomState.clear();QSqlQuery query;QString strSql = "";strSql = QString("SELECT * FROM T_ROOMSTATE");query.exec(strSql);while (query.next()){m_mapRoomState[query.value("id").toInt()] = query.value("roomState").toString();}
}void DatabaseInfo::loadRoomNumber()
{m_vecRoomNumber.clear();QSqlQuery query;QString strSql = "";strSql = QString("SELECT roomNumber FROM T_ROOMINFO GROUP BY roomNumber");query.exec(strSql);while (query.next()){m_vecRoomNumber.push_back(query.value("roomNumber").toInt());}
}
2、登记入住部分
void FormCheckIn::on_btnSubmitOrder_clicked()
{QString userName = ui.lEditName->text().trimmed();QString userSex = ui.rdoBtnMale->isChecked() ? "男" : "女";QString userIdNumber = ui.lEditIdNumber->text().trimmed();QString userPhone = ui.lEditPhone->text().trimmed();if (userName.isEmpty() || userIdNumber.isEmpty() || userPhone.isEmpty()){ui.lblError->setProperty("checkIn", false);ui.lblError->setText("登记信息内容不能为空!");return;}QString checkInTime = ui.lblCheckInTime->text();QString checkOutTime = ui.lblCheckOutTime->text();int checkInDays = (QDate::fromString(checkInTime, "yyyy-MM-dd")).daysTo(QDate::fromString(checkOutTime, "yyyy-MM-dd"));int userState = (m_type == RESERVER) ? 1 : 2;QString userPayment = ui.lblMoney->text();userPayment = QString::number((userPayment.left(userPayment.length() - 1)).toInt() / checkInDays);int userRoomNumber = getRoomNumber(m_roomId);QSqlQuery query;SqlManage::getDataBase().transaction(); // 数据库开启事务for (int i = 0; i < checkInDays; i++){// 插入T_USERINFO表QString tempCheckInTime = (QDate::fromString(checkInTime, "yyyy-MM-dd")).addDays(i).toString("yyyy-MM-dd");QString tempCheckOutTime = (QDate::fromString(checkInTime, "yyyy-MM-dd")).addDays(i + 1).toString("yyyy-MM-dd");QString strSql = QString("INSERT INTO T_USERINFO(userName, userSex, userIdNumber, userPhone, userRoomNumber, userCheckInTime, userCheckInDays,""userCheckOutTime, userState, userPayment) values ('%1', '%2', '%3', '%4', %5, '%6', %7, '%8', %9, %10);").arg(userName).arg(userSex).arg(userIdNumber).arg(userPhone).arg(userRoomNumber).arg(tempCheckInTime).arg(1).arg(tempCheckOutTime).arg(userState).arg(userPayment.toInt());qDebug() << strSql;if (!query.exec(strSql)){SqlManage::getDataBase().rollback(); // 数据库回滚操作,撤销所有插入语句ui.lblError->setProperty("checkIn", false);ui.lblError->setText("订单提交失败!");return;}strSql = QString("UPDATE T_ROOMINFO SET roomState = %1 WHERE roomNumber = %2 AND roomDate = '%3'").arg((m_type == RESERVER) ? Hotel::ROOM_STATE_PRE_PLEDGE : Hotel::ROOM_STATE_LIVING).arg(userRoomNumber).arg(tempCheckInTime);qDebug() << strSql;if (!query.exec(strSql)){SqlManage::getDataBase().rollback(); // 数据库回滚操作,撤销所有插入语句ui.lblError->setProperty("checkIn", false);ui.lblError->setText("订单提交失败!");return;}}SqlManage::getDataBase().commit(); // 数据库提交事务,真正执行所有插入语句ui.lblError->setProperty("checkIn", true);ui.lblError->setText("订单提交成功!");setBtnEnabled(false);// 更新客房界面信息UpdateFormInfo::addFormInfo(Hotel::UPDATE_CHECK_IN);
}
3、房间图片部分
void FormUpdateRoomPixmap::updatePixmap()
{int i = 0;QSqlQuery query;QString strSql = "SELECT roomPixmap FROM T_ROOMTYPE";query.exec(strSql);while (query.next()){QByteArray byteArray = query.value("roomPixmap").toByteArray();QPixmap pixmap;pixmap.loadFromData(byteArray);QSize size = m_listLables[i]->size();m_listLables[i++]->setPixmap(pixmap.scaled(size));}
}