Exemple #1
0
void BookUI::slotSaveEdit()
{
    this->confirnBtn->setDisabled(true);
    QVector<QString> bookInfoVec = this->getBookInfo();

    if (bookInfoVec.at(7).toInt() < bookInfoVec.at(8).toInt())
    {
        QMessageBox::warning(this, tr("警告"), tr("在馆数量大于图书数量,请仔细检查"), "确定");
        this->confirnBtn->setDisabled(false);
        return;
    }

    User *user = NULL;
    if (bookInfoVec.at(9) == "是")
    {
        QString userCode = bookInfoVec.at(10);
        user = SYSTYPE->getUserByCode(userCode);
        if (user == NULL)
        {
            QMessageBox::warning(this, tr("警告"), tr("不存在该读者,请仔细检查"), "确定");
            this->confirnBtn->setDisabled(false);
            return;
        }
    }

    Books *book = SYSTYPE->getBook(this->uniqueBook->getBookId());
    if (book == NULL)
    {
        this->confirnBtn->setDisabled(false);
        return;
    }
    book->setBookName(bookInfoVec.at(0));
    book->setAuthor(bookInfoVec.at(2));
    book->setPublishing(bookInfoVec.at(3));
    book->setCateGoryId(bookInfoVec.at(4).toInt());
    book->setPrice(bookInfoVec.at(5));
    book->setdateIN(QDate::fromString(bookInfoVec.at(6), "yyyy-MM-dd"));
    book->setQuantity(bookInfoVec.at(7).toInt());
    book->setQuantityIn(bookInfoVec.at(8).toInt());
    book->setQuantityOut(bookInfoVec.at(7).toInt() - bookInfoVec.at(8).toInt());

    this->uniqueBook->setBookCode(bookInfoVec.at(1));

    int isBorrow = this->uniqueBook->getIsBorrow();   
    Borrow *borrow = NULL;
    if (isBorrow == sysinclude::yesBorrow)
    {
        borrow= SYSTYPE->getBorrowByUniqueBook(this->uniqueBook->getId());
        if (borrow == NULL)
        {
            QMessageBox::warning(this, tr("警告"), tr("数据出错,无法保存,请关闭"), "确定");
            this->confirnBtn->setEnabled(true);
            return;
        }
    }

    if (bookInfoVec.at(9) == "是")
        this->uniqueBook->setIsBorrow(sysinclude::yesBorrow);
    else
        this->uniqueBook->setIsBorrow(sysinclude::noBorrow);

    DaoBorrow daoBorrow;
    DaoUniqueBook daoUniqueBook;
    DaoBooks daoBook;

    if (QSqlDatabase::database().driver()->hasFeature(QSqlDriver::Transactions))
    {
        if (GLobal_DB->transaction())
        {
            bool retBook = daoBook.Update(book);
            bool retUnique = daoUniqueBook.Update(uniqueBook);
            if (retBook == false || retUnique == false)
            {
                if(GLobal_DB->rollback())
                {
                    SYSTYPE->reverBook(book->getBookId());
                    SYSTYPE->reverUniqueBook(uniqueBook->getId());
                    QMessageBox::information(this, tr("提醒"), tr("更新失败!"), "确定");
                    this->confirnBtn->setDisabled(false);
                    return;
                }
            }

            bool borrowRet = false;

            if(isBorrow == sysinclude::yesBorrow)
            {
                if (bookInfoVec.at(9) == "是")
                {
                    borrow->setReaderId(user->getUserID());
                    borrow->setDateBorrow(QDate::fromString(bookInfoVec.at(11), "yyyy-MM-dd"));
                    borrow->setDateReturn(QDate::fromString(bookInfoVec.at(12), "yyyy-MM-dd"));
                    if (bookInfoVec.at(13) == "是")
                        borrow->setLoss(sysinclude::yesLoss);
                    else
                        borrow->setLoss(sysinclude::noLoss);
                    borrowRet = daoBorrow.Update(borrow);
                }
                else
                {
                    borrowRet = daoBorrow.Delete(borrow);
                }
                if (borrowRet == false)
                {
                    if(GLobal_DB->rollback())
                    {
                        SYSTYPE->reverBook(book->getBookId());
                        SYSTYPE->reverUniqueBook(uniqueBook->getId());
                        SYSTYPE->reverBorrow(borrow->getBorrowId());
                        QMessageBox::information(this, tr("提醒"), tr("更新失败!"), "确定");
                        this->confirnBtn->setDisabled(false);
                        return;
                    }
                }

            }

            else
            {
                if (bookInfoVec.at(9) == "是")
                {
                    Borrow *newBorrow = new Borrow();
                    newBorrow->setReaderId(user->getUserID());
                    newBorrow->setBookId(this->uniqueBook->getId());
                    newBorrow->setDateBorrow(QDate::fromString(bookInfoVec.at(11), "yyyy-MM-dd"));
                    newBorrow->setDateReturn(QDate::fromString(bookInfoVec.at(12), "yyyy-MM-dd"));
                    if (bookInfoVec.at(13) == "是")
                        newBorrow->setLoss(sysinclude::yesLoss);
                    else
                        newBorrow->setLoss(sysinclude::noLoss);
                    borrowRet = daoBorrow.Add(newBorrow);

                    BaseDao basedao;
                    QString tableName = "BORROW";
                    QString outFields = "ID";
                    int borrowId = basedao.getId(tableName, outFields);
                    newBorrow->setBorrowId(borrowId);

                    if (borrowRet == false)
                    {
                        GLobal_DB->rollback();
                        SYSTYPE->reverBook(book->getBookId());
                        SYSTYPE->reverUniqueBook(uniqueBook->getId());
                        QMessageBox::information(this, tr("提醒"), tr("更新失败!"), "确定");
                        this->confirnBtn->setEnabled(true);
                        return;
                    }
                    SYSTYPE->addBorrow(newBorrow->getBorrowId(), newBorrow);
                }
            }

            if (GLobal_DB->commit())
            {
                QMessageBox::information(this, tr("提醒"), tr("更新成功!"), "确定");
                this->confirnBtn->setEnabled(true);
            }
        }
    }
}