BusinessObject.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. namespace cuidian.OpenApi.Utils
  9. {
  10. public class BusinessObject : ApiResponse
  11. {
  12. private static BusinessObject businessObject;
  13. private static IDictionary<string, object> properties = new Dictionary<string, object>();
  14. private ApiDictionary full;
  15. private string resourceId;
  16. private string method;
  17. private BusinessObject(Http.Response resp) : base(resp) { }
  18. [JsonIgnore]
  19. public string ResourceId
  20. {
  21. get { return businessObject.resourceId; }
  22. protected set { businessObject.resourceId = value; }
  23. }
  24. [JsonIgnore]
  25. public string Method
  26. {
  27. get { return businessObject.method; }
  28. }
  29. /// <summary>
  30. /// 查询单个资源
  31. /// </summary>
  32. /// <param name="resoureId"></param>
  33. /// <param name="resp"></param>
  34. /// <returns></returns>
  35. public static BusinessObject Get(string resoureId, Http.Response resp)
  36. {
  37. ConstructBusinessObject("get", resoureId, resp);
  38. return businessObject;
  39. }
  40. /// <summary>
  41. /// 批量查询
  42. /// </summary>
  43. /// <param name="resourceId"></param>
  44. /// <param name="resp"></param>
  45. /// <returns></returns>
  46. public static BusinessObject BatchGet(string resourceId, Http.Response resp)
  47. {
  48. ConstructBusinessObject("batch_get", resourceId, resp);
  49. return businessObject;
  50. }
  51. /// <summary>
  52. /// 添加一个资源
  53. /// </summary>
  54. /// <param name="resourceId"></param>
  55. /// <param name="resp"></param>
  56. /// <returns></returns>
  57. public static BusinessObject Add(string resourceId, Http.Response resp)
  58. {
  59. ConstructBusinessObject("add", resourceId, resp);
  60. return businessObject;
  61. }
  62. /// <summary>
  63. /// 工作流:审核
  64. /// </summary>
  65. /// <param name="resourceId"></param>
  66. /// <param name="resp"></param>
  67. /// <returns></returns>
  68. public static BusinessObject Audit(string resourceId, Http.Response resp)
  69. {
  70. ConstructBusinessObject("audit", resourceId, resp);
  71. return businessObject;
  72. }
  73. /// <summary>
  74. /// 其他操作
  75. /// </summary>
  76. /// <param name="method"></param>
  77. /// <param name="resourceId"></param>
  78. /// <param name="resp"></param>
  79. /// <returns></returns>
  80. public static BusinessObject Do(string method, string resourceId, Http.Response resp)
  81. {
  82. ConstructBusinessObject(method, resourceId, resp);
  83. return businessObject;
  84. }
  85. /// <summary>
  86. /// 工作流:弃审
  87. /// </summary>
  88. /// <param name="resourceId"></param>
  89. /// <param name="resp"></param>
  90. /// <returns></returns>
  91. public static BusinessObject Abandon(string resourceId, Http.Response resp)
  92. {
  93. ConstructBusinessObject("abandon", resourceId, resp);
  94. return businessObject;
  95. }
  96. /// <summary>
  97. /// 工作流:审批进程
  98. /// </summary>
  99. /// <param name="resourceId"></param>
  100. /// <param name="resp"></param>
  101. /// <returns></returns>
  102. public static BusinessObject History(string resourceId, Http.Response resp)
  103. {
  104. ConstructBusinessObject("history", resourceId, resp);
  105. return businessObject;
  106. }
  107. /// <summary>
  108. /// 工作流:待办任务
  109. /// </summary>
  110. /// <param name="resourceId"></param>
  111. /// <param name="resp"></param>
  112. /// <returns></returns>
  113. public static BusinessObject Tasks(string resourceId, Http.Response resp)
  114. {
  115. ConstructBusinessObject("tasks", resourceId, resp);
  116. return businessObject;
  117. }
  118. private static void ConstructBusinessObject(string method, string resourceId, Http.Response resp)
  119. {
  120. if (businessObject == null)
  121. {
  122. businessObject = new BusinessObject(resp);
  123. }
  124. string body = resp.GetResponseAsString();
  125. businessObject.NativeResponseString = body;
  126. businessObject.resourceId = resourceId;
  127. businessObject.full = businessObject.ParseJson(body);
  128. businessObject.method = method;
  129. if (null != businessObject.full.GetValue("errcode"))
  130. {
  131. businessObject.ErrCode = businessObject.full.GetValue("errcode").ToString();
  132. }
  133. if (businessObject.full.GetValue("errmsg") == null)
  134. {
  135. businessObject.ErrMsg = "";
  136. }
  137. else
  138. {
  139. businessObject.ErrMsg = businessObject.full.GetValue("errmsg").ToString();
  140. }
  141. if (!businessObject.IsError)
  142. {
  143. if (method.Equals("batch_get"))
  144. {
  145. if (null != businessObject.full.GetValue("page_index"))
  146. {
  147. businessObject.PageIndex = Int32.Parse(businessObject.full.GetValue("page_index").ToString());
  148. businessObject.PageCount = Int32.Parse(businessObject.full.GetValue("page_count").ToString());
  149. businessObject.RowCount = Int32.Parse(businessObject.full.GetValue("row_count").ToString());
  150. businessObject.RowsPerPage = Int32.Parse(businessObject.full.GetValue("rows_per_page").ToString());
  151. }
  152. }
  153. if (method.Equals("add"))
  154. {
  155. if (businessObject.full.ContainsName("id"))
  156. {
  157. businessObject.Id = businessObject.full.GetValue("id").ToString();
  158. }
  159. if (businessObject.full.ContainsName("tradeid"))
  160. {
  161. businessObject.TradeId = businessObject.full.GetValue("tradeid").ToString();
  162. }
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// Api返回的全部信息
  168. /// </summary>
  169. [JsonIgnore]
  170. public ApiDictionary Full
  171. {
  172. get { return this.full; }
  173. }
  174. [JsonProperty("body", NullValueHandling = NullValueHandling.Ignore)]
  175. public object Body
  176. {
  177. get
  178. {
  179. return this.Full.Get(businessObject.ResourceId);
  180. }
  181. }
  182. /// <summary>
  183. /// Api返回的仅业务信息
  184. /// </summary>
  185. //[JsonProperty("body", NullValueHandling = NullValueHandling.Ignore)]
  186. [JsonIgnore]
  187. public ApiDictionary BodyObject
  188. {
  189. get
  190. {
  191. return this.Full.GetObject(businessObject.ResourceId);
  192. }
  193. }
  194. /// <summary>
  195. /// Api返回的仅业务信息
  196. /// </summary>
  197. [JsonIgnore]
  198. public ApiList BodyArray
  199. {
  200. get
  201. {
  202. return this.Full.GetArray(businessObject.ResourceId);
  203. }
  204. }
  205. [JsonProperty("body_array", NullValueHandling = NullValueHandling.Ignore)]
  206. public JArray BodyJArray
  207. {
  208. get
  209. {
  210. if (this.Full.GetArray(businessObject.ResourceId) != null)
  211. {
  212. return this.Full.GetArray(businessObject.ResourceId).GetJArray();
  213. }
  214. return null;
  215. }
  216. }
  217. private ApiDictionary ParseJson(string json)
  218. {
  219. return JsonConvert.DeserializeObject<ApiDictionary>(json);
  220. }
  221. public override string ToString()
  222. {
  223. return JsonConvert.SerializeObject(this).Replace("body_array", businessObject.ResourceId).Replace("body", businessObject.ResourceId);
  224. }
  225. }
  226. }