纽威
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.

249 lines
11 KiB

3 years 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.Create();
  58. }
  59. service.SubmitForm(userEntity, userLogOnEntity, keyValue);
  60. }
  61. public void UpdateForm(UserEntity userEntity)
  62. {
  63. service.Update(userEntity);
  64. }
  65. public UserEntity CheckLogins(string username, string password, string WorkPoint)
  66. {
  67. UserEntity userEntity = service.FindEntity(t => t.F_Account == username);
  68. if (userEntity != null)
  69. {
  70. if (userEntity.F_EnabledMark == true)
  71. {
  72. UserLogOnEntity userLogOnEntity = userLogOnApp.GetForm(userEntity.F_Id);
  73. string dbPassword = Md5.md5(DESEncrypt.Encrypt(password.ToLower(), userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
  74. if (dbPassword == userLogOnEntity.F_UserPassword)
  75. {
  76. DateTime lastVisitTime = DateTime.Now;
  77. int LogOnCount = (userLogOnEntity.F_LogOnCount).ToInt() + 1;
  78. if (userLogOnEntity.F_LastVisitTime != null)
  79. {
  80. userLogOnEntity.F_PreviousVisitTime = userLogOnEntity.F_LastVisitTime.ToDate();
  81. }
  82. userLogOnEntity.F_LastVisitTime = lastVisitTime;
  83. userLogOnEntity.F_LogOnCount = LogOnCount;
  84. userLogOnApp.UpdateForm(userLogOnEntity);
  85. return userEntity;
  86. }
  87. else
  88. {
  89. throw new Exception("密码不正确,请重新输入");
  90. }
  91. }
  92. else
  93. {
  94. throw new Exception("账户被系统锁定,请联系管理员");
  95. }
  96. }
  97. else
  98. {
  99. throw new Exception("账户不存在,请重新输入");
  100. }
  101. }
  102. /// <summary>
  103. /// 多站点
  104. /// </summary>
  105. /// <param name="username"></param>
  106. /// <param name="password"></param>
  107. /// <param name="WorkPoint"></param>
  108. /// <returns></returns>
  109. public List<UserEntity> CheckLogin(string username, string password, string WorkPoint)
  110. {
  111. List<UserEntity> userEntitys = service.FindList("SELECT * FROM Sys_SRM_User where F_Account='" + username + "'");
  112. for (int i = 0; i < userEntitys.Count; i++)
  113. {
  114. UserEntity userEntityss = userEntitys[i];
  115. }
  116. UserEntity userEntity = service.FindEntity(w => w.F_Account == username);
  117. if (userEntity != null)
  118. {
  119. if (userEntity.F_EnabledMark == true)
  120. {
  121. UserLogOnEntity userLogOnEntity = userLogOnApp.GetForm(userEntity.F_Id);
  122. string dbPassword = Md5.md5(DESEncrypt.Encrypt(password.ToLower(), userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();
  123. if (dbPassword == userLogOnEntity.F_UserPassword)
  124. {
  125. DateTime lastVisitTime = DateTime.Now;
  126. int LogOnCount = (userLogOnEntity.F_LogOnCount).ToInt() + 1;
  127. if (userLogOnEntity.F_LastVisitTime != null)
  128. {
  129. userLogOnEntity.F_PreviousVisitTime = userLogOnEntity.F_LastVisitTime.ToDate();
  130. }
  131. userLogOnEntity.F_LastVisitTime = lastVisitTime;
  132. userLogOnEntity.F_LogOnCount = LogOnCount;
  133. userLogOnApp.UpdateForm(userLogOnEntity);
  134. return userEntitys;
  135. }
  136. else
  137. {
  138. throw new Exception("密码不正确,请重新输入");
  139. }
  140. }
  141. else
  142. {
  143. throw new Exception("账户被系统锁定,请联系管理员");
  144. }
  145. }
  146. else
  147. {
  148. throw new Exception("账户不存在,请重新输入");
  149. }
  150. }
  151. public void AgentUserAndVenCode()
  152. {
  153. string sql = @"SELECT UserCode,UserName,WorkPointCode FROM dbo.Sys_User
  154. WHERE UserCode NOT IN (SELECT F_Account FROM dbo.Sys_SRM_User)";
  155. DataTable dtUser = SqlHelper.GetDataTableBySql(sql);
  156. foreach (DataRow dr in dtUser.Rows)
  157. {
  158. string sqlUser = string.Empty;
  159. string NewGuid = Common.GuId();
  160. sqlUser = @"INSERT INTO dbo.Sys_SRM_User
  161. ( F_Id ,F_Account ,F_RealName ,F_NickName ,
  162. F_RoleId ,F_IsAdministrator , F_EnabledMark ,
  163. F_CreatorTime ,F_CreatorUserId ,F_Location ,
  164. F_VenCode)
  165. SELECT '"+ NewGuid + @"',UserCode,UserName,UserName,
  166. '5130ce87-a5ed-409f-b035-9277f65e1d7f',0,1,
  167. GETDATE(),'9f2ec079-7d0f-4fe2-90ab-8b09a8302aba','" + dr["WorkPointCode"].ToString() + @"',UserCode
  168. FROM dbo.Sys_User WHERE UserCode='" +dr["UserCode"].ToString()+ "' and WorkPointCode='" + dr["WorkPointCode"].ToString() + @"'";
  169. try
  170. {
  171. SqlHelper.ExecuteNonQuery(sqlUser);
  172. }
  173. catch (Exception ex)
  174. {
  175. throw new Exception(ex.Message);
  176. }
  177. string UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
  178. string pwd = Md5.md5(DESEncrypt.Encrypt(Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower();
  179. sqlUser = @"INSERT INTO dbo.Sys_SRM_UserLogOn
  180. ( F_Id ,F_UserId ,F_UserPassword ,F_UserSecretkey
  181. )
  182. VALUES ( '" + NewGuid + @"','" + NewGuid + @"','"+ pwd + "','" + UserSecretkey + "')";
  183. try
  184. {
  185. SqlHelper.ExecuteNonQuery(sqlUser);
  186. }
  187. catch (Exception ex)
  188. {
  189. throw new Exception(ex.Message);
  190. }
  191. }
  192. //供应商同步
  193. sql = @"SELECT cVenCode,cVenName,WorkPoint FROM dbo.ICSVendor
  194. WHERE cVenCode NOT IN (SELECT F_Account FROM dbo.Sys_SRM_User)";
  195. DataTable dtVendor = SqlHelper.GetDataTableBySql(sql);
  196. foreach (DataRow dr in dtVendor.Rows)
  197. {
  198. string sqlUser = string.Empty;
  199. string NewGuid = Common.GuId();
  200. sqlUser = @"INSERT INTO dbo.Sys_SRM_User
  201. ( F_Id ,F_Account ,F_RealName ,F_NickName ,
  202. F_RoleId ,F_IsAdministrator , F_EnabledMark ,
  203. F_CreatorTime ,F_CreatorUserId ,F_Location ,
  204. F_VenCode)
  205. SELECT '" + NewGuid + @"',cVenCode,cVenName,cVenName,
  206. '2691AB91-3010-465F-8D92-60A97425A45E',0,1,
  207. GETDATE(),'9f2ec079-7d0f-4fe2-90ab-8b09a8302aba','" + dr["WorkPoint"].ToString() + @"',''
  208. FROM dbo.ICSVendor WHERE cVenCode='" + dr["cVenCode"].ToString() + "' and WorkPoint='" + dr["WorkPoint"].ToString() + @"'";
  209. try
  210. {
  211. SqlHelper.ExecuteNonQuery(sqlUser);
  212. }
  213. catch (Exception ex)
  214. {
  215. throw new Exception(ex.Message);
  216. }
  217. string UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
  218. string pwd = Md5.md5(DESEncrypt.Encrypt(Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower();
  219. sqlUser = @"INSERT INTO dbo.Sys_SRM_UserLogOn
  220. ( F_Id ,F_UserId ,F_UserPassword ,F_UserSecretkey
  221. )
  222. VALUES ( '" + NewGuid + @"','" + NewGuid + @"','" + pwd + "','" + UserSecretkey + "')";
  223. try
  224. {
  225. SqlHelper.ExecuteNonQuery(sqlUser);
  226. }
  227. catch (Exception ex)
  228. {
  229. throw new Exception(ex.Message);
  230. }
  231. }
  232. //供应商同步
  233. }
  234. }
  235. }