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.

213 lines
8.5 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
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.Extension;
  7. using ICSSoft.FromERP.Model;
  8. using Newtonsoft.Json;
  9. using Quartz;
  10. namespace ICSSoft.FromERP
  11. {
  12. /// <summary>
  13. /// 客户档案(威迈)
  14. /// </summary>
  15. public class SyncCustomer_WeiMas : IJob
  16. {
  17. private static object key = new object();
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. public void Execute(IJobExecutionContext context)
  20. {
  21. try
  22. {
  23. lock (key)
  24. {
  25. log.Info("开始……………………………………………………………………");
  26. Execute();
  27. log.Info("结束……………………………………………………………………");
  28. }
  29. }
  30. catch (Exception ex)
  31. {
  32. log.Error(ex.ToString());
  33. }
  34. }
  35. public void Execute()
  36. {
  37. try
  38. {
  39. string conStr = ICSHelper.GetConnectString();
  40. string Namespace = this.GetType().Namespace;
  41. //string Class = this.GetType().Name;
  42. DataTable dt = ICSHelper.GetERPDB(conStr);
  43. foreach (DataRow dr in dt.Rows)
  44. {
  45. var dtNowBegin = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间
  46. var dtNow = DateTime.Now;
  47. string erpName = ICSHelper.GetConfigString()["ERPDB"];
  48. string TenantId = dr["TenantId"].ToString();//mes 组织
  49. string TenantCode = dr["TenantCode"].ToString();
  50. string ErpId = dr["ErpID"].ToString(); //erpID
  51. string Class = this.GetType().Name + TenantCode;
  52. erpName = string.Format(erpName, TenantId);
  53. string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'";
  54. var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull();
  55. if (!lastDate.HasValue)
  56. {
  57. lastDate = dtNowBegin;
  58. }
  59. string sql = @" select '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempCustomer ";
  60. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempCustomer");
  61. sql += "DROP TABLE #TempCustomer";
  62. ICSHelper.ExecuteDate(conStr, sql);
  63. #region 获取Token
  64. var tokenUrl = ICSHelper.GetConfigString()["WeiMasTokenUrl"] + @"/identity/connect/token";
  65. var inputTokenDto = new TokenInputDto();
  66. inputTokenDto.route = "sj_token";
  67. inputTokenDto.grant_type = "password";
  68. inputTokenDto.client_id = ICSHelper.GetConfigString()["app_id"];
  69. inputTokenDto.client_secret = ICSHelper.GetConfigString()["app_secret"];
  70. inputTokenDto.timestamp = dtNow.ToMillisecondsTimeStamp();
  71. inputTokenDto.username = "api_token";
  72. inputTokenDto.sign = ICSHelper.ApiSign(inputTokenDto.client_id, inputTokenDto.client_secret, inputTokenDto.timestamp);
  73. inputTokenDto.clientExplorer = "";
  74. var tokenDto = HttpHelper.PostForm<TokenDto, TokenInputDto>(tokenUrl, inputTokenDto).Result;
  75. if (string.IsNullOrEmpty(tokenDto.access_token))
  76. {
  77. throw new Exception(" no access_token ");
  78. }
  79. // return;
  80. #endregion
  81. #region 获取子公司列表
  82. var companyUrl = ICSHelper.GetConfigString()["WeiMasOrgUrl"] + @"/EnterpriseService/FromWeMec/GetSubCompanyList";
  83. var companyDto = HttpHelper.HttpClientGet<WeiMasResult<List<SubCompanyDto>>>(companyUrl, "", tokenDto.access_token).Result;
  84. if (companyDto.Data == null || companyDto.Data.Count == 0)
  85. {
  86. throw new Exception(" 获取子公司列表失败 ");
  87. }
  88. // return;
  89. #endregion
  90. var input = new CustomerInputDto();
  91. input.CustomerId = companyDto.Data.First().customerId;
  92. input.BeginTime = dtNowBegin.ToString("yyyy-MM-dd HH:mm:ss");
  93. input.EndTime = dtNow.ToString("yyyy-MM-dd HH:mm:ss");
  94. var inputObj = JsonConvert.SerializeObject(input);
  95. string url = ICSHelper.GetConfigString()["WeiMasErpUrl"] + @"/BasicAssistingService/FromWeMec/APICustomerToMES";
  96. var result = HttpHelper.HttpClientPost<WeiMasResult<List<CustomerDto>>>(url, inputObj, token: tokenDto.access_token).Result;
  97. if (result.IsSuccess)
  98. {
  99. if (result.Data.Count == 0)
  100. {
  101. return;
  102. }
  103. var insertSql = @"
  104. if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempFromErp4Customer') and type='U')
  105. DROP TABLE #tempFromErp4Customer
  106. select
  107. *
  108. into #tempFromErp4Customer
  109. from
  110. (";
  111. Dictionary<string, string> dics = new Dictionary<string, string>();
  112. for (int i = 0; i < result.Data.Count; i++)
  113. {
  114. var item = result.Data[i];
  115. dics.Clear();
  116. dics.Add("CusCode", item.CusCode);
  117. dics.Add("CusName", item.CusName);
  118. dics.Add("CusAbbName", item.CusAbbName);
  119. dics.Add("CusAddress", item.CusAddress);
  120. dics.Add("CusPerson", item.CusPerson);
  121. dics.Add("CusPhone", item.CusPhone);
  122. dics.Add("CusEmail", item.CusEmail);
  123. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  124. dics.Add("Free1", item.Free1);
  125. dics.Add("Free2", item.Free2);
  126. dics.Add("Free3", item.Free3);
  127. dics.Add("Free4", item.Free4);
  128. if (i == 0)
  129. {
  130. insertSql += " select ";
  131. }
  132. else
  133. {
  134. insertSql += " union all ";
  135. insertSql += " select ";
  136. }
  137. foreach (var keyValue in dics)
  138. {
  139. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " ,";
  140. }
  141. insertSql = insertSql.TrimEnd(',');
  142. }
  143. insertSql += " ) a";
  144. insertSql += @"
  145. insert into IcsCustomer
  146. (CusCode,CusName,CusShortName,ContactUser,ContactPhone,ContactAddress,ContactEmail,Default1,Default2,Default3,Default4
  147. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  148. select a.CusCode,a.CusName,CusAbbName,CusPerson,CusPhone,CusAddress,CusEmail, Free1,Free2,Free3,Free4
  149. ,Mtime,'job','job','{0}'
  150. from #tempFromErp4Customer a
  151. left join IcsCustomer b with(nolock) on a.CusCode=b.CusCode
  152. where 1=1
  153. and b.id is null
  154. update IcsCustomer
  155. set
  156. CusName=a.CusName,CusShortName=a.CusAbbName,ContactUser=a.CusPerson,ContactPhone=a.CusPhone,ContactAddress=a.CusAddress,ContactEmail=a.CusEmail
  157. ,Default1=a.Free1,Default2=a.Free2,Default3=a.Free3,Default4=a.Free4
  158. ,LastModificationTime=a.Mtime,LastModifierUserId='job',LastModifierUserName='job'
  159. from #tempFromErp4Customer a
  160. left join IcsCustomer b with(nolock) on a.CusCode=b.CusCode
  161. where 1=1
  162. and b.id is not null
  163. ";
  164. insertSql = string.Format(insertSql, TenantId);
  165. ICSHelper.ExecuteDate(conStr, insertSql);
  166. }
  167. else
  168. {
  169. throw new Exception(result.Message);
  170. }
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. log.Error(ex.ToString());
  176. }
  177. }
  178. }
  179. }