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.

154 lines
5.9 KiB

1 month ago
1 month ago
1 month 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 SyncCustomer_WeiMas
  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 #TempCustomer ";
  58. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempCustomer");
  59. sql += "DROP TABLE #TempCustomer";
  60. ICSHelper.ExecuteDate(conStr, sql);
  61. var input = new CustomerInputDto();
  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<CustomerDto>>>(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 #tempFromErp4Customer
  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("CusCode", item.CusCode);
  83. dics.Add("CusName", item.CusName);
  84. dics.Add("CusAbbName", item.CusAbbName);
  85. dics.Add("CusAddress", item.CusAddress);
  86. dics.Add("CusPerson", item.CusPerson);
  87. dics.Add("CusPhone", item.CusPhone);
  88. dics.Add("CusEmail", item.CusEmail);
  89. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  90. dics.Add("Free1", item.Free1);
  91. dics.Add("Free2", item.Free2);
  92. dics.Add("Free3", item.Free3);
  93. dics.Add("Free4", item.Free4);
  94. if (i == 0)
  95. {
  96. insertSql += " select ";
  97. }
  98. else
  99. {
  100. insertSql += " union all ";
  101. insertSql += " select ";
  102. }
  103. foreach (var keyValue in dics)
  104. {
  105. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " , ";
  106. }
  107. insertSql = insertSql.TrimEnd(',');
  108. }
  109. insertSql += " ) a";
  110. insertSql += @"
  111. insert into IcsCustomer
  112. (CusCode,CusName,CusShortName,ContactUser,ContactPhone,ContactAddress,ContactEmail,Default1,Default2,Default3,Default4
  113. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  114. select a.CusCode,CusAbbName,CusPerson,CusPhone,CusAddress,CusEmail, Free1,Free2,Free3,Free4
  115. ,Mtime,'job','job','{0}'
  116. from #tempFromErp4Customer a
  117. left join IcsCustomer b with(nolock) on a.CusCode=b.CusCode
  118. where 1=1
  119. and b.id is null";
  120. insertSql = string.Format(insertSql, TenantId);
  121. ICSHelper.ExecuteDate(conStr, insertSql);
  122. }
  123. else
  124. {
  125. throw new Exception(result.Message);
  126. }
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. log.Error(ex.ToString());
  132. }
  133. }
  134. }
  135. }