/// <summary> /// 序列化用户对象 /// </summary> /// <param name="reader"></param> /// <returns></returns> private static User SerializeToUser(MySqlDataReader reader) { #region 用户基本信息 var user = new User(); user.Id = EvaluationHelper.ObjectToInt(reader["UserID"]); user.Name = reader["UserName"].ToString(); user.Password = reader["Password"].ToString(); user.DisplayName = reader["DisplayName"].ToString(); user.RegTime = EvaluationHelper.ObjectToDateTime(reader["RegTime"]); user.UserType = EvaluationHelper.ObjectToInt(reader["UserType"]); user.IsLogin = EvaluationHelper.ObjectToInt2(reader["IsLogin"]); user.LoginTime = EvaluationHelper.ObjectToDateTime(reader["LoginTime"]); user.Avater = null; if (reader["Avatar"] != DBNull.Value) { try { BitmapImage img = ImageHelper.Bytearraytobitmapimage(reader["Avatar"] as byte[]); img.Freeze(); //冻结bitmap,使其可以跨线程访问 user.Avater = img; } catch (Exception ex) { LogHelper.MainLog( string.Format("Convert To BitmapImage produce error!UserName={0},ex={1}", user.Name, ex)); } } //角色 user.UserRole.RoleId = EvaluationHelper.ObjectToInt(reader["RoleID"]); user.UserRole.RoleName = reader["GroupName"].ToString(); #region 用户对应的员工信息 var us = new Staff(); us.Department = reader["Department"].ToString(); us.EmployeeNumber = reader["StaffNO"].ToString(); us.Id = EvaluationHelper.ObjectToInt(string.IsNullOrEmpty(reader["StaffID"].ToString()) ? "0" : reader["StaffID"]); us.Name = reader["StaffName"].ToString(); us.Phone = reader["Telephone"].ToString(); us.Sex = EvaluationHelper.ObjectToInt2(string.IsNullOrEmpty(reader["Sex"].ToString()) ? "false" : reader["Sex"]); //职位 us.StaffPosition.PositionId = EvaluationHelper.ObjectToInt(string.IsNullOrEmpty(reader["PositionID"].ToString()) ? "0" : reader["PositionID"]); us.StaffPosition.PositionName = reader["PositionName"].ToString(); #endregion user.UserStaff = us; //用户对应的员工信息对象赋值 #endregion return(user); }
/// <summary> /// 通过id获取员工 /// </summary> /// <returns></returns> public static Staff GetStaveById(int id) { string sqlStr = "Select * from ipvt_staffmessagetable where StaffID=?id"; var param = new MySqlParameter[1]; param[0] = new MySqlParameter("?id", id); MySqlDataReader reader = null; try { reader = CustomMySqlHelper.ExecuteDataReader(sqlStr, param); //执行SQL while (reader.Read()) { var staff = new Staff(); staff.Id = EvaluationHelper.ObjectToInt(reader["StaffID"]); staff.Name = reader["StaffName"].ToString(); staff.Sex = EvaluationHelper.ObjectToInt2(reader["Sex"]); staff.EmployeeNumber = reader["StaffNO"].ToString(); staff.Phone = reader["Telephone"].ToString(); staff.Department = reader["Department"].ToString(); // staff.StaffPosition.PositionId = EvaluationHelper.ObjectToInt(reader["PositionID"]); if (staff.StaffPosition.PositionId != 0) //如果职位id存在,尝试获取职位名称 { staff.StaffPosition.PositionName = GetPositionNameById(staff.StaffPosition.PositionId); } return(staff); } } catch (Exception ex) { LogHelper.MainLog("error in UserManager.GetStaveById(int id)!" + ex.Message); } finally { if (reader != null) { reader.Close(); //读取完关闭reader对象 } } return(new Staff()); }
/// <summary> /// 获取员工 /// </summary> /// <returns></returns> public static void GetStaves(out List <Staff> list) { list = new List <Staff>(); string sqlStr = "Select * from ipvt_staffmessagetable"; MySqlDataReader reader = null; try { reader = CustomMySqlHelper.ExecuteDataReader(sqlStr); //执行SQL while (reader.Read()) { var staff = new Staff(); staff.Id = EvaluationHelper.ObjectToInt(reader["StaffID"]); staff.Name = reader["StaffName"].ToString(); staff.Sex = EvaluationHelper.ObjectToInt2(reader["Sex"]); staff.EmployeeNumber = reader["StaffNO"].ToString(); staff.Phone = reader["Telephone"].ToString(); staff.Department = reader["Department"].ToString(); // staff.StaffPosition.PositionId = EvaluationHelper.ObjectToInt(reader["PositionID"]); if (staff.StaffPosition.PositionId != 0) //如果职位id存在,尝试获取职位名称 { staff.StaffPosition.PositionName = GetPositionNameById(staff.StaffPosition.PositionId); } list.Add(staff); } } catch (Exception ex) { LogHelper.MainLog("error in UserManager.GetStaves()!" + ex.Message); } finally { if (reader != null) { reader.Close(); //读取完关闭reader对象 } } }