Gplot.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Linq;
  12. using NPOI.HSSF.UserModel;
  13. using NPOI.SS.UserModel;
  14. using NPOI.XSSF.UserModel;
  15. using SAGA.DotNetUtils.Extend;
  16. using SAGA.DotNetUtils.Logger;
  17. using SAGA.MBI.RequestData;
  18. using SAGA.Models;
  19. namespace SAGA.GplotManage.UploadRelated
  20. {
  21. public abstract class Gplot
  22. {
  23. public int Id { get; set; }
  24. public string Name { get; set; }
  25. public DateTime Date { get; set; } = DateTime.Now;
  26. /// <summary>
  27. /// 存储拓扑图的数据类型
  28. /// </summary>
  29. public abstract Type DataType { get; }
  30. public abstract GraphTypeEnum GraphType { get; }
  31. public string Operator { get; set; } = "关系变化";
  32. public string GraphName => GraphType.GetDescription();
  33. private string graphId;
  34. public string GraphId
  35. {
  36. get
  37. {
  38. if (string.IsNullOrEmpty(graphId))
  39. {
  40. graphId = GetGraphId();
  41. }
  42. return graphId;
  43. }
  44. }
  45. private string m_RelationShip;
  46. public string Relationship
  47. {
  48. get => m_RelationShip;
  49. set => m_RelationShip = value;
  50. }
  51. private string m_ComputerEffectRelationShip;
  52. public string ComputerEffectRelationShip
  53. {
  54. get
  55. {
  56. if (string.IsNullOrEmpty(m_ComputerEffectRelationShip))
  57. {
  58. m_ComputerEffectRelationShip = this.GraphType + "";
  59. }
  60. return m_ComputerEffectRelationShip;
  61. }
  62. set => m_ComputerEffectRelationShip = value;
  63. }
  64. public bool IsUpload { get; set; } = true;
  65. protected bool IsSpace { get; set; }
  66. protected Criteria Criterias { get; set; } = new Criteria
  67. {
  68. criterias = new List<ReleateData>()
  69. };
  70. /// <summary>
  71. /// 上传数据
  72. /// </summary>
  73. public void Upload()
  74. {
  75. //统一数据处理
  76. Log4Net.Debug($"{GraphName}:数据开始上传");
  77. //数据上传
  78. if (IsUpload)
  79. {
  80. DealUploadData();
  81. TableToExcel(Criterias.criterias, @"D:\\uploadGraphData.xlsx");
  82. // return;
  83. var str = ConvertJsonString(Criterias);
  84. // Log4Net.Debug(str);
  85. bool result = RelatedDataUpload.CreateRelation(Criterias);
  86. //MessageBox.Show(str);
  87. //上传完成,去掉相应的拓扑图
  88. var relationShips = DrawDataServer.GetData<List<ChangesRelationship>>();
  89. var rss = Relationship.Split(';').Where(rs => !string.IsNullOrEmpty(rs)).ToList();
  90. foreach (var s in rss)
  91. {
  92. relationShips.RemoveAll(t => t.RelationshipType == s);
  93. }
  94. DrawDataServer.SaveAsFile(relationShips);
  95. Log4Net.Debug($"{GraphName}上传结果:{result}");
  96. }
  97. }
  98. private string ConvertJsonString(object object1)
  99. {
  100. //格式化json字符串
  101. JsonSerializer serializer = new JsonSerializer();
  102. if (object1 != null)
  103. {
  104. StringWriter textWriter = new StringWriter();
  105. JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
  106. {
  107. Formatting = Formatting.Indented,
  108. Indentation = 4,
  109. IndentChar = ' '
  110. };
  111. serializer.Serialize(jsonWriter, object1);
  112. return textWriter.ToString();
  113. }
  114. else
  115. {
  116. return "";
  117. }
  118. }
  119. /// <summary>
  120. /// 处理上传数据
  121. /// </summary>
  122. protected abstract void DealUploadData();
  123. /// <summary>
  124. /// 计算拓扑图数据
  125. /// </summary>
  126. public abstract void Computer();
  127. /// <summary>
  128. /// 显示拓扑数据
  129. /// </summary>
  130. public abstract void Show(GplotShowType showType = GplotShowType.Default);
  131. /// <summary>
  132. /// 获取服务器设备Id
  133. /// </summary>
  134. /// <param name="id"></param>
  135. /// <returns></returns>
  136. public string GetCloudId(string id)
  137. {
  138. //TODO:缓存建筑下所有设备,id从缓存中获取
  139. var cloudId = GetServerId(id);
  140. if (cloudId == null)
  141. {
  142. Log4Net.Debug($"无法找的ID为:{id}");
  143. }
  144. return cloudId;
  145. }
  146. private static Dictionary<string, string> serverIdDic;
  147. /// <summary>
  148. /// 获取服务端的空间和设备数据
  149. /// </summary>
  150. private Dictionary<string, string> ServerIdDic
  151. {
  152. get
  153. {
  154. if (serverIdDic == null)
  155. {
  156. var str = new string[] { "Eq", "Si" };
  157. List<JObject> datas = CommonConvert.QueryObjectInfoByTypes(str);
  158. serverIdDic = datas
  159. .Select(d => new
  160. { key = GetValue(d, "EquipID") + GetValue(d, "RoomID"), value = GetValue(d, "BIMID") }).Where(t => !string.IsNullOrEmpty(t.key))
  161. .ToDictionary(d => d.key, d => d.value);
  162. //Log4Net.Debug("服务器数据===="+ConvertJsonString(serverIdDic));
  163. }
  164. return serverIdDic;
  165. }
  166. }
  167. /// <summary>
  168. /// 根据Revit的ElementId获取服务端Id
  169. /// </summary>
  170. /// <param name="revitId"></param>
  171. /// <returns></returns>
  172. public string GetServerId(string revitId)
  173. {
  174. var result = ServerIdDic.FirstOrDefault(t =>
  175. {
  176. if (string.IsNullOrEmpty(t.Value)) return false;
  177. var ids = t.Value.Split(':');
  178. if (ids.Length == 2)
  179. {
  180. return ids[1] == revitId;
  181. }
  182. return false;
  183. });
  184. if (result.Key != null)
  185. return result.Key;
  186. return "";
  187. }
  188. /// <summary>
  189. /// 根据属性获取相应的值
  190. /// </summary>
  191. /// <param name="equip"></param>
  192. /// <param name="propertyName"></param>
  193. /// <returns></returns>
  194. public static string GetValue(JObject equip, string propertyName)
  195. {
  196. string result = "";
  197. if (equip != null)
  198. {
  199. result = equip["infos"][propertyName] + "";
  200. //本地名称为空显示设备名称
  201. if (propertyName == "EquipLocalName" && result == "")
  202. {
  203. result = equip["infos"]["EquipName"] + "";
  204. }
  205. }
  206. return result;
  207. }
  208. /// <summary>
  209. /// 获取图实例ID
  210. /// </summary>
  211. /// <returns></returns>
  212. public string GetGraphId(string graphyType = "")
  213. {
  214. string graphData;
  215. //读取当前最新图类型
  216. if (string.IsNullOrEmpty(graphyType))
  217. graphyType = GraphType + "";
  218. graphData = RelatedDataUpload.QueryGraphInstance(graphyType);
  219. if (graphData.IsNullOrEmptyExt() || !graphData.IsSuccess()) throw new Exception("无法查询GraphId");
  220. if (graphData.GetValue("Count") != "0")
  221. //将图类型的终止时间改成当前时间
  222. RelatedDataUpload.UpdateGraphInstance(graphData.GetValue("graph_id"), graphData.GetValue("begin_time"), Date.ToString("yyyyMMddHHmmss"));
  223. //创建新的图类型
  224. var newGraphId = RelatedDataUpload.CreateGraphInstance(graphyType, Date.ToString("yyyyMMddHHmmss"));
  225. return newGraphId;
  226. }
  227. /// <summary>
  228. /// 创建两个设备的关系
  229. /// </summary>
  230. /// <param name="from_id"></param>
  231. /// <param name="to_id"></param>
  232. /// <param name="relType"></param>
  233. /// <returns></returns>
  234. protected ReleateData GetReleateData(string from_id, string to_id, string relType = "1")
  235. {
  236. return new ReleateData()
  237. {
  238. graph_id = GraphId,
  239. from_id = GetCloudId(from_id),
  240. to_id = GetCloudId(to_id),
  241. rel_type = relType
  242. };
  243. }
  244. /// <summary>
  245. /// Datable导出成Excel
  246. /// </summary>
  247. /// <param name="dt"></param>
  248. /// <param name="file">导出路径(包括文件名与扩展名)</param>
  249. public static void TableToExcel(List<ReleateData> dt, string file)
  250. {
  251. IWorkbook workbook;
  252. string fileExt = Path.GetExtension(file).ToLower();
  253. if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; }
  254. if (workbook == null) { return; }
  255. ISheet sheet = workbook.CreateSheet("Sheet1");
  256. //表头
  257. IRow row = sheet.CreateRow(0);
  258. var sheetNames = new List<string> { "GraphId", "FromId", "ToId", "RelType" };
  259. for (int i = 0; i < sheetNames.Count; i++)
  260. {
  261. ICell cell = row.CreateCell(i);
  262. cell.SetCellValue(sheetNames[i]);
  263. }
  264. //数据
  265. for (int i = 0; i < dt.Count; i++)
  266. {
  267. IRow row1 = sheet.CreateRow(i + 1);
  268. ICell cell = row1.CreateCell(0);
  269. cell.SetCellValue(dt[i].graph_id);
  270. ICell cell1 = row1.CreateCell(1);
  271. cell1.SetCellValue(dt[i].from_id);
  272. ICell cell2 = row1.CreateCell(2);
  273. cell2.SetCellValue(dt[i].to_id);
  274. ICell cell3 = row1.CreateCell(3);
  275. cell3.SetCellValue(dt[i].rel_type);
  276. }
  277. //转为字节数组
  278. MemoryStream stream = new MemoryStream();
  279. workbook.Write(stream);
  280. var buf = stream.ToArray();
  281. //保存为Excel文件
  282. using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
  283. {
  284. fs.Write(buf, 0, buf.Length);
  285. fs.Flush();
  286. }
  287. }
  288. }
  289. }