LandSJRescueMission.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using KYFramework;
  2. using NPOI.SS.Formula.PTG;
  3. using SimulationServer.Utils;
  4. namespace SimulationServer;
  5. public class LandSJRescueMission : Entity
  6. {
  7. public string MissionId; // 任务ID
  8. public bool Success; // 任务是否成功
  9. public List<AircraftLandSJ> aircrafts = new List<AircraftLandSJ>();
  10. public bool IsRunning;
  11. public double SimulationTime;
  12. public Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> singleReport = new();
  13. // 灭火的 整体能力
  14. // <sheet,<指标名,值列表>>
  15. public Dictionary<string, Dictionary<string, List<string>>> totalReport = new();
  16. public Dictionary<string, Dictionary<string, List<string>>> aircraftSJDatas = new();
  17. public bool IsOver = false;
  18. private int currentExecuteCount = 1;
  19. public int ExecutionContext = 0;
  20. public string date;
  21. public void Start()
  22. {
  23. IsRunning = true;
  24. aircrafts.ForEach(a => a.Start());
  25. Log.Info($"{MissionId} 任务开始!");
  26. }
  27. public void Reset()
  28. {
  29. SimulationTime = 0;
  30. aircrafts?.ForEach(a => a.Reset());
  31. }
  32. public void EndMission()
  33. {
  34. IsRunning = false;
  35. Log.Info($"{MissionId} 任务结束!");
  36. //SaveSJ();
  37. //SaveTotalMH();
  38. //ReportSJ();
  39. if (currentExecuteCount > ExecutionContext)
  40. {
  41. return;
  42. }
  43. if (currentExecuteCount < ExecutionContext)
  44. {
  45. SaveSJ();
  46. //SaveTotalMH();
  47. SaveAircraftSJDatas();
  48. }
  49. if (currentExecuteCount == ExecutionContext)
  50. {
  51. SaveSJ();
  52. SaveAircraftSJDatas();
  53. //SaveTotalMH();
  54. ReportSJ();
  55. //this.StartAsyncMH();
  56. IsOver = true;
  57. return;
  58. }
  59. this.Reset();
  60. this.Start();
  61. currentExecuteCount++;
  62. // var readyTime = AircraftXHs.First().TaskReadyTime;
  63. // Task.Delay(TimeSpan.FromSeconds(readyTime)).ContinueWith(t => this.StartAsyncXH());
  64. }
  65. public void SaveAircraftSJDatas()
  66. {
  67. foreach (AircraftLandSJ aircraftEntity in aircrafts)
  68. {
  69. string key = aircraftEntity.AircraftId;
  70. if (!aircraftSJDatas.ContainsKey(key))
  71. {
  72. aircraftSJDatas[key] = new Dictionary<string, List<string>>();
  73. }
  74. if (!aircraftSJDatas[key].ContainsKey("识别成功率"))
  75. aircraftSJDatas[key]["识别成功率"] = new List<string>();
  76. aircraftSJDatas[key]["识别成功率"].Add(aircraftEntity.isseePerson ? aircraftEntity.sbCount.ToString() : "0");
  77. if (!aircraftSJDatas[key].ContainsKey("识别数量"))
  78. aircraftSJDatas[key]["识别数量"] = new List<string>();
  79. aircraftSJDatas[key]["识别数量"].Add(aircraftEntity.sbCount.ToString());
  80. if (!aircraftSJDatas[key].ContainsKey("人员数量"))
  81. aircraftSJDatas[key]["人员数量"] = new List<string>();
  82. aircraftSJDatas[key]["人员数量"].Add(aircraftEntity.targetCount.ToString());
  83. if (!aircraftSJDatas[key].ContainsKey("任务准备时间"))
  84. aircraftSJDatas[key]["任务准备时间"] = new List<string>();
  85. aircraftSJDatas[key]["任务准备时间"].Add(aircraftEntity.TaskReadyTime.ToString());
  86. if (!aircraftSJDatas[key].ContainsKey("平均搜索时间"))
  87. aircraftSJDatas[key]["平均搜索时间"] = new List<string>();
  88. aircraftSJDatas[key]["平均搜索时间"].Add(aircraftEntity.SearchTime.ToString());
  89. if (!aircraftSJDatas[key].ContainsKey("平均救助时间"))
  90. aircraftSJDatas[key]["平均救助时间"] = new List<string>();
  91. aircraftSJDatas[key]["平均救助时间"].Add(aircraftEntity.resulttime.ToString());
  92. if (!aircraftSJDatas[key].ContainsKey("总飞行时间"))
  93. aircraftSJDatas[key]["总飞行时间"] = new List<string>();
  94. aircraftSJDatas[key]["总飞行时间"].Add(aircraftEntity.TotalTime.ToString());
  95. if (!aircraftSJDatas[key].ContainsKey("人员存活率"))
  96. aircraftSJDatas[key]["人员存活率"] = new List<string>();
  97. aircraftSJDatas[key]["人员存活率"].Add(aircraftEntity.Success ? aircraftEntity.targetCount.ToString() : "0");
  98. }
  99. }
  100. public void SaveSJ()
  101. {
  102. foreach (AircraftLandSJ aircraftEntity in aircrafts)
  103. {
  104. var staticCapacity = aircraftEntity.GetComponent<SJStaticCapacityComponent>();
  105. if (staticCapacity == null) continue;
  106. staticCapacity.FillData1(aircraftEntity.Db);
  107. string key = aircraftEntity.AircraftId;
  108. if (!singleReport.ContainsKey(key))
  109. {
  110. singleReport[key] = new Dictionary<string, Dictionary<string, List<string>>>();
  111. }
  112. Dictionary<string, Dictionary<string, string>> staticReport = staticCapacity.GetReport1();
  113. foreach (var kv in staticReport)
  114. {
  115. if (!singleReport[key].ContainsKey(kv.Key)) singleReport[key][kv.Key] = new Dictionary<string, List<string>>();
  116. foreach (var kv2 in kv.Value)
  117. {
  118. if (!singleReport[key][kv.Key].ContainsKey(kv2.Key)) singleReport[key][kv.Key][kv2.Key] = new List<string>();
  119. singleReport[key][kv.Key][kv2.Key].Add(kv2.Value);
  120. }
  121. }
  122. }
  123. if (currentExecuteCount == ExecutionContext)
  124. {
  125. foreach (var kv in singleReport)
  126. {
  127. foreach (var kv2 in kv.Value)
  128. {
  129. foreach (var kv3 in kv2.Value)
  130. {
  131. var sum = 0f;
  132. foreach (var kv4 in kv3.Value)
  133. {
  134. bool isfloat = float.TryParse(kv4, out float f);
  135. if (isfloat)
  136. sum += float.Parse(kv4);
  137. else
  138. sum = -1f;
  139. }
  140. if (sum != -1f)
  141. kv3.Value.Add((sum / kv3.Value.Count).ToString());
  142. else
  143. kv3.Value.Add("");
  144. }
  145. }
  146. }
  147. }
  148. }
  149. public void SaveTotalMH()
  150. {
  151. var totalPerformance = GetComponent<SJTotalTaskPerformanceComponent>();
  152. totalPerformance.FillData();
  153. var totalPerformanceReport = totalPerformance.GetReport();
  154. foreach (var kv in totalPerformanceReport)
  155. {
  156. if (!totalReport.ContainsKey(kv.Key)) totalReport[kv.Key] = new Dictionary<string, List<string>>();
  157. foreach (var kv2 in kv.Value)
  158. {
  159. if (!totalReport[kv.Key].ContainsKey(kv2.Key)) totalReport[kv.Key][kv2.Key] = new List<string>();
  160. totalReport[kv.Key][kv2.Key].Add(kv2.Value);
  161. }
  162. }
  163. }
  164. public void ReportSJ()
  165. {
  166. //string data = DateTime.Now.ToString("yyyy-MM-dd");
  167. string path = $"Reports/{date}/{MissionId}";
  168. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  169. foreach (var kv in singleReport)
  170. {
  171. string filePath = $"{path}/{kv.Key}搜救任务单机指标报告.xls";
  172. DataTableExtensions.SaveToExcel(filePath, kv.Value, true);
  173. }
  174. //foreach (KeyValuePair<string, Dictionary<string, List<string>>> keyValuePair in totalReport)
  175. //{
  176. // foreach (KeyValuePair<string, List<string>> kv in keyValuePair.Value)
  177. // {
  178. // List<string> values = kv.Value;
  179. // double sum = 0;
  180. // foreach (string value in values)
  181. // {
  182. // if (double.TryParse(value, out double num))
  183. // {
  184. // sum += num;
  185. // }
  186. // }
  187. // double average = sum / values.Count;
  188. // values.Add(average.ToString("#0.00"));
  189. // }
  190. //}
  191. //string totalPath = $"{path}/{"搜救任务总体指标报告"}.xls";
  192. //DataTableExtensions.SaveToExcel(totalPath, totalReport, true);
  193. }
  194. }
  195. [ObjectSystem]
  196. public class LandSJRescueMissionUpdateSystem : UpdateSystem<LandSJRescueMission>
  197. {
  198. public override void Update(LandSJRescueMission self)
  199. {
  200. if (!self.IsRunning) return;
  201. self.aircrafts?.ForEach(a => a.Update(self.SimulationTime));
  202. if (self.aircrafts.All(a => a.IsOver))
  203. {
  204. self.EndMission();
  205. }
  206. }
  207. }