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.

248 lines
11 KiB

3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
2 months ago
2 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
2 months ago
1 month ago
3 months ago
3 months ago
3 months ago
1 month ago
2 months ago
3 months ago
1 month ago
2 months ago
3 months ago
2 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. Dictionary<string, string> dic = new Dictionary<string, string>();
  64. dic.Add("WEP", "MSP");
  65. dic.Add("WEI", "MSI");
  66. dic.Add("WP", "MS0P");
  67. dic.Add("WEO", "MSO");
  68. dic.Add("WEF", "MSF");
  69. dic.Add("RMRD", "MSRM");
  70. List<string> keyList = new List<string>();
  71. keyList.AddRange(dic.Keys.ToList());
  72. List<string> valueList = new List<string>();
  73. valueList.AddRange(dic.Values.ToList());
  74. #region 获取Token
  75. var tokenUrl = ICSHelper.GetConfigString()["WeiMasTokenUrl"];
  76. var inputTokenDto = new TokenInputDto();
  77. inputTokenDto.route = "sj_token";
  78. inputTokenDto.grant_type = "password";
  79. inputTokenDto.client_id = ICSHelper.GetConfigString()["app_id"];
  80. inputTokenDto.client_secret = ICSHelper.GetConfigString()["app_secret"];
  81. inputTokenDto.timestamp = dtNow.ToMillisecondsTimeStamp();
  82. // inputTokenDto.timestamp = 1727252722913;
  83. inputTokenDto.username = "api_token";
  84. inputTokenDto.sign = ICSHelper.ApiSign(inputTokenDto.client_id, inputTokenDto.client_secret, inputTokenDto.timestamp);
  85. // inputTokenDto.sign = "0e29ab77f05117b979901723dbcffecf";
  86. inputTokenDto.clientExplorer = "";
  87. var tokenDto= HttpHelper.PostForm<TokenDto, TokenInputDto> (tokenUrl, inputTokenDto).Result;
  88. if(string.IsNullOrEmpty(tokenDto.access_token))
  89. {
  90. throw new Exception(" no access_token ");
  91. }
  92. // return;
  93. #endregion
  94. #region 获取子公司列表
  95. //var companyUrl = ICSHelper.GetConfigString()["WeiMasOrgUrl"] + @"/EnterpriseService/FromWeMec/GetSubCompanyList";
  96. //var companyDto = HttpHelper.HttpClientGet<WeiMasResult<List<SubCompanyDto>>>(companyUrl,"", tokenDto.access_token).Result;
  97. //if(companyDto.Data==null|| companyDto.Data.Count == 0)
  98. //{
  99. // throw new Exception(" 获取子公司列表失败 ");
  100. //}
  101. // return;
  102. #endregion
  103. var input = new ItemInputDto();
  104. input.CustomerId = ICSHelper.GetConfigString()["WeiMasOrgCustomerId"];
  105. input.CASNo = "";
  106. input.BeginTime = dtNowBegin.ToString("yyyy-MM-dd HH:mm:ss");
  107. input.EndTime =dtNow.ToString("yyyy-MM-dd HH:mm:ss");
  108. var inputObj = JsonConvert.SerializeObject(input);
  109. string url = ICSHelper.GetConfigString()["WeiMasErpUrl"]+ @"/BasicAssistingService/FromWeMec/APIItemToMES";
  110. var result = HttpHelper.HttpClientPost<WeiMasResult<List<ItemDto>>>(url, inputObj,token: tokenDto.access_token).Result;
  111. if (result.IsSuccess)
  112. {
  113. if (result.Data.Count == 0)
  114. {
  115. return;
  116. }
  117. // return;
  118. var insertSql = @"
  119. if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempFromErp4ItemCode') and type='U')
  120. DROP TABLE #tempFromErp4ItemCode
  121. select
  122. *
  123. into #tempFromErp4ItemCode
  124. from
  125. (";
  126. Dictionary<string, string> dics = new Dictionary<string, string>();
  127. for (int i = 0; i < result.Data.Count; i++)
  128. {
  129. var item = result.Data[i];
  130. dics.Clear();
  131. var subKey = keyList.Where(x => item.ItemCode.StartsWith(x)).FirstOrDefault();
  132. if (subKey != null)
  133. {
  134. string value = valueList[keyList.FindIndex(x => x == subKey)];
  135. dics.Add("ItemCode", item.ItemCode.Replace(subKey, value).Replace("-",""));
  136. }
  137. else
  138. {
  139. dics.Add("ItemCode", item.ItemCode.Replace("-", ""));
  140. }
  141. dics.Add("ItemAddCode", item.ItemCode);
  142. dics.Add("ItemName", item.ItemName);
  143. dics.Add("ItemStd", item.ItemStd);
  144. dics.Add("ItemDesc", item.ItemDesc);
  145. dics.Add("ItemCasNo", item.ItemCasNo);
  146. dics.Add("ItemMolecularFormula", item.ItemMolecularFormula);
  147. dics.Add("ItemAttribute", item.ItemAttribute);
  148. dics.Add("ItemMainCategoryCode", item.ItemMainCategoryCode);
  149. dics.Add("ItemUnit", item.ItemUnit);
  150. dics.Add("Itemmodel", item.Itemmodel);
  151. dics.Add("GR", item.GR);
  152. dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
  153. dics.Add("Free1", item.Free1);
  154. dics.Add("Free2", item.Free2);
  155. dics.Add("Free3", item.Free3);
  156. dics.Add("Free4", item.Free4);
  157. if (i == 0)
  158. {
  159. insertSql += " select ";
  160. }
  161. else
  162. {
  163. insertSql += " union all ";
  164. insertSql += " select ";
  165. }
  166. foreach (var keyValue in dics)
  167. {
  168. insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " ,";
  169. }
  170. insertSql = insertSql.TrimEnd(',');
  171. }
  172. insertSql += " ) a";
  173. insertSql += @"
  174. insert into IcsInventory
  175. (ItemCode,ItemAddCode,ItemName,ItemDesc,ItemStd,ItemType,ItemControlType,ItemContype
  176. ,ItemAttribute,ItemUnit,ItemMainCategoryCode,ItemModel,ItemCasNo,ItemMolecularFormula,GR,Default1,Default2,Default3,Default4
  177. ,CreationTime,CreatorUserId,CreatorUserName,TenantId)
  178. select a.ItemCode,a.ItemAddCode,a.ItemName,a.ItemDesc,a.ItemStd,'itemtype_finishedproduct','item_control_lot','item_control_nocontrol'
  179. ,left( a.ItemAttribute,1),a.ITEMUnit,a.ItemMainCategoryCode,a.ITEMMODEL,a.ItemCasNo,a.ItemMolecularFormula,a.GR,a.Free1,a.Free2,a.Free3,a.Free4
  180. ,Mtime,'job','job','{0}'
  181. from #tempFromErp4ItemCode a
  182. left join IcsInventory b with(nolock) on a.itemcode=b.itemcode
  183. where 1=1
  184. and b.id is null
  185. update IcsInventory
  186. set
  187. ItemName=a.ItemName,ItemDesc=a.ItemDesc,ItemStd=a.ItemStd,ItemAttribute=left( a.ItemAttribute,1) ,ITEMUnit=a.ITEMUnit,ItemMainCategoryCode=a.ItemMainCategoryCode,ITEMMODEL=a.ITEMMODEL
  188. ,ItemCasNo=a.ItemCasNo,ItemMolecularFormula=a.ItemMolecularFormula,Default1=a.Free1,Default2=a.Free2,Default3=a.Free3,Default4=a.Free4
  189. ,LastModificationTime=a.Mtime,LastModifierUserId='job',LastModifierUserName='job'
  190. from #tempFromErp4ItemCode a
  191. left join IcsInventory b with(nolock) on a.itemcode=b.itemcode
  192. where 1=1
  193. and b.Id is not null
  194. ";
  195. insertSql = string.Format(insertSql, TenantId);
  196. ICSHelper.ExecuteDate(conStr, insertSql);
  197. }
  198. else
  199. {
  200. throw new Exception(result.Message);
  201. }
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. log.Error(ex.ToString());
  207. }
  208. }
  209. }
  210. }