IcsFromERPJob
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.

137 lines
6.8 KiB

4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Quartz;
  6. using System.Data;
  7. namespace ICSSoft.FromERP
  8. {
  9. /// <summary>
  10. /// 用户
  11. /// </summary>
  12. public class ICSUser : IJob
  13. {
  14. private static object key = new object();
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. public void Execute(IJobExecutionContext context)
  17. {
  18. try
  19. {
  20. lock (key)
  21. {
  22. log.Info("开始……………………………………………………………………");
  23. Execute();
  24. log.Info("结束……………………………………………………………………");
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. log.Error(ex.ToString());
  30. }
  31. }
  32. public void Execute()
  33. {
  34. try
  35. {
  36. //string conERPStr = ICSHelper.GetERPConnectString();
  37. string conStr = ICSHelper.GetConnectString();
  38. string Namespace = this.GetType().Namespace;
  39. //string Class = this.GetType().Name;
  40. DataTable dt = ICSHelper.GetERPDB(conStr);
  41. foreach (DataRow dr in dt.Rows)
  42. {
  43. string erpName = ICSHelper.GetConfigString()["ERPDB"];
  44. string WorkPoint = dr["WorkPointCode"].ToString();
  45. string Class = this.GetType().Name + WorkPoint;
  46. erpName = string.Format(erpName, WorkPoint);
  47. #region SQL
  48. string sql = @"select aa.code AS UserCode,bb.name AS UserName,cc.Code AS DepCode,dd.Name AS DepName,
  49. NEWID() AS ID,CASE WHEN aa.Effective_IsEffective='1' THEN '1' ELSE '0' END AS StartFlag,
  50. aa.CreatedOn AS CreateDate,
  51. aa.CreatedBy AS CreateUserCode,
  52. aa.ModifiedBy AS ModifyUserCode,
  53. aa.ModifiedOn AS ModifyDate,
  54. GETDATE() AS MTIME
  55. ,ee.Code as F_Location
  56. INTO #TempUser
  57. from {1}.DBO.CBO_Operators aa with (nolock)
  58. left join {1}.DBO.CBO_Operators_Trl bb with (nolock) on aa.id=bb.id AND bb.SysMLFlag='zh-CN'
  59. left join {1}.DBO.cbo_department cc with (nolock) on aa.dept=cc.id
  60. left join {1}.DBO.CBO_Department_Trl dd with (nolock) on cc.id=dd.id AND dd.SysMLFlag='zh-CN'
  61. left join {1}.DBO.Base_Organization ee with (nolock) on aa.org=ee.id
  62. left join {1}.DBO.Base_Organization_trl ff with (nolock) on ee.id=ff.ID AND ff.SysMLFlag='zh-CN'
  63. where aa.ModifiedOn>=@LastTime AND ee.Code='{0}'
  64. ORDER BY aa.code";
  65. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempUser");
  66. sql += @"--删除数据
  67. --DELETE Sys_SRM_User where F_Account NOT IN (SELECT cPsn_Num FROM {1}.dbo.[hr_hi_person])and Sys_SRM_User.F_Account<>'admin'
  68. SELECT a.F_Account INTO #tableSys_SRM_User
  69. FROM Sys_SRM_User a With(NoLock)
  70. LEFT JOIN {1}.dbo.[CBO_Operators] b With(NoLock) ON a.F_Account=b.Code
  71. WHERE a.F_Location ='{0}' and b.Code IS NULL and a.F_Account<>'admin'
  72. DELETE Sys_SRM_User
  73. WHERE F_Location='{0}' and F_Account IN ( SELECT F_Account from #tableSys_SRM_User)
  74. ";
  75. Dictionary<string, string> values = new Dictionary<string, string>();
  76. values.Add("F_RealName", "a.UserName");
  77. values.Add("F_CreatorTime", "a.CreateDate");
  78. values.Add("F_CreatorUserId", "a.CreateUserCode");
  79. values.Add("F_LastModifyUserId", "a.ModifyUserCode");
  80. values.Add("F_LastModifyTime", "a.ModifyDate");
  81. values.Add("F_DepartmentId", "a.DepCode");
  82. //values.Add("DepName", "a.DepName");
  83. values.Add("F_EnabledMark", "a.StartFlag");
  84. //更新存在数据
  85. sql += ICSHelper.UpdateSQL("b", values)
  86. + @" #TempUser a
  87. INNER JOIN Sys_SRM_User b ON a.UserCode=b.F_Account and a.F_Location=b.F_Location
  88. WHERE a.F_Location='" + WorkPoint + "' ";
  89. values.Add("F_ID", "a.ID");
  90. values.Add("F_Account", "a.UserCode");
  91. //values.Add("UserPwd", "'NjMDK5c7vIs='");
  92. values.Add("F_Location", "'" + WorkPoint + "'");
  93. //插入新增数据
  94. sql += ICSHelper.InsertSQL("Sys_SRM_User", values)
  95. + @" #TempUser a
  96. LEFT JOIN Sys_SRM_User b ON a.UserCode=b.F_Account and a.F_Location=b.F_Location
  97. WHERE b.F_ID IS NULL and a.F_Location='" + WorkPoint + @"'
  98. ";
  99. Dictionary<string, string> value = new Dictionary<string, string>();
  100. value.Add("F_Id", "a.F_Id");
  101. value.Add("F_UserId", "a.F_Id");
  102. string UserSecretkey = SRMUser.Md5.md5(Common.CreateNo(), 16).ToLower();
  103. string pwd = SRMUser.Md5.md5(ICSSoft.FromERP.SRMUser.DESEncrypt.Encrypt(SRMUser.Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower();
  104. value.Add("F_UserPassword", "'" + pwd + "'");
  105. value.Add("F_UserSecretkey", "'" + UserSecretkey + "'");
  106. //插入新增数据->Sys_SRM_UserLogOn表
  107. sql += ICSHelper.InsertSQL("Sys_SRM_UserLogOn", value)
  108. + @"Sys_SRM_User a
  109. LEFT JOIN Sys_SRM_UserLogOn b ON a.F_Id=b.F_Id and a.F_Id=b.F_UserId
  110. WHERE b.F_ID IS NULL and a.F_Location='" + WorkPoint + @"'";
  111. sql += " DROP TABLE #TempUser" +
  112. " DROP TABLE #tableSys_SRM_User";
  113. sql = string.Format(sql, WorkPoint, erpName);
  114. ICSHelper.ExecuteDate(conStr, sql);
  115. SRMUser.User();
  116. #endregion
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. log.Error(ex.ToString());
  122. }
  123. }
  124. }
  125. }