# 人事管理课设 **Repository Path**: GoBDN/personnel_matters ## Basic Information - **Project Name**: 人事管理课设 - **Description**: 人事管理系统课设; 成员:段嘉宝,杨磊,邓周楠,谢立志,曾宇德(组长) - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 8 - **Created**: 2021-07-03 - **Last Updated**: 2021-07-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 一、项目采用的架构和数据库介绍 程序前端用的是 jinjia模板框架,后端是 flask Flask-SQLAlchemy Flask-Security,MySQL数据库,Nginx进行反向代理,使用gunicorn启动程序完成生产力环境,使用 supervisor 管理进程 防止进程异常退出,发布的网址 www.akitten.cn (超级管理员账号:2020001,密码:123456) 1. Jinja2 是一个现代的,设计者友好的,仿照 Django 模板的 Python 模板语言。 它速度快,被广泛使用,并且提供了可选的沙箱模板执行环境保证安全,它的特性有:沙箱中执行,强大的 HTML 自动转义系统保护系统免受 XSS,模板继承,及时编译最优的 python 代码,可选提前编译模板的时间,易于调试。异常的行数直接指向模板中的对应行,可配置的语法。 2. Flask是一个轻量级的可定制框架,使用Python语言编写,较其他同类型框架更为灵活、轻便、安全且容易上手。它可以很好地结合MVC模式进行开发,开发人员分工合作,小型团队在短时间内就可以完成功能丰富的中小型网站或Web服务的实现。另外,Flask还有很强的定制性,用户可以根据自己的需求来添加相应的功能,在保持核心功能简单的同时实现功能的丰富与扩展,其强大的插件库可以让用户实现个性化的网站定制,开发出功能强大的网站。 3. MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的关系数据库管理系统应用软件之一。关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。 4. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强。 5. Gunicorn是一个被广泛使用的高性能的Python WSGI UNIX HTTP Server。和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点。 6. Supervisor是用Python开发的一套通用的进程管理程序。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。 ### 二、项目gitee地址及贡献图 https://gitee.com/personnel_management_system_zeng/personnel_matters ![段嘉宝](image/%E6%88%AA%E5%9B%BE.jpg) 段嘉宝 ![曾宇德](image/gitee%E8%B4%A1%E7%8C%AE%E5%BA%A6.PNG) 曾宇德 ![](image/%E6%8D%95%E8%8E%B7.PNG) 谢立志 ![](image/%E9%82%93%E5%91%A8%E6%A5%A0%E7%9A%84%E8%B4%A1%E7%8C%AE%E5%BA%A6.PNG) 邓周楠 ![](image/%E6%9D%A8%E7%A3%8A.png) 杨磊 ### 三、数据库创建脚本和ER图 1. 部门 ```python DROP TABLE IF EXISTS `department`; CREATE TABLE `department` ( `departmentID` int(11) NOT NULL AUTO_INCREMENT, `departmentname` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `departmenttype` enum('部门','公司') COLLATE utf8_unicode_ci NOT NULL DEFAULT '部门', `departmentphone` bigint(20) NOT NULL, `departmentfax` bigint(20) DEFAULT NULL, `describe` text COLLATE utf8_unicode_ci, `superior` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `establish` date NOT NULL, PRIMARY KEY (`departmentID`), UNIQUE KEY `departmentname` (`departmentname`), UNIQUE KEY `departmentphone` (`departmentphone`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 2. 民族 ```python DROP TABLE IF EXISTS `nation`; CREATE TABLE `nation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nation` varchar(20) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 3. 岗位 ```python DROP TABLE IF EXISTS `post`; CREATE TABLE `post` ( `postID` int(11) NOT NULL AUTO_INCREMENT, `postname` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `posttype` enum('市场','营销','技术','管理') COLLATE utf8_unicode_ci NOT NULL DEFAULT '技术', `establishment` int(11) DEFAULT NULL, PRIMARY KEY (`postID`), UNIQUE KEY `postname` (`postname`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 4. 试用期 ```python DROP TABLE IF EXISTS `staff_probation`; CREATE TABLE `staff_probation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `startdate` date NOT NULL, `enddate` date NOT NULL, PRIMARY KEY (`id`), KEY `userID` (`userID`), CONSTRAINT `staff_probation_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; DROP TABLE IF EXISTS `probation`; CREATE TABLE `probation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `examine_remark` text COLLATE utf8_unicode_ci NOT NULL, `examine_result` enum('转正','延期','不予录用') COLLATE utf8_unicode_ci NOT NULL DEFAULT '转正', `disposedate` date NOT NULL, `remark` text COLLATE utf8_unicode_ci, PRIMARY KEY (`id`), KEY `userID` (`userID`), CONSTRAINT `probation_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 5. 员工角色 ```python DROP TABLE IF EXISTS `role`; CREATE TABLE `role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL, `description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; DROP TABLE IF EXISTS `roles_users`; CREATE TABLE `roles_users` ( `user_id` int(11) DEFAULT NULL, `role_id` int(11) DEFAULT NULL, KEY `user_id` (`user_id`), KEY `role_id` (`role_id`), CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 6. 员工基础信息 ```python DROP TABLE IF EXISTS `staff`; CREATE TABLE `staff` ( `userID` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `sex` enum('男','女') COLLATE utf8_unicode_ci NOT NULL DEFAULT '男', `birthday` date NOT NULL, `IDnumber` varchar(18) COLLATE utf8_unicode_ci NOT NULL, `departmentID` int(11) NOT NULL, `postID` int(11) NOT NULL, `hiredate` date NOT NULL, `takejobdate` date NOT NULL, `employment_form` enum('正式员工','临时员工') COLLATE utf8_unicode_ci NOT NULL DEFAULT '正式员工', `personnel_source` enum('校园招聘','社会招聘','其他') COLLATE utf8_unicode_ci NOT NULL DEFAULT '社会招聘', `politics_status` enum('党员','预备党员','团员','其他') COLLATE utf8_unicode_ci DEFAULT NULL, `nation` enum('基诺族','珞巴族','门巴族','赫哲族','鄂伦春族','独龙族','塔塔尔族','京族','裕固族','保安族','德昂族','鄂温克族','俄罗斯族','乌孜别克族','怒族','塔吉克族','普米族','阿昌族','锡伯族','仡佬族','毛南族','撒拉族','布朗族','羌族','仫佬族','达斡尔族','土族','柯尔克孜族','景颇族','纳西族','东乡族','水族','拉祜族','高山族','畲族','佤族','僳僳族','黎族','傣族','哈萨克族','哈尼族','土家族','白族','瑶族','侗族','满族','朝鲜族','布依族','壮族','彝族','苗族','维吾尔族','藏族','回族','蒙古族','汉族') COLLATE utf8_unicode_ci DEFAULT NULL, `native_place` text COLLATE utf8_unicode_ci, `phone` bigint(20) NOT NULL, `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `height` int(11) DEFAULT NULL, `bloodtype` enum('A型','B型','AB型','O型','其他') COLLATE utf8_unicode_ci DEFAULT NULL, `marriage` enum('未婚','已婚','丧偶','离婚','其他') COLLATE utf8_unicode_ci DEFAULT NULL, `birthplace` text COLLATE utf8_unicode_ci, `domicileplace` text COLLATE utf8_unicode_ci, `highest_degree` enum('高中及以下','大专','本科','研究生') COLLATE utf8_unicode_ci DEFAULT NULL, `highest_offering` enum('无学位','学士','双学士','硕士','博士','博士后') COLLATE utf8_unicode_ci DEFAULT NULL, `graduate_school` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `major` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `graduation_date` date DEFAULT NULL, `now` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`userID`), UNIQUE KEY `IDnumber` (`IDnumber`), UNIQUE KEY `phone` (`phone`), UNIQUE KEY `email` (`email`), KEY `departmentID` (`departmentID`), KEY `postID` (`postID`), CONSTRAINT `staff_ibfk_1` FOREIGN KEY (`departmentID`) REFERENCES `department` (`departmentID`), CONSTRAINT `staff_ibfk_2` FOREIGN KEY (`postID`) REFERENCES `post` (`postID`) ) ENGINE=InnoDB AUTO_INCREMENT=2020013 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 7. 员工生涯 ```python DROP TABLE IF EXISTS `staff_career`; CREATE TABLE `staff_career` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `starttime` date NOT NULL, `endtime` date NOT NULL, `company_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `job_content` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `duty` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `pay` float DEFAULT NULL, `certifier` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL, `certifier_duty` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `certifier_phone` bigint(11) DEFAULT NULL, `remark` text COLLATE utf8_unicode_ci, PRIMARY KEY (`id`), KEY `userID` (`userID`), CONSTRAINT `staff_career_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 8. 员工离职 ```python DROP TABLE IF EXISTS `staff_dimission`; CREATE TABLE `staff_dimission` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `termdate` date NOT NULL, `dimission_type` enum('主动辞职','辞退','退休','开除','试用期未通过') COLLATE utf8_unicode_ci NOT NULL DEFAULT '主动辞职', `leave_to` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `talent_pool` enum('是','否') COLLATE utf8_unicode_ci NOT NULL DEFAULT '是', `remark` text COLLATE utf8_unicode_ci, PRIMARY KEY (`id`), KEY `userID` (`userID`), CONSTRAINT `staff_dimission_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 9. 员工外语水平 ```python DROP TABLE IF EXISTS `staff_languages`; CREATE TABLE `staff_languages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `language` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `prificiency` enum('完全不懂','少量,不能进行业务沟通','有限的业务沟通','一般,业务沟通仍受少量限制','好,无困难地进行谈判和演讲','流利,在商务中自如地运用') COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`), KEY `userID` (`userID`), CONSTRAINT `staff_languages_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 10. 员工家庭成员及社会关系 ```python DROP TABLE IF EXISTS `staff_relation`; CREATE TABLE `staff_relation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `relation` enum('父亲','母亲','配偶') COLLATE utf8_unicode_ci NOT NULL DEFAULT '父亲', `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `company` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `position` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `phone` bigint(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `phone` (`phone`), KEY `userID` (`userID`), CONSTRAINT `staff_relation_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` 11. 系统用户 ```python DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `password` varchar(128) COLLATE utf8_unicode_ci NOT NULL, `phone` bigint(20) NOT NULL, `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `active` tinyint(4) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `phone` (`phone`), UNIQUE KEY `email` (`email`), KEY `userID` (`userID`), CONSTRAINT `user_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `staff` (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ``` #### E-R图: ![](image/TIM%E5%9B%BE%E7%89%8720200701212745.png) ### 四、触发器和存储过程 1. 员工登录 ```python @app.route('/user/login', methods=['GET', 'POST']) def login(): # 登录 if not current_user.is_anonymous: return redirect(url_for('index')) form = forms.LoginForm() if request.method == 'POST': userID = request.form.get('userID') password = request.form.get('password') if not userID or not password: flash('请输入') return redirect(url_for('login')) user = User.query.filter_by(userID=userID).first() if user is None: flash('员工号错误') return redirect(url_for('login')) now = Staff.query.filter_by(now=True,userID=user.userID).first() if now is None: flash('员工已离职') return redirect(url_for('login')) if not user.validate_password(password): flash('密码错误') return redirect(url_for('login')) flash('登录成功') login_user(user, remember=True) return redirect(url_for('index')) return render_template('login.html', form=form) ``` 2. 注册 ```python @app.route('/logging', methods=['GET', 'POST']) def logging(): # 注册 if not current_user.is_anonymous: return redirect(url_for('index')) if request.method == 'POST': userID = request.form.get('userID') password1 = request.form.get('password1') password2 = request.form.get('password2') phone = request.form.get('phone') email = request.form.get('email') if password1 != password2: flash('两次输入的密码不同') return redirect(url_for('logging')) staff = Staff.query.filter_by(userID=userID).first() if staff is None: flash('员工号错误') return redirect(url_for('logging')) now = Staff.query.filter_by(now=True, userID=userID).first() if now is None: flash('员工已离职') return redirect(url_for('logging')) user = User.query.filter_by(userID=userID).first() if user is not None: flash('用户已注册') return redirect(url_for('login')) user = Staff.query.filter_by(userID=userID).first() if user is None: flash('用户不存在,请检查员工号') return redirect(url_for('logging')) new_user = user_datastore.create_user(userID=userID, password=User.set_password(password1), phone=phone, email=email) normal_role = user_datastore.find_role('User') db.session.add(new_user) user_datastore.add_role_to_user(new_user, normal_role) db.session.commit() login_user(new_user) return redirect(url_for('index')) return render_template('logging.html') ``` 3. 登出 ```python @app.route('/logout') def logout(): # 登出 logout_user() flash('登出') return redirect(url_for('index')) ``` 4. 员工基本信息 ```python @app.route('/staff',methods=['GET','POST']) @login_required # 用于视图保护 def staff(): # 员工基本信息 if current_user.has_role('Admin') or current_user.has_role('Root'): if request.method == 'POST': info = request.form.get('info') date = request.form.get('date') if info == 'userID': staffs = Staff.query.filter_by(userID=date,now=True).first() if staffs is None: flash('员工号不存在') return redirect(url_for('staff')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'username': staffs = Staff.query.filter_by(username=date,now=True).first() if staffs is None: flash('员工不存在') return redirect(url_for('staff')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'IDnumber': staffs = Staff.query.filter_by(IDnumber=date,now=True).first() if staffs is None: flash('员工不存在') return redirect(url_for('staff')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'departmentID': staffs = Staff.query.filter_by(departmentID=date,now=True).all() if staffs is None: flash('部门号不存在') return redirect(url_for('staff')) departments = Department.query.filter_by(departmentID=date).first() if departments is None: flash('部门号不存在') return redirect(url_for('staff')) posts = Post.query.all() return render_template('staff.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'departmentname': departments = Department.query.filter_by(departmentname=date).first() if departments is None: flash('部门不存在') return redirect(url_for('staff')) staffs = Staff.query.filter_by(departmentID=departments.departmentID,now=True).all() posts = Post.query.all() return render_template('staff.html',staffs=staffs, departments=departments, posts=posts,info=info) staffs = Staff.query.filter_by(now=True).all() departments = Department.query.all() posts = Post.query.all() info = 1 return render_template('staff.html', staffs=staffs, departments=departments, posts=posts,info=info) elif current_user.has_role('User'): staffs = Staff.query.filter_by(userID=current_user.userID).first() departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff.html', staffs=staffs, departments=departments, posts=posts) ``` 5.详细信息 ```python @app.route('/detailed_staff/') @login_required # 用于视图保护 def detailed_staff(userID): # 详细信息 staff = Staff.query.get_or_404(userID) department = Department.query.filter_by(departmentID=staff.departmentID).first() post = Post.query.filter_by(postID=staff.postID).first() return render_template('detailed_staff.html',staff=staff,department=department,post=post) ``` 6. 填写员工基本信息 ```python @app.route('/write_staff', methods=['GET', 'POST']) def write_staff(): # 填写员工基本信息 departments = Department.query.all() posts = Post.query.all() nations = Nation.query.all() if request.method == 'POST': if not current_user.is_authenticated: return redirect(url_for('index')) else: username = request.form.get('username') sex = request.form.get('sex') birthday = request.form.get('birthday') IDnumber = request.form.get('IDnumber') departmentID = request.form.get('departmentID') postID = request.form.get('postID') hiredate = request.form.get('hiredate') takejobdate = request.form.get('takejobdate') employment_form = request.form.get('employment_form') personnel_source = request.form.get('personnel_source') phone = request.form.get('phone') email = request.form.get('email') politics_status = request.form.get('politics_status') nation = request.form.get('nation') native_place = request.form.get('native_place') height = request.form.get('height') if height == '': height = None bloodtype = request.form.get('bloodtype') marriage = request.form.get('marriage') birthplace = request.form.get('birthplace') domicileplace = request.form.get('domicileplace') highest_degree = request.form.get('highest_degree') highest_offering = request.form.get('highest_offering') graduate_school = request.form.get('graduate_school') major = request.form.get('major') graduation_date = request.form.get('graduation_date') if graduation_date == '': graduation_date = None startdate = request.form.get('startdate') enddate = request.form.get('enddate') staff_phone = Staff.query.filter_by(phone=phone).first() if staff_phone is not None: flash('电话号码已存在') return redirect(url_for('write_staff')) staff_email = Staff.query.filter_by(email=email).first() if staff_email is not None: flash('邮箱已存在') return redirect(url_for('write_staff')) print("输入") staff = Staff(username=username, sex=sex, birthday=birthday, IDnumber=IDnumber, departmentID=departmentID, postID=postID, hiredate=hiredate, takejobdate=takejobdate, employment_form=employment_form, personnel_source=personnel_source, phone=phone, email=email, now=True, politics_status=politics_status,nation=nation,native_place=native_place, height=height,bloodtype=bloodtype,marriage=marriage,birthplace=birthplace, domicileplace=domicileplace,highest_degree=highest_degree, highest_offering=highest_offering,graduate_school=graduate_school, major=major,graduation_date=graduation_date) db.session.add(staff) db.session.commit() user = Staff.query.filter_by(IDnumber = IDnumber).first() staff_probation = Staff_probation(userID = user.userID,startdate=startdate,enddate=enddate) db.session.add(staff_probation) db.session.commit() flash('添加成功') return redirect(url_for('staff')) return render_template('write_staff.html', departments=departments, posts=posts,nations=nations) ``` 7. 修改员工基本信息 ```python @app.route('/change_staff/',methods=['GET','POST']) @login_required def change_staff(userID): # 修改员工基本信息 staff = Staff.query.get_or_404(userID) departments = Department.query.all() posts = Post.query.all() nations = Nation.query.all() if request.method == 'POST': username = request.form.get('username') departmentID = request.form.get('departmentID') postID = request.form.get('postID') employment_form = request.form.get('employment_form') phone = request.form.get('phone') email = request.form.get('email') politics_status = request.form.get('politics_status') nation = request.form.get('nation') native_place = request.form.get('native_place') height = request.form.get('height') if height == '': height = None bloodtype = request.form.get('bloodtype') marriage = request.form.get('marriage') birthplace = request.form.get('birthplace') domicileplace = request.form.get('domicileplace') highest_degree = request.form.get('highest_degree') highest_offering = request.form.get('highest_offering') graduate_school = request.form.get('graduate_school') major = request.form.get('major') graduation_date = request.form.get('graduation_date') if graduation_date == '': graduation_date = None staff_phone = Staff.query.filter_by(phone=phone).first() if staff_phone is not None: if staff_phone.userID != userID: flash('电话号码已存在') return redirect(url_for('change_staff',userID=userID)) staff_email = Staff.query.filter_by(email=email).first() if staff_email is not None: if staff_email.userID != userID: flash('邮箱已存在') return redirect(url_for('change_staff',userID=userID)) staff.username=username staff.departmentID = departmentID staff.postID = postID staff.employment_form=employment_form staff.politics_status=politics_status staff.nation=nation staff.native_place=native_place staff.phone=phone staff.email=email staff.height=height staff.bloodtype=bloodtype staff.marriage=marriage staff.birthplace=birthplace staff.domicileplae=domicileplace staff.highest_degree =highest_degree staff.highest_offering=highest_offering staff.graduate_school=graduate_school staff.major=major staff.graduation_date=graduation_date db.session.commit() flash('更新成功') return redirect(url_for('staff')) return render_template('change_staff.html',staff=staff,departments=departments,posts=posts,nations=nations) ``` 8. 填写员工离职信息 ```python @app.route('/write_staff_dimission/',methods=['GET','POST']) @login_required def write_staff_dimission(userID): # 填写员工离职信息 staff = Staff.query.get_or_404(userID) if request.method == 'POST': userID=userID termdate = request.form.get('termdate') dimission_type = request.form.get('dimission_type') leave_to = request.form.get('leave_to') talent_pool = request.form.get('talent_pool') remark = request.form.get('remark') staff_dimission = Staff_dimission(userID=userID,termdate=termdate,dimission_type=dimission_type, leave_to=leave_to,talent_pool=talent_pool,remark=remark) db.session.add(staff_dimission) staff.now = False db.session.commit() flash('修改成功') return redirect(url_for('staff')) return render_template('/write_staff_dimission.html',staff=staff) ``` 9. 查看员工离职信息 ```python @app.route('/staff_dimission',methods=['GET','POST']) @login_required def staff_dimission(): # 查看员工离职信息 if current_user.has_role('Admin') or current_user.has_role('Root'): if request.method == 'POST': info = request.form.get('info') date = request.form.get('date') if info == 'userID': staffs = Staff.query.filter_by(userID=date,now = False).first() if staffs is None: flash('员工号不存在或员工未离职') return redirect(url_for('staff_dimission')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() staff_dimissions = Staff_dimission.query.filter_by(userID=date).first() return render_template('staff_dimission.html',staff_dimissions=staff_dimissions,staffs=staffs, departments=departments, posts=posts,info=info) if info == 'username': staffs = Staff.query.filter_by(username=date,now =False).first() if staffs is None: flash('员工不存在或员工未离职') return redirect(url_for('staff_dimission')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() staff_dimissions = Staff_dimission.query.filter_by(userID=staffs.userID).first() return render_template('staff_dimission.html',staff_dimissions=staff_dimissions,staffs=staffs, departments=departments, posts=posts,info=info) if info == 'departmentname': departments = Department.query.filter_by(departmentname=date).first() if departments is None: flash('部门不存在') return redirect(url_for('staff_dimission')) staffs = Staff.query.filter_by(departmentID=departments.departmentID,now=False).all() if staffs is None: flash('该部门不存在离职员工') return redirect(url_for('staff_dimission')) posts = Post.query.all() staff_dimissions=Staff_dimission.query.all() return render_template('staff_dimission.html',staff_dimissions=staff_dimissions,staffs=staffs, departments=departments, posts=posts,info=info) if info == 'postname': posts = Post.query.filter_by(postname=date).first() if posts is None: flash('岗位不存在') return redirect(url_for('staff_dimission')) staffs = Staff.query.filter_by(postID = posts.postID,now = False).all() if staffs is None: flash('该岗位不存在离职员工') return redirect(url_for('staff_dimission')) departments = Department.query.all() staff_dimissions = Staff_dimission.query.all() return render_template('staff_dimission.html', staff_dimissions=staff_dimissions, staffs=staffs, departments=departments, posts=posts, info=info) if info == 'dimission_type': staff_dimissions = Staff_dimission.query.filter(Staff_dimission.dimission_type.like('%{date}%'.format(date=date))).all() if staff_dimissions is None: flash('该类型没有离职员工') return redirect(url_for('staff_dimission')) staffs = Staff.query.filter_by(now=False).all() departments = Department.query.all() posts = Post.query.all() return render_template('staff_dimission.html', staff_dimissions=staff_dimissions, staffs=staffs, departments=departments, posts=posts, info=info) if info == 'termdate': staff_dimissions = Staff_dimission.query.filter(Staff_dimission.termdate <= date).all() if staff_dimissions is None: flash('该时间之前没有离职员工') return redirect(url_for('staff_dimission')) staffs = Staff.query.filter_by(now=False).all() departments = Department.query.all() posts = Post.query.all() return render_template('staff_dimission.html', staff_dimissions=staff_dimissions, staffs=staffs, departments=departments, posts=posts, info=info) staff_dimissions =Staff_dimission.query.all() staffs = Staff.query.filter_by(now = False).all() departments = Department.query.all() posts = Post.query.all() info = 1 return render_template('/staff_dimission.html',staff_dimissions=staff_dimissions,staffs=staffs,departments=departments,posts=posts,info=info) ``` 10. 修改员工离职信息 ```python @app.route('/change_staff_dimission/',methods=['GET','POST']) @login_required def change_staff_dimission(userID): # 修改员工离职信息 staff = Staff.query.get_or_404(userID) staff_dimission = Staff_dimission.query.filter_by(userID=userID).first() if staff_dimission is None: flash('员工未离职') return redirect(url_for('staff_dimission')) if request.method == 'POST': dimission_type = request.form.get('dimission_type') leave_to = request.form.get('leave_to') remark = request.form.get('remark') staff_dimission.dimission_type = dimission_type staff_dimission.leave_to=leave_to staff_dimission.remark = remark db.session.commit() flash('更新成功') return redirect(url_for('staff_dimission')) return render_template('change_staff_dimission.html',staff=staff,staff_dimission=staff_dimission) ``` 11. 升为管理员 ```python @app.route('/promote_role/') @login_required def promote_role(userID): # 升为管理员 user = User.query.filter_by(userID=userID).first() adminrole = user_datastore.find_role('Admin') userrole = user_datastore.find_role('User') user_datastore.remove_role_from_user(user,userrole) user_datastore.add_role_to_user(user, adminrole) db.session.commit() return redirect(url_for('user_role')) ``` 12. 降为普通用户 ```python @app.route('/reduce_role/') @login_required def reduce_role(userID): # 降为普通用户 user = User.query.filter_by(userID=userID).first() adminrole = user_datastore.find_role('Admin') userrole = user_datastore.find_role('User') user_datastore.remove_role_from_user(user,adminrole) user_datastore.add_role_to_user(user, userrole) db.session.commit() return redirect(url_for('user_role')) ``` 13. 员工信息中心主界面 ```python @app.route('/staff_information',methods=['GET','POST']) @login_required def staff_information(): # 员工信息中心主界面 if current_user.has_role('Admin') or current_user.has_role('Root'): if request.method == 'POST': info = request.form.get('info') date = request.form.get('date') if info == 'userID': staffs = Staff.query.filter_by(userID=date).first() if staffs is None: flash('员工号不存在') return redirect(url_for('staff_information')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff_information.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'username': staffs = Staff.query.filter_by(username=date).first() if staffs is None: flash('员工不存在') return redirect(url_for('staff_information')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff_information.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'IDnumber': staffs = Staff.query.filter_by(IDnumber=date).first() if staffs is None: flash('员工不存在') return redirect(url_for('staff_information')) departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff_information.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'departmentID': staffs = Staff.query.filter_by(departmentID=date).all() if staffs is None: flash('部门号不存在') return redirect(url_for('staff_information')) departments = Department.query.filter_by(departmentID=date).first() if departments is None: flash('部门号不存在') return redirect(url_for('staff_information')) posts = Post.query.all() return render_template('staff_information.html',staffs=staffs, departments=departments, posts=posts,info=info) if info == 'departmentname': departments = Department.query.filter_by(departmentname=date).first() if departments is None: flash('部门不存在') return redirect(url_for('staff_information')) staffs = Staff.query.filter_by(departmentID=departments.departmentID).all() posts = Post.query.all() return render_template('staff_information.html',staffs=staffs, departments=departments, posts=posts,info=info) staffs=Staff.query.filter_by(now = True).all() departments = Department.query.all() posts = Post.query.all() info = 1 return render_template('staff_information.html',staffs=staffs,departments=departments,posts=posts,info=info) elif current_user.has_role('User'): staffs = Staff.query.filter_by(userID=current_user.userID).first() departments = Department.query.filter_by(departmentID=staffs.departmentID).first() posts = Post.query.filter_by(postID=staffs.postID).first() return render_template('staff_information.html', staffs=staffs, departments=departments, posts=posts) ``` 14. 新聘员工报表 ```python #新聘员工报表 @app.route('/report_form_newstaff/', methods=['GET','POST']) @login_required def report_form_newstaff(departmentID): # 获取当前时间 now_time = datetime.datetime.now() # 格式化时间字符串 begin_date = now_time.strftime("%Y-%m-1") end_date = now_time.strftime("%Y-%m-%d") if departmentID == 0 : department = Department.query.all() posts = Post.query.all() staffs = Staff.query.filter(Staff.hiredate >= begin_date).filter(Staff.hiredate <= end_date).filter_by(now=True).all() if request.method == 'POST': begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') staffs = Staff.query.filter(Staff.hiredate >= begin_date).filter(Staff.hiredate <= end_date).filter_by( now=True).all() return render_template('report_form_newstaff.html',posts=posts,departmentID=departmentID, departments=department, staffs=staffs) return render_template('report_form_newstaff.html',posts=posts,departmentID=departmentID, departments=department, staffs=staffs) else: department = Department.query.filter_by(departmentID=departmentID).all() staffs = Staff.query.filter(Staff.hiredate >= begin_date).filter(Staff.hiredate <= end_date).filter_by(departmentID=departmentID,now=True).all() posts = Post.query.all() begin_date = 0 end_date = 0 if request.method == 'POST': begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') staffs = Staff.query.filter(Staff.hiredate >= begin_date).filter(Staff.hiredate <= end_date).filter_by(now=True,departmentID=departmentID).all() return render_template('report_form_newstaff.html',posts=posts,departmentID=departmentID, departments=department, staffs=staffs) return render_template('report_form_newstaff.html',posts=posts,departmentID=departmentID, departments=department, staffs=staffs) ``` 15. 离职员工报表 ```python #离职员工报表 @app.route('/report_form_dimission/', methods=['GET','POST']) @login_required def report_form_dimission(departmentID): # 获取当前时间 now_time = datetime.datetime.now() # 格式化时间字符串 begin_date = now_time.strftime("%Y-%m-1") end_date = now_time.strftime("%Y-%m-%d") if departmentID == 0 : staff_dimissions = Staff_dimission.query.filter(Staff_dimission.termdate >= begin_date).filter(Staff_dimission.termdate <= end_date).all() department = Department.query.all() posts = Post.query.all() staffs = Staff.query.filter_by(now=False).all() if request.method == 'POST': begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') staff_dimissions = Staff_dimission.query.filter(Staff_dimission.termdate >= begin_date).filter(Staff_dimission.termdate <= end_date).all() return render_template('report_form_dimission.html',staff_dimissions=staff_dimissions,posts=posts,departmentID=departmentID, departments=department, staffs=staffs) return render_template('report_form_dimission.html',staff_dimissions=staff_dimissions,posts=posts,departmentID=departmentID, departments=department, staffs=staffs) else: staff_dimissions = Staff_dimission.query.filter(Staff_dimission.termdate >= begin_date).filter(Staff_dimission.termdate <= end_date).all() department = Department.query.filter_by(departmentID=departmentID).all() staffs = Staff.query.filter_by(departmentID=departmentID,now=False).all() posts = Post.query.all() if request.method == 'POST': begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') staff_dimissions = Staff_dimission.query.filter(Staff_dimission.termdate >= begin_date).filter(Staff_dimission.termdate <= end_date).all() return render_template('report_form_dimission.html',staff_dimissions=staff_dimissions,posts=posts,departmentID=departmentID, departments=department, staffs=staffs) return render_template('report_form_dimission.html',staff_dimissions=staff_dimissions,posts=posts,departmentID=departmentID, departments=department, staffs=staffs) ``` 16. 人月报表 ```python #人月报表 @app.route('/report_form_personnnel_monthly', methods=['GET','POST']) @login_required def report_form_personnnel_monthly(): # 获取当前时间 now_time = datetime.datetime.now() # 格式化时间字符串 begin_date = now_time.strftime("%Y-%m-1") end_date = now_time.strftime("%Y-%m-%d") report_form_monthly=[] departments = Department.query.all() for department in departments: end_person = Staff.query.filter_by(departmentID=department.departmentID,now=True).all() leave_persons = Staff.query.filter_by(departmentID=department.departmentID,now=False).all() dimission_persons = Staff_dimission.query.filter(Staff_dimission.termdate >= begin_date).filter(Staff_dimission.termdate <= end_date).all() new_person = Staff.query.filter(Staff.hiredate >= begin_date).filter(Staff.hiredate <= end_date).filter_by( now=True,departmentID=department.departmentID).all() postgraduate = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='研究生',now=True).all() undergraduate = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='本科', now=True).all() junior_college = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='大专', now=True).all() high_school = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='高中及以下', now=True).all() dimission_person_number = 0 for dimission_person in dimission_persons: for leave_person in leave_persons: if leave_person.userID == dimission_person.userID: dimission_person_number +=1 begin_person = len(end_person)+dimission_person_number end_person = len(end_person) new_person = len(new_person) postgraduate = len(postgraduate) undergraduate = len(undergraduate) junior_college = len(junior_college) high_school = len(high_school) personnnel_monthly = { 'department':department.departmentname, 'begin_persons':begin_person, 'end_persons':end_person, 'new_persons':new_person, 'dimission_persons':dimission_person_number, 'postgraduates':postgraduate, 'undergraduates':undergraduate, 'junior_colleges':junior_college, 'high_schools':high_school } report_form_personnnel={} report_form_personnnel=personnnel_monthly report_form_monthly.append(report_form_personnnel) print(report_form_monthly) if request.method == 'POST': begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') report_form_monthly = [] departments = Department.query.all() for department in departments: end_person = Staff.query.filter_by(departmentID=department.departmentID, now=True).all() leave_persons = Staff.query.filter_by(departmentID=department.departmentID, now=False).all() dimission_persons = Staff_dimission.query.filter(Staff_dimission.termdate >= begin_date).filter( Staff_dimission.termdate <= end_date).all() new_person = Staff.query.filter(Staff.hiredate >= begin_date).filter(Staff.hiredate <= end_date).filter_by( now=True,departmentID=department.departmentID).all() postgraduate = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='研究生', now=True).all() undergraduate = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='本科', now=True).all() junior_college = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='大专', now=True).all() high_school = Staff.query.filter_by(departmentID=department.departmentID,highest_degree='高中及以下', now=True).all() dimission_person_number = 0 for dimission_person in dimission_persons: for leave_person in leave_persons: if leave_person.userID == dimission_person.userID: dimission_person_number += 1 begin_person = len(end_person) + dimission_person_number end_person = len(end_person) new_person = len(new_person) postgraduate = len(postgraduate) undergraduate = len(undergraduate) junior_college = len(junior_college) high_school = len(high_school) personnnel_monthly = { 'department': department.departmentname, 'begin_persons': begin_person, 'end_persons': end_person, 'new_persons': new_person, 'dimission_persons': dimission_person_number, 'postgraduates': postgraduate, 'undergraduates': undergraduate, 'junior_colleges': junior_college, 'high_schools': high_school } report_form_personnnel = {} report_form_personnnel = personnnel_monthly report_form_monthly.append(report_form_personnnel) return render_template('report_form_personnnel_monthly.html',report_form_monthlys=report_form_monthly,begin_date=begin_date,end_date=end_date) return render_template('report_form_personnnel_monthly.html',report_form_monthlys=report_form_monthly,begin_date=begin_date,end_date=end_date) ``` ### 五、数据库视图 ![输入图片说明](https://images.gitee.com/uploads/images/2020/0701/144944_02943d82_7692930.jpeg "16A05A2FD30F0450E49640354E18770C.jpg") ### 六、编码结构和项目编码模块说明 程序前端主要包括9大功能 1. 登录注册界面,用户通过这个界面注册,并登录系统,登录需要正确的密码 2. 首页,这个界面有所有功能页面的按钮,点击不同的按钮就会进入相应的功能界面,界面主要包括两部分,偏左侧是功能按钮,其余为功能操作界面 3. 部门管理,页面显示了所有部门,并且每个部门均有删除修改功能,还有添加部门和查询(通过编号,名称,电话等)部门的功能 4. 岗位管理,页面显示了所有岗位,并且每个岗位均有删除修改功能,还有添加岗位和查询(通过编号,名称,电话等)岗位的功能 5. 员工入职信息,显示了所有的员工基本信息,每个员工均有修改,离职,和详情(查看员工的详细信息)按钮,也有添加和查询按钮 6. 员工信息中心,可以查看每个员工的生涯信息,外语水平,家庭成员及社会关系,还有查询按钮,用来搜索员工,方便快速找到员工 7. 部门报表管理,显示的是每个部门的新聘员工报表和离职员工报表,可以通过查询按钮来,查自己想要看的部门 8. 离职员工管理,显示的是所有离职员工的信息,可以查询员工,和修改他的离职信息 9. 试用期管理,显示的是试用期员工的信息,每个员工有个填写功能,填写的是公司对于这个员工的去留决定