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.

209 lines
8.6 KiB

3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
4 weeks ago
4 weeks ago
3 months ago
4 weeks ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
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.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 SyncVendor_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 #TempVendor ";
  60. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempVendor");
  61. sql += "DROP TABLE #TempVendor";
  62. ICSHelper.ExecuteDate(conStr, sql);
  63. // return;
  64. #region 获取Token
  65. var tokenUrl = ICSHelper.GetConfigString()["WeiMasTokenUrl"];
  66. var inputTokenDto = new TokenInputDto();
  67. inputTokenDto.route = "sj_token";
  68. inputTokenDto.grant_type = "password";
  69. inputTokenDto.client_id = ICSHelper.GetConfigString()["app_id"];
  70. inputTokenDto.client_secret = ICSHelper.GetConfigString()["app_secret"];
  71. inputTokenDto.timestamp = dtNow.ToMillisecondsTimeStamp();
  72. inputTokenDto.username = "api_token";
  73. inputTokenDto.sign = ICSHelper.ApiSign(inputTokenDto.client_id, inputTokenDto.client_secret, inputTokenDto.timestamp);
  74. inputTokenDto.clientExplorer = "";
  75. var tokenDto = HttpHelper.PostForm<TokenDto, TokenInputDto>(tokenUrl, inputTokenDto).Result;
  76. if (string.IsNullOrEmpty(tokenDto.access_token))
  77. {
  78. throw new Exception(" no access_token ");
  79. }
  80. // return;
  81. #endregion
  82. #region 获取子公司列表
  83. //var companyUrl = ICSHelper.GetConfigString()["WeiMasOrgUrl"] + @"/EnterpriseService/FromWeMec/GetSubCompanyList";
  84. //var companyDto = HttpHelper.HttpClientGet<WeiMasResult<List<SubCompanyDto>>>(companyUrl, "", tokenDto.access_token).Result;
  85. //if (companyDto.Data == null || companyDto.Data.Count == 0)
  86. //{
  87. // throw new Exception(" 获取子公司列表失败 ");
  88. //}
  89. // return;
  90. #endregion
  91. var input = new VendorInputDto();
  92. input.CustomerId = ICSHelper.GetConfigString()["WeiMasOrgCustomerId"];
  93. input.BeginTime = dtNowBegin.ToString("yyyy-MM-dd HH:mm:ss");
  94. input.EndTime = dtNow.ToString("yyyy-MM-dd HH:mm:ss");
  95. var inputObj = JsonConvert.SerializeObject(input);
  96. string url = ICSHelper.GetConfigString()["WeiMasErpUrl"] + @"/BasicAssistingService/FromWeMec/APIVenderToMES";
  97. var result = HttpHelper.HttpClientPost<WeiMasResult<List<VendorDto>>>(url, inputObj, token: tokenDto.access_token).Result;
  98. if (result.IsSuccess)
  99. {
  100. if (result.Data.Count == 0)
  101. {
  102. return;
  103. }
  104. var insertSql = @"
  105. if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempFromErp4Vendor') and type='U')
  106. DROP TABLE #tempFromErp4Vendor
  107. select
  108. *
  109. into #tempFromErp4Vendor
  110. from
  111. (";
  112. Dictionary<string, string> dics = new Dictionary<string, string>();
  113. for (int i = 0; i < result.Data.Count; i++)
  114. {
  115. var item = result.Data[i];
  116. dics.Clear();
  117. dics.Add("VenCode", item.VenCode);
  118. dics.Add("VenName", item.VenName);
  119. dics.Add("VenAbbName", item.VenAbbName);
  120. dics.Add("VenAddress", item.VenAddress);
  121. dics.Add("VenPerson", item.VenPerson);
  122. dics.Add("VenPhone", item.VenPhone);
  123. dics.Add("VenEmail", item.VenEmail);
  124. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  125. dics.Add("Free1", item.Free1);
  126. dics.Add("Free2", item.Free2);
  127. dics.Add("Free3", item.Free3);
  128. dics.Add("Free4", item.Free4);
  129. if (i == 0)
  130. {
  131. insertSql += " select ";
  132. }
  133. else
  134. {
  135. insertSql += " union all ";
  136. insertSql += " select ";
  137. }
  138. foreach (var keyValue in dics)
  139. {
  140. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " ,";
  141. }
  142. insertSql = insertSql.TrimEnd(',');
  143. }
  144. insertSql += " ) a";
  145. insertSql += @"
  146. insert into IcsVendor
  147. (VendorCode,VendorName,VendorShortName,ContactAddress,ContactUser,ContactPhone,ContactEmail,Default1,Default2,Default3,Default4
  148. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  149. select a.VenCode,VenName,VenAbbName,VenAddress,VenPerson,VenPhone,VenEmail , Free1,Free2,Free3,Free4
  150. ,Mtime,'job','job','{0}'
  151. from #tempFromErp4Vendor a
  152. left join IcsVendor b with(nolock) on a.VenCode=b.VendorCode
  153. where 1=1
  154. and b.id is null
  155. update IcsVendor
  156. set
  157. VendorCode=a.VenCode,VendorName=a.VenName,VendorShortName=a.VenAbbName,ContactAddress=a.VenAddress,ContactUser=a.VenPerson
  158. ,ContactEmail=a.VenEmail ,Default1=a.Free1,Default2=a.Free2,Default3=a.Free3,Default4=a.Free4
  159. ,LastModificationTime=a.Mtime,LastModifierUserId='job',LastModifierUserName='job'
  160. from #tempFromErp4Vendor a
  161. left join IcsVendor b with(nolock) on a.VenCode=b.VendorCode
  162. where 1=1
  163. and b.id is not null
  164. ";
  165. insertSql = string.Format(insertSql, TenantId);
  166. // insertSql += "DROP TABLE #tempFromErp4Vendor";
  167. ICSHelper.ExecuteDate(conStr, insertSql);
  168. }
  169. else
  170. {
  171. throw new Exception(result.Message);
  172. }
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. log.Error(ex.ToString());
  178. }
  179. }
  180. }
  181. }