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.

151 lines
6.7 KiB

4 months ago
4 months ago
  1. using Quartz;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. namespace ICSSoft.FromERP
  8. {
  9. /// <summary>
  10. /// 物料主分类(凯尔克)
  11. /// </summary>
  12. public class IcsMainCategoryCode : 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 TenantId = dr["TenantId"].ToString();//mes 组织
  45. string TenantCode = dr["TenantCode"].ToString();
  46. string ErpId = dr["ErpID"].ToString(); //erpID
  47. string Class = this.GetType().Name + TenantCode;
  48. erpName = string.Format(erpName, TenantId);
  49. #region SQL
  50. string sql = @" select a.code as ItemMainCategoryCode
  51. ,b.Name as ItemMainCategoryName
  52. ,b.Description as ItemMainCategoryDesc
  53. ,a.ModifiedOn MTime
  54. ,I.id as Org
  55. ,p.parentCode
  56. INTO #TempItemCategory
  57. from {1}.dbo.[CBO_Category] a
  58. left join {1}.dbo.CBO_CategoryType catt on catt.id=a.CategorySystem
  59. left join {1}.dbo.[CBO_Category_Trl] b on a.id=b.id AND b.SysMLFlag='zh-CN'
  60. left join {1}.dbo.[Base_Organization] AS I ON ( A.[Org] = I.[ID] )
  61. left join {1}.dbo.[Base_Organization_Trl] AS J ON ( I.[ID] = J.[ID] AND J.SysMLFlag='zh-CN')
  62. left join
  63. (
  64. SELECT
  65. a2.code parentCode
  66. ,a3.name parentName
  67. , A .code
  68. , A1.[Name]
  69. , a.level+1
  70. FROM {1}.dbo.Base_DefineValue AS A
  71. LEFT JOIN {1}.dbo.[Base_DefineValue_Trl] AS A1 ON(A1.SysMlFlag = 'zh-CN') AND(A.[ID] = A1.[ID])
  72. left join {1}.dbo.Base_DefineValue a2 on a2.id=a.ParentNode
  73. and a2.Effective_IsEffective=1
  74. and a2.Effective_DisableDate>= CAST ( CAST ( GETDATE( ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00'
  75. AND a2.Effective_EffectiveDate< CAST ( CAST ( dateadd( DAY, 1, GETDATE( ) ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00'
  76. LEFT JOIN {1}.dbo.[Base_DefineValue_Trl] AS A3 ON(A3.SysMlFlag = 'zh-CN') AND(A2.[ID] = A3.[ID])
  77. WHERE 1=1
  78. and A.[ValueSetDef] = (SELECT ID FROM {1}.dbo.Base_ValueSetDef_trl DD WHERE name = '' )
  79. and a.Effective_IsEffective=1
  80. and a.Effective_DisableDate>= CAST ( CAST ( GETDATE( ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00'
  81. AND a.Effective_EffectiveDate< CAST ( CAST ( dateadd( DAY, 1, GETDATE( ) ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00'
  82. --order by a.level+1, a2.code,A .code
  83. ) p ON a.code=p.code
  84. where 1=1
  85. and A.ModifiedOn>=@LastTime AND I.id={0}
  86. and catt.IsStock='1'
  87. ORDER BY A.Code
  88. IF NOT EXISTS(SELECT * FROM #TempItemCategory)
  89. RETURN";
  90. sql = string.Format(sql, ErpId, erpName);
  91. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempItemCategory");
  92. sql += @"--删除数据
  93. -- DELETE ICSINVENTORY WHERE ItemCode NOT IN (SELECT cInvCode FROM {1}.dbo.[CBO_ItemMaster])
  94. SELECT a.ItemMainCategoryCode INTO #tableItemCategory
  95. FROM IcsMainCategoryCode a With(NoLock)
  96. LEFT JOIN {1}.dbo.[CBO_Category] b With(NoLock) ON a.ItemMainCategoryCode=b.Code
  97. WHERE b.code is null
  98. and a.TenantId='{0}'
  99. AND b.Org={2}
  100. DELETE IcsMainCategoryCode
  101. WHERE TenantId='{0}' and ItemMainCategoryCode IN ( SELECT ItemMainCategoryCode from #tableItemCategory) ";
  102. Dictionary<string, string> values = new Dictionary<string, string>();
  103. values.Add("ItemMainCategoryCode", "a.ItemMainCategoryCode");//物料编号
  104. values.Add("ItemMainCategoryName", "a.ItemMainCategoryNAME");//物料名称
  105. values.Add("ItemMainCategoryDesc", "a.ItemMainCategoryDesc");//物料描述
  106. values.Add("LastModificationTime", "a.MTime");//操作时间
  107. values.Add("PCode", "a.parentCode");//操作人
  108. //更新存在数据
  109. sql += ICSHelper.UpdateSQL("b", values)
  110. + @" #TempItemCategory a
  111. INNER JOIN IcsMainCategoryCode b ON a.ItemMainCategoryCode=b.ItemMainCategoryCode
  112. WHERE b.TenantId='" + TenantId + "' and a.Org={2}";
  113. values.Add("CreationTime", "a.MTime");//操作人
  114. values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  115. values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  116. values.Add("TenantId", "'" + TenantId + "'");//站点
  117. //插入新增数据
  118. sql += ICSHelper.InsertSQL("IcsMainCategoryCode", values)
  119. + @" #TempItemCategory a
  120. LEFT JOIN IcsMainCategoryCode b ON a.ItemMainCategoryCode=b.ItemMainCategoryCode and b.TenantId='{0}'
  121. WHERE 1=1 and b.ItemMainCategoryCode is null
  122. and a.Org={2}
  123. ";
  124. sql += " DROP TABLE #TempItemCategory " +
  125. " DROP TABLE #tableItemCategory ";
  126. sql = string.Format(sql, TenantId, erpName,ErpId);
  127. // return;
  128. ICSHelper.ExecuteDate(conStr, sql);
  129. #endregion
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. log.Error(ex.ToString());
  135. }
  136. }
  137. }
  138. }