ERPHelper.cs 5.7 KB

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