|
@@ -0,0 +1,175 @@
|
|
|
+using Newtonsoft.Json;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+using SAGA.DotNetUtils.Http;
|
|
|
+using SAGA.MBI.Common;
|
|
|
+using SAGA.Models;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace SAGA.GplotRelationComputerManage
|
|
|
+{
|
|
|
+ public class RelationBll
|
|
|
+ {
|
|
|
+ #region 地址相关
|
|
|
+ /// <summary>
|
|
|
+ /// 获取基础地址
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public string GetBaseUrl()
|
|
|
+ {
|
|
|
+ return $"{ MBIConst.DataPlatformLocalHost}data - platform - 3 / relation / ";
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取密码相关地址字符串
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ private string GetPasswordQuerry()
|
|
|
+ {
|
|
|
+ return $"projectId={MBIControl.ProjectCur.Id}&secret={MBIControl.ProjectCur.Password}";
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建图实例
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="graphType">图类型</param>
|
|
|
+ /// <param name="beginTime">实例启用时间</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public string AddGraphInstance(string graphType, string beginTime)
|
|
|
+ {
|
|
|
+ var baseUrl = GetBaseUrl();
|
|
|
+ var password = GetPasswordQuerry();
|
|
|
+ string url = $"{baseUrl}graph_instance/create?{password}";
|
|
|
+ JObject jObject = new JObject();
|
|
|
+ jObject.Add("graph_type", graphType);
|
|
|
+ var periods = new JArray();
|
|
|
+ var timeObject = new JObject();
|
|
|
+ timeObject.Add("begin_time", beginTime);
|
|
|
+ timeObject.Add("end_time", "29000101000000");
|
|
|
+ jObject.Add("periods", periods);
|
|
|
+ //string postData = $"{{\"graph_type\":\"{graphType}\",\"periods\":[{{\"begin_time\":\"{begin_time}\",\"end_time\":\"29000101000000\"}}]}}";
|
|
|
+ RestClient restClient = new RestClient(url, HttpVerb.POST, jObject.ToString());
|
|
|
+
|
|
|
+ return restClient.GetRequest().GetValue("graph_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据图类型获取图实例id
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="graphType"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public GraphInstance QueryGraphInstance(string graphType)
|
|
|
+ {
|
|
|
+ var baseUrl = GetBaseUrl();
|
|
|
+ var password = GetPasswordQuerry();
|
|
|
+ string url = $"{baseUrl}graph_instance/query?{password}";
|
|
|
+ JObject jObject = new JObject();
|
|
|
+ JObject criteria = new JObject();
|
|
|
+ jObject.Add("criteria", criteria);
|
|
|
+ criteria.Add("jObjectValue", graphType);
|
|
|
+ //string postData = $"{{\"graph_type\":\"{graphType}\"}}";
|
|
|
+ //postData = $"{{\"criteria\":{{\"graph_type\":\"{graphType}\"}}}}";
|
|
|
+ RestClient restClient = new RestClient(url, HttpVerb.POST, jObject.ToString());
|
|
|
+ GraphInstance instance = new GraphInstance();
|
|
|
+ var result = restClient.GetRequest();
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 修改实例(如果当前时间点与最新的节点有重复,
|
|
|
+ /// 修改之前的图实例时间,然后创建新的,20171123)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="graphId">图类型</param>
|
|
|
+ /// <param name="statrDateTime"></param>
|
|
|
+ /// <param name="endDateTime"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public string UpdateGraphInstance(string graphId, string biginTime, string endTime)
|
|
|
+ {
|
|
|
+ var baseUrl = GetBaseUrl();
|
|
|
+ var password = GetPasswordQuerry();
|
|
|
+ string url = $"{baseUrl}graph_instance/update?{password}";
|
|
|
+ JObject jObject = new JObject();
|
|
|
+ JObject criteria = new JObject();
|
|
|
+ jObject.Add("criteria", criteria);
|
|
|
+ criteria.Add("graph_id", graphId);
|
|
|
+
|
|
|
+ JObject set = new JObject();
|
|
|
+ jObject.Add("set", set);
|
|
|
+ var periods = new JArray();
|
|
|
+ set.Add("periods", periods);
|
|
|
+ var timeObject = new JObject();
|
|
|
+ timeObject.Add("begin_time", biginTime);
|
|
|
+ timeObject.Add("end_time", "endTime");
|
|
|
+
|
|
|
+ //string postData = $"{{\"criteria\":{{\"graph_id\":\"{graphId}\"}},\"set\":{{\"periods\":[{{\"begin_time\":\"{statrDateTime}\",\"end_time\":\"{endDateTime}\"}}]}}}}";
|
|
|
+ RestClient restClient = new RestClient(url, HttpVerb.POST, jObject.ToString());
|
|
|
+
|
|
|
+ return restClient.GetRequest().GetValue("graph_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取图实例ID
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public string GetGraphId(string graphyType)
|
|
|
+ {
|
|
|
+ var instance = QueryGraphInstance(graphyType);
|
|
|
+ var nowTime = DateTime.Now;
|
|
|
+
|
|
|
+ if (instance.IsValid)
|
|
|
+ {
|
|
|
+ //将图类型的终止时间改成当前时间
|
|
|
+ UpdateGraphInstance(instance.Id, instance.BeginTime, nowTime.ToString("yyyyMMddHHmmss"));
|
|
|
+ }
|
|
|
+ //创建新的图类型
|
|
|
+ var newGraphId = AddGraphInstance(graphyType, nowTime.ToString("yyyyMMddHHmmss"));
|
|
|
+ return newGraphId;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 创建关系数据
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="criteria">关系数据</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public bool CreateRelations(List<GraphRelationItem> items)
|
|
|
+ {
|
|
|
+ var baseUrl = GetBaseUrl();
|
|
|
+ var password = GetPasswordQuerry();
|
|
|
+ string url = $"{baseUrl}create?{password}";
|
|
|
+ var useItems = new List<GraphRelationItem>();
|
|
|
+ foreach (var item in items)
|
|
|
+ {
|
|
|
+ if(item.IsValid)
|
|
|
+ {
|
|
|
+ useItems.Add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(useItems.Count==0)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ JObject jobject = new JObject();
|
|
|
+ jobject.Add("criterias", JArray.FromObject(useItems));
|
|
|
+ RestClient restClient1 = new RestClient(url, HttpVerb.POST, JsonConvert.SerializeObject(jobject).ToString());
|
|
|
+ var result =restClient1.GetRequest().IsSuccess();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建关系数据
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="graphType"></param>
|
|
|
+ /// <param name="items"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public bool CreateRelations(string graphType, List<GraphRelationItem> items)
|
|
|
+ {
|
|
|
+ var newGraphId = GetGraphId(graphType);
|
|
|
+ if (string.IsNullOrWhiteSpace(newGraphId))
|
|
|
+ return false;
|
|
|
+ items.ForEach(item => item.GraphId = newGraphId);
|
|
|
+ return CreateRelations(items);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|