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.

223 lines
9.4 KiB

1 month ago
4 weeks ago
1 week ago
1 week ago
3 weeks ago
3 weeks ago
3 weeks 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 SyncSO_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 #TempSaleOrder ";
  60. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempSaleOrder");
  61. sql += "DROP TABLE #TempSaleOrder";
  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 QuerySOInputDto();
  92. input.CustomerId = ICSHelper.GetConfigString()["WeiMasOrgCustomerId"];
  93. input.BeginTime = dtNow.AddDays(-90);
  94. input.EndTime = dtNow;
  95. var inputObj = JsonConvert.SerializeObject(input);
  96. string url = ICSHelper.GetConfigString()["Doc_SOUrl"];
  97. var result = HttpHelper.HttpClientPost<WeiMasResult<List<SODto>>>(url, inputObj, token: tokenDto.access_token).Result;
  98. if (result.Status==200)
  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..#tempFromErp4SaleOrder') and type='U')
  106. DROP TABLE #tempFromErp4SaleOrder
  107. select
  108. *
  109. into #tempFromErp4SaleOrder
  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("SOCODE", item.SOCODE);
  118. dics.Add("SOTYPE", item.SOTYPE);
  119. dics.Add("SOORDERDATE", item.SOORDERDATE.ToString("yyyy-MM-dd HH:mm:ss"));
  120. dics.Add("RequestedDeliveryDate", item.RequestedDeliveryDate.ToString("yyyy-MM-dd HH:mm:ss"));
  121. dics.Add("CUSCODE", item.CUSCODE);
  122. dics.Add("CUSNAME", item.CUSNAME);
  123. dics.Add("SOLineNo", item.SOLineNo.ToString());
  124. dics.Add("ITEMCODE", item.ITEMCODE);
  125. dics.Add("CASNo", item.CASNo);
  126. dics.Add("Qty", item.Qty.ToString());
  127. dics.Add("tax", item.Tax.ToString());
  128. dics.Add("SQCode", item.SQCode==null?"": item.SQCode.Trim());
  129. dics.Add("SQLineNo", item.SQLineNo == null ? "" : item.SQLineNo.Trim());
  130. dics.Add("ProduceArea", item.ITEMCODE.Substring(item.ITEMCODE.Length-2,2));
  131. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  132. dics.Add("Free1", item.Free1);
  133. dics.Add("Free2", item.Free2);
  134. dics.Add("Free3", item.Free3);
  135. dics.Add("Free4", item.Free4);
  136. if (i == 0)
  137. {
  138. insertSql += " select ";
  139. }
  140. else
  141. {
  142. insertSql += " union all ";
  143. insertSql += " select ";
  144. }
  145. foreach (var keyValue in dics)
  146. {
  147. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " ,";
  148. }
  149. insertSql = insertSql.TrimEnd(',');
  150. }
  151. insertSql += " ) a";
  152. insertSql += @"
  153. insert into IcsSaleOrder
  154. (SoCode,SoType,[SoOrderDate],[RequestedDeliveryDate],[CusCode],[CusName],[SoLineNo],[ItemCode],[CASNo],[Qty]
  155. ,[Tax],[SqCode],[SqLineNo],ProduceArea
  156. ,Default1,Default2,Default3,Default4
  157. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  158. select a.SoCode,a.SoType,a.SOORDERDATE,a.RequestedDeliveryDate,a.CUSCODE,a.CUSNAME,a.SOLineNo ,a.ITEMCODE,a.CASNo,a.Qty
  159. ,a.tax,a.SQCode,a.SQLineNo
  160. ,a.ProduceArea
  161. , a.Free1,a.Free2,a.Free3,a.Free4
  162. ,Mtime,'job','job','{0}'
  163. from #tempFromErp4SaleOrder a
  164. left join IcsSaleOrder b with(nolock) on a.SoCode=b.SoCode and a.SoLineNo=b.SoLineNo
  165. where 1=1
  166. and b.id is null
  167. update IcsSaleOrder
  168. set
  169. SoType=a.SoType,SoOrderDate=a.SoOrderDate,RequestedDeliveryDate=a.RequestedDeliveryDate,CusCode=a.CusCode,CusName=a.CusName
  170. ,SoLineNo=a.SoLineNo
  171. ,ItemCode=a.ItemCode,CASNo=a.CASNo,Qty=a.Qty,Tax=a.Tax,SqCode=a.SqCode,SqLineNo=a.SqLineNo
  172. ,Default1=a.Free1,Default2=a.Free2,Default3=a.Free3,Default4=a.Free4
  173. ,LastModificationTime=a.Mtime,LastModifierUserId='job',LastModifierUserName='job'
  174. from #tempFromErp4SaleOrder a
  175. left join IcsSaleOrder b with(nolock) on a.SoCode=b.SoCode and a.SoLineNo=b.SoLineNo
  176. where 1=1
  177. and b.id is not null
  178. ";
  179. insertSql = string.Format(insertSql, TenantId);
  180. // insertSql += "DROP TABLE #tempFromErp4Vendor";
  181. ICSHelper.ExecuteDate(conStr, insertSql);
  182. }
  183. else
  184. {
  185. throw new Exception(result.Message);
  186. }
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. log.Error(ex.ToString());
  192. }
  193. }
  194. }
  195. }