def update_department(self, dpt_id, dpt_name, p_id): """ 修改部门 :param dpt_id: 部门ID :param dpt_name: 部门名称 :return: """ db_conn = MysqlConn() try: db_name = configuration.get_database_name() table_name = 'department' filed = 'dpt_name' condition = "dpt_id <> %s and dpt_name = '%s'" % (dpt_id, dpt_name) old_sql = DbBase.create_select_sql(self, db_name, table_name, filed, condition) old_pla_name_data = DbBase.execute_fetch_one( self, db_conn, old_sql) if old_pla_name_data: return response_code.RECORD_EXIST else: fields = ['dpt_id', 'dpt_name', 'p_id'] pla_data = [dpt_id, dpt_name, p_id] update_condition = 'dpt_id = %s' % str(dpt_id) update_sql = DbBase.create_update_sql(self, db_name, table_name, fields, pla_data, update_condition) DbBase.execute_update_sql(self, db_conn, update_sql) return response_code.SUCCESS except Exception as e: lg.error(e) return response_code.UPDATE_DATA_FAIL finally: db_conn.close()
def add_department(self, dpt_name, dpt_p_id): """ 添加部门 :param dpt_name: 部门名称 :param dpt_p_id: 上级部门id :return: """ db_conn = MysqlConn() try: db_name = configuration.get_database_name() table_name = 'department' filed = 'dpt_name' condition = "dpt_name = '%s'" % dpt_name system_name_sql = DbBase.create_select_sql(self, db_name, table_name, filed, condition) res = DbBase.execute_fetch_one(self, db_conn, system_name_sql) if res: return response_code.RECORD_EXIST else: fields = '(dpt_name, p_id)' pla_data = (dpt_name, dpt_p_id) sql = DbBase.create_insert_sql(self, db_name, table_name, fields, pla_data) DbBase.execute_sql_return_count(self, db_conn, sql) return response_code.SUCCESS except Exception as e: lg.error(e) return response_code.ADD_DATA_FAIL finally: db_conn.close()
def update_department_staff(self, dpt_id, user_ids): """ 给部门下面添加员工 :param dpt_id: 部门ID :param user_ids: 员工ID :return: """ db_conn = MysqlConn() try: db_name = configuration.get_database_name() table_name = 'users_department' filed = 'dpt_id' condition = "dpt_id = %s " % dpt_id old_sql = self.create_select_sql(db_name, table_name, filed, condition) # 获取用户和部门管理表的数据 old_dpt_data = self.execute_fetch_one(db_conn, old_sql) if old_dpt_data: condition = "dpt_id=%s" % old_dpt_data.get('dpt_id') del_association_sql = self.create_delete_sql(db_name, table_name, condition) # 删除旧的用户和部门关联表的数据 self.execute_del_data(db_conn, del_association_sql) # 添加新的员工 for user_id in eval(user_ids): fields = '(dpt_id,user_id)' insert_data = (dpt_id, user_id) sql = DbBase.create_insert_sql(self, db_name, table_name, fields, insert_data) DbBase.execute_sql_return_count(self, db_conn, sql) return response_code.SUCCESS except Exception as e: lg.error(e) return response_code.ADD_DATA_FAIL finally: db_conn.close()
def get_role_permission_info(self, role_id): """ 根据角色ID获取权限列表 :param role_id: 角色ID :return: """ db_conn = MysqlConn() try: db_name = configuration.get_database_name() table_name = 'roles_permission' condition = 'role_id=%s' % role_id # 获取权限ID sql = DbBase.create_select_sql(self, db_name, table_name, 'permission_id', condition) result = DbBase.execute_fetch_all(self, db_conn, sql) permission_ids = [ permission.get('permission_id') for permission in result ] if len(permission_ids) != 0: # 获取权限的父ID permission_table = 'permission' if len(permission_ids) == 1: condition = 'id = %s' % permission_ids else: condition = 'id in %s' % str(tuple(permission_ids)) per_p_sql = DbBase.create_select_sql(self, db_name, permission_table, 'p_id', condition) result = DbBase.execute_fetch_all(self, db_conn, per_p_sql) permission_p_ids = [ permission.get('p_id') for permission in result ] # 获取权限ID和权限父ID 的差集给前端显示 result = list( set(permission_ids).difference(set(permission_p_ids))) data = response_code.SUCCESS data['data'] = result return data except Exception as e: lg.error(e) return response_code.GET_DATA_FAIL finally: db_conn.close()
def add_operation_log(self, user_key, ip_address, level, description): """ 添加操作日志 :param user_key: :param ip_address: :param level: :param description: :return: """ db_conn = MysqlConn() time_create = get_system_datetime() db_name = configuration.get_database_name() table_name = 'system_oper_log' fields = '(user_key,ip_address,level,description,time_create)' pla_data = (user_key, ip_address, level, description, time_create) sql = DbBase.create_insert_sql(self, db_name, table_name, fields, pla_data) DbBase.execute_sql_return_count(self, db_conn, sql) db_conn.close()
def add_role_permission(self, role_id, per_keys): """ 给角色添加权限 :param role_id: 角色id :param per_keys: 权限列表 :return: """ db_conn = MysqlConn() try: db_name = configuration.get_database_name() table_name = 'roles_permission' filed = 'role_id' condition = " role_id = '%s'" % role_id # 删除旧的权限 old_role_sql = DbBase.create_select_sql(self, db_name, table_name, filed, condition) old_role_id_data = DbBase.execute_fetch_one( self, db_conn, old_role_sql) if old_role_id_data: condition = "role_id=%s" % old_role_id_data.get('role_id') del_association_sql = self.create_delete_sql( db_name, table_name, condition) # 删除用户和部门关联表的数据 self.execute_del_data(db_conn, del_association_sql) # 添加新的权限 for per_key in eval(per_keys): fields = '(role_id, permission_id)' pla_data = (role_id, per_key) sql = DbBase.create_insert_sql(self, db_name, table_name, fields, pla_data) DbBase.execute_sql_return_count(self, db_conn, sql) return response_code.SUCCESS except Exception as e: lg.error(e) return response_code.ADD_DATA_FAIL finally: db_conn.close()