using System; using System.Collections.Generic; using cuidian.OpenApi.Exceptions; using cuidian.OpenApi.Http; using cuidian.OpenApi.Utils; using Logs; namespace cuidian.OpenApi.Api { /// /// 开放平台 API 基类 /// public abstract class BasicApi : Api { public BasicApi(string resourceId) : base(resourceId) { } public BasicApi(string resourceId, string fromAccount, string toAccount) : base(resourceId, fromAccount, toAccount) { } public BasicApi(string resourceId, string fromAccount, string toAccount, string appKey) : base(resourceId, fromAccount, toAccount, appKey) { } /// /// 获取单个资源 /// /// /// public BusinessObject Get(string id) { try { this.Method = "get"; IDictionary parameters = new Dictionary(); parameters.Add("id", id); return BusinessObject.Get(this.ResourceId , new Response(Client.Get(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters)))); } catch (Exception e) { LogHelper.Error(e.Message); throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e); } } /// /// 批量查询 /// /// 查询参数 /// public BusinessObject BatchGet(IDictionary parameters) { try { this.Method = "batch_get"; return BusinessObject.BatchGet(this.ResourceId , new Response(Client.Get(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters)))); } catch (Exception e) { LogHelper.Error(e.Message); throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e); } } /// /// 有上游业务的新增,比如网上报销单生成凭证。 /// /// 凭证数据 /// 上游主键 /// public BusinessObject Add(string data, string biz_id) { try { this.Method = "add"; IDictionary parameters = new Dictionary(); parameters.Add("biz_id", biz_id); parameters.Add("sync", "1"); BusinessObject bo = BusinessObject.Add(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data))); if (bo.IsError) { return bo; } if (bo.Full.ContainsName("url")) { int pingAfter = int.Parse(bo.Full.GetValue("ping_after").ToString()); int counter = 0; String url = bo.Full.GetValue("url").ToString(); bool success = false; // 同步获取新增结果,重试 MAX_TIMES_TRADEID_RETRY 次 while (counter < MAX_TIMES_TRADEID_RETRY && !success) { counter++; System.Threading.Thread.Sleep(1000 * pingAfter); bo = BusinessObject.Add(this.ResourceId, new Response(Client.Get(url, null))); success = !bo.Full.ContainsName("url"); } } return bo; } catch (Exception e) { LogHelper.Error(e.Message); throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e); } } /// /// 无上游关系的新增 /// /// 凭证数据 /// public BusinessObject Add(string data) { try { BusinessObject trade = new Trade().Get(); string tradeid = trade.Full.GetObject("trade").GetValue("id").ToString(); //string tradeid = new Trade().Get().BodyObject.Get("id"); this.Method = "add"; IDictionary parameters = new Dictionary(); parameters.Add("tradeid", tradeid); parameters.Add("sync", "1"); BusinessObject bo = BusinessObject.Add(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data))); if (bo.IsError) { return bo; } if (bo.Full.ContainsName("url")) { int pingAfter = int.Parse(bo.Full.GetValue("ping_after").ToString()); int counter = 0; String url = bo.Full.GetValue("url").ToString(); bool success = false; while (counter < MAX_TIMES_TRADEID_RETRY && !success) { counter++; System.Threading.Thread.Sleep(1000 * pingAfter); bo = BusinessObject.Add(this.ResourceId, new Response(Client.Get(url, null))); success = !bo.Full.ContainsName("url"); } } return bo; } catch (Exception e) { LogHelper.Error(e.Message); throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e); } } /// /// 审核 /// /// public BusinessObject Audit(string voucherCode) { try { UserApi user = new UserApi(); user.Login(); this.Method = "verify"; //string data = String.Format("{\"{1}\":{\"voucher_code\":\"{0}\",\"person_code\":\"demo\"}}", voucherCode, this.ResourceId); //string data = "{\"saleorder\": {\"voucher_code\": \"0000000001\",\"person_code\": \"demo\"}}"; string data = "{{\"{0}\": {{\"voucher_code\": \"{1}\"}}}}"; data = string.Format(data, this.ResourceId, voucherCode); BusinessObject trade = new Trade().Get(); string tradeid = trade.Full.GetObject("trade").GetValue("id").ToString(); IDictionary parameters = new Dictionary(); parameters.Add("tradeid", tradeid); parameters.Add("sync", "1"); BusinessObject bo = BusinessObject.Audit(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data))); return bo; } catch (Exception e) { LogHelper.Error(e.Message); throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e); } } } }