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.

226 lines
9.6 KiB

3 months 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
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 SyncItem_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 #TempItem ";
  60. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempItem");
  61. sql += "DROP TABLE #TempItem";
  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 ItemInputDto();
  91. input.CustomerId = companyDto.Data.First().customerId;
  92. input.CASNo = "";
  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/APIItemToMES";
  97. var result = HttpHelper.HttpClientPost<WeiMasResult<List<ItemDto>>>(url, inputObj,token: tokenDto.access_token).Result;
  98. if (result.IsSuccess)
  99. {
  100. if (result.Data.Count == 0)
  101. {
  102. return;
  103. }
  104. // return;
  105. var insertSql = @"
  106. if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempFromErp4ItemCode') and type='U')
  107. DROP TABLE #tempFromErp4ItemCode
  108. select
  109. *
  110. into #tempFromErp4ItemCode
  111. from
  112. (";
  113. Dictionary<string, string> dics = new Dictionary<string, string>();
  114. for (int i = 0; i < result.Data.Count; i++)
  115. {
  116. var item = result.Data[i];
  117. dics.Clear();
  118. dics.Add("ItemCode", item.ItemCode);
  119. dics.Add("ItemName", item.ItemName);
  120. dics.Add("ItemStd", item.ItemStd);
  121. dics.Add("ItemDesc", item.ItemDesc);
  122. dics.Add("ItemCasNo", item.ItemCasNo);
  123. dics.Add("ItemMolecularFormula", item.ItemMolecularFormula);
  124. dics.Add("ItemAttribute", item.ItemAttribute);
  125. dics.Add("ItemMainCategoryCode", item.ItemMainCategoryCode);
  126. dics.Add("ItemUnit", item.ItemUnit);
  127. dics.Add("Itemmodel", item.Itemmodel);
  128. dics.Add("GR", item.GR);
  129. dics.Add("SGFS", item.SGFS);
  130. dics.Add("TGFS", item.TGFS);
  131. dics.Add("WG", item.WG);
  132. dics.Add("NI", item.NI);
  133. dics.Add("MA", item.MA);
  134. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  135. dics.Add("Other", item.Other);
  136. dics.Add("Free1", item.Free1);
  137. dics.Add("Free2", item.Free2);
  138. dics.Add("Free3", item.Free3);
  139. dics.Add("Free4", item.Free4);
  140. if (i == 0)
  141. {
  142. insertSql += " select ";
  143. }
  144. else
  145. {
  146. insertSql += " union all ";
  147. insertSql += " select ";
  148. }
  149. foreach (var keyValue in dics)
  150. {
  151. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " ,";
  152. }
  153. insertSql = insertSql.TrimEnd(',');
  154. }
  155. insertSql += " ) a";
  156. insertSql += @"
  157. insert into IcsInventory
  158. (ItemCode,ItemName,ItemDesc,ItemStd,ItemType,ItemControlType,ItemContype
  159. ,ItemAttribute,ItemUnit,ItemMainCategoryCode,ItemModel,ItemCasNo,ItemMolecularFormula,GR,SGFS,TGFS,WG,NI,MA,Other,Default1,Default2,Default3,Default4
  160. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  161. select a.ItemCode,a.ItemName,a.ItemDesc,a.ItemStd,'itemtype_finishedproduct','item_control_lot','item_control_nocontrol'
  162. ,a.ItemAttribute,a.ITEMUnit,a.ItemMainCategoryCode,a.ITEMMODEL,a.ItemCasNo,a.ItemMolecularFormula,a.GR,a.SGFS,a.TGFS,a.WG,a.NI,a.MA,a.Other,a.Free1,a.Free2,a.Free3,a.Free4
  163. ,Mtime,'job','job','{0}'
  164. from #tempFromErp4ItemCode a
  165. left join IcsInventory b with(nolock) on a.itemcode=b.itemcode
  166. where 1=1
  167. and b.id is null
  168. update IcsInventory
  169. set
  170. ItemName=a.ItemName,ItemDesc=a.ItemDesc,ItemStd=a.ItemStd,ItemAttribute=a.ItemAttribute ,ITEMUnit=a.ITEMUnit,ItemMainCategoryCode=a.ItemMainCategoryCode,ITEMMODEL=a.ITEMMODEL
  171. ,ItemCasNo=a.ItemCasNo,ItemMolecularFormula=a.ItemMolecularFormula,Default1=a.Free1,Default2=a.Free2,Default3=a.Free3,Default4=a.Free4
  172. ,LastModificationTime=a.Mtime,LastModifierUserId='job',LastModifierUserName='job'
  173. from #tempFromErp4ItemCode a
  174. left join IcsInventory b with(nolock) on a.itemcode=b.itemcode
  175. where 1=1
  176. and b.Id is not null
  177. ";
  178. insertSql = string.Format(insertSql, TenantId);
  179. ICSHelper.ExecuteDate(conStr, insertSql);
  180. }
  181. else
  182. {
  183. throw new Exception(result.Message);
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. log.Error(ex.ToString());
  190. }
  191. }
  192. }
  193. }