ERPHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using JLHHJSvr.BLL;
  2. using JLHHJSvr.Com.Model;
  3. using JLHHJSvr.LJException;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using LJLib.DAL.SQL;
  8. namespace JLHHJSvr.Helper
  9. {
  10. internal class ERPHelper : HelperBase
  11. {
  12. private JObject BuildLoginRequest()
  13. {
  14. return new JObject
  15. {
  16. { "token", GlobalVar.ERP_TOKEN },
  17. { "account", GlobalVar.ERP_ACCOUNT_NAME },
  18. { "userid", GlobalVar.ERP_ACCOUNT_USERNAME },
  19. { "password", GlobalVar.ERP_ACCOUNT_PASSWORD },
  20. { "clientType", 30 }
  21. };
  22. }
  23. private static readonly Dictionary<string, string> _apiResultMap = new Dictionary<string, string>()
  24. {
  25. {"GetL1Mtrldef", "mtrldefList"},
  26. {"GetSCWorkgroupList", "scworkgroupList"},
  27. {"GetL1Mtrltype", "mtrltypeList"},
  28. {"GetL1ConfigureCode", "resultList"},
  29. {"CommonDynamicSelect", "datatable"},
  30. };
  31. protected void Login()
  32. {
  33. var request = BuildLoginRequest();
  34. try
  35. {
  36. var result = DoExecute("Login", request);
  37. var token = result.GetValue("token").ToObject<string>();
  38. if (!string.IsNullOrEmpty(token))
  39. {
  40. GlobalVar.ERP_TOKEN = token;
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. throw new LJCommonException($"ERP登录失败: {ex.Message}");
  46. }
  47. }
  48. public void CheckLogin()
  49. {
  50. if(string.IsNullOrEmpty(GlobalVar.ERP_TOKEN))
  51. {
  52. Login();
  53. }
  54. }
  55. public List<T> GetERPList<T>(string apiMethod, JObject parameters = null, int tryCnt = 0)
  56. {
  57. CheckLogin();
  58. try
  59. {
  60. var request = BuildRequest(parameters);
  61. var result = DoExecute(apiMethod, request);
  62. if (!_apiResultMap.TryGetValue(apiMethod, out var listKey)) throw new ArgumentException($"Unsupported API Method: {apiMethod}");
  63. return result[listKey]?.ToObject<List<T>>() ?? new List<T>();
  64. }
  65. catch (Exception ex) when (IsTokenExpired(ex))
  66. {
  67. if (tryCnt >= 3) throw new LJCommonException("超过最大重连次数,请检查连接!");
  68. Login();
  69. return GetERPList<T>(apiMethod, parameters, tryCnt + 1);
  70. }
  71. catch
  72. {
  73. throw;
  74. }
  75. }
  76. public List<T> GetERPList<T>(string apiMethod, ref int total, JObject parameters = null, int tryCnt = 0)
  77. {
  78. CheckLogin();
  79. try
  80. {
  81. var request = BuildRequest(parameters);
  82. var result = DoExecute(apiMethod, request);
  83. if (!_apiResultMap.TryGetValue(apiMethod, out var listKey)) throw new ArgumentException($"Unsupported API Method: {apiMethod}");
  84. total = ((int)result["totalcnt"]);
  85. return result[listKey]?.ToObject<List<T>>() ?? new List<T>();
  86. }
  87. catch (Exception ex) when (IsTokenExpired(ex))
  88. {
  89. if (tryCnt >= 3) throw new LJCommonException("超过最大重连次数,请检查连接!");
  90. Login();
  91. return GetERPList<T>(apiMethod, ref total, parameters, tryCnt + 1);
  92. }
  93. catch
  94. {
  95. throw;
  96. }
  97. }
  98. // 提取请求构建逻辑
  99. private JObject BuildRequest(JObject parameters)
  100. {
  101. var request = new JObject { ["token"] = GlobalVar.ERP_TOKEN };
  102. parameters = parameters ?? new JObject();
  103. foreach (var param in parameters)
  104. {
  105. request.Add(param.Key, param.Value);
  106. }
  107. return request;
  108. }
  109. private bool IsTokenExpired(Exception ex)
  110. {
  111. const string expiredFlag = "已与服务器失联";
  112. return ex.Message.Contains(expiredFlag);
  113. }
  114. /// <summary>
  115. /// 保存物料资料,成功后保存erp_mtrlid
  116. /// </summary>
  117. /// <param name="mtrl">核价物料资料</param>
  118. /// <returns>错误信息</returns>
  119. public string SaveMtrldef(u_mattress mtrl)
  120. {
  121. var errMsg = string.Empty;
  122. var l1Req = new JObject()
  123. {
  124. ["mtrls"] = new JArray
  125. {
  126. new JObject()
  127. {
  128. ["mtrlcode"] = mtrl.erp_mtrlcode,
  129. ["mtrlname"] = mtrl.erp_mtrlname,
  130. ["mtrlmode"] = mtrl.erp_mtrlmode,
  131. ["mtrltypeid"] = mtrl.erp_mtrltypeid,
  132. ["mtrltype"] = mtrl.erp_mtrltype,
  133. ["unit"] = mtrl.erp_mtrlunit,
  134. ["mtrlengname"] = mtrl.erp_mtrlengname
  135. }
  136. },
  137. ["updateFields"] = "mtrlcode,mtrlname,mtrlmode,mtrltypeid,mtrltype,unit,mtrlengname"
  138. };
  139. var l1Rslt = DoExecute("SavePrdPf", JObject.FromObject(l1Req));
  140. errMsg = $"{l1Rslt.GetValue("ErrMsg")}";
  141. if (string.IsNullOrEmpty(errMsg))
  142. {
  143. mtrl.erp_mtrlid = (l1Rslt.GetValue("mtrlids") as JArray)[0].Value<int>();
  144. DbSqlHelper.Update(cmd, mtrl, "erp_mtrlid");
  145. }
  146. return errMsg;
  147. }
  148. }
  149. }