//#include"my_main.h"
#include"my_company.h"
#include<stdio.h>
#include<time.h>
#include<iomanip>
#include<iostream>
#include<string.h>
#include<mysql/mysql.h>
#include<stdlib.h>
#include <list>
#include <climits>
#include<unistd.h>
using namespace std;
MYSQL* conn;
list<Employee*> Link; //创建一个用于存储Employee对象指针的list容器;
void Load_Menu();
void Main_Menu();
int login_account(int num,string pwd);
void conn_mysql();
void load();
void boss_fun(list<Employee*>::iterator p);
void man_fun(list<Employee*>::iterator p);
void manager_fun(list<Employee*>::iterator p);
void view_all(list<Employee*>::iterator p);
void view_all(list<Employee*>::iterator p);
void add_manager(list<Employee*>::iterator p);
void del(list<Employee*>::iterator p);
void modify_salary(list<Employee*>::iterator p,list<Employee*>::iterator q);
void modify_mark(list<Employee*>::iterator p);
void update(list<Employee*>::iterator p);
void add_man(list<Employee*>::iterator p);
void show_depart(list<Employee*>::iterator p);
void save_all();
void change_pwd(list<Employee*>::iterator p);
void show_personal(list<Employee*>::iterator p);
bool sort_any (const Employee *first,const Employee * second);
bool sort_lead (const Employee *first,const Employee * second);
bool sort_depart (const Employee *first,const Employee * second);
bool sort_num (const Employee *first, const Employee * second);
bool sort_name (const Employee *first,const Employee * second);
int check=0;
void Employee::save_str(char *savestr) //save information to mysql
{
list<Employee*>::iterator l=Link.begin(); //创建容器Link1的迭代器 l;
while(l != Link.end())
{
sprintf(savestr,"insert into Employee (number,name,password,position,salary,logtime,flag,remark,leader) values(%d,'%s','%s','%s',%f,'%s',%d,'%s','%s')",
(*l)->number,(*l)->name.c_str(),(*l)->password.c_str(),(*l)->position.c_str(),
(*l)->salary,(*l)->logtime.c_str(),(*l)->flag,(*l)->mark.c_str(),(*l)->leader.c_str());
if(mysql_query(conn,savestr)) //发送SQL语句,成功返回0;
{
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
l++;
}
}
////////////////////////////////////////////////////
bool sort_lead (const Employee *first,const Employee * second) //sort from little to greater by lead
{
return (first->leader) < (second->leader)?true:false;
}
////////////////////////
bool sort_depart (const Employee *first,const Employee * second) //sort from little to greater by depart
{
return (first->flag) <(second->flag)?true:false;
}
////////////////////////////
bool sort_num (const Employee *first, const Employee * second) //sort from little to greater by number
{
return (first->number) < (second->number)?true:false;
}
//////////////////////////
bool sort_name (const Employee *first,const Employee * second) //sort from little to greater by name
{
return (first->name) < (second->name)?true:false;
}
////////////////////////////////////////////////////////
bool sort_any (const Employee *first,const Employee * second) //sort from little to greater by depart
{
bool res;
if((first->flag) < (second->flag))
{
if((first->leader) < (second->leader))
{
if((first->number) < (second->number))
{
res = true;
}
else
{
res = false;
}
}
}
else
{
res = false;
}
return res = true?true:false;
}
///////////////////////////////////////////////
void show_personal(list<Employee*>::iterator p) //show saleman's information
{
system("clear");
cout<<"\033[34;1m您的个人信息为:\033[0m"<<endl<<endl;
cout<<"----------------------------------------------------------------------------------------------"<<endl;
cout.width(10); cout<<"工号 |";
cout.width(19); cout<<"姓名 |";
cout.width(12); cout<<"职位 |";
cout.width(9); cout<<"薪水 |";
cout.width(9); cout<<"等级 |";
cout.width(19); cout<<"REMARK |";
cout.width(19); cout<<"上级 |";
cout.width(40); cout<<"上次登录时间";
cout<<endl;
cout<<"----------------------------------------------------------------------------------------------"<<endl;
(*p)->print();
cin.ignore(1000,'\n');
cin.get();
}
/////////////////////////////////////////////
void change_pwd(list<Employee*>::iterator p) //change salesman's password
{
string pwd;
string c_pwd;
while(1)
{
cout<<"\n\033[34;1m请输入新密码:\033[0m";
system("stty -echo");
cin>>pwd;
system("stty echo");
cout<<"\n\033[34;1m请在输入一次:\033[0m";
system("stty -echo");
cin>>c_pwd;
system("stty echo");
if(pwd == c_pwd)
{
(*p)->set_passwd(pwd);
cout<<"\n\033[34;1m密码修改成功!\033[0m"<<endl;
cout<<"Input any key";
cin.ignore(1000,'\n');
cin.get();
break;
}
}
save_all();
}
//////////////////////////////////////////////
void save_all() //save all data to mysql
{
list<Employee*>::iterator p=Link.begin();
if(mysql_query(conn,"drop table Employee"))
{
cout<<"mysql error"<<endl;
exit(1);
}
if(mysql_query(conn,"create table Employee(no int auto_increment not null primary key,number int not null,name varchar(20) not null,password varchar(20) not null default '1234',position varchar(20) not null,salary float not null,logtime timestamp default current_timestamp,flag tinyint not null,remark varchar(128),leader varchar(20))default charset=utf8"))
{
cout<<"\033[34;1m创建数据库失败!\033[0m"<<endl;
exit(1);
}
char str[1024]="";
while(p!=Link.end())
{
(*p)->save_str(str);
if(mysql_query(conn,str))
{
cout<<"\033[34;1m添加数据失败!\033[0m"<<endl;
exit(1);
}
p++;
}
}
/////////////////////////////////////////////
void show_depart(list<Employee*>::iterator p) //view depart staff information
{
string name;
name = (*p)->get_name();
p=Link.begin();
system("clear");
cout<<"---------------------------------------------------------------------------------------------------------"<<endl;
cout.width(10); cout<<"工号 |";
cout.width(19); cout<<"姓名 |";
cout.width(12); cout<<"职位 |";
cout.width(9); cout<<"薪水 |";
cout.width(9); cout<<"等级 |";
cout.width(19); cout<<"REMARK |";
cout.width(19); cout<<"上级 |";
cout.width(40); cout<<"上次登录时间 |";
cout<<endl;
cout<<"---------------------------------------------------------------------------------------------------------"<<endl;
while(p!=Link.end())
{
if( (((*p)->get_lead()) == name) )
{
(*p)->print();
}
p++;
}
cin.ignore(1000,'\n');
cin.get();
}
//////////////////////////////////////////
void add_man(list<Employee*>::iterator p) //add sales man
{
Employee* ptr =NULL;
string name; cout<<"\033[34;1m请输入姓名:\033[0m"; cin>>name;
string pwd = "www";
string pos;
float d_salary =3000;
int flag;
string mark; cout<<"Input remark:"; cin>>mark;
string lead = (*p)->get_name();
time_t t; t=time(NULL);
string g_t;
char tt[1024]="";
struct tm *tmper;
tmper = localtime(&t);
sprintf(tt,"%d-%d-%d %d:%d:%d",tmper->tm_year+1900,tmper->tm_mon+1,tmper->tm_mday,tmper->tm_hour,tmper->tm_min,tmper->tm_sec);
g_t = tt;
int wt;
float sale;
float m_fixed;
Link.sort(sort_num);
list<Employee*>::iterator q =Link.begin();
q=Link.begin();
int number = ((*q)->get_num());
while(q!=Link.end())
{
if( number < ((*q)->get_num()) )
{
break;
}
q++;
number++;
}
switch((*p)->get_flag())
{
case 2:
flag = 1;
pos="销售职员";
ptr=new S_man(number,name,pwd,pos,d_salary,g_t,flag,mark,lead);
cout<<"请输入销售额:"; cin>>sale;
ptr->set_sales(sale);
ptr->set_salary();
m_fixed = (*p)->get_salary();
(*p)->set_sales(sale);
(*p)->set_fixed_salary(m_fixed);
(*p)->set_salary();
break;
case 4:
flag = 3;
pos="技术职员";
ptr=new T_man(number,name,pwd,pos,d_salary,g_t,flag,mark,lead);
cout<<"\033[34;1m请输入工作时间:\033[0m"; cin>>wt;
ptr->set_worktime(wt);
ptr->set_salary();
break;
default:
break;
}
Link.push_front(ptr