123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /* ==============================================================================
- * 功能描述:EquipInSpaceRequest
- * 创 建 者:Garrett
- * 创建日期:2018/4/1 16:15:45
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json.Linq;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Http;
- using SAGA.DotNetUtils.Others;
- using SAGA.MBI.Common;
- using SAGA.MBI.Tools;
- namespace SAGA.MBI.RequestData
- {
- /// <summary>
- /// EquipInSpaceRequest
- /// </summary>
- public class RelationRequest
- {
- /// <summary>
- /// 获取当前的GraphId
- /// </summary>
- /// <param name="graphType"></param>
- /// <returns></returns>
- public static string GetCurrentGraphId(string graphType)
- {
- return GetExistGraphId(graphType) ?? CreateNewGraphId(graphType);
- }
- /// <summary>
- /// 获取已经存在的GraphId
- /// </summary>
- /// <param name="graphType"></param>
- /// <returns></returns>
- private static string GetExistGraphId(string graphType)
- {
- string graphid = null;
- try
- {
- string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/query?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
- JObject jObject = new JObject();
- jObject.Add("graph_type", graphType);
- JObject queryJObject = new JObject();
- queryJObject.Add("criteria", jObject);
- string postData = queryJObject.ToString();
- RestClient client = new RestClient(url, HttpVerb.POST, postData);
- string request = client.PostRequest();
- if (!request.IsSuccessRequest()) return graphid;
- JObject result = JObject.Parse(request);
- //获取生成的Id和Name
- var count = result["Count"].ToInt();
- if (count > 0)
- {
- JArray jArray = JArray.Parse(result["Content"].ToString());
- var jToken = jArray.First();
- graphid = jToken.Value<string>("graph_id");
- }
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return graphid;
- }
- /// <summary>
- /// 创建新的GraphicId
- /// </summary>
- /// <param name="graphType"></param>
- /// <returns></returns>
- private static string CreateNewGraphId(string graphType)
- {
- string graphid = null;
- try
- {
- string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/graph_instance/create?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
- JObject jObject = new JObject();
- jObject.Add("graph_type", graphType);
- string postData = jObject.ToString();
- RestClient client = new RestClient(url, HttpVerb.POST, postData);
- string request = client.PostRequest();
- if (!request.IsSuccessRequest()) return graphid;
- JObject result = JObject.Parse(request);
- //获取生成的Id和Name
- graphid = result["graph_id"].ToString();
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return graphid;
- }
- /// <summary>
- /// 增加关系实例操作
- /// </summary>
- /// <param name="graphId"></param>
- /// <param name="fromId"></param>
- /// <param name="toId"></param>
- /// <param name="relType"></param>
- /// <returns></returns>
- public static bool AddRelation(string graphId, string fromId, string toId, string relType)
- {
- try
- {
- if (graphId == null || fromId == null || toId == null) return false;
- JObject jObject = new JObject();
- jObject.Add("from_id", fromId);
- jObject.Add("to_id", toId);
- jObject.Add("graph_id", graphId);
- jObject.Add("rel_type", relType);
- return AddRelation(jObject);
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return true;
- }
- /// <summary>
- /// 增加关系实例操作
- /// </summary>
- /// <returns></returns>
- public static bool AddRelation(JObject jObject)
- {
- try
- {
- string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/create?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
-
- string postData = jObject.ToString();
- RestClient client = new RestClient(url, HttpVerb.POST, postData);
- string request = client.PostRequest();
- return (request.IsSuccessRequest());
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return true;
- }
- /// <summary>
- /// 删除关系实例操作
- /// </summary>
- /// <returns></returns>
- public static bool DeleteRelation(string graphId, string fromId, string toId, string relType)
- {
- if (fromId.IsNullOrEmpty() && toId.IsNullOrEmpty())
- return false;
- try
- {
- JObject jObject = new JObject();
- if (fromId.IsNotNullEmpty())
- jObject.Add("from_id", fromId);
- if (toId.IsNotNullEmpty())
- jObject.Add("to_id", toId);
- if (graphId.IsNotNullEmpty())
- jObject.Add("graph_id", graphId);
- if (relType.IsNotNullEmpty())
- jObject.Add("rel_type", relType);
- JObject delJObject = new JObject();
- delJObject.Add("criteria", jObject);
- return DeleteRelation(delJObject);
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return true;
- }
- /// <summary>
- /// 删除关系实例操作
- /// </summary>
- /// <returns></returns>
- public static bool DeleteRelation(JObject jObject)
- {
- try
- {
- string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/delete?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
- string postData = jObject.ToString();
- RestClient client = new RestClient(url, HttpVerb.POST, postData);
- string request = client.PostRequest();
- return (request.IsSuccessRequest());
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return true;
- }
- /// <summary>
- /// 查询关系
- /// </summary>
- /// <param name="graphId"></param>
- /// <param name="fromId"></param>
- /// <param name="toId"></param>
- /// <param name="relType"></param>
- /// <returns></returns>
- public static string QueryRelation(string graphId, string fromId, string toId, string relType)
- {
- try
- {
- string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/relation/query?secret={MBIControl.ProjectCur.Password}&projectId={MBIControl.ProjectCur.Id}";
- JObject jObject = new JObject();
- if (fromId.IsNotNullEmpty())
- jObject.Add("from_id", fromId);
- if (toId.IsNotNullEmpty())
- jObject.Add("to_id", toId);
- if (graphId.IsNotNullEmpty())
- jObject.Add("graph_id", graphId);
- if (relType.IsNotNullEmpty())
- jObject.Add("rel_type", relType);
- JObject delJObject = new JObject();
- delJObject.Add("criteria", jObject);
- string postData = delJObject.ToString();
- RestClient client = new RestClient(url, HttpVerb.POST, postData);
- string request = client.PostRequest();
- return request;
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return null;
- }
- /// <summary>
- /// 全局图类型关系类型定义
- /// </summary>
- /// <returns></returns>
- public static string GetGlobalRelationDefination()
- {
- try
- {
- string url = MBIConst.DataPlatformLocalHost + $"data-platform-3/dict/query/global_relation_defination";
-
- RestClient client = new RestClient(url, HttpVerb.GET);
- string request = client.PostRequest();
- return request;
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- return null;
- }
- }
- }
|