AccountBLLService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using PMS.BusinessModels.Account;
  7. using PMS.BusinessModels.SysManager;
  8. using PMS.DBService.SysManager;
  9. using PMS.EntityModels.SysManager;
  10. using PMS.Interface.SysManager;
  11. using QWPlatform.IService;
  12. using QWPlatform.SystemLibrary;
  13. using QWPlatform.SystemLibrary.Security;
  14. using QWPlatform.SystemLibrary.Web;
  15. using PMS.BusinessModels.Person;
  16. using PMS.EntityModels.MedicalRecordManager;
  17. namespace PMS.BusinessService.SysManager
  18. {
  19. /// <summary>
  20. /// 创 建 人:王海洋
  21. /// 创建日期:2018-12-7
  22. /// 功能描述:系统账户业务逻辑处理
  23. /// </summary>
  24. public class AccountBLLService : IAccount
  25. {
  26. private static System.Collections.Concurrent.ConcurrentDictionary<int, UserInfo> _cacheUsers = new System.Collections.Concurrent.ConcurrentDictionary<int, UserInfo>();
  27. //数据服务
  28. private static AccountDBService db_account;
  29. //锁定对象
  30. private static object _lockobj = new object();
  31. static AccountBLLService()
  32. {
  33. db_account = DataServiceBase.Instance<AccountDBService>();
  34. }
  35. #region 账户管理
  36. //根据账户ID获取到账户信息
  37. public UserInfo GetAccountInfo(int id)
  38. {
  39. if (_cacheUsers != null && _cacheUsers.ContainsKey(id))
  40. {//从缓存中获取
  41. return _cacheUsers[id];
  42. }
  43. else
  44. {//从数据库查询
  45. var dt = db_account.GetAccountInfo(id);
  46. if (dt != null && dt.Rows.Count > 0)
  47. {
  48. var model = new UserInfo();
  49. model.Account = dt.GetValueByName<string>("账户");
  50. model.ID = dt.GetValueByName<int>("ID");
  51. model.IsSuperAdmin = dt.GetValueByName<int>("类型") == 1;
  52. model.Name = dt.GetValueByName<string>("姓名");
  53. model.PersonID = dt.GetValueByName<string>("人员ID");
  54. model.Email = dt.GetValueByName<string>("邮箱");
  55. model.Company = dt.GetValueByName<string>("渠道名称");
  56. model.CompanyID = dt.GetValueByName<string>("公司ID");
  57. model.OrgID = dt.GetValueByName<string>("机构ID");
  58. model.JobCode = dt.GetValueByName<string>("职务");
  59. model.PersonJob = dt.GetValueByName<string>("职务名称");
  60. model.DefaultProjectID = dt.GetValueByName<string>("项目ID");
  61. model.PersonProperty = dt.GetValueByName<int>("性质");
  62. model.WechatID = dt.GetValueByName<string>("微信id");
  63. model.AuthorizeAll = dt.GetValueByName<int>("渠道所有项目");
  64. model.Roles = new List<string>();
  65. var roleIds = dt.GetValueByName<string>("角色ID")?.Split(',');
  66. if (roleIds != null && roleIds.Length > 0)
  67. {//添加角色列表
  68. model.Roles.AddRange(roleIds);
  69. }
  70. //查询数据权限(已执行的授权目录)
  71. var authDt = db_account.SelectAccountAuthData(id, model.AuthorizeAll, model.CompanyID);
  72. if (authDt != null && authDt.Rows.Count > 0)
  73. {//如果查询到有数据授权,则添加
  74. var list = new List<account_authdata_model>();
  75. foreach (DataRow dr in authDt.Rows)
  76. {//读取权限列表
  77. var itemIds = new string[] { };
  78. var orgid = dr.GetValueByName<string>("公司ID");
  79. var items = dr.GetValueByName<string>("项目ID");
  80. if (!string.IsNullOrEmpty(items))
  81. {
  82. itemIds = items.Split(',');//多个项目使用","区别
  83. }
  84. var authdataModel = new account_authdata_model()
  85. {
  86. uid = id,
  87. orgid = orgid,
  88. items = new List<string>()
  89. };
  90. if (itemIds.Length > 0)
  91. {//如果包括了项目ID
  92. authdataModel.items.AddRange(itemIds);
  93. }
  94. //添加到集合中
  95. list.Add(authdataModel);
  96. }//end foreach
  97. //添加到账户授权信息
  98. model.AuthDats = list;
  99. }
  100. //获取当前账户登录所在渠道(公司)的机构ID
  101. if (model.AuthDats == null)
  102. {//如果没有一个授权的,则实例化一次
  103. model.AuthDats = new List<account_authdata_model>();
  104. }
  105. if (!model.AuthDats.Exists(p => p.orgid == model.CompanyID))
  106. {//如果数据授权信息中不包括当前机构,则将当前机构添加到授权中
  107. model.AuthDats.Add(new account_authdata_model()
  108. {//将当前机构添加到授权体系中
  109. uid = model.ID,
  110. orgid = model.CompanyID
  111. });
  112. }
  113. //放入缓存中,下次不再从数据库读取
  114. if (!_cacheUsers.ContainsKey(id))
  115. {//添加到缓存中
  116. _cacheUsers.TryAdd(id, model);
  117. }
  118. return model;
  119. }
  120. }
  121. return null;
  122. }
  123. //移除缓存用户信息,当用户发生授权变更需要移除
  124. public void RemoveCacheUser(int id)
  125. {
  126. if (_cacheUsers.ContainsKey(id))
  127. {//移除用户
  128. var user = new UserInfo();
  129. _cacheUsers.TryRemove(id, out user);
  130. }
  131. }
  132. /// <summary>
  133. /// 登录
  134. /// </summary>
  135. /// <param name="account"></param>
  136. /// <param name="pwd"></param>
  137. /// <returns></returns>
  138. public LoginResult Login(string account, string pwd, string ip)
  139. {
  140. var model = new AccountModel();
  141. model.ZH = account;
  142. model.MM = QWPlatform.SystemLibrary.Utils.Strings.MD5(pwd);
  143. model.SetWhereColumns("账户", "密码");
  144. //登录认证
  145. var qmodel = db_account.Select(model);
  146. if (qmodel != null && qmodel.ID > 0)
  147. {//获取到账户,检查是否被锁定
  148. if (qmodel.ZT == 0)
  149. {//账户已被锁定
  150. return new LoginResult() { Success = false, Message = "账户已被锁定,请联系管理员。" };
  151. }
  152. else if (string.IsNullOrEmpty(qmodel.GSID) || string.IsNullOrEmpty(qmodel.RYID))
  153. {
  154. return new LoginResult() { Success = false, Message = "该账户没有关联到人员信息,缺少公司ID或人员ID." };
  155. }
  156. else
  157. {//登录成功,更新数据库登录状态
  158. //获取密钥
  159. var secret_key = DESEncrypt.DesEncrypt(model.ID.ToString());
  160. //写入到登录成功的cookie中(用户ID加密存储),有效一天
  161. CookiesHelper.AddCookie("UserID", secret_key);
  162. model.ZX = 1;
  163. model.DLIP = ip;
  164. model.DLSJ = DateTime.Now;
  165. db_account.Update(model);
  166. var accountId = qmodel.ID.Value;
  167. //登录成功,更新缓存
  168. var r = _cacheUsers.ContainsKey(accountId);
  169. if (r)
  170. {//移除缓存即可
  171. UserInfo userInfo = null;
  172. _cacheUsers.TryRemove(accountId, out userInfo);
  173. }
  174. return new LoginResult() { Success = true, AccountID = model.ID.Value, Message = "登录成功" };
  175. }
  176. }
  177. else
  178. {//登录失败
  179. return new LoginResult() { Success = false, Message = "账户不存在,或密码不正确." };
  180. }
  181. }
  182. //获取所有机构
  183. public string GetOrgsDataGridJson()
  184. {
  185. var dt = db_account.GetOrgs();
  186. return dt.ToJson();
  187. }
  188. //根据机构ID获取账户目录
  189. public string GetUserListByOrgId(string id, int page, int rows)
  190. {
  191. int total = 0;
  192. DataTable dt = db_account.SelectAccountByOrgId(id, page, rows, out total);
  193. if (dt != null)
  194. {
  195. return dt.ToEasyUIGridJson(total);
  196. }
  197. return string.Empty;
  198. }
  199. //账户设置角色
  200. public bool SaveAuthRoleToAccount(int? uid, string rids)
  201. {
  202. rids = rids.Trim(',', ' ');
  203. //先把所有权限删除
  204. db_account.DeleteUserRole(uid.Value);
  205. if (!string.IsNullOrEmpty(rids))
  206. {//为空时回收所有授权
  207. var rid = rids.Split(',');
  208. for (int i = 0; i < rid.Length; i++)
  209. {
  210. var roleid = 0;
  211. if (int.TryParse(rid[i], out roleid))
  212. {//转换成功
  213. db_account.AddUserRoleAuth(new AccountRoleModel()
  214. {
  215. ZHID = uid,
  216. JSID = roleid
  217. });
  218. }
  219. }
  220. }
  221. //需要更新缓存
  222. RemoveCacheUser(uid.Value);
  223. return true;
  224. }
  225. /// <summary>
  226. /// 批量授权
  227. /// </summary>
  228. /// <param name="id"></param>
  229. /// <returns></returns>
  230. public int BatchAuth(string id, IEnumerable<account_authdata_model> models)
  231. {
  232. if (models != null)
  233. {
  234. string[] data = id.Split(',');
  235. for(int i=0;i<data.Length;i++)
  236. {
  237. //清除所有数据权限
  238. db_account.RemoveAllAuth(data[i]);
  239. foreach (var item in models)
  240. {
  241. var projectids = "";
  242. if (item.items != null && item.items.Count > 0)
  243. {
  244. projectids = string.Join(",", item.items);
  245. }
  246. var GSID = item.orgid;
  247. //添加数据权限
  248. db_account.AddAuth(data[i].ToInt32(),item.orgid,projectids);
  249. }
  250. }
  251. }
  252. return 1;
  253. }
  254. //保存账户的数据授权
  255. public bool SaveAuthDataToAccount(IEnumerable<account_authdata_model> models)
  256. {
  257. if (models != null)
  258. {
  259. //清空原来的权限
  260. db_account.DeleteDataAuth(models.First().uid);
  261. //移除原来的数据授权信息
  262. foreach (var item in models)
  263. {
  264. var projectids = "";
  265. if (item.items != null && item.items.Count > 0)
  266. {
  267. projectids = string.Join(",", item.items);
  268. }
  269. var model = new AccountDataModel()
  270. {
  271. ZHID = item.uid,
  272. GSID = item.orgid,
  273. XMID = projectids
  274. };
  275. db_account.InsertAuthDataToAccount(model);
  276. }
  277. //需要更新缓存
  278. RemoveCacheUser(models.First().uid);
  279. return true;
  280. }
  281. return false;
  282. }
  283. //根据账户ID获取对应的账户授权信息
  284. public List<account_authdata_model> GetAccountDataAuth(int id)
  285. {
  286. DataTable dt = db_account.SelectAccountAuthData(id);
  287. List<account_authdata_model> list = new List<account_authdata_model>();
  288. if (dt != null && dt.Rows.Count > 0)
  289. {
  290. foreach (DataRow dr in dt.Rows)
  291. {//循环各公司(渠道)
  292. account_authdata_model model = new account_authdata_model();
  293. model.orgid = dr.GetValueByName<string>("公司ID");
  294. model.items = new List<string>();
  295. //项目ID
  296. var items = dr.GetValueByName<string>("项目ID");
  297. if (!string.IsNullOrEmpty(items))
  298. {//包括了项目名称
  299. model.items.AddRange(items.Split(','));
  300. }
  301. list.Add(model);
  302. }//end foreach
  303. return list;
  304. }
  305. return null;
  306. }
  307. //根据当前用户,当前路径,分组名,查询出可用的按钮集合
  308. public string GetButtionsForUserRole(List<string> roles, string menuPath, string groupName, bool isSuperAdmin)
  309. {
  310. var str_roles = string.Join(",", roles);
  311. //执行数据库查询
  312. DataTable dt = db_account.SelectModuleButtonsForUserRole(str_roles, menuPath, groupName, isSuperAdmin);
  313. return dt.ToJson();
  314. }
  315. //将项目授权到指定的账户上
  316. public bool AuthProjectToAccount(int uid, string orgid, string projectid)
  317. {//执行账户授权项目(一般是在用户创建项目时授权项目使用)
  318. //移除缓存(针对指定的账户需要重新从数据库读取)
  319. this.RemoveCacheUser(uid);
  320. return db_account.AuthProjectToAccount(uid, orgid, projectid) > 0;
  321. }
  322. //检查旧密码是否输入正确
  323. public bool CheckOldPwd(int id, string pwd)
  324. {
  325. //密码加密
  326. var md5pwd = QWPlatform.SystemLibrary.Utils.Strings.StrToMD5(pwd);
  327. return db_account.CheckOldPwd(id, md5pwd);
  328. }
  329. //修改密码
  330. public bool ChangePassword(int id, string pwd)
  331. {
  332. //密码加密
  333. var md5pwd = QWPlatform.SystemLibrary.Utils.Strings.StrToMD5(pwd);
  334. return db_account.ChangePassword(id, md5pwd);
  335. }
  336. #endregion
  337. #region 角色管理
  338. //查询到角色列表
  339. public string GetRolesList(int page, int rows)
  340. {
  341. int total = 0;
  342. DataTable dt = db_account.SelectRoles(page, rows, out total);
  343. return dt.ToEasyUIGridJson(total, null);
  344. }
  345. //查询可用的角色列表
  346. public string GetRolesForActiveList(int page, int rows)
  347. {
  348. int total = 0;
  349. DataTable dt = db_account.SelectRolesForActive(page, rows, out total);
  350. return dt.ToEasyUIGridJson(total, null);
  351. }
  352. //获取角色授权
  353. public string GetRoleAuthJson(int roleid)
  354. {
  355. var dt = db_account.SelectRoleAuth(roleid);
  356. return dt.ToJson();
  357. }
  358. //获取角色模块功能列表
  359. public string GetRoleAuthFunGridJson(int roleid, int moduleid)
  360. {
  361. var dt = db_account.SelectRoleAuthFuns(roleid, moduleid);
  362. return dt.ToEasyUIGridJson(0, null);
  363. }
  364. //保存角色的授权信息
  365. public bool SaveRoleAuthorzie(int rid, string mids, string bids)
  366. {
  367. //移除两边的","号
  368. mids = mids.Trim(',', ' ');
  369. bids = bids.Trim(',', ' ');
  370. if (!string.IsNullOrEmpty(mids))
  371. {//模块不为空则解析模块
  372. var moduleids = mids.Split(',');
  373. for (int i = 0; i < moduleids.Length; i++)
  374. {//获取每个模块,进行授权。
  375. var mid = 0;
  376. if (int.TryParse(moduleids[i], out mid))
  377. {//转换获取模块ID
  378. var model = new RoleAuthModel();
  379. model.MKID = mid;
  380. model.JSID = rid;
  381. //删除原来的权限
  382. db_account.DeleteRoleAuth(model);
  383. if (i == 0)
  384. {//第一个节点,是授权的功能子节点
  385. model.GNIDLB = bids;
  386. model.SQJD = 1;
  387. }
  388. //执行一次插入
  389. db_account.InsertRoleAuth(model);
  390. }
  391. }
  392. return true;
  393. }
  394. return false;
  395. }
  396. //根据账户的已授权角色目录
  397. public DataTable GetAccountRoles(int uid)
  398. {
  399. return db_account.SelectAccountRoles(uid);
  400. }
  401. //获取所有渠道下的项目信息
  402. public string GetOrgProjecgTree()
  403. {
  404. var dt = db_account.SelectOrgProjectTree();
  405. return dt.ToEasyUITreeJson("ID", "名称", "渠道ID", null, null, new string[] { "是否渠道" });
  406. }
  407. //根据角色id获取用户信息
  408. public string GetUserByRoleId(int roleid, int rows, int page)
  409. {
  410. int total = 0;
  411. DataTable dt = db_account.GetUserByRoleId(roleid, rows, page, out total);
  412. return DataToEasyUI.ToEasyUIGridJson(dt, total);
  413. }
  414. #endregion
  415. //保存配置文件
  416. public bool SaveConfig(my_configinfo myconfigInfo, int account)
  417. {
  418. var myconfig = new NotefiyConfigInfo();
  419. //解决问题时配置
  420. myconfig.SolveTime = new ConfigInfo()
  421. {
  422. SendEmail = myconfigInfo.SolveTime_Email,
  423. SendSMS = myconfigInfo.SolveTime_SMS,
  424. SendSite = myconfigInfo.SolveTime_Site
  425. };
  426. //被指派时
  427. myconfig.Assign = new ConfigInfo()
  428. {
  429. SendEmail = myconfigInfo.Assign_Email,
  430. SendSMS = myconfigInfo.Assign_SMS,
  431. SendSite = myconfigInfo.Assign_Site
  432. };
  433. //被终止时
  434. myconfig.StopTime = new ConfigInfo()
  435. {
  436. SendEmail = myconfigInfo.StopTime_Email,
  437. SendSMS = myconfigInfo.StopTime_SMS,
  438. SendSite = myconfigInfo.StopTime_Site
  439. };
  440. //被回退时
  441. myconfig.BackTime = new ConfigInfo()
  442. {
  443. SendEmail = myconfigInfo.BackTime_Email,
  444. SendSMS = myconfigInfo.BackTime_SMS,
  445. SendSite = myconfigInfo.BackTime_Site
  446. };
  447. myconfig.ProductID = myconfigInfo.ProductID;
  448. myconfig.ModuleID = myconfigInfo.ModuleID;
  449. //保存配置信息
  450. return db_account.SaveConfig(account, myconfig);
  451. }
  452. //保存消息配置
  453. public bool SaveMessageInfo(Mssage_config Mssage_config, int account)
  454. {
  455. return db_account.SaveMessageInfo(Mssage_config, account);
  456. }
  457. //获取消息配置
  458. public Mssage_config GetMessageInfo(int account)
  459. {
  460. return db_account.GetMessageInfo(account);
  461. }
  462. //获取配置文件
  463. public NotefiyConfigInfo GetConfigInfo(int account)
  464. {
  465. return db_account.GetConfigInfo(account);
  466. }
  467. /// <summary>
  468. /// 查询结该人员的邮件及配置信息
  469. /// </summary>
  470. /// <param name="personId">人员信息ID</param>
  471. /// <returns></returns>
  472. public NotefiyConfigInfo GetNotifyConfigInfoByUserId(string personId)
  473. {
  474. return db_account.GetNotifyConfigInfoByUserId(personId);
  475. }
  476. /// <summary>
  477. /// 修改个人信息
  478. /// </summary>
  479. /// <returns></returns>
  480. public int UpdatePersonInfo(PersonBusinessModel model)
  481. {
  482. return db_account.UpdatePersonInfo(model);
  483. }
  484. /// <summary>
  485. /// 微信登陆(密码无需再次加密)
  486. /// </summary>
  487. /// <param name="account"></param>
  488. /// <param name="pwd"></param>
  489. /// <param name="ip"></param>
  490. /// <returns></returns>
  491. public LoginResult WeChatLogin(string account, string pwd, string ip)
  492. {
  493. var model = new AccountModel();
  494. model.ZH = account;
  495. model.MM = pwd;
  496. model.SetWhereColumns("账户", "密码");
  497. //登录认证
  498. var qmodel = db_account.Select(model);
  499. if (qmodel != null && qmodel.ID > 0)
  500. {//获取到账户,检查是否被锁定
  501. if (qmodel.ZT == 0)
  502. {//账户已被锁定
  503. return new LoginResult() { Success = false, Message = "账户已被锁定,请联系管理员。" };
  504. }
  505. else if (string.IsNullOrEmpty(qmodel.GSID) || string.IsNullOrEmpty(qmodel.RYID))
  506. {
  507. return new LoginResult() { Success = false, Message = "该账户没有关联到人员信息,缺少公司ID或人员ID." };
  508. }
  509. else
  510. {//登录成功,更新数据库登录状态
  511. //获取密钥
  512. var secret_key = DESEncrypt.DesEncrypt(model.ID.ToString());
  513. //写入到登录成功的cookie中(用户ID加密存储),有效五天
  514. CookiesHelper.AddCookie("UserID", secret_key,DateTime.Now.AddDays(5));
  515. model.ZX = 1;
  516. model.DLIP = ip;
  517. model.DLSJ = DateTime.Now;
  518. db_account.Update(model);
  519. var accountId = qmodel.ID.Value;
  520. //登录成功,更新缓存
  521. var r = _cacheUsers.ContainsKey(accountId);
  522. if (r)
  523. {//移除缓存即可
  524. UserInfo userInfo = null;
  525. _cacheUsers.TryRemove(accountId, out userInfo);
  526. }
  527. return new LoginResult() { Success = true, AccountID = model.ID.Value, Message = "登录成功" };
  528. }
  529. }
  530. else
  531. {//登录失败
  532. return new LoginResult() { Success = false, Message = "账户不存在,或密码不正确." };
  533. }
  534. }
  535. /// <summary>
  536. /// 根据个人id获取账户信息
  537. /// </summary>
  538. /// <param name="personId"></param>
  539. /// <returns></returns>
  540. public DataTable GetAccountInfo(string personId)
  541. {
  542. return db_account.GetAccountInfo(personId);
  543. }
  544. /// <summary>
  545. /// 根据手机号获取系统账号信息
  546. /// </summary>
  547. /// <param name="tel"></param>
  548. /// <returns></returns>
  549. public DataTable GetTelAccountInfo(string tel)
  550. {
  551. return db_account.GetTelAccountInfo(tel);
  552. }
  553. /// <summary>
  554. /// 查询字典表
  555. /// </summary>
  556. /// <param name="ID"></param>
  557. /// <returns></returns>
  558. public List<BasicDictionaryModel> SelectNature(string ID)
  559. {
  560. return db_account.SelectNature(ID);
  561. }
  562. }
  563. }