#include "yggllog.h"
#include "ui_yggllog.h"
#include<QSqlQueryModel>
#include<QTableView>
#include<QSqlTableModel>
#include<QSqlQuery>
#include<QDebug>
#include<QSortFilterProxyModel>
#include<QAbstractItemModel>
#include<QAbstractProxyModel>
#include <QFrame>
#include<QMouseEvent>
#include<QMessageBox>
const static int pos_min_x = 0;
const static int pos_max_x = 1141 - 40;
const static int pos_min_y = 0;
const static int pos_max_y = 31;
Yggllog::Yggllog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Yggllog)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
MAX();
//下拉combox读取数据库中的部门
QSqlQueryModel *model1 = new QSqlQueryModel;
model1->setQuery(QString("SELECT [部门] FROM [dbo].[部门]"));
ui->comboBox_3->setModel(model1);
ui->comboBox_4->setModel(model1);
ui->tableView->verticalHeader()->hide();
// 下拉combox指定显示文本
ui->comboBox->addItem("工号");
ui->comboBox->addItem("姓名");
ui->comboBox_2->addItem("技术员");
ui->comboBox_2->addItem("销售员");
ui->comboBox_2->addItem("销售经理");
ui->comboBox_2->addItem("经理");
//tableview显示所有员工信息
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery(QString("select*from [dbo].[员工信息]"));
QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);
sqlproxy->setSourceModel(model);
ui->tableView->setModel(sqlproxy);
//控制tableview的列宽
ui->tableView->setColumnWidth(0,100);
ui->tableView->setColumnWidth(1,100);
ui->tableView->setColumnWidth(2,100);
ui->tableView->setColumnWidth(3,100);
ui->tableView->setColumnWidth(4,100);
ui->tableView->setColumnWidth(5,100);
ui->tableView->setColumnWidth(6,100);
ui->tableView->setColumnWidth(7,100);
ui->tableView->horizontalHeader()->setStretchLastSection(true);
}
Yggllog::~Yggllog()
{
delete ui;
}
void Yggllog::on_pushButton_clicked()//查找员工信息 按工号或姓名
{
QSqlQueryModel *model = new QSqlQueryModel;
QString seek = ui->lineEdit->text();
QString seektype=ui->comboBox->currentText();
if(seek=="")
{
QSqlQueryModel *model1 = new QSqlQueryModel;
model1->setQuery(QString("select*from [dbo].[员工信息]"));
QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);
sqlproxy->setSourceModel(model1);
ui->tableView->setModel(sqlproxy);
}else{
if(seektype=="工号"){
model->setQuery(QString("select*from [dbo].[员工信息]where [工号]='%1'").arg(seek));
ui->tableView->setModel(model);
}else
{
model->setQuery(QString("select*from [dbo].[员工信息]where [姓名]='%1'").arg(seek));
ui->tableView->setModel(model);
}
}
}
void Yggllog::on_comboBox_2_currentTextChanged()//按职位查看员工
{
QSqlQueryModel *model = new QSqlQueryModel;
QString post=ui->comboBox_2->currentText();
model->setQuery(QString("select*from [dbo].[员工信息]where [岗位]='%1'").arg(post));
QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);
sqlproxy->setSourceModel(model);
ui->tableView->setModel(sqlproxy);
}
// if(post=="技术员"){
// model->setQuery(QString("select*from [dbo].[员工信息]where [岗位]='%1'").arg(post));
// ui->tableView->setModel(model);
// }else if(post=="销售员")
// {
// model->setQuery(QString("select*from [dbo].[员工信息]where [岗位]='%1'").arg(post));
// ui->tableView->setModel(model);
// }else if(post=="销售经理")
// {
// model->setQuery(QString("select*from [dbo].[员工信息]where [岗位]='%1'").arg(post));
// ui->tableView->setModel(model);
// }else if(post=="经理")
// {
// model->setQuery(QString("select*from [dbo].[员工信息]where [岗位]='%1'").arg(post));
// ui->tableView->setModel(model);
// }
//}
void Yggllog::on_comboBox_3_currentIndexChanged()//按部门查看员工
{
QSqlQueryModel *model = new QSqlQueryModel;
QString department=ui->comboBox_3->currentText();
model->setQuery(QString("select*from [dbo].[员工信息]where [部门]='%1'").arg(department));
QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);
sqlproxy->setSourceModel(model);
ui->tableView->setModel(sqlproxy);
}
void Yggllog::on_tableView_clicked()//读取tableview单元格内容
{
int row= ui->tableView->currentIndex().row();
QAbstractItemModel *model = ui->tableView->model ();
// model->index(row,0);//选中行第一列的内容
QModelIndex index = model->index(row,1);//选中行第一列的内容
QVariant data = model->data(index);
QString str=data.toString();
ui->lineEdit_2->setText(str);
QModelIndex index1 = model->index(row,0);//选中行第一列的内容
QVariant data1 = model->data(index1);
QString str1=data1.toString();
ui->lineEdit_3->setText(str1);
QModelIndex index2 = model->index(row,4);//选中行第一列的内容
QVariant data2 = model->data(index2);
QString str2=data2.toString();
ui->lineEdit_4->setText(str2);
QModelIndex index3 = model->index(row,5);//选中行第一列的内容
QVariant data3 = model->data(index3);
QString str3=data3.toString();
ui->lineEdit_5->setText(str3);
}
void Yggllog::on_pushButton_2_clicked()//删除员工信息
{
QString nummber=ui->lineEdit_3->text();
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery(QString("DELETE FROM [dbo].[员工信息] WHERE [工号] ='%1'").arg(nummber));
QSqlQueryModel *model1 = new QSqlQueryModel;
ui->lineEdit_2->clear();
ui->lineEdit_3->clear();
ui->lineEdit_4->clear();
ui->lineEdit_5->clear();
model1->setQuery(QString("select*from [dbo].[员工信息]"));
ui->tableView->setModel(model1);//数据放置进去
ui->tableView->verticalHeader()->hide();
QMessageBox message(QMessageBox::NoIcon, "提示", "删除该员工信息成功!");
message.setStyleSheet("font: 10pt '楷体';QPushButton{background-color: rgb(19, 129, 197);font: 10pt '楷体';color: white; border: 1px groove gray;border-style: outset;} QPushButton:hover{background-color:white; color: black;}QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset; }");
message.setButtonText(QMessageBox::Ok,QString(" 确 定 "));
QIcon *icon = new QIcon(":/new/ico/t.ico");
message.setWindowIcon(*icon);
message.exec();
}
void Yggllog::on_pushButton_3_clicked()//跳转到添加员工界面
{
ygxx->show();
}
void Yggllog::MAX()//公司员工工资情况
{
QString maxgz;
QString mingz;
QString avggz;
QSqlQuery query;
query.exec(QString("SELECT MAX([工资]) FROM [dbo].[员工信息]"));
while(query.next())
{
maxgz=query.value(0).toString();
}
QSqlQuery query1;
query1.exec(QString("SELECT MIN([工资]) FROM [dbo].[员工信息]"));
while(query1.next())
{
mingz=query1.value(0).toString();
}
QSqlQuery query2;
query2.exec(QString("SELECT AVG([工资]) FROM [dbo].[员工信息]"));
while(query2.next())
{
avggz=query2.value(0).toString();
}
QString all=(QString("公司最高工资为:%1 \r\n公司最低工资为:%2\r\n公司平均工资为:%3\r\n").arg(maxgz).arg(mingz).arg(avggz));
ui->textEdit->insertPlainText(all);
}
void Yggllog::on_comboBox_4_cu