using MySql.Data.MySqlClient; using System; using System.Collections.ObjectModel; using System.Data; using XCommon.Log; using XCommon.MySql; using DataRWDAL.Base; using System.Collections.Generic; using DataEntity.User; using System.Web.UI.WebControls; using SqlSugar; using XHandler.Class; using System.Linq; using DataRWDAL; using DataEntity.UserManager; namespace XImagingXhandler.XDAL { /// /// 用户表读写操作 /// public class UserDB:BaseDB { #region 检查用户、密码是否存在 /// /// 检查用户、密码是否存在 /// /// /// /// public static bool isExistUserByUserInfo(string username, string password = "") { bool isExistUser = false; using (var db = GetInstance()) { if (password != "") { var query = db.Queryable().Where(it => it.status.Equals(1) && it.username.Equals(username) && it.userpwd.Equals(password)).ToList(); if (query != null && query.Count > 0) { isExistUser = true; } else { isExistUser = false; } } else { var query = db.Queryable().Where(it => it.status.Equals(1) && it.username.Equals(username)).ToList(); if (query != null && query.Count > 0) { isExistUser = true; } else { isExistUser = false; } } } return isExistUser; } #endregion #region 根据用户名返回用户信息 /// /// 根据用户名返回用户信息 /// /// 用户名 /// 用户信息 public static UserInfo GetUserInfByUserName(string username) { UserInfo user = null; string sqlstr = string.Empty; using (var db = GetInstance()) { var query = db.Queryable().Where(it => it.status.Equals(1) && it.username.Equals(username)).Single(); return query; } } #endregion #region 根据用户Id返回用户信息 /// /// 根据用户Id返回用户信息 /// /// 用户Id /// 用户信息 public static UserInfo GetUserInfByUserId(string userId) { UserInfo user = null; string sqlstr = string.Empty; using (var db = GetInstance()) { var query = db.Queryable().Where(it => it.status.Equals(1) && it.id.Equals(userId)).Single(); return query; } } #endregion #region 根据返回用户信息列表 /// /// 根据用户Id返回用户信息 /// /// 用户信息集合 public static List GetAllUserInfo() { List userList = null; string sqlstr = string.Empty; using (var db = GetInstance()) { //var query = db.Queryable().Where((it,im) =>it.authority==im.role_id && it.status.Equals(1)).ToList(); var query = db.Queryable("t_userinfo", "t").AddJoinInfo("t_roles", "l", "t.authority=l.Id", JoinType.Inner) .Select("t.*,l.name"); if(query!=null) { userList = new List(); foreach (var item in query.ToList()) { UserInfo userInfo = new UserInfo(); foreach (var property in (IDictionary)item) { //Console.WriteLine($"{property.Key}: {property.Value}"); //foreach (var property in item.GetType().GetProperties()) //{ var propertyName = property.Key.ToString();//property.Name.ToString(); var propertyValue = property.Value==null?"": property.Value.ToString(); if (propertyName == "Id") { userInfo.id = propertyValue.ToString(); continue; } if (propertyName == "authority") { userInfo.authority = propertyValue.ToString(); continue; } if (propertyName == "status") { userInfo.status = Convert.ToInt32(propertyValue.Trim() != "" ? propertyValue : "0"); continue; } if (propertyName == "username") { userInfo.username = propertyValue.ToString(); continue; } if (propertyName == "userpwd") { userInfo.userpwd = propertyValue.ToString(); continue; } if (propertyName == "truename") { userInfo.truename = propertyValue.ToString(); continue; } if (propertyName == "loginstate") { userInfo.loginstate = Convert.ToInt32(propertyValue.Trim()!=""? propertyValue:"0"); continue; } if (propertyName == "logintime") { userInfo.logintime = Convert.ToDateTime(propertyValue.Trim() != "" ? propertyValue : "2022-10-09"); continue; } if (propertyName == "name") { userInfo.authorityname = propertyValue.ToString(); continue; } if (propertyName == "department") { userInfo.department = propertyValue.ToString(); continue; } if (propertyName == "phone") { userInfo.phone = propertyValue.ToString(); continue; } if (propertyName == "email") { userInfo.email = propertyValue.ToString(); continue; } if (propertyName == "EffectiveDate") { userInfo.EffectiveDate = Convert.ToDateTime(propertyValue.Trim() != "" ? propertyValue: "2022-10-09"); continue; } if (propertyName == "pcmac") { userInfo.pcmac = propertyValue.ToString(); continue; } if (propertyName == "pcip") { userInfo.pcip = propertyValue.ToString(); continue; } if (propertyName == "authorize") { userInfo.authorize = propertyValue.ToString(); continue; } if (propertyName == "Remark") { userInfo.Remark = propertyValue.ToString(); continue; } if (propertyName == "loginerrornum") { userInfo.loginerrornum = Convert.ToInt32(propertyValue.Trim() != "" ? propertyValue : "0"); continue; } if (propertyName == "LockTime") { userInfo.LockTime = Convert.ToDateTime(propertyValue.Trim() != "" ? propertyValue : "2022-10-09"); continue; } if (propertyName == "createname") { userInfo.createname = propertyValue.ToString(); continue; } if (propertyName == "createtime") { userInfo.createtime = Convert.ToDateTime(propertyValue.Trim() != "" ? propertyValue: "2022-10-09"); continue; } if (propertyName == "modifyname") { userInfo.modifyname = propertyValue.ToString(); continue; } if (propertyName == "modifytime") { userInfo.modifytime = Convert.ToDateTime(propertyValue.Trim() != "" ? propertyValue : "2022-10-09"); continue; } } userList.Add(userInfo); } } } return userList; } #endregion #region 根据返回角色信息列表 /// /// 根据返回角色信息列表 /// /// 角色信息集合 public static List GetRoles() { List userList = null; string sqlstr = string.Empty; using (var db = GetInstance()) { var query = db.Queryable().ToList(); userList = query; } return userList; } #endregion /// /// 获取所有权限列表 /// /// public static List GetAllRoles() { using (var db = GetInstance()) { List list = db.Queryable().ToList(); foreach (var role in list) { if (role.Type == "0") role.TypeName = "锁定"; else role.TypeName = "正常"; } return list; } } #region 修改一条新的角色数据 by 角色数据 /// /// 修改一条新的角色数据 by 角色数据 /// /// 角色数据 /// 1:成功;0:失败 public static int UpdateRoleIntodb(RoleTab roleTab) { using (var db = GetInstance()) { return db.Updateable(roleTab).ExecuteCommand(); } } #endregion #region 添加一条新的用户数据 by 用户数据 /// /// 添加一条新的用户数据 by 用户数据 /// /// 用户数据 /// 1:成功;0:失败 public static int AddUserIntodb(UserInfo userInfo) { using (var db = GetInstance()) { return db.Insertable(userInfo).ExecuteCommand(); } } #endregion #region 修改一条新的用户数据 by 用户数据 /// /// 修改一条新的用户数据 by 用户数据 /// /// 用户数据 /// 1:成功;0:失败 public static int UpdateUserIntodb(UserInfo userInfo) { using (var db = GetInstance()) { return db.Updateable(userInfo).ExecuteCommand(); } } #endregion #region 删除 /// /// 删除 /// /// /// public static bool DelUserById(string id) { #region AddLog #endregion using (var db = GetInstance()) { var query = GetUserInfByUserId(id); query.status = 0; var result = db.Updateable(query).ExecuteCommand(); if(result>0) { return true; } } return false; } #endregion public static List Search(string name, string phone, string state, string authority) { var result = GetAllUserInfo(); if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(phone) && string.IsNullOrEmpty(state) && string.IsNullOrEmpty(authority)) { if (result != null) { return result.ToList(); } } if (!string.IsNullOrEmpty(authority)) result = result.Where(s => s.authority == authority).ToList(); if (result == null) return null; if (!string.IsNullOrEmpty(state)) result = result.Where(s => s.status == Convert.ToInt32(state)).ToList(); if (result == null) return null; if (!string.IsNullOrEmpty(phone)) result = result.Where(s => s.phone != null && s.phone.Contains(phone)).ToList(); if (result == null) return null; if (!string.IsNullOrEmpty(name)) result = result.Where(s => s.username.Contains(name)).ToList(); if (result == null) return null; return result.ToList(); } #region 获取用户所在的角色权限菜单 /// /// 获取用户所在的角色权限菜单 /// /// 用户Id /// public static List GetRoleMenuByUserId(string userId) { roleAllMenus = new List(); string sqlText = "select r.Name,r.MenuId from t_userinfo t, t_roles r where t.authority=r.Id and t.Id='" + userId + "' and t.status=1"; try { using (var db = GetInstance()) { // 多表查询 var query = db.Queryable ( (sc, st) => new JoinQueryInfos(JoinType.Inner, st.authority == sc.role_id) ) .Where((sc, st) => st.id == userId && st.status == 1).Single(); if (query != null) { //string menuIdTxts = query.GetType().GetProperties()[].ToString(); string menuIdTxts = string.Empty; menuIdTxts = query.menu_id.ToString(); if (menuIdTxts != "") { string[] menuIdArray = menuIdTxts.Split(','); for (int i = 0; i < menuIdArray.Length; i++) { if (!menuIdArray[i].Equals("")) { RoleMenu roleMenu = new RoleMenu(); roleMenu = GetARoleRealMenu(menuIdArray[i], menuIdTxts); roleAllMenus.Add(roleMenu); } } //所有权限菜单序列化成Json文件 //string j = JsonConvert.SerializeObject(roleAllMenus); //resultRoleMenu = JObject.Parse(j); } } } //MySqlCommand sqlcom = new MySqlCommand(); //MySqlParameter[] commandParameters; //sqlcom.CommandText = sqlText; //commandParameters = new MySqlParameter[0]; //MySqlConnection mysqlcon = MySqlHelper.GetConnection(); //DataTable dt = MySqlHelper.GetDataTable(mysqlcon.ConnectionString, CommandType.Text, sqlcom.CommandText, commandParameters); //if (dt != null && dt.Rows.Count > 0) //{ // if (dt.Rows[0]["MenuId"] != null) // { // string menuIdTxts = dt.Rows[0]["MenuId"].ToString(); // if (menuIdTxts != "") // { // string[] menuIdArray = menuIdTxts.Split(','); // for (int i = 0; i < menuIdArray.Length; i++) // { // if (!menuIdArray[i].Equals("")) // { // RoleMenu roleMenu = new RoleMenu(); // roleMenu = GetARoleMenu(menuIdArray[i]); // roleAllMenus.Add(roleMenu); // } // } // //所有权限菜单序列化成Json文件 // //string j = JsonConvert.SerializeObject(roleAllMenus); // //resultRoleMenu = JObject.Parse(j); // } // } //} //else //{ // //isExistUser = false; //} } catch (MySqlException me) { LoggerHelper.ErrorLog("执行:" + sqlText, me); } return roleAllMenus; } #endregion #region 获取用户所在的角色权限的菜单加工完整 static List roleAllMenuLists = null; /// /// 获取用户所在的角色权限的菜单加工完整 /// /// 用户Id /// public static List GetWholeRoleMenuByUserId(string userId) { List roleMenuList = GetRoleMenuByUserId(userId);//当前角色的菜单 roleAllMenuLists = new List(); string sqlText = "select t.Id,t.Name,t.ChildrenId from t_menu t where t.Level=0 and t.state=1"; try { using (var db = GetInstance()) { var query = db.Queryable().Where(it => it.level.Equals(0)&&it.state.Equals(1)); if(query.Count() > 0) { for (int i = 0; i < query.Count(); i++) { RoleMenu roleMenu = new RoleMenu(); roleMenu = GetARoleMenu(query.ToList()[i].menu_id.ToString()); roleAllMenuLists.Add(roleMenu); } } } //清除不是该用户拥有的权限 if (roleAllMenuLists.Count > 0) { List mainMenu = new List();//未包含权限的主菜单 for (int i = 0; i < roleAllMenuLists.Count; i++) { int countSubMenu = 0; if (roleAllMenuLists[i].rolemenuchildren != null && roleAllMenuLists[i].rolemenuchildren.Count > 0) { int subMenusNum = roleAllMenuLists[i].rolemenuchildren.Count;//子菜单的总数 List subMenu = new List();//未包含权限的子菜单 for (int j = 0; j < subMenusNum; j++) { if (roleMenuList.Exists(x => x.rolemenuid.Equals(roleAllMenuLists[i].rolemenuchildren[j].rolemenuid))) { countSubMenu++; } else { var mainAMenu = roleMenuList.FirstOrDefault(x => x.rolemenuid.Equals(roleAllMenuLists[i].rolemenuid)); if(mainAMenu !=null&& mainAMenu.rolemenuchildren.Exists(x=>x.rolemenuid.Equals(roleAllMenuLists[i].rolemenuchildren[j].rolemenuid))) { if (roleAllMenuLists[i].rolemenuchildren[j].rolemenuChildIds != "") { for (int k = 0; k < mainAMenu.rolemenuchildren.Count; k++) { var subAMenu = mainAMenu.rolemenuchildren.FirstOrDefault(x => x.rolemenuid.Equals(roleAllMenuLists[i].rolemenuchildren[j].rolemenuid)); if (subAMenu != null) { if (subAMenu.rolemenuChildIds == "") { roleAllMenuLists[i].rolemenuchildren[j].rolemenuchildren.Clear(); } else { foreach (var m in roleAllMenuLists[i].rolemenuchildren[j].rolemenuchildren) { var q = subAMenu.rolemenuchildren.FirstOrDefault(x => x.rolemenuid.Equals(m.rolemenuid)); if (q != null) { } else { roleAllMenuLists[i].rolemenuchildren[j].rolemenuchildren.Remove(m); } } } } } } } else { int countofSub = 0; //查看看看是否包含子菜单 if(roleAllMenuLists[i].rolemenuchildren[j].rolemenuchildren!=null) { foreach(var m in roleAllMenuLists[i].rolemenuchildren[j].rolemenuchildren) { var n = roleMenuList.FirstOrDefault(x => x.rolemenuid.Equals(m.rolemenuid)); if(n!=null) { countofSub++; countSubMenu++; } } } if (countofSub > 0) { } else { subMenu.Add(roleAllMenuLists[i].rolemenuchildren[j]); } } } } for (int k = 0; k < subMenu.Count; k++) { roleAllMenuLists[i].rolemenuchildren.Remove(subMenu[k]); } } if (countSubMenu > 0) { continue; } else { if (roleMenuList.Exists(x => x.rolemenuid.Equals(roleAllMenuLists[i].rolemenuid))) { } else { mainMenu.Add(roleAllMenuLists[i]); } } } for (int k = 0; k < mainMenu.Count; k++) { roleAllMenuLists.Remove(mainMenu[k]); } } } catch (MySqlException me) { LoggerHelper.ErrorLog("执行:" + sqlText, me); } return roleAllMenuLists; } #endregion #region 根据菜单Id获取该菜单的实例对象 public static RoleMenu GetARoleMenu(string menuId) { RoleMenu roleMenu = new RoleMenu(); //string sqlText = "select t.Id,t.ChildrenId,t.Name,t.State=1 from t_menu t where t.Id='" + menuId + "'"; try { using (var db = GetInstance()) { var query = db.Queryable().Where(it => it.menu_id.Equals(menuId) ).Single(); if(query != null) { roleMenu.rolemenuid = query.menu_id.ToString(); roleMenu.rolemenuname = query.name.ToString(); roleMenu.rolemenuChildIds = query.children_id == null ? "" : query.children_id.ToString(); if (roleMenu.rolemenuChildIds.Length > 0) { roleMenu.rolemenuchildren = new List(); string[] childIds = roleMenu.rolemenuChildIds.Split(','); for (int i = 0; i < childIds.Length; i++) { RoleMenu childMenu = new RoleMenu(); childMenu = GetARoleMenu(childIds[i]); roleMenu.rolemenuchildren.Add(childMenu); } } else { roleMenu.rolemenuChildIds = ""; } } else { roleMenu = null; } } //MySqlCommand sqlcom = new MySqlCommand(); //MySqlParameter[] commandParameters; //sqlcom.CommandText = sqlText; //commandParameters = new MySqlParameter[0]; //MySqlConnection mysqlcon = MySqlHelper.GetConnection(); //DataTable dt = MySqlHelper.GetDataTable(mysqlcon.ConnectionString, CommandType.Text, sqlcom.CommandText, commandParameters); //if (dt != null && dt.Rows.Count > 0) //{ // if (dt.Rows[0]["Name"] != null) // { // roleMenu.rolemenuid = dt.Rows[0]["Id"].ToString(); // roleMenu.rolemenuname = dt.Rows[0]["Name"].ToString(); // roleMenu.rolemenuChildIds = dt.Rows[0]["ChildrenId"] == null ? "" : dt.Rows[0]["ChildrenId"].ToString(); // if (roleMenu.rolemenuChildIds.Length > 0) // { // roleMenu.rolemenuchildren = new List(); // string[] childIds = roleMenu.rolemenuChildIds.Split(','); // for (int i = 0; i < childIds.Length; i++) // { // RoleMenu childMenu = new RoleMenu(); // childMenu = GetARoleMenu(childIds[i]); // roleMenu.rolemenuchildren.Add(childMenu); // } // } // else // { // roleMenu.rolemenuChildIds = ""; // } // } // else // { // roleMenu = null; // } //} //else //{ // roleMenu = null; //} } catch (MySqlException me) { LoggerHelper.ErrorLog("执行:" , me); } return roleMenu; } #endregion #region 根据菜单Id获取该菜单的实际权限实例对象 public static RoleMenu GetARoleRealMenu(string menuId,string menuIdsWith) { RoleMenu roleMenu = new RoleMenu(); //string sqlText = "select t.Id,t.ChildrenId,t.Name,t.State=1 from t_menu t where t.Id='" + menuId + "'"; try { using (var db = GetInstance()) { var query = db.Queryable().Where(it => it.menu_id.Equals(menuId)).Single(); if (query != null) { roleMenu.rolemenuid = query.menu_id.ToString(); roleMenu.rolemenuname = query.name.ToString(); roleMenu.rolemenuChildIds = query.children_id == null ? "" : query.children_id.ToString(); if (roleMenu.rolemenuChildIds.Length > 0) { roleMenu.rolemenuchildren = new List(); string[] childIds = roleMenu.rolemenuChildIds.Split(','); for (int i = 0; i < childIds.Length; i++) { RoleMenu childMenu = new RoleMenu(); childMenu = GetARoleRealMenu(childIds[i],menuIdsWith); if(menuIdsWith.Contains(childMenu.rolemenuid)) { roleMenu.rolemenuchildren.Add(childMenu); } else { if(childMenu.rolemenuchildren!=null&&childMenu.rolemenuchildren.Count>0) { roleMenu.rolemenuchildren.Add(childMenu); } } } } else { roleMenu.rolemenuChildIds = ""; } } else { roleMenu = null; } } } catch (MySqlException me) { LoggerHelper.ErrorLog("执行:", me); } return roleMenu; } #endregion #region 根据角色Id获取该角色的实例对象 public static RoleTab GetARoleTab(string roleId) { RoleTab roleTab = null; try { using (var db = GetInstance()) { var query = db.Queryable().Where(it => it.role_id.Equals(roleId)).Single(); roleTab = query; } } catch (MySqlException me) { LoggerHelper.ErrorLog("执行:", me); } return roleTab; } #endregion #region 获取系统内所有菜单对象 public static List GetAllMenuTab() { List menuTabs = null; try { using (var db = GetInstance()) { var query = db.Queryable().ToList(); menuTabs = query; } } catch (MySqlException me) { LoggerHelper.ErrorLog("执行:", me); } return menuTabs; } #endregion #region 获取某个菜单下的子菜单数据 static List roleAllMenus = new List();//保存某个用户的所有菜单 public static List GetSubRolwMenus(string menuId) { List subRoleMenus = new List(); RoleMenu roleMenu = GetARoleMenu(menuId); if (roleMenu.rolemenuChildIds != "") { } return subRoleMenus; } #endregion } }