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.

164 lines
6.7 KiB

4 months ago
4 months ago
4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using ICSSoft.FromERP.Model;
  7. using Newtonsoft.Json;
  8. using Quartz;
  9. namespace ICSSoft.FromERP
  10. {
  11. /// <summary>
  12. /// 物料档案(威迈)
  13. /// </summary>
  14. public class SyncItem_WeiMas : IJob
  15. {
  16. private static object key = new object();
  17. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  18. public void Execute(IJobExecutionContext context)
  19. {
  20. try
  21. {
  22. lock (key)
  23. {
  24. log.Info("开始……………………………………………………………………");
  25. Execute();
  26. log.Info("结束……………………………………………………………………");
  27. }
  28. }
  29. catch (Exception ex)
  30. {
  31. log.Error(ex.ToString());
  32. }
  33. }
  34. public void Execute()
  35. {
  36. try
  37. {
  38. string conStr = ICSHelper.GetConnectString();
  39. string Namespace = this.GetType().Namespace;
  40. //string Class = this.GetType().Name;
  41. DataTable dt = ICSHelper.GetERPDB(conStr);
  42. foreach (DataRow dr in dt.Rows)
  43. {
  44. var dtNow = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间
  45. string erpName = ICSHelper.GetConfigString()["ERPDB"];
  46. string TenantId = dr["TenantId"].ToString();//mes 组织
  47. string TenantCode = dr["TenantCode"].ToString();
  48. string ErpId = dr["ErpID"].ToString(); //erpID
  49. string Class = this.GetType().Name + TenantCode;
  50. erpName = string.Format(erpName, TenantId);
  51. string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'";
  52. var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull();
  53. if (!lastDate.HasValue)
  54. {
  55. lastDate = dtNow;
  56. }
  57. string sql = @" select '"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempItem ";
  58. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempItem");
  59. sql += "DROP TABLE #TempItem";
  60. ICSHelper.ExecuteDate(conStr, sql);
  61. var input = new ItemInputDto();
  62. input.BeginTime = lastDate.Value;
  63. input.EndTime = new DateTime(9999, 1, 1, 0, 0, 0);
  64. var inputObj = JsonConvert.SerializeObject(input);
  65. string url = ICSHelper.GetConfigString()["WeiMasErpUrl"];
  66. var result = HttpHelper.HttpClientPost<WeiMasResult<List<ItemDto>>>(url, inputObj).Result;
  67. if (result.IsSuccess)
  68. {
  69. if (result.Data.Count == 0)
  70. {
  71. return;
  72. }
  73. var insertSql = @"select
  74. *
  75. into #tempFromErp4ItemCode
  76. from
  77. (";
  78. Dictionary<string, string> dics = new Dictionary<string, string>();
  79. for (int i = 0; i < result.Data.Count; i++)
  80. {
  81. var item = result.Data[i];
  82. dics.Add("ItemCode", item.ItemCode);
  83. dics.Add("ItemName", item.ItemName);
  84. dics.Add("ItemStd", item.ItemStd);
  85. dics.Add("ItemDesc", item.ItemDesc);
  86. dics.Add("ItemCasNo", item.ItemCasNo);
  87. dics.Add("ItemMolecularFormula", item.ItemMolecularFormula);
  88. dics.Add("ItemAttribute", item.ItemAttribute);
  89. dics.Add("ItemMainCategoryCode", item.ItemMainCategoryCode);
  90. dics.Add("ITEMUnit", item.ITEMUnit);
  91. dics.Add("ITEMMODEL", item.ITEMMODEL);
  92. dics.Add("GR", item.GR);
  93. dics.Add("SGFS", item.SGFS);
  94. dics.Add("TGFS", item.TGFS);
  95. dics.Add("WG", item.WG);
  96. dics.Add("NI", item.NI);
  97. dics.Add("MA", item.MA);
  98. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  99. dics.Add("Other", item.Other);
  100. dics.Add("Free1", item.Free1);
  101. dics.Add("Free2", item.Free2);
  102. dics.Add("Free3", item.Free3);
  103. dics.Add("Free4", item.Free4);
  104. if (i == 0)
  105. {
  106. insertSql += " select ";
  107. }
  108. else
  109. {
  110. insertSql += " union all ";
  111. insertSql += " select ";
  112. }
  113. foreach (var keyValue in dics)
  114. {
  115. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " , ";
  116. }
  117. insertSql = insertSql.TrimEnd(',');
  118. }
  119. insertSql += " ) a";
  120. insertSql += @"
  121. insert into IcsInventory
  122. (ItemCode,ItemName,ItemDesc,ItemStd,ItemType,ItemControlType,ItemContype
  123. ,ItemAttribute,ItemUnit,ItemMainCategoryCode,ItemModel,ItemCasNo,ItemMolecularFormula,GR,SGFS,TGFS,WG,NI,MA,Other,Default1,Default2,Default3,Default4
  124. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  125. select a.ItemCode,ItemName,ItemDesc,ItemStd,'itemtype_finishedproduct','item_control_lot','item_control_nocontrol'
  126. ,ItemAttribute,ITEMUnit,ItemMainCategoryCode,ITEMMODEL,ItemCasNo,ItemMolecularFormula,GR,SGFS,TGFS,WG,NI,MA,Other,Free1,Free2,Free3,Free4
  127. ,Mtime,'job','job','{0}'
  128. from #tempFromErp4ItemCode a
  129. left join IcsInventory b with(nolock) on a.itemcode=b.itemcode
  130. where 1=1
  131. and b.id is null";
  132. insertSql = string.Format(insertSql, TenantId);
  133. ICSHelper.ExecuteDate(conStr, insertSql);
  134. }
  135. else
  136. {
  137. throw new Exception(result.Message);
  138. }
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. log.Error(ex.ToString());
  144. }
  145. }
  146. }
  147. }