RelationConvert.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* ==============================================================================
  2. * 功能描述:关系实例请求接口
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/7/31 16:55:30
  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.Extend;
  13. using SAGA.DotNetUtils.Others;
  14. using SAGA.MBI.Common;
  15. using SAGA.MBI.Model;
  16. using SAGA.MBI.RequestData;
  17. using SAGA.MBI.Tools;
  18. using SAGA.DotNetUtils;
  19. using SAGA.DotNetUtils.Logger;
  20. namespace SAGA.MBI.JsonConvert
  21. {
  22. /// <summary>
  23. /// RelationConvert
  24. /// </summary>
  25. public class RelationConvert
  26. {
  27. /// <summary>
  28. /// 设备所在空间关系更新 用新的岗位替换旧的岗位
  29. /// </summary>
  30. /// <param name="oldequipid"></param>
  31. /// <param name="newequipid"></param>
  32. public static void UpdateEquipInSpaceRelation(string oldequipid,string newequipid)
  33. {
  34. string graphtype = RelationConst.EquipInSpaceGraphId;
  35. string reltype = RelationConst.EquipInSpaceGraphRelationType;
  36. UpdateFromIdRelation(graphtype, reltype, oldequipid, newequipid);
  37. }
  38. /// <summary>
  39. /// 更新关系实例
  40. /// </summary>
  41. /// <param name="graphType"></param>
  42. /// <param name="relType"></param>
  43. /// <param name="oldfromId"></param>
  44. /// <param name="newfromId"></param>
  45. private static void UpdateFromIdRelation(string graphType, string relType, string oldfromId,string newfromId)
  46. {
  47. try
  48. {
  49. var graphid = RelationRequest.GetCurrentGraphId(graphType);
  50. if (graphid == null) return;
  51. string json = RelationRequest.QueryRelation(graphid, oldfromId, null, relType);
  52. if (json.IsRequestHasItem())
  53. {
  54. JObject jObject = JObject.Parse(json);
  55. foreach (JObject jobj in jObject["Content"])
  56. {
  57. string toid = jobj.GetValueEx("to_id");
  58. UpdateFromIdRelation(graphid,relType,toid, oldfromId, newfromId);
  59. }
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. MessageShowBase.Show(e);
  65. }
  66. }
  67. /// <summary>
  68. /// 更新关系实例
  69. /// </summary>
  70. /// <param name="graphId"></param>
  71. /// <param name="relType"></param>
  72. /// <param name="toid"></param>
  73. /// <param name="oldfromId"></param>
  74. /// <param name="newfromId"></param>
  75. private static void UpdateFromIdRelation(string graphId, string relType, string toid, string oldfromId, string newfromId)
  76. {
  77. try
  78. {
  79. if (RelationRequest.DeleteRelation(graphId, oldfromId, toid, relType))
  80. RelationRequest.AddRelation(graphId, newfromId, toid, relType);
  81. }
  82. catch (Exception e)
  83. {
  84. MessageShowBase.Show(e);
  85. }
  86. }
  87. /// <summary>
  88. /// 跟据设备所在空间关系,取出空间
  89. /// </summary>
  90. /// <param name="fromid"></param>
  91. /// <returns></returns>
  92. public static string GetSpaceByRelationElementInSpace(string fromid)
  93. {
  94. string toid = null;
  95. string graphType = RelationConst.EquipInSpaceGraphId;
  96. string relType = RelationConst.EquipInSpaceGraphRelationType;
  97. var graphid = RelationRequest.GetCurrentGraphId(graphType);
  98. if (graphid == null) return null;
  99. string json = RelationRequest.QueryRelation(graphid, fromid, null, relType);
  100. if (json.IsRequestHasItem())
  101. {
  102. JObject jObject = JObject.Parse(json);
  103. foreach (JObject jobj in jObject["Content"])
  104. {
  105. toid = jobj.GetValueEx("to_id");
  106. //取第一个即可
  107. break;
  108. }
  109. }
  110. return toid;
  111. }
  112. }
  113. }