You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.9 KiB

3 weeks ago
  1. using NFine.Code;
  2. using NFine.Domain.Entity.SystemManage;
  3. using NFine.Domain.IRepository.SystemManage;
  4. using NFine.Repository.SystemManage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Data;
  9. using System.Data.Common;
  10. using System.Text;
  11. using NFine.Data.Extensions;
  12. namespace NFine.Application.SystemManage
  13. {
  14. public class UserApp
  15. {
  16. private IUserRepository service = new UserRepository();
  17. private UserLogOnApp userLogOnApp = new UserLogOnApp();
  18. public List<UserEntity> GetList()
  19. {
  20. return service.IQueryable().OrderBy(t => t.F_CreatorTime).ToList();
  21. }
  22. public List<UserEntity> GetList(Pagination pagination, string keyword)
  23. {
  24. var expression = ExtLinq.True<UserEntity>();
  25. if (!string.IsNullOrEmpty(keyword))
  26. {
  27. expression = expression.And(t => t.F_Account.Contains(keyword));
  28. expression = expression.Or(t => t.F_RealName.Contains(keyword));
  29. expression = expression.Or(t => t.F_MobilePhone.Contains(keyword));
  30. }
  31. expression = expression.And(t => t.F_Account != "admin");
  32. if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode == "Vendor")
  33. {
  34. string UserCode = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  35. expression = expression.And(t => t.F_Account== UserCode);
  36. }
  37. return service.FindList(expression, pagination);
  38. }
  39. public UserEntity GetForm(string keyValue)
  40. {
  41. if (keyValue == "SYS")
  42. keyValue = NFine.Code.OperatorProvider.Provider.GetCurrent().UserId;
  43. return service.FindEntity(keyValue);
  44. }
  45. public void DeleteForm(string keyValue)
  46. {
  47. service.DeleteForm(keyValue);
  48. }
  49. public void SubmitForm(UserEntity userEntity, UserLogOnEntity userLogOnEntity, string keyValue)
  50. {
  51. if (!string.IsNullOrEmpty(keyValue))
  52. {
  53. userEntity.Modify(keyValue);
  54. }
  55. else
  56. {
  57. UserEntity UserList = service.FindEntity(t => t.F_Account == userEntity.F_Account && t.F_Location == userEntity.F_Location);
  58. if (UserList != null)
  59. {
  60. throw new Exception("该用户已在站点:" + userEntity.F_Location + "存在!");
  61. }
  62. userEntity.Create();
  63. }
  64. service.SubmitForm(userEntity, userLogOnEntity, keyValue);
  65. }
  66. public void UpdateForm(UserEntity userEntity)
  67. {
  68. service.Update(userEntity);
  69. }
  70. public UserEntity CheckLogin(string username, string password, string WorkPoint)
  71. {
  72. UserEntity userEntity;
  73. if (username=="admin")
  74. {
  75. userEntity = service.FindEntity(t => t.F_Account == username );
  76. }
  77. else
  78. {
  79. userEntity = service.FindEntity(t => t.F_Account == username && t.F_Location == WorkPoint);
  80. }
  81. if (userEntity != null)
  82. {
  83. if (userEntity.F_EnabledMark == true)
  84. {
  85. UserLogOnEntity userLogOnEntity = userLogOnApp.GetForm(userEntity.F_Id);
  86. string dbPassword = Md5.md5(DESEncrypt.Encrypt(password.ToLower(), userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
  87. if (dbPassword == userLogOnEntity.F_UserPassword)
  88. {
  89. DateTime lastVisitTime = DateTime.Now;
  90. int LogOnCount = (userLogOnEntity.F_LogOnCount).ToInt() + 1;
  91. if (userLogOnEntity.F_LastVisitTime != null)
  92. {
  93. userLogOnEntity.F_PreviousVisitTime = userLogOnEntity.F_LastVisitTime.ToDate();
  94. }
  95. userLogOnEntity.F_LastVisitTime = lastVisitTime;
  96. userLogOnEntity.F_LogOnCount = LogOnCount;
  97. userLogOnApp.UpdateForm(userLogOnEntity);
  98. return userEntity;
  99. }
  100. else
  101. {
  102. throw new Exception("密码不正确,请重新输入");
  103. }
  104. }
  105. else
  106. {
  107. throw new Exception("账户被系统锁定,请联系管理员");
  108. }
  109. }
  110. else
  111. {
  112. throw new Exception("账户不存在,请重新输入");
  113. }
  114. }
  115. public void AgentUserAndVenCode()
  116. {
  117. string sql = @"SELECT UserCode,UserName,WorkPointCode FROM dbo.Sys_User
  118. WHERE UserCode NOT IN (SELECT F_Account FROM dbo.Sys_SRM_User)";
  119. DataTable dtUser = SqlHelper.GetDataTableBySql(sql);
  120. foreach (DataRow dr in dtUser.Rows)
  121. {
  122. string sqlUser = string.Empty;
  123. string NewGuid = Common.GuId();
  124. sqlUser = @"INSERT INTO dbo.Sys_SRM_User
  125. ( F_Id ,F_Account ,F_RealName ,F_NickName ,
  126. F_RoleId ,F_IsAdministrator , F_EnabledMark ,
  127. F_CreatorTime ,F_CreatorUserId ,F_Location ,
  128. F_VenCode)
  129. SELECT '"+ NewGuid + @"',UserCode,UserName,UserName,
  130. '5130ce87-a5ed-409f-b035-9277f65e1d7f',0,1,
  131. GETDATE(),'9f2ec079-7d0f-4fe2-90ab-8b09a8302aba','"+dr["WorkPointCode"].ToString() +@"',''
  132. FROM dbo.Sys_User WHERE UserCode='"+dr["UserCode"].ToString()+ "' and WorkPointCode='" + dr["WorkPointCode"].ToString() + @"'";
  133. try
  134. {
  135. SqlHelper.ExecuteNonQuery(sqlUser);
  136. }
  137. catch (Exception ex)
  138. {
  139. throw new Exception(ex.Message);
  140. }
  141. string UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
  142. string pwd = Md5.md5(DESEncrypt.Encrypt(Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower();
  143. sqlUser = @"INSERT INTO dbo.Sys_SRM_UserLogOn
  144. ( F_Id ,F_UserId ,F_UserPassword ,F_UserSecretkey
  145. )
  146. VALUES ( '" + NewGuid + @"','" + NewGuid + @"','"+ pwd + "','" + UserSecretkey + "')";
  147. try
  148. {
  149. SqlHelper.ExecuteNonQuery(sqlUser);
  150. }
  151. catch (Exception ex)
  152. {
  153. throw new Exception(ex.Message);
  154. }
  155. }
  156. }
  157. }
  158. }