| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace cuidian.OpenApi.Utils
- {
- public class BusinessObject : ApiResponse
- {
- private static BusinessObject businessObject;
- private static IDictionary<string, object> properties = new Dictionary<string, object>();
- private ApiDictionary full;
- private string resourceId;
- private string method;
- private BusinessObject(Http.Response resp) : base(resp) { }
- [JsonIgnore]
- public string ResourceId
- {
- get { return businessObject.resourceId; }
- protected set { businessObject.resourceId = value; }
- }
- [JsonIgnore]
- public string Method
- {
- get { return businessObject.method; }
- }
- /// <summary>
- /// 查询单个资源
- /// </summary>
- /// <param name="resoureId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject Get(string resoureId, Http.Response resp)
- {
- ConstructBusinessObject("get", resoureId, resp);
- return businessObject;
- }
- /// <summary>
- /// 批量查询
- /// </summary>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject BatchGet(string resourceId, Http.Response resp)
- {
- ConstructBusinessObject("batch_get", resourceId, resp);
- return businessObject;
- }
- /// <summary>
- /// 添加一个资源
- /// </summary>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject Add(string resourceId, Http.Response resp)
- {
- ConstructBusinessObject("add", resourceId, resp);
- return businessObject;
- }
- /// <summary>
- /// 工作流:审核
- /// </summary>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject Audit(string resourceId, Http.Response resp)
- {
- ConstructBusinessObject("audit", resourceId, resp);
- return businessObject;
- }
- /// <summary>
- /// 其他操作
- /// </summary>
- /// <param name="method"></param>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject Do(string method, string resourceId, Http.Response resp)
- {
- ConstructBusinessObject(method, resourceId, resp);
- return businessObject;
- }
- /// <summary>
- /// 工作流:弃审
- /// </summary>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject Abandon(string resourceId, Http.Response resp)
- {
- ConstructBusinessObject("abandon", resourceId, resp);
- return businessObject;
- }
- /// <summary>
- /// 工作流:审批进程
- /// </summary>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject History(string resourceId, Http.Response resp)
- {
- ConstructBusinessObject("history", resourceId, resp);
- return businessObject;
- }
- /// <summary>
- /// 工作流:待办任务
- /// </summary>
- /// <param name="resourceId"></param>
- /// <param name="resp"></param>
- /// <returns></returns>
- public static BusinessObject Tasks(string resourceId, Http.Response resp)
- {
- ConstructBusinessObject("tasks", resourceId, resp);
- return businessObject;
- }
- private static void ConstructBusinessObject(string method, string resourceId, Http.Response resp)
- {
- if (businessObject == null)
- {
- businessObject = new BusinessObject(resp);
- }
- string body = resp.GetResponseAsString();
- businessObject.NativeResponseString = body;
- businessObject.resourceId = resourceId;
- businessObject.full = businessObject.ParseJson(body);
- businessObject.method = method;
- if (null != businessObject.full.GetValue("errcode"))
- {
- businessObject.ErrCode = businessObject.full.GetValue("errcode").ToString();
- }
- if (businessObject.full.GetValue("errmsg") == null)
- {
- businessObject.ErrMsg = "";
- }
- else
- {
- businessObject.ErrMsg = businessObject.full.GetValue("errmsg").ToString();
- }
- if (!businessObject.IsError)
- {
- if (method.Equals("batch_get"))
- {
- if (null != businessObject.full.GetValue("page_index"))
- {
- businessObject.PageIndex = Int32.Parse(businessObject.full.GetValue("page_index").ToString());
- businessObject.PageCount = Int32.Parse(businessObject.full.GetValue("page_count").ToString());
- businessObject.RowCount = Int32.Parse(businessObject.full.GetValue("row_count").ToString());
- businessObject.RowsPerPage = Int32.Parse(businessObject.full.GetValue("rows_per_page").ToString());
- }
- }
- if (method.Equals("add"))
- {
- if (businessObject.full.ContainsName("id"))
- {
- businessObject.Id = businessObject.full.GetValue("id").ToString();
- }
- if (businessObject.full.ContainsName("tradeid"))
- {
- businessObject.TradeId = businessObject.full.GetValue("tradeid").ToString();
- }
- }
- }
- }
- /// <summary>
- /// Api返回的全部信息
- /// </summary>
- [JsonIgnore]
- public ApiDictionary Full
- {
- get { return this.full; }
- }
- [JsonProperty("body", NullValueHandling = NullValueHandling.Ignore)]
- public object Body
- {
- get
- {
- return this.Full.Get(businessObject.ResourceId);
- }
- }
- /// <summary>
- /// Api返回的仅业务信息
- /// </summary>
- //[JsonProperty("body", NullValueHandling = NullValueHandling.Ignore)]
- [JsonIgnore]
- public ApiDictionary BodyObject
- {
- get
- {
- return this.Full.GetObject(businessObject.ResourceId);
- }
- }
- /// <summary>
- /// Api返回的仅业务信息
- /// </summary>
- [JsonIgnore]
- public ApiList BodyArray
- {
- get
- {
- return this.Full.GetArray(businessObject.ResourceId);
- }
- }
- [JsonProperty("body_array", NullValueHandling = NullValueHandling.Ignore)]
- public JArray BodyJArray
- {
- get
- {
- if (this.Full.GetArray(businessObject.ResourceId) != null)
- {
- return this.Full.GetArray(businessObject.ResourceId).GetJArray();
- }
- return null;
- }
- }
- private ApiDictionary ParseJson(string json)
- {
- return JsonConvert.DeserializeObject<ApiDictionary>(json);
- }
- public override string ToString()
- {
- return JsonConvert.SerializeObject(this).Replace("body_array", businessObject.ResourceId).Replace("body", businessObject.ResourceId);
- }
- }
- }
|