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.

156 lines
6.0 KiB

3 months ago
3 months ago
3 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 SyncVendor_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 #TempVendor ";
  58. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempVendor");
  59. sql += "DROP TABLE #TempVendor";
  60. ICSHelper.ExecuteDate(conStr, sql);
  61. return;
  62. var input = new VendorInputDto();
  63. input.BeginTime = lastDate.Value;
  64. input.EndTime = new DateTime(9999, 1, 1, 0, 0, 0);
  65. var inputObj = JsonConvert.SerializeObject(input);
  66. string url = ICSHelper.GetConfigString()["WeiMasErpUrl"];
  67. var result = HttpHelper.HttpClientPost<WeiMasResult<List<VendorDto>>>(url, inputObj).Result;
  68. if (result.IsSuccess)
  69. {
  70. if (result.Data.Count == 0)
  71. {
  72. return;
  73. }
  74. var insertSql = @"select
  75. *
  76. into #tempFromErp4Vendor
  77. from
  78. (";
  79. Dictionary<string, string> dics = new Dictionary<string, string>();
  80. for (int i = 0; i < result.Data.Count; i++)
  81. {
  82. var item = result.Data[i];
  83. dics.Add("VenCode", item.VenCode);
  84. dics.Add("VenName", item.VenName);
  85. dics.Add("VenAbbName", item.VenAbbName);
  86. dics.Add("VenAddress", item.VenAddress);
  87. dics.Add("VenPerson", item.VenPerson);
  88. dics.Add("VenPhone", item.VenPhone);
  89. dics.Add("VenEmail", item.VenEmail);
  90. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  91. dics.Add("Free1", item.Free1);
  92. dics.Add("Free2", item.Free2);
  93. dics.Add("Free3", item.Free3);
  94. dics.Add("Free4", item.Free4);
  95. if (i == 0)
  96. {
  97. insertSql += " select ";
  98. }
  99. else
  100. {
  101. insertSql += " union all ";
  102. insertSql += " select ";
  103. }
  104. foreach (var keyValue in dics)
  105. {
  106. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " , ";
  107. }
  108. insertSql = insertSql.TrimEnd(',');
  109. }
  110. insertSql += " ) a";
  111. insertSql += @"
  112. insert into IcsVendor
  113. (VendorCode,VendorName,VendorShortName,ContactAddress,ContactUser,ContactPhone,ContactEmail,Default1,Default2,Default3,Default4
  114. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  115. select a.VenCode,VenName,VenAbbName,VenAddress,VenPerson,VenPhone,VenEmail , Free1,Free2,Free3,Free4
  116. ,Mtime,'job','job','{0}'
  117. from #tempFromErp4Vendor a
  118. left join IcsVendor b with(nolock) on a.VenCode=b.VendorCode
  119. where 1=1
  120. and b.id is null";
  121. insertSql = string.Format(insertSql, TenantId);
  122. insertSql += "DROP TABLE #tempFromErp4Vendor";
  123. ICSHelper.ExecuteDate(conStr, insertSql);
  124. }
  125. else
  126. {
  127. throw new Exception(result.Message);
  128. }
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. log.Error(ex.ToString());
  134. }
  135. }
  136. }
  137. }