380 lines
16 KiB
C#
380 lines
16 KiB
C#
// Services.Manager.UserInfoServer
|
|
using Models;
|
|
using Models.ModelItems;
|
|
using Models.View;
|
|
using Services.Cache;
|
|
using Services.Extensions;
|
|
using Services.Manager;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using Newtonsoft.Json;
|
|
using System.Threading;
|
|
using UH_CENTER;
|
|
|
|
public class UserInfoServer
|
|
{
|
|
//普通用户信息
|
|
|
|
#region 返回用户信息
|
|
public static dynamic GetUserInfo( Func<dynamic,bool> func = null)
|
|
{
|
|
//取得普通用户信息
|
|
var res = CacheHelp.cacheSysUserInfo.OrderByDescending(u => u.CreateTime).Select((View_UserInfo u) =>
|
|
{
|
|
var hoteles = SyncHotelServer.FindUHotelInfo(u);
|
|
return new
|
|
{
|
|
Id = u.Id,
|
|
IsValid = u.IsValid,
|
|
Age = u.Age,
|
|
Uid = u.Uid,
|
|
sex = ((u.Sex == 0) ? "男" : ((u.Sex == 1) ? "女" : "未知")),
|
|
Sex = u.Sex,
|
|
OrganizationName = u.OrganizationName,
|
|
Authoncount = u.Authoncount,
|
|
isValid = (u.IsValid == 1) ? "冻结" : (u.IsValid >= 2 ? "管理" : "正常"),
|
|
u.HeadImg,
|
|
u.EndTime,
|
|
u.Desc,
|
|
u.CreateTime,
|
|
u.IsImport,
|
|
Gs = HotelServer.FindUserinfoGs(u),
|
|
HotelInfo = hoteles,
|
|
Hoteles = hoteles.Select(x=>x.id).ToArray()
|
|
};
|
|
}).Where(X=> func == null || func(X)).ToList();
|
|
return res;
|
|
}
|
|
#endregion
|
|
#region 冻结激活用户账号
|
|
public static bool EditIsValid(string uid, int isvalid, string CreatedBy)
|
|
{
|
|
try
|
|
{
|
|
//UserInfo userInfo = authorityDB.UserInfos.FirstOrDefault((UserInfo x) => x.Uid == uid);
|
|
UserInfo userInfo =SqlSugarBase.Db.Queryable<UserInfo>().First((UserInfo x) => x.Uid == uid);
|
|
if (CacheHelp.cacheSysUserInfo.FirstOrDefault(X => X.Uid == CreatedBy).IsValid <= userInfo.IsValid && CreatedBy.ToLower() != "adminblv")
|
|
return false;
|
|
if (userInfo == null)
|
|
{
|
|
throw new CustomException("未查找到" + uid + "!");
|
|
}
|
|
userInfo.IsValid = isvalid;
|
|
SqlSugarBase.Db.Updateable(userInfo).ExecuteCommand();
|
|
//authorityDB.SaveChanges();
|
|
DbLogServer.WriteDbLog((isvalid == 0 ? $"激活了用户{userInfo.Uid}({userInfo.Id })" : $"冻结了用户{userInfo.Id }({userInfo.Uid})"), 1);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserInfoListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysView_UAListKey);
|
|
return true;
|
|
}
|
|
catch (CustomException ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
#endregion
|
|
#region 用户权限用户组信息等
|
|
public static List<View_UOA> GetUOA(string uid)
|
|
{
|
|
// 目前这些功能并未使用 可以直接 返回 [];
|
|
return new List<View_UOA>();
|
|
return CacheHelp.cacheView_UOA.Where(X => X.Uid == uid).ToList();
|
|
}
|
|
#endregion
|
|
#region 用户权限信息等
|
|
public static dynamic GetUA(string uid)
|
|
{
|
|
var RES = SqlSugarBase.Db.Queryable<View_UA>().Where(X => X.Uid == uid && X.AuthotypeId != 1).Select(X=>new{HotelId=X.HotelId,AuthorityId = X.AuthorityId, AuthotypeId = X.AuthotypeId, AuthorityName = X.AuthorityName }).ToArray();
|
|
return RES;
|
|
}
|
|
#endregion
|
|
#region 更改用户权限
|
|
public static bool EditAuthoType(string uid, int type, int authorityid, int hotels, string CreatedBy)
|
|
{
|
|
try
|
|
{
|
|
Hotels hotels1 = CacheHelp.cacheHotels.FirstOrDefault(x => x.Id == hotels);
|
|
Authority authority = CacheHelp.cacheSysAutho.FirstOrDefault(x => x.Id == authorityid);
|
|
AuthoStatusType statusType = CacheHelp.cacheSysAuthoStatusType.FirstOrDefault(x => x.Id == type);
|
|
var uinfo = CacheHelp.cacheSysUserInfo.FirstOrDefault(x => x.Uid.ToLower() == uid.ToLower());
|
|
if (uinfo == null)
|
|
{
|
|
Logs.WriteTimingUDPLog("模型验证失败~");
|
|
return false;
|
|
}
|
|
if (CreatedBy.ToLower() != uid.ToLower() && CacheHelp.cacheSysUserInfo.FirstOrDefault(X => X.Uid == CreatedBy).IsValid <= uinfo.IsValid && CreatedBy.ToLower() != "adminblv")
|
|
{
|
|
Logs.WriteTimingUDPLog("权限不足~");
|
|
return false;
|
|
}
|
|
//UserAutho autho = db.UserAuthos.FirstOrDefault(x => x.UserId == uinfo.Id && x.AuthorityId == authorityid && x.HotelId == hotels);
|
|
UserAuthoes autho = SqlSugarBase.Db.Queryable<UserAuthoes>().First(x => x.UserId == uinfo.Id && x.AuthorityId == authorityid && x.HotelId == hotels);
|
|
if (autho == null)
|
|
{
|
|
DbLogServer.WriteDbLog($"新增了用户{uinfo.Uid }({uinfo.Id})对{hotels1.Name}({hotels})酒店的{authority.AuthorityName}({authority.Id})权限:无 => {statusType.Name}");
|
|
//db.UserAuthos.Add(new UserAutho() { AuthotypeId = type, UserId = uinfo.Id, AuthorityId = authorityid, HotelId = hotels, CreatedBy = CreatedBy });
|
|
SqlSugarBase.Db.Insertable(new UserAuthoes() { AuthotypeId = type, UserId = uinfo.Id, AuthorityId = authorityid, HotelId = hotels, CreatedBy = CreatedBy }).ExecuteCommand();
|
|
}
|
|
else
|
|
{
|
|
AuthoStatusType statusTypeold = CacheHelp.cacheSysAuthoStatusType.FirstOrDefault(x => x.Id == autho.AuthotypeId);
|
|
DbLogServer.WriteDbLog($"更改了用户{uinfo.Uid }({uinfo.Id})对{hotels1.Name}({hotels})酒店的{authority.AuthorityName}({authority.Id})权限:{statusTypeold.Name} => {statusType.Name}", 1);
|
|
autho.AuthotypeId = type;
|
|
autho.CreatedBy = CreatedBy;
|
|
SqlSugarBase.Db.Updateable(autho).ExecuteCommand();
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserAuthoListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysView_UAListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserInfoListKey);
|
|
}
|
|
}
|
|
#endregion
|
|
#region 检查用户uid是否可用
|
|
public static bool CheckUid(string uid)
|
|
{
|
|
if (CacheHelp.cacheSysUserInfo.FirstOrDefault(u => u.Uid.ToLower() == uid.ToLower()) == null)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
#endregion
|
|
#region 添加、修改用户
|
|
public static bool AddUserinfo(View_UserInfo userInfo, string uid, int type = 0)
|
|
{
|
|
try
|
|
{
|
|
int ID=-1;
|
|
OrgUsers orgUser = new OrgUsers();
|
|
UserInfo user = new UserInfo();
|
|
if (type != 0)
|
|
{
|
|
user = SqlSugarBase.Db.Queryable<UserInfo>().First(x => x.Id == userInfo.Id) ?? null;
|
|
orgUser = SqlSugarBase.Db.Queryable<OrgUsers>().First(x => x.UserId == user.Id) ?? null;
|
|
if (user == null)
|
|
return false;
|
|
if (orgUser == null)
|
|
return false;
|
|
int? orgid = userInfo.OrgId;
|
|
orgUser.OrgId = int.Parse(orgid.ToString());
|
|
SqlSugarBase.Db.Updateable(orgUser).ExecuteCommand();
|
|
|
|
if (user.Uid.ToLower() != uid.ToLower() && CacheHelp.cacheSysUserInfo.FirstOrDefault(X => X.Uid == uid).IsValid <= user.IsValid && uid.ToLower() != "adminblv")
|
|
return false;
|
|
}
|
|
user.Age = userInfo.Age;
|
|
user.Uid = userInfo.Uid.ToLower();
|
|
user.HeadImg = userInfo.HeadImg;
|
|
user.Sex = userInfo.Sex;
|
|
user.Desc = userInfo.Desc;
|
|
user.IsValid = userInfo.IsValid;
|
|
user.CreatedBy = uid;
|
|
user.Company = userInfo.Company;
|
|
user.EndTime =DateTime.Now.AddYears(200);/* userInfo.EndTime*/
|
|
if (!string.IsNullOrEmpty(userInfo.Pwd))
|
|
{
|
|
user.Pwd = userInfo.Pwd;
|
|
Services.Tool.RSA rSA = HttpContext.Current.Session["ras"] as Services.Tool.RSA ?? throw new Exception();
|
|
string pwd = rSA.DecodeOrNull(user.Pwd) ?? throw new CustomException("出错!");
|
|
user.Pwd = user.PwdSee = pwd;
|
|
user = user.ComputePasswordHash();
|
|
}
|
|
if (user.Uid.ToLower() != uid.ToLower() && CacheHelp.cacheSysUserInfo.FirstOrDefault(X => X.Uid == uid).IsValid < user.IsValid && uid.ToLower() != "adminblv")
|
|
return false;
|
|
Logs.WriteUserinfo(user);
|
|
if (type == 0)
|
|
{
|
|
ID= SqlSugarBase.Db.Insertable(user).ExecuteReturnIdentity();
|
|
}
|
|
if (type == 0)
|
|
{
|
|
orgUser = new OrgUsers();
|
|
orgUser.OrgId = userInfo.OrgId ?? 1;
|
|
orgUser.UserId = ID;
|
|
|
|
SqlSugarBase.Db.Insertable(orgUser).ExecuteCommand();
|
|
|
|
DbLogServer.WriteDbLog($"添加了用户信息{user.Uid}({ID})");
|
|
}
|
|
else
|
|
{
|
|
|
|
SqlSugarBase.Db.Updateable(user).ExecuteCommand();
|
|
DbLogServer.WriteDbLog($"修改了用户信息{user.Uid}({ID})");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new CustomException(ex.ToString());
|
|
}
|
|
finally
|
|
{
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserAuthoListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysView_UAListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserInfoListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysView_UOAListKey);
|
|
|
|
}
|
|
return true;
|
|
}
|
|
#endregion
|
|
#region 删除用户
|
|
public static bool DelUserinfo(int id, string uid)
|
|
{
|
|
UserInfo user = new UserInfo();
|
|
try
|
|
{
|
|
using (AuthorityDB db = new AuthorityDB())
|
|
{
|
|
user = db.UserInfos.FirstOrDefault(x => x.Id == id);
|
|
if (user.Uid.ToLower() != uid.ToLower() && CacheHelp.cacheSysUserInfo.FirstOrDefault(X => X.Uid == uid).IsValid <= user.IsValid && uid.ToLower() != "adminblv")
|
|
return false;
|
|
if (user == null)
|
|
{
|
|
DbLogServer.WriteDbLog($"删除用户{id}失败!");
|
|
return false;
|
|
}
|
|
db.OrgUsers.Remove(db.OrgUsers.FirstOrDefault(x => x.UserId == id));
|
|
db.UserAuthos.RemoveRange(db.UserAuthos.Where(x => x.UserId == id));
|
|
db.UserInfos.Remove(user);
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
DbLogServer.WriteDbLog($"删除用户{id}失败!");
|
|
throw new CustomException(ex.ToString());
|
|
}
|
|
finally
|
|
{
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserAuthoListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysView_UAListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserInfoListKey);
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysView_UOAListKey);
|
|
}
|
|
DbLogServer.WriteDbLog($"删除用户{user.Uid}({user.Id})成功!");
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 获取用户信息
|
|
/// </summary>
|
|
/// <param name="start"></param>
|
|
/// <param name="length"></param>
|
|
/// <param name="draw"></param>
|
|
/// <param name="gs"></param>
|
|
/// <param name="IsImport"></param>
|
|
/// <param name="search"></param>
|
|
/// <returns></returns>
|
|
|
|
public static dynamic GetUserinfoView(int start, int length, int draw, int gs = -1, int IsImport = -1, Dictionary<string, string> search = null)
|
|
{
|
|
var data = CacheHelp.cacheView_UserInfo.OrderByDescending(X=>X.CreateTime).ToList();
|
|
string searchstr = string.Empty;
|
|
if(search != null && search["value"].Length>0)
|
|
{
|
|
int s = search["value"].Length;
|
|
searchstr = $"uid like '%{search["value"].ToUpper()}%'";
|
|
data = data.Where(x=>x.Uid.Trim().ToUpper().Contains(search["value"].ToUpper())).ToList();
|
|
}
|
|
if (gs!=-1)
|
|
{
|
|
data= data.Where(x => x.Company == gs).ToList();
|
|
if (searchstr == string.Empty)
|
|
{
|
|
searchstr = $"Company = '{gs}'";
|
|
}
|
|
else
|
|
{
|
|
searchstr += $" and Company = '{gs}' ";
|
|
}
|
|
}
|
|
if (IsImport != -1)
|
|
{
|
|
data = data.Where(x => x.IsImport == IsImport).ToList();
|
|
|
|
if (searchstr == string.Empty)
|
|
{
|
|
searchstr = $"IsImport = '{IsImport}'";
|
|
}
|
|
else
|
|
{
|
|
searchstr += $" and IsImport = '{IsImport}' ";
|
|
}
|
|
}
|
|
if (searchstr == string.Empty)
|
|
{
|
|
searchstr = $" 1 = 1 ";
|
|
}
|
|
int recordsFiltered = 0;
|
|
string sqlsum = $"select count(*) from View_UserInfo where {searchstr}";
|
|
//recordsFiltered = db.Database.SqlQuery<int>(sqlsum).ToList()[0];
|
|
// recordsFiltered = SqlSugarBase.Db.Ado.SqlQuery<int>(sqlsum).ToList()[0]; 推荐使用
|
|
recordsFiltered = data.Count();
|
|
//if (start == -10)
|
|
//{
|
|
// searchstr += $@" and id not in (select
|
|
// id from View_UserInfo where { searchstr}
|
|
// order by CreateTime desc LIMIT 1,{ start}) ";
|
|
//}
|
|
|
|
List <dynamic> res = null;
|
|
bool SX = false;
|
|
res = new List<dynamic>();
|
|
string sql = $"select * from View_UserInfo where {searchstr} order by CreateTime desc LIMIT {start},{ length}";
|
|
res.AddRange(
|
|
data.Skip(start).Take(length).Select((View_UserInfo u) =>
|
|
{
|
|
var hoteles = UH_CENTER_Help.Sel_Hotel(u);
|
|
// 上面的操作会刷新数据 这里刷新缓存 架构 问题
|
|
if (string.IsNullOrEmpty(u.Hotel_Data))
|
|
SX = true;
|
|
return new
|
|
{
|
|
Id = u.Id,
|
|
IsValid = u.IsValid,
|
|
Age = u.Age,
|
|
Uid = u.Uid,
|
|
sex = ((u.Sex == 0) ? "男" : ((u.Sex == 1) ? "女" : "未知")),
|
|
Sex = u.Sex,
|
|
OrganizationName = u.OrganizationName,
|
|
Authoncount = u.Authoncount,
|
|
isValid = (u.IsValid == 1 ? "冻结" :
|
|
(u.IsValid == 0 ? "正常" :
|
|
(u.IsValid == 2 ? "管理" :
|
|
(u.IsValid == 3 ? "超管" : "其他")))),
|
|
u.HeadImg,
|
|
u.EndTime,
|
|
u.Desc,
|
|
u.CreateTime,
|
|
u.IsImport,
|
|
Gs = HotelServer.FindUserinfoGs(u),
|
|
Hoteles = hoteles.Select(X=>X.id).ToArray(),
|
|
HotelInfo = hoteles
|
|
};
|
|
}
|
|
));
|
|
if(SX)
|
|
CacheHelp.Removesys(CacheHelp.syskey.sysUserInfoListKey);
|
|
return new
|
|
{
|
|
draw,
|
|
recordsTotal = CacheHelp.cacheSysUserInfo.Count(),
|
|
recordsFiltered = recordsFiltered,
|
|
data = res
|
|
};
|
|
}
|
|
}
|