CheckDuplicationBIMId.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* ==============================================================================
  2. * 功能描述:检查BimID是否重复,多个岗位对应一个模型
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/12/13 17:25:22
  5. * ==============================================================================*/
  6. using SAGA.MBI.Calc;
  7. using SAGA.MBI.DataArrange;
  8. using SAGA.MBI.Model;
  9. using SAGA.MBI.RequestData;
  10. using SAGA.MBI.WinView.Upload;
  11. using SAGA.RevitUtils.Extends;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using DevExpress.Data.Helpers;
  19. using Microsoft.Win32;
  20. using NPOI.SS.UserModel;
  21. using NPOI.XSSF.UserModel;
  22. using SAGA.DotNetUtils.Extend;
  23. using SAGA.DotNetUtils.Logger;
  24. using SAGA.DotNetUtils.Others;
  25. using SAGA.MBI.Common;
  26. using SAGA.MBI.Tools;
  27. namespace SAGA.MBI.ToolsData
  28. {
  29. /// <summary>
  30. /// CheckDuplicationBIMId
  31. /// </summary>
  32. class CheckDuplicationBIMId
  33. {
  34. /// <summary>
  35. /// 检查BIMId是否重复
  36. /// </summary>
  37. public static void CorrectAll()
  38. {
  39. List<IEnumerable<IGrouping<string, MRevitEquipBase>>> groupss=new List<IEnumerable<IGrouping<string, MRevitEquipBase>>>();
  40. foreach (TreeNodeItem buildingItem in MBIControl.ProjectTree.Children)
  41. {
  42. string buildingId = buildingItem.Item.Id;
  43. var groups=(Check(buildingId));
  44. if (groups == null) continue;
  45. Correct(groups);
  46. }
  47. }
  48. /// <summary>
  49. /// 检查BIMId是否重复
  50. /// </summary>
  51. private static IEnumerable<IGrouping<string, MRevitEquipBase>> Check(string buildingId)
  52. {
  53. var dutys = DalCommon.GetAllDutys(buildingId);
  54. var bybimidGroups = dutys.GroupBy(t => t.BimID);
  55. var exceptionGroups = bybimidGroups.Where(t => t.Count() >= 2 && !t.Key.IsOnlyDutyNoModelBIMID());
  56. return exceptionGroups;
  57. }
  58. /// <summary>
  59. /// 修正Bimdid重复数据,保留信息点最全的
  60. /// </summary>
  61. /// <param name="groups"></param>
  62. private static void Correct(IEnumerable<IGrouping<string, MRevitEquipBase>> groups)
  63. {
  64. foreach (IGrouping<string, MRevitEquipBase> equipBases in groups)
  65. {
  66. var list=equipBases.ToList();
  67. list.Sort((x, y) => { return x.CloundJObject.Count.CompareTo(y.CloundJObject.Count);});
  68. for (int i = 0; i < list.Count-1; i++)
  69. {
  70. var item = list[i];
  71. item.DelObject();
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 导出数据
  77. /// </summary>
  78. /// <param name="xyzsList"></param>
  79. private static void ExportToExcel(List<IEnumerable<IGrouping<string, MRevitEquipBase>>> groupss)
  80. {
  81. SaveFileDialog sflg = new SaveFileDialog();
  82. sflg.Filter = "Excel(*.xlsx)|*.xlsx";
  83. if (sflg.ShowDialog() != true) return;
  84. string fileName = sflg.FileName;
  85. IWorkbook book = new XSSFWorkbook();
  86. ISheet sheet = book.CreateSheet("Summary");
  87. foreach (IEnumerable<IGrouping<string, MRevitEquipBase>> groups in groupss)
  88. {
  89. ExportToExcel(sheet, groups, fileName);
  90. }
  91. Save(book, fileName);
  92. }
  93. /// <summary>
  94. /// 导出数据到指定目录
  95. /// </summary>
  96. /// <param name="xyzsList"></param>
  97. /// <param name="fileName"></param>
  98. private static void ExportToExcel(ISheet sheet, IEnumerable<IGrouping<string, MRevitEquipBase>> groups, string fileName)
  99. {
  100. try
  101. {
  102. #region 添加表头
  103. IRow row = sheet.CreateRow(0);
  104. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  105. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  106. cell0.SetCellValue("BIMID");
  107. ICell cell1 = row.CreateCell(1);
  108. cell1.SetCellType(CellType.String);
  109. cell1.SetCellValue("DutyId");
  110. #endregion
  111. #region 添加数据
  112. int index = 1;
  113. foreach (var group in groups)
  114. {
  115. var bimid = group.Key;
  116. var dutys = "";
  117. foreach (var equip in group)
  118. {
  119. dutys += equip.Id + ";";
  120. }
  121. IRow rowN = sheet.CreateRow(index);
  122. NPOI.SS.UserModel.ICell cellN0 = rowN.CreateCell(0);
  123. cellN0.SetCellType(NPOI.SS.UserModel.CellType.String);
  124. cellN0.SetCellValue(bimid);
  125. ICell cellN1 = rowN.CreateCell(1);
  126. cellN1.SetCellType(CellType.String);
  127. cellN1.SetCellValue(dutys);
  128. index++;
  129. }
  130. #endregion
  131. }
  132. catch (Exception e)
  133. {
  134. MessageShowBase.Show(e);
  135. }
  136. }
  137. private static void Save(IWorkbook book, string fileName)
  138. {
  139. #region 写入
  140. MemoryStream ms = new MemoryStream();
  141. book.Write(ms);
  142. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  143. {
  144. byte[] data = ms.ToArray();
  145. fs.Write(data, 0, data.Length);
  146. fs.Flush();
  147. }
  148. ms.Close();
  149. ms.Dispose();
  150. #endregion
  151. }
  152. }
  153. }