RelationRequest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 GetCurrentId(graphType);
  32. //return GetExistGraphId(graphType) ?? CreateNewGraphId(graphType);
  33. }
  34. /// <summary>
  35. /// 获取已经存在的GraphId
  36. /// </summary>
  37. /// <param name="graphType"></param>
  38. /// <returns></returns>
  39. private static string GetExistGraphId(string graphType)
  40. {
  41. string graphid = null;
  42. try
  43. {
  44. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/query?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  45. JObject jObject = new JObject();
  46. jObject.Add("graph_type", graphType);
  47. JObject queryJObject = new JObject();
  48. queryJObject.Add("criteria", jObject);
  49. string postData = queryJObject.ToString();
  50. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  51. string request = client.PostRequest();
  52. if (!request.IsSuccessRequest()) return graphid;
  53. JObject result = JObject.Parse(request);
  54. //获取生成的Id和Name
  55. var count = result["Count"].ToInt();
  56. if (count > 0)
  57. {
  58. JArray jArray = JArray.Parse(result["Content"].ToString());
  59. var jToken = jArray.First();
  60. graphid = jToken.Value<string>("graph_id");
  61. }
  62. }
  63. catch (Exception e)
  64. {
  65. MessageShowBase.Show(e);
  66. }
  67. return graphid;
  68. }
  69. /// <summary>
  70. /// 创建新的GraphicId
  71. /// </summary>
  72. /// <param name="graphType"></param>
  73. /// <returns></returns>
  74. private static string CreateNewGraphId(string graphType)
  75. {
  76. string graphid = null;
  77. try
  78. {
  79. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/create?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  80. JObject jObject = new JObject();
  81. jObject.Add("graph_type", graphType);
  82. string postData = jObject.ToString();
  83. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  84. string request = client.PostRequest();
  85. if (!request.IsSuccessRequest()) return graphid;
  86. JObject result = JObject.Parse(request);
  87. //获取生成的Id和Name
  88. graphid = result["graph_id"].ToString();
  89. }
  90. catch (Exception e)
  91. {
  92. MessageShowBase.Show(e);
  93. }
  94. return graphid;
  95. }
  96. /// <summary>
  97. /// 从服务器获取相关图实例Id,不存在的话后台自动创建
  98. /// </summary>
  99. /// <param name="graphType"></param>
  100. /// <returns></returns>
  101. public static string GetCurrentId(string graphType)
  102. {
  103. string graphid = null;
  104. try
  105. {
  106. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/get?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  107. JObject jObject = new JObject();
  108. jObject.Add("graph_type", graphType);
  109. string postData = jObject.ToString();
  110. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  111. string request = client.PostRequest();
  112. if (!request.IsSuccessRequest()) return graphid;
  113. JObject result = JObject.Parse(request);
  114. //获取生成的Id和Name
  115. // result.TryGetValue<string>("graph_id", out graphid);
  116. graphid = result["graph_id"].ToString();
  117. }
  118. catch (Exception e)
  119. {
  120. MessageShowBase.Show(e);
  121. }
  122. return graphid;
  123. }
  124. /// <summary>
  125. /// 增加关系实例操作
  126. /// </summary>
  127. /// <param name="graphId"></param>
  128. /// <param name="fromId"></param>
  129. /// <param name="toId"></param>
  130. /// <param name="relType"></param>
  131. /// <returns></returns>
  132. public static bool AddRelation(string graphId, string fromId, string toId, string relType)
  133. {
  134. try
  135. {
  136. if (graphId == null || fromId == null || toId == null) return false;
  137. JObject jObject = new JObject();
  138. jObject.Add("from_id", fromId);
  139. jObject.Add("to_id", toId);
  140. jObject.Add("graph_id", graphId);
  141. jObject.Add("rel_type", relType);
  142. return AddRelation(jObject);
  143. }
  144. catch (Exception e)
  145. {
  146. MessageShowBase.Show(e);
  147. }
  148. return true;
  149. }
  150. /// <summary>
  151. /// 增加关系实例操作
  152. /// </summary>
  153. /// <returns></returns>
  154. public static bool AddRelation(JObject jObject)
  155. {
  156. try
  157. {
  158. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/create?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  159. string postData = jObject.ToString();
  160. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  161. string request = client.PostRequest();
  162. return (request.IsSuccessRequest());
  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(string graphId, string fromId, string toId, string relType)
  175. {
  176. if (fromId.IsNullOrEmpty() && toId.IsNullOrEmpty())
  177. return false;
  178. try
  179. {
  180. JObject jObject = new JObject();
  181. if (fromId.IsNotNullEmpty())
  182. jObject.Add("from_id", fromId);
  183. if (toId.IsNotNullEmpty())
  184. jObject.Add("to_id", toId);
  185. if (graphId.IsNotNullEmpty())
  186. jObject.Add("graph_id", graphId);
  187. if (relType.IsNotNullEmpty())
  188. jObject.Add("rel_type", relType);
  189. JObject delJObject = new JObject();
  190. delJObject.Add("criteria", jObject);
  191. return DeleteRelation(delJObject);
  192. }
  193. catch (Exception e)
  194. {
  195. MessageShowBase.Show(e);
  196. }
  197. return true;
  198. }
  199. /// <summary>
  200. /// 删除关系实例操作
  201. /// </summary>
  202. /// <returns></returns>
  203. public static bool DeleteRelation(JObject jObject)
  204. {
  205. try
  206. {
  207. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/delete?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  208. string postData = jObject.ToString();
  209. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  210. string request = client.PostRequest();
  211. return (request.IsSuccessRequest());
  212. }
  213. catch (Exception e)
  214. {
  215. MessageShowBase.Show(e);
  216. }
  217. return true;
  218. }
  219. /// <summary>
  220. /// 查询关系
  221. /// </summary>
  222. /// <param name="graphId"></param>
  223. /// <param name="fromId"></param>
  224. /// <param name="toId"></param>
  225. /// <param name="relType"></param>
  226. /// <returns></returns>
  227. public static string QueryRelation(string graphId, string fromId, string toId, string relType)
  228. {
  229. try
  230. {
  231. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/query?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
  232. JObject jObject = new JObject();
  233. if (fromId.IsNotNullEmpty())
  234. jObject.Add("from_id", fromId);
  235. if (toId.IsNotNullEmpty())
  236. jObject.Add("to_id", toId);
  237. if (graphId.IsNotNullEmpty())
  238. jObject.Add("graph_id", graphId);
  239. if (relType.IsNotNullEmpty())
  240. jObject.Add("rel_type", relType);
  241. JObject delJObject = new JObject();
  242. delJObject.Add("criteria", jObject);
  243. string postData = delJObject.ToString();
  244. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  245. string request = client.PostRequest();
  246. return request;
  247. }
  248. catch (Exception e)
  249. {
  250. MessageShowBase.Show(e);
  251. }
  252. return null;
  253. }
  254. /// <summary>
  255. /// 全局图类型关系类型定义
  256. /// </summary>
  257. /// <returns></returns>
  258. public static string GetGlobalRelationDefination()
  259. {
  260. try
  261. {
  262. string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/dict/query/global_relation_defination";
  263. RestClient client = new RestClient(url, HttpVerb.GET);
  264. string request = client.PostRequest();
  265. return request;
  266. }
  267. catch (Exception e)
  268. {
  269. MessageShowBase.Show(e);
  270. }
  271. return null;
  272. }
  273. }
  274. }