123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Logger;
- using SAGA.MBI.RequestData;
- using SAGA.Models;
- namespace SAGA.GplotManage.UploadRelated
- {
- public abstract class Gplot
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public DateTime Date { get; set; } = DateTime.Now;
- /// <summary>
- /// 存储拓扑图的数据类型
- /// </summary>
- public abstract Type DataType { get; }
- public abstract GraphTypeEnum GraphType { get; }
- public string Operator { get; set; } = "关系变化";
- public string GraphName => GraphType.GetDescription();
- private string graphId;
- public string GraphId
- {
- get
- {
- if (string.IsNullOrEmpty(graphId))
- {
- graphId = GetGraphId();
- }
- return graphId;
- }
- }
- private string m_RelationShip;
- public string Relationship
- {
- get => m_RelationShip;
- set => m_RelationShip = value;
- }
- private string m_ComputerEffectRelationShip;
- public string ComputerEffectRelationShip
- {
- get
- {
- if (string.IsNullOrEmpty(m_ComputerEffectRelationShip))
- {
- m_ComputerEffectRelationShip = this.GraphType + "";
- }
- return m_ComputerEffectRelationShip;
- }
- set => m_ComputerEffectRelationShip = value;
- }
- public bool IsUpload { get; set; } = true;
- protected bool IsSpace { get; set; }
- protected Criteria Criterias { get; set; } = new Criteria
- {
- criterias = new List<ReleateData>()
- };
- /// <summary>
- /// 上传数据
- /// </summary>
- public void Upload()
- {
- //统一数据处理
- Log4Net.Debug($"{GraphName}:数据开始上传");
- //数据上传
- if (IsUpload)
- {
- DealUploadData();
- TableToExcel(Criterias.criterias, @"D:\\uploadGraphData.xlsx");
- // return;
- var str = ConvertJsonString(Criterias);
- // Log4Net.Debug(str);
- bool result = RelatedDataUpload.CreateRelation(Criterias);
- //MessageBox.Show(str);
- //上传完成,去掉相应的拓扑图
- var relationShips = DrawDataServer.GetData<List<ChangesRelationship>>();
- var rss = Relationship.Split(';').Where(rs => !string.IsNullOrEmpty(rs)).ToList();
- foreach (var s in rss)
- {
- relationShips.RemoveAll(t => t.RelationshipType == s);
- }
- DrawDataServer.SaveAsFile(relationShips);
- Log4Net.Debug($"{GraphName}上传结果:{result}");
- }
- }
- private string ConvertJsonString(object object1)
- {
- //格式化json字符串
- JsonSerializer serializer = new JsonSerializer();
- if (object1 != null)
- {
- StringWriter textWriter = new StringWriter();
- JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
- {
- Formatting = Formatting.Indented,
- Indentation = 4,
- IndentChar = ' '
- };
- serializer.Serialize(jsonWriter, object1);
- return textWriter.ToString();
- }
- else
- {
- return "";
- }
- }
- /// <summary>
- /// 处理上传数据
- /// </summary>
- protected abstract void DealUploadData();
- /// <summary>
- /// 计算拓扑图数据
- /// </summary>
- public abstract void Computer();
- /// <summary>
- /// 显示拓扑数据
- /// </summary>
- public abstract void Show(GplotShowType showType = GplotShowType.Default);
- /// <summary>
- /// 获取服务器设备Id
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public string GetCloudId(string id)
- {
- //TODO:缓存建筑下所有设备,id从缓存中获取
- var cloudId = GetServerId(id);
- if (cloudId == null)
- {
- Log4Net.Debug($"无法找的ID为:{id}");
- }
- return cloudId;
- }
- private static Dictionary<string, string> serverIdDic;
- /// <summary>
- /// 获取服务端的空间和设备数据
- /// </summary>
- private Dictionary<string, string> ServerIdDic
- {
- get
- {
- if (serverIdDic == null)
- {
- var str = new string[] { "Eq", "Si" };
- List<JObject> datas = CommonConvert.QueryObjectInfoByTypes(str);
- serverIdDic = datas
- .Select(d => new
- { key = GetValue(d, "EquipID") + GetValue(d, "RoomID"), value = GetValue(d, "BIMID") }).Where(t => !string.IsNullOrEmpty(t.key))
- .ToDictionary(d => d.key, d => d.value);
- //Log4Net.Debug("服务器数据===="+ConvertJsonString(serverIdDic));
- }
- return serverIdDic;
- }
- }
- /// <summary>
- /// 根据Revit的ElementId获取服务端Id
- /// </summary>
- /// <param name="revitId"></param>
- /// <returns></returns>
- public string GetServerId(string revitId)
- {
- var result = ServerIdDic.FirstOrDefault(t =>
- {
- if (string.IsNullOrEmpty(t.Value)) return false;
- var ids = t.Value.Split(':');
- if (ids.Length == 2)
- {
- return ids[1] == revitId;
- }
- return false;
- });
- if (result.Key != null)
- return result.Key;
- return "";
- }
- /// <summary>
- /// 根据属性获取相应的值
- /// </summary>
- /// <param name="equip"></param>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public static string GetValue(JObject equip, string propertyName)
- {
- string result = "";
- if (equip != null)
- {
- result = equip["infos"][propertyName] + "";
- //本地名称为空显示设备名称
- if (propertyName == "EquipLocalName" && result == "")
- {
- result = equip["infos"]["EquipName"] + "";
- }
- }
- return result;
- }
- /// <summary>
- /// 获取图实例ID
- /// </summary>
- /// <returns></returns>
- public string GetGraphId(string graphyType = "")
- {
- string graphData;
- //读取当前最新图类型
- if (string.IsNullOrEmpty(graphyType))
- graphyType = GraphType + "";
- graphData = RelatedDataUpload.QueryGraphInstance(graphyType);
- if (graphData.IsNullOrEmptyExt() || !graphData.IsSuccess()) throw new Exception("无法查询GraphId");
- if (graphData.GetValue("Count") != "0")
- //将图类型的终止时间改成当前时间
- RelatedDataUpload.UpdateGraphInstance(graphData.GetValue("graph_id"), graphData.GetValue("begin_time"), Date.ToString("yyyyMMddHHmmss"));
- //创建新的图类型
- var newGraphId = RelatedDataUpload.CreateGraphInstance(graphyType, Date.ToString("yyyyMMddHHmmss"));
- return newGraphId;
- }
- /// <summary>
- /// 创建两个设备的关系
- /// </summary>
- /// <param name="from_id"></param>
- /// <param name="to_id"></param>
- /// <param name="relType"></param>
- /// <returns></returns>
- protected ReleateData GetReleateData(string from_id, string to_id, string relType = "1")
- {
- return new ReleateData()
- {
- graph_id = GraphId,
- from_id = GetCloudId(from_id),
- to_id = GetCloudId(to_id),
- rel_type = relType
- };
- }
- /// <summary>
- /// Datable导出成Excel
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="file">导出路径(包括文件名与扩展名)</param>
- public static void TableToExcel(List<ReleateData> dt, string file)
- {
- IWorkbook workbook;
- string fileExt = Path.GetExtension(file).ToLower();
- if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; }
- if (workbook == null) { return; }
- ISheet sheet = workbook.CreateSheet("Sheet1");
- //表头
- IRow row = sheet.CreateRow(0);
- var sheetNames = new List<string> { "GraphId", "FromId", "ToId", "RelType" };
- for (int i = 0; i < sheetNames.Count; i++)
- {
- ICell cell = row.CreateCell(i);
- cell.SetCellValue(sheetNames[i]);
- }
- //数据
- for (int i = 0; i < dt.Count; i++)
- {
- IRow row1 = sheet.CreateRow(i + 1);
- ICell cell = row1.CreateCell(0);
- cell.SetCellValue(dt[i].graph_id);
- ICell cell1 = row1.CreateCell(1);
- cell1.SetCellValue(dt[i].from_id);
- ICell cell2 = row1.CreateCell(2);
- cell2.SetCellValue(dt[i].to_id);
- ICell cell3 = row1.CreateCell(3);
- cell3.SetCellValue(dt[i].rel_type);
- }
- //转为字节数组
- MemoryStream stream = new MemoryStream();
- workbook.Write(stream);
- var buf = stream.ToArray();
- //保存为Excel文件
- using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
- {
- fs.Write(buf, 0, buf.Length);
- fs.Flush();
- }
- }
- }
- }
|