123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using PMS.BusinessModels.Account;
- using PMS.BusinessModels.SysManager;
- using PMS.DBService.SysManager;
- using PMS.EntityModels.SysManager;
- using PMS.Interface.SysManager;
- using QWPlatform.IService;
- using QWPlatform.SystemLibrary;
- using QWPlatform.SystemLibrary.Security;
- using QWPlatform.SystemLibrary.Web;
- using PMS.BusinessModels.Person;
- using PMS.EntityModels.MedicalRecordManager;
- namespace PMS.BusinessService.SysManager
- {
- /// <summary>
- /// 创 建 人:王海洋
- /// 创建日期:2018-12-7
- /// 功能描述:系统账户业务逻辑处理
- /// </summary>
- public class AccountBLLService : IAccount
- {
- private static System.Collections.Concurrent.ConcurrentDictionary<int, UserInfo> _cacheUsers = new System.Collections.Concurrent.ConcurrentDictionary<int, UserInfo>();
- //数据服务
- private static AccountDBService db_account;
- //锁定对象
- private static object _lockobj = new object();
- static AccountBLLService()
- {
- db_account = DataServiceBase.Instance<AccountDBService>();
- }
- #region 账户管理
- //根据账户ID获取到账户信息
- public UserInfo GetAccountInfo(int id)
- {
- if (_cacheUsers != null && _cacheUsers.ContainsKey(id))
- {//从缓存中获取
- return _cacheUsers[id];
- }
- else
- {//从数据库查询
- var dt = db_account.GetAccountInfo(id);
- if (dt != null && dt.Rows.Count > 0)
- {
- var model = new UserInfo();
- model.Account = dt.GetValueByName<string>("账户");
- model.ID = dt.GetValueByName<int>("ID");
- model.IsSuperAdmin = dt.GetValueByName<int>("类型") == 1;
- model.Name = dt.GetValueByName<string>("姓名");
- model.PersonID = dt.GetValueByName<string>("人员ID");
- model.Email = dt.GetValueByName<string>("邮箱");
- model.Company = dt.GetValueByName<string>("渠道名称");
- model.CompanyID = dt.GetValueByName<string>("公司ID");
- model.OrgID = dt.GetValueByName<string>("机构ID");
- model.JobCode = dt.GetValueByName<string>("职务");
- model.PersonJob = dt.GetValueByName<string>("职务名称");
- model.DefaultProjectID = dt.GetValueByName<string>("项目ID");
- model.PersonProperty = dt.GetValueByName<int>("性质");
- model.WechatID = dt.GetValueByName<string>("微信id");
- model.AuthorizeAll = dt.GetValueByName<int>("渠道所有项目");
- model.Roles = new List<string>();
- var roleIds = dt.GetValueByName<string>("角色ID")?.Split(',');
- if (roleIds != null && roleIds.Length > 0)
- {//添加角色列表
- model.Roles.AddRange(roleIds);
- }
- //查询数据权限(已执行的授权目录)
- var authDt = db_account.SelectAccountAuthData(id, model.AuthorizeAll, model.CompanyID);
- if (authDt != null && authDt.Rows.Count > 0)
- {//如果查询到有数据授权,则添加
- var list = new List<account_authdata_model>();
- foreach (DataRow dr in authDt.Rows)
- {//读取权限列表
- var itemIds = new string[] { };
- var orgid = dr.GetValueByName<string>("公司ID");
- var items = dr.GetValueByName<string>("项目ID");
- if (!string.IsNullOrEmpty(items))
- {
- itemIds = items.Split(',');//多个项目使用","区别
- }
- var authdataModel = new account_authdata_model()
- {
- uid = id,
- orgid = orgid,
- items = new List<string>()
- };
- if (itemIds.Length > 0)
- {//如果包括了项目ID
- authdataModel.items.AddRange(itemIds);
- }
- //添加到集合中
- list.Add(authdataModel);
- }//end foreach
- //添加到账户授权信息
- model.AuthDats = list;
- }
- //获取当前账户登录所在渠道(公司)的机构ID
- if (model.AuthDats == null)
- {//如果没有一个授权的,则实例化一次
- model.AuthDats = new List<account_authdata_model>();
- }
- if (!model.AuthDats.Exists(p => p.orgid == model.CompanyID))
- {//如果数据授权信息中不包括当前机构,则将当前机构添加到授权中
- model.AuthDats.Add(new account_authdata_model()
- {//将当前机构添加到授权体系中
- uid = model.ID,
- orgid = model.CompanyID
- });
- }
- //放入缓存中,下次不再从数据库读取
- if (!_cacheUsers.ContainsKey(id))
- {//添加到缓存中
- _cacheUsers.TryAdd(id, model);
- }
- return model;
- }
- }
- return null;
- }
- //移除缓存用户信息,当用户发生授权变更需要移除
- public void RemoveCacheUser(int id)
- {
- if (_cacheUsers.ContainsKey(id))
- {//移除用户
- var user = new UserInfo();
- _cacheUsers.TryRemove(id, out user);
- }
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="account"></param>
- /// <param name="pwd"></param>
- /// <returns></returns>
- public LoginResult Login(string account, string pwd, string ip)
- {
- var model = new AccountModel();
- model.ZH = account;
- model.MM = QWPlatform.SystemLibrary.Utils.Strings.MD5(pwd);
- model.SetWhereColumns("账户", "密码");
- //登录认证
- var qmodel = db_account.Select(model);
- if (qmodel != null && qmodel.ID > 0)
- {//获取到账户,检查是否被锁定
- if (qmodel.ZT == 0)
- {//账户已被锁定
- return new LoginResult() { Success = false, Message = "账户已被锁定,请联系管理员。" };
- }
- else if (string.IsNullOrEmpty(qmodel.GSID) || string.IsNullOrEmpty(qmodel.RYID))
- {
- return new LoginResult() { Success = false, Message = "该账户没有关联到人员信息,缺少公司ID或人员ID." };
- }
- else
- {//登录成功,更新数据库登录状态
- //获取密钥
- var secret_key = DESEncrypt.DesEncrypt(model.ID.ToString());
- //写入到登录成功的cookie中(用户ID加密存储),有效一天
- CookiesHelper.AddCookie("UserID", secret_key);
- model.ZX = 1;
- model.DLIP = ip;
- model.DLSJ = DateTime.Now;
- db_account.Update(model);
- var accountId = qmodel.ID.Value;
- //登录成功,更新缓存
- var r = _cacheUsers.ContainsKey(accountId);
- if (r)
- {//移除缓存即可
- UserInfo userInfo = null;
- _cacheUsers.TryRemove(accountId, out userInfo);
- }
- return new LoginResult() { Success = true, AccountID = model.ID.Value, Message = "登录成功" };
- }
- }
- else
- {//登录失败
- return new LoginResult() { Success = false, Message = "账户不存在,或密码不正确." };
- }
- }
- //获取所有机构
- public string GetOrgsDataGridJson()
- {
- var dt = db_account.GetOrgs();
- return dt.ToJson();
- }
- //根据机构ID获取账户目录
- public string GetUserListByOrgId(string id, int page, int rows)
- {
- int total = 0;
- DataTable dt = db_account.SelectAccountByOrgId(id, page, rows, out total);
- if (dt != null)
- {
- return dt.ToEasyUIGridJson(total);
- }
- return string.Empty;
- }
- //账户设置角色
- public bool SaveAuthRoleToAccount(int? uid, string rids)
- {
- rids = rids.Trim(',', ' ');
- //先把所有权限删除
- db_account.DeleteUserRole(uid.Value);
- if (!string.IsNullOrEmpty(rids))
- {//为空时回收所有授权
- var rid = rids.Split(',');
- for (int i = 0; i < rid.Length; i++)
- {
- var roleid = 0;
- if (int.TryParse(rid[i], out roleid))
- {//转换成功
- db_account.AddUserRoleAuth(new AccountRoleModel()
- {
- ZHID = uid,
- JSID = roleid
- });
- }
- }
- }
- //需要更新缓存
- RemoveCacheUser(uid.Value);
- return true;
- }
- /// <summary>
- /// 批量授权
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public int BatchAuth(string id, IEnumerable<account_authdata_model> models)
- {
- if (models != null)
- {
- string[] data = id.Split(',');
- for(int i=0;i<data.Length;i++)
- {
- //清除所有数据权限
- db_account.RemoveAllAuth(data[i]);
- foreach (var item in models)
- {
- var projectids = "";
- if (item.items != null && item.items.Count > 0)
- {
- projectids = string.Join(",", item.items);
- }
- var GSID = item.orgid;
- //添加数据权限
- db_account.AddAuth(data[i].ToInt32(),item.orgid,projectids);
- }
- }
- }
- return 1;
- }
- //保存账户的数据授权
- public bool SaveAuthDataToAccount(IEnumerable<account_authdata_model> models)
- {
- if (models != null)
- {
- //清空原来的权限
- db_account.DeleteDataAuth(models.First().uid);
- //移除原来的数据授权信息
- foreach (var item in models)
- {
- var projectids = "";
- if (item.items != null && item.items.Count > 0)
- {
- projectids = string.Join(",", item.items);
- }
- var model = new AccountDataModel()
- {
- ZHID = item.uid,
- GSID = item.orgid,
- XMID = projectids
- };
- db_account.InsertAuthDataToAccount(model);
- }
- //需要更新缓存
- RemoveCacheUser(models.First().uid);
- return true;
- }
- return false;
- }
- //根据账户ID获取对应的账户授权信息
- public List<account_authdata_model> GetAccountDataAuth(int id)
- {
- DataTable dt = db_account.SelectAccountAuthData(id);
- List<account_authdata_model> list = new List<account_authdata_model>();
- if (dt != null && dt.Rows.Count > 0)
- {
- foreach (DataRow dr in dt.Rows)
- {//循环各公司(渠道)
- account_authdata_model model = new account_authdata_model();
- model.orgid = dr.GetValueByName<string>("公司ID");
- model.items = new List<string>();
- //项目ID
- var items = dr.GetValueByName<string>("项目ID");
- if (!string.IsNullOrEmpty(items))
- {//包括了项目名称
- model.items.AddRange(items.Split(','));
- }
- list.Add(model);
- }//end foreach
- return list;
- }
- return null;
- }
- //根据当前用户,当前路径,分组名,查询出可用的按钮集合
- public string GetButtionsForUserRole(List<string> roles, string menuPath, string groupName, bool isSuperAdmin)
- {
- var str_roles = string.Join(",", roles);
- //执行数据库查询
- DataTable dt = db_account.SelectModuleButtonsForUserRole(str_roles, menuPath, groupName, isSuperAdmin);
- return dt.ToJson();
- }
- //将项目授权到指定的账户上
- public bool AuthProjectToAccount(int uid, string orgid, string projectid)
- {//执行账户授权项目(一般是在用户创建项目时授权项目使用)
- //移除缓存(针对指定的账户需要重新从数据库读取)
- this.RemoveCacheUser(uid);
- return db_account.AuthProjectToAccount(uid, orgid, projectid) > 0;
- }
- //检查旧密码是否输入正确
- public bool CheckOldPwd(int id, string pwd)
- {
- //密码加密
- var md5pwd = QWPlatform.SystemLibrary.Utils.Strings.StrToMD5(pwd);
- return db_account.CheckOldPwd(id, md5pwd);
- }
- //修改密码
- public bool ChangePassword(int id, string pwd)
- {
- //密码加密
- var md5pwd = QWPlatform.SystemLibrary.Utils.Strings.StrToMD5(pwd);
- return db_account.ChangePassword(id, md5pwd);
- }
- #endregion
- #region 角色管理
- //查询到角色列表
- public string GetRolesList(int page, int rows)
- {
- int total = 0;
- DataTable dt = db_account.SelectRoles(page, rows, out total);
- return dt.ToEasyUIGridJson(total, null);
- }
- //查询可用的角色列表
- public string GetRolesForActiveList(int page, int rows)
- {
- int total = 0;
- DataTable dt = db_account.SelectRolesForActive(page, rows, out total);
- return dt.ToEasyUIGridJson(total, null);
- }
- //获取角色授权
- public string GetRoleAuthJson(int roleid)
- {
- var dt = db_account.SelectRoleAuth(roleid);
- return dt.ToJson();
- }
- //获取角色模块功能列表
- public string GetRoleAuthFunGridJson(int roleid, int moduleid)
- {
- var dt = db_account.SelectRoleAuthFuns(roleid, moduleid);
- return dt.ToEasyUIGridJson(0, null);
- }
- //保存角色的授权信息
- public bool SaveRoleAuthorzie(int rid, string mids, string bids)
- {
- //移除两边的","号
- mids = mids.Trim(',', ' ');
- bids = bids.Trim(',', ' ');
- if (!string.IsNullOrEmpty(mids))
- {//模块不为空则解析模块
- var moduleids = mids.Split(',');
- for (int i = 0; i < moduleids.Length; i++)
- {//获取每个模块,进行授权。
- var mid = 0;
- if (int.TryParse(moduleids[i], out mid))
- {//转换获取模块ID
- var model = new RoleAuthModel();
- model.MKID = mid;
- model.JSID = rid;
- //删除原来的权限
- db_account.DeleteRoleAuth(model);
- if (i == 0)
- {//第一个节点,是授权的功能子节点
- model.GNIDLB = bids;
- model.SQJD = 1;
- }
- //执行一次插入
- db_account.InsertRoleAuth(model);
- }
- }
- return true;
- }
- return false;
- }
- //根据账户的已授权角色目录
- public DataTable GetAccountRoles(int uid)
- {
- return db_account.SelectAccountRoles(uid);
- }
- //获取所有渠道下的项目信息
- public string GetOrgProjecgTree()
- {
- var dt = db_account.SelectOrgProjectTree();
- return dt.ToEasyUITreeJson("ID", "名称", "渠道ID", null, null, new string[] { "是否渠道" });
- }
- //根据角色id获取用户信息
- public string GetUserByRoleId(int roleid, int rows, int page)
- {
- int total = 0;
- DataTable dt = db_account.GetUserByRoleId(roleid, rows, page, out total);
- return DataToEasyUI.ToEasyUIGridJson(dt, total);
- }
- #endregion
- //保存配置文件
- public bool SaveConfig(my_configinfo myconfigInfo, int account)
- {
- var myconfig = new NotefiyConfigInfo();
- //解决问题时配置
- myconfig.SolveTime = new ConfigInfo()
- {
- SendEmail = myconfigInfo.SolveTime_Email,
- SendSMS = myconfigInfo.SolveTime_SMS,
- SendSite = myconfigInfo.SolveTime_Site
- };
- //被指派时
- myconfig.Assign = new ConfigInfo()
- {
- SendEmail = myconfigInfo.Assign_Email,
- SendSMS = myconfigInfo.Assign_SMS,
- SendSite = myconfigInfo.Assign_Site
- };
- //被终止时
- myconfig.StopTime = new ConfigInfo()
- {
- SendEmail = myconfigInfo.StopTime_Email,
- SendSMS = myconfigInfo.StopTime_SMS,
- SendSite = myconfigInfo.StopTime_Site
- };
- //被回退时
- myconfig.BackTime = new ConfigInfo()
- {
- SendEmail = myconfigInfo.BackTime_Email,
- SendSMS = myconfigInfo.BackTime_SMS,
- SendSite = myconfigInfo.BackTime_Site
- };
- myconfig.ProductID = myconfigInfo.ProductID;
- myconfig.ModuleID = myconfigInfo.ModuleID;
- //保存配置信息
- return db_account.SaveConfig(account, myconfig);
- }
- //保存消息配置
- public bool SaveMessageInfo(Mssage_config Mssage_config, int account)
- {
- return db_account.SaveMessageInfo(Mssage_config, account);
- }
- //获取消息配置
- public Mssage_config GetMessageInfo(int account)
- {
- return db_account.GetMessageInfo(account);
- }
- //获取配置文件
- public NotefiyConfigInfo GetConfigInfo(int account)
- {
- return db_account.GetConfigInfo(account);
- }
- /// <summary>
- /// 查询结该人员的邮件及配置信息
- /// </summary>
- /// <param name="personId">人员信息ID</param>
- /// <returns></returns>
- public NotefiyConfigInfo GetNotifyConfigInfoByUserId(string personId)
- {
- return db_account.GetNotifyConfigInfoByUserId(personId);
- }
- /// <summary>
- /// 修改个人信息
- /// </summary>
- /// <returns></returns>
- public int UpdatePersonInfo(PersonBusinessModel model)
- {
- return db_account.UpdatePersonInfo(model);
- }
- /// <summary>
- /// 微信登陆(密码无需再次加密)
- /// </summary>
- /// <param name="account"></param>
- /// <param name="pwd"></param>
- /// <param name="ip"></param>
- /// <returns></returns>
- public LoginResult WeChatLogin(string account, string pwd, string ip)
- {
- var model = new AccountModel();
- model.ZH = account;
- model.MM = pwd;
- model.SetWhereColumns("账户", "密码");
- //登录认证
- var qmodel = db_account.Select(model);
- if (qmodel != null && qmodel.ID > 0)
- {//获取到账户,检查是否被锁定
- if (qmodel.ZT == 0)
- {//账户已被锁定
- return new LoginResult() { Success = false, Message = "账户已被锁定,请联系管理员。" };
- }
- else if (string.IsNullOrEmpty(qmodel.GSID) || string.IsNullOrEmpty(qmodel.RYID))
- {
- return new LoginResult() { Success = false, Message = "该账户没有关联到人员信息,缺少公司ID或人员ID." };
- }
- else
- {//登录成功,更新数据库登录状态
- //获取密钥
- var secret_key = DESEncrypt.DesEncrypt(model.ID.ToString());
- //写入到登录成功的cookie中(用户ID加密存储),有效五天
- CookiesHelper.AddCookie("UserID", secret_key,DateTime.Now.AddDays(5));
- model.ZX = 1;
- model.DLIP = ip;
- model.DLSJ = DateTime.Now;
- db_account.Update(model);
- var accountId = qmodel.ID.Value;
- //登录成功,更新缓存
- var r = _cacheUsers.ContainsKey(accountId);
- if (r)
- {//移除缓存即可
- UserInfo userInfo = null;
- _cacheUsers.TryRemove(accountId, out userInfo);
- }
- return new LoginResult() { Success = true, AccountID = model.ID.Value, Message = "登录成功" };
- }
- }
- else
- {//登录失败
- return new LoginResult() { Success = false, Message = "账户不存在,或密码不正确." };
- }
- }
- /// <summary>
- /// 根据个人id获取账户信息
- /// </summary>
- /// <param name="personId"></param>
- /// <returns></returns>
- public DataTable GetAccountInfo(string personId)
- {
- return db_account.GetAccountInfo(personId);
- }
- /// <summary>
- /// 根据手机号获取系统账号信息
- /// </summary>
- /// <param name="tel"></param>
- /// <returns></returns>
- public DataTable GetTelAccountInfo(string tel)
- {
- return db_account.GetTelAccountInfo(tel);
- }
- /// <summary>
- /// 查询字典表
- /// </summary>
- /// <param name="ID"></param>
- /// <returns></returns>
- public List<BasicDictionaryModel> SelectNature(string ID)
- {
- return db_account.SelectNature(ID);
- }
- }
- }
|