#include "selldialog.h"
#include "ui_selldialog.h"
#include <QMovie>
#include <QSqlQueryModel>
#include <QMessageBox>
#include <QSqlQuery>
SellDialog::SellDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SellDialog)
{
ui->setupUi(this);
ui->label_6->setScaledContents(true);
QMovie *movie= new QMovie("F:/qt/hujie/2.gif");
ui->label_6->setMovie(movie);
ui->label_6->setBaseSize(30,30);
movie->start();
QSqlQueryModel *typeModel= new QSqlQueryModel(this);
typeModel->setQuery("select name from type");
ui->comboBox->setModel(typeModel);
//QSqlQuery query;
}
SellDialog::~SellDialog()
{
delete ui;
}
void SellDialog::on_comboBox_currentIndexChanged(const QString &arg1)
{
if(arg1=="请选择类型")
{
//on_sellCancelBtn_clicked();
}
else
{
ui->comboBox_2->setEnabled(true);
QSqlQueryModel *sellBrandModel = new QSqlQueryModel(this);
sellBrandModel->setQuery(QString("select name from brand where type='%1'")
.arg(arg1));
ui->comboBox_2->setModel(sellBrandModel);
//ui->pushButton->setEnabled(true);
}
}
void SellDialog::on_pushButton_clicked()
{
QString type=ui->comboBox->currentText();
QString name=ui->comboBox_2->currentText();
QSqlQuery query;
query.exec(QString("select sum from brand where name='%1'").arg(ui->comboBox_2->currentText()));
query.first();
int max= query.value(0).toInt();
ui->spinBox->setMaximum(max);
int value=ui->spinBox->value();
query.exec(QString("select sell from brand where name='%1'").arg(ui->comboBox_2->currentText()));
query.first();
int rsell=query.value(0).toInt();
value=value+rsell;
int last=ui->spinBox->maximum()-value;
//获取销售量
query.exec(QString("select sell from brand where name='%1'and type='%2'")
.arg(name).arg(type));
query.next();
int sell=query.value(0).toInt()+ui->spinBox->text().toInt();
//实务操作
QSqlDatabase::database().transaction();
bool rtn=query.exec(QString("update brand set sell='%1',last='%2' where name='%3'and type='%4'")
.arg(sell).arg(last).arg(name).arg(type));
if(rtn)
{
QSqlDatabase::database().commit();
QMessageBox::information(this,tr("提示"),tr("购买成功!"),QMessageBox::Ok);
}else{
QSqlDatabase::database().rollback();
QMessageBox::information(this,tr("提示"),tr("购买失败无法访问数据库!"),QMessageBox::Ok);
}
}
void SellDialog::on_comboBox_2_currentIndexChanged(const QString &arg1)
{
ui->spinBox->setValue(0);
ui->spinBox->setEnabled(false);
ui->lineEdit->clear();
ui->lineEdit->setEnabled(false);
ui->pushButton->setEnabled(false);
QSqlQuery query;
query.exec(QString("select price from brand where name='%1'and type='%2'").arg(arg1)
.arg(ui->comboBox->currentText()));
query.next();
ui->lineEdit_2->setEnabled(true);
ui->lineEdit_2->setReadOnly(true);
ui->lineEdit_2->setText(query.value(0).toString());
query.exec(QString("select last from brand where name='%1'and type='%2'").arg(arg1).arg(ui->comboBox->currentText()));
query.first();
int num=query.value(0).toInt();
if(num==0)
{
QMessageBox::information(this,tr("提示"),tr("商品已经卖完"),QMessageBox::Ok);
}
else{
ui->spinBox->setEnabled(true);
ui->spinBox->setMaximum(num);
// ui->sellLast
}
}
void SellDialog::on_spinBox_valueChanged(int arg1)
{
if(arg1==0)
{
ui->lineEdit->clear();
ui->lineEdit->setEnabled(false);
ui->pushButton->setEnabled(false);
}
else
{
ui->lineEdit->setEnabled(true);
ui->lineEdit->setReadOnly(true);\
qreal sum= arg1 *ui->lineEdit_2->text().toInt();
ui->lineEdit->setText(QString::number(sum));
ui->pushButton->setEnabled(true);
}
}