using JLHHJSvr.BLL; using JLHHJSvr.Com.Model; using JLHHJSvr.LJException; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace JLHHJSvr.Helper { internal class ERPHelper : HelperBase { private JObject BuildLoginRequest() { return new JObject { { "token", GlobalVar.ERP_TOKEN }, { "account", GlobalVar.ERP_ACCOUNT_NAME }, { "userid", GlobalVar.ERP_ACCOUNT_USERNAME }, { "password", GlobalVar.ERP_ACCOUNT_PASSWORD }, { "clientType", 30 } }; } private static readonly Dictionary _apiResultMap = new Dictionary() { {"GetL1Mtrldef", "mtrldefList"}, {"GetSCWorkgroupList", "scworkgroupList"}, {"GetL1Mtrltype", "mtrltypeList"}, {"GetL1ConfigureCode", "resultList"}, {"CommonDynamicSelect", "datatable"}, }; protected void Login() { var request = BuildLoginRequest(); try { var result = DoExecute("Login", request); var token = result.GetValue("token").ToObject(); if (!string.IsNullOrEmpty(token)) { GlobalVar.ERP_TOKEN = token; } } catch (Exception ex) { throw new LJCommonException($"ERP登录失败: {ex.Message}"); } } public void CheckLogin() { if(string.IsNullOrEmpty(GlobalVar.ERP_TOKEN)) { Login(); } } public List GetERPList(string apiMethod, JObject parameters = null, int tryCnt = 0) { CheckLogin(); try { var request = BuildRequest(parameters); var result = DoExecute(apiMethod, request); if (!_apiResultMap.TryGetValue(apiMethod, out var listKey)) throw new ArgumentException($"Unsupported API Method: {apiMethod}"); return result[listKey]?.ToObject>() ?? new List(); } catch (Exception ex) when (IsTokenExpired(ex)) { if (tryCnt >= 3) throw new LJCommonException("超过最大重连次数,请检查连接!"); Login(); return GetERPList(apiMethod, parameters, tryCnt + 1); } catch { throw; } } public List GetERPList(string apiMethod, ref int total, JObject parameters = null, int tryCnt = 0) { CheckLogin(); try { var request = BuildRequest(parameters); var result = DoExecute(apiMethod, request); if (!_apiResultMap.TryGetValue(apiMethod, out var listKey)) throw new ArgumentException($"Unsupported API Method: {apiMethod}"); total = ((int)result["totalcnt"]); return result[listKey]?.ToObject>() ?? new List(); } catch (Exception ex) when (IsTokenExpired(ex)) { if (tryCnt >= 3) throw new LJCommonException("超过最大重连次数,请检查连接!"); Login(); return GetERPList(apiMethod, ref total, parameters, tryCnt + 1); } catch { throw; } } // 提取请求构建逻辑 private JObject BuildRequest(JObject parameters) { var request = new JObject { ["token"] = GlobalVar.ERP_TOKEN }; parameters = parameters ?? new JObject(); foreach (var param in parameters) { request.Add(param.Key, param.Value); } return request; } private bool IsTokenExpired(Exception ex) { const string expiredFlag = "已与服务器失联"; return ex.Message.Contains(expiredFlag); } /// /// 保存物料资料 /// /// 核价物料资料 /// 错误信息 public string SaveMtrldef(u_mattress mtrl) { var errMsg = string.Empty; var l1Req = new JObject() { ["mtrls"] = new JArray { new JObject() { ["mtrlcode"] = mtrl.erp_mtrlcode, ["mtrlname"] = mtrl.erp_mtrlname, ["mtrlmode"] = mtrl.erp_mtrlmode, ["mtrltypeid"] = mtrl.erp_mtrltypeid, ["mtrltype"] = mtrl.erp_mtrltype, ["unit"] = mtrl.erp_mtrlunit, ["mtrlengname"] = mtrl.erp_mtrlengname } }, ["updateFields"] = "mtrlcode,mtrlname,mtrlmode,mtrltypeid,mtrltype,unit,mtrlengname" }; var l1Rslt = DoExecute("SavePrdPf", JObject.FromObject(l1Req)); errMsg = $"{l1Rslt.GetValue("ErrMsg")}"; return errMsg; } } }