RelationRequest.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* ==============================================================================
  2. * 功能描述:EquipInSpaceRequest
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/4/1 16:15:45
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Newtonsoft.Json.Linq;
  12. using SAGA.DotNetUtils;
  13. using SAGA.DotNetUtils.Http;
  14. using SAGA.DotNetUtils.Others;
  15. using SAGA.MBI.Common;
  16. using SAGA.MBI.Tools;
  17. namespace SAGA.MBI.RequestData
  18. {
  19. /// <summary>
  20. /// EquipInSpaceRequest
  21. /// </summary>
  22. public class RelationRequest
  23. {
  24. /// <summary>
  25. /// 获取当前的GraphId
  26. /// </summary>
  27. /// <param name="graphType"></param>
  28. /// <returns></returns>
  29. public static string GetCurrentGraphId(string graphType)
  30. {
  31. return GetExistGraphId(graphType) ?? CreateNewGraphId(graphType);
  32. }
  33. /// <summary>
  34. /// 获取已经存在的GraphId
  35. /// </summary>
  36. /// <param name="graphType"></param>
  37. /// <returns></returns>
  38. private static string GetExistGraphId(string graphType)
  39. {
  40. string graphid = null;
  41. try
  42. {
  43. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/query?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  44. JObject jObject = new JObject();
  45. jObject.Add("graph_type", graphType);
  46. JObject queryJObject = new JObject();
  47. queryJObject.Add("criteria", jObject);
  48. string postData = queryJObject.ToString();
  49. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  50. string request = client.PostRequest();
  51. if (!request.IsSuccessRequest()) return graphid;
  52. JObject result = JObject.Parse(request);
  53. //获取生成的Id和Name
  54. var count = result["Count"].ToInt();
  55. if (count > 0)
  56. {
  57. JArray jArray = JArray.Parse(result["Content"].ToString());
  58. var jToken = jArray.First();
  59. graphid = jToken.Value<string>("graph_id");
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. MessageShowBase.Show(e);
  65. }
  66. return graphid;
  67. }
  68. /// <summary>
  69. /// 创建新的GraphicId
  70. /// </summary>
  71. /// <param name="graphType"></param>
  72. /// <returns></returns>
  73. private static string CreateNewGraphId(string graphType)
  74. {
  75. string graphid = null;
  76. try
  77. {
  78. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/create?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  79. JObject jObject = new JObject();
  80. jObject.Add("graph_type", graphType);
  81. string postData = jObject.ToString();
  82. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  83. string request = client.PostRequest();
  84. if (!request.IsSuccessRequest()) return graphid;
  85. JObject result = JObject.Parse(request);
  86. //获取生成的Id和Name
  87. graphid = result["graph_id"].ToString();
  88. }
  89. catch (Exception e)
  90. {
  91. MessageShowBase.Show(e);
  92. }
  93. return graphid;
  94. }
  95. /// <summary>
  96. /// 增加关系实例操作
  97. /// </summary>
  98. /// <param name="graphId"></param>
  99. /// <param name="fromId"></param>
  100. /// <param name="toId"></param>
  101. /// <param name="relType"></param>
  102. /// <returns></returns>
  103. public static bool AddRelation(string graphId, string fromId, string toId, string relType)
  104. {
  105. try
  106. {
  107. if (graphId == null || fromId == null || toId == null) return false;
  108. JObject jObject = new JObject();
  109. jObject.Add("from_id", fromId);
  110. jObject.Add("to_id", toId);
  111. jObject.Add("graph_id", graphId);
  112. jObject.Add("rel_type", relType);
  113. return AddRelation(jObject);
  114. }
  115. catch (Exception e)
  116. {
  117. MessageShowBase.Show(e);
  118. }
  119. return true;
  120. }
  121. /// <summary>
  122. /// 增加关系实例操作
  123. /// </summary>
  124. /// <returns></returns>
  125. public static bool AddRelation(JObject jObject)
  126. {
  127. try
  128. {
  129. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/create?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  130. string postData = jObject.ToString();
  131. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  132. string request = client.PostRequest();
  133. return (request.IsSuccessRequest());
  134. }
  135. catch (Exception e)
  136. {
  137. MessageShowBase.Show(e);
  138. }
  139. return true;
  140. }
  141. /// <summary>
  142. /// 删除关系实例操作
  143. /// </summary>
  144. /// <returns></returns>
  145. public static bool DeleteRelation(string graphId, string fromId, string toId, string relType)
  146. {
  147. if (fromId.IsNullOrEmpty() && toId.IsNullOrEmpty())
  148. return false;
  149. try
  150. {
  151. JObject jObject = new JObject();
  152. if (fromId.IsNotNullEmpty())
  153. jObject.Add("from_id", fromId);
  154. if (toId.IsNotNullEmpty())
  155. jObject.Add("to_id", toId);
  156. if (graphId.IsNotNullEmpty())
  157. jObject.Add("graph_id", graphId);
  158. if (relType.IsNotNullEmpty())
  159. jObject.Add("rel_type", relType);
  160. JObject delJObject = new JObject();
  161. delJObject.Add("criteria", jObject);
  162. return DeleteRelation(delJObject);
  163. }
  164. catch (Exception e)
  165. {
  166. MessageShowBase.Show(e);
  167. }
  168. return true;
  169. }
  170. /// <summary>
  171. /// 删除关系实例操作
  172. /// </summary>
  173. /// <returns></returns>
  174. public static bool DeleteRelation(JObject jObject)
  175. {
  176. try
  177. {
  178. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/delete?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  179. string postData = jObject.ToString();
  180. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  181. string request = client.PostRequest();
  182. return (request.IsSuccessRequest());
  183. }
  184. catch (Exception e)
  185. {
  186. MessageShowBase.Show(e);
  187. }
  188. return true;
  189. }
  190. /// <summary>
  191. /// 查询关系
  192. /// </summary>
  193. /// <param name="graphId"></param>
  194. /// <param name="fromId"></param>
  195. /// <param name="toId"></param>
  196. /// <param name="relType"></param>
  197. /// <returns></returns>
  198. public static string QueryRelation(string graphId, string fromId, string toId, string relType)
  199. {
  200. try
  201. {
  202. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/query?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  203. JObject jObject = new JObject();
  204. if (fromId.IsNotNullEmpty())
  205. jObject.Add("from_id", fromId);
  206. if (toId.IsNotNullEmpty())
  207. jObject.Add("to_id", toId);
  208. if (graphId.IsNotNullEmpty())
  209. jObject.Add("graph_id", graphId);
  210. if (relType.IsNotNullEmpty())
  211. jObject.Add("rel_type", relType);
  212. JObject delJObject = new JObject();
  213. delJObject.Add("criteria", jObject);
  214. string postData = delJObject.ToString();
  215. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  216. string request = client.PostRequest();
  217. return request;
  218. }
  219. catch (Exception e)
  220. {
  221. MessageShowBase.Show(e);
  222. }
  223. return null;
  224. }
  225. /// <summary>
  226. /// 全局图类型关系类型定义
  227. /// </summary>
  228. /// <returns></returns>
  229. public static string GetGlobalRelationDefination()
  230. {
  231. try
  232. {
  233. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/dict/query/global_relation_defination";
  234. RestClient client = new RestClient(url, HttpVerb.GET);
  235. string request = client.PostRequest();
  236. return request;
  237. }
  238. catch (Exception e)
  239. {
  240. MessageShowBase.Show(e);
  241. }
  242. return null;
  243. }
  244. }
  245. }