XlRepository.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using XdCxRhDW.Entity;
  10. namespace XdCxRhDW.Repostory
  11. {
  12. public static class XlRepository
  13. {
  14. /// <summary>
  15. /// 获取数据库所有双行根数
  16. /// </summary>
  17. /// <returns></returns>
  18. public static async Task<List<XlInfo>> GetAllAsync()
  19. {
  20. try
  21. {
  22. using (RHDWContext db = new RHDWContext())
  23. {
  24. var res = await db.XlInfos.OrderByDescending(p => p.TimeUTC).OrderByDescending(p => p.Lon).Take(5000).ToListAsync();
  25. return res;
  26. }
  27. }
  28. catch (Exception ex)
  29. {
  30. Serilog.Log.Error(ex, "加载星历信息异常");
  31. return null;
  32. }
  33. }
  34. public static async Task<int> GetCount(int? satCode)
  35. {
  36. using (RHDWContext db = new RHDWContext())
  37. {
  38. if (satCode == null)
  39. {
  40. var count = await db.XlInfos.CountAsync();
  41. return count;
  42. }
  43. else
  44. {
  45. var count = await db.XlInfos.Where(w => w.SatCode == satCode).CountAsync();
  46. return count;
  47. }
  48. }
  49. }
  50. public static async Task<List<SatInfo>> GetAllSat()
  51. {
  52. using (RHDWContext db = new RHDWContext())
  53. {
  54. string sql = "select * from XlInfo where id in (select max(id) maxid from xlinfo GROUP BY SatCode )";
  55. var data = await db.Database.SqlQuery<XlInfo>(sql).ToListAsync();
  56. var sats = data.Select(p => new SatInfo()
  57. {
  58. SatCode = p.SatCode,
  59. SatName = p.SatName,
  60. SatLon = p.Lon,
  61. }).ToList();
  62. return sats;
  63. }
  64. }
  65. /// <summary>
  66. /// 获取数据库所有双行根数
  67. /// </summary>
  68. /// <returns></returns>
  69. public static async Task<List<XlInfo>> GetPageAsync(int currentPage, int pageSize, int? satCode)
  70. {
  71. try
  72. {
  73. using (RHDWContext db = new RHDWContext())
  74. {
  75. var topNumber = (currentPage - 1) * pageSize;
  76. StringBuilder sb = new StringBuilder();
  77. sb.Append($"select * from xlinfo ");
  78. if (satCode != null)
  79. {
  80. sb.Append($"where SatCode={satCode} ");
  81. }
  82. sb.Append($"order by TimeUTC desc limit {pageSize} offset {topNumber}");
  83. string sql = sb.ToString();
  84. var res = await db.Database.SqlQuery<XlInfo>(sql).ToListAsync();
  85. return res;
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. Serilog.Log.Error(ex, "加载星历信息异常");
  91. return null;
  92. }
  93. }
  94. /// <summary>
  95. /// 获取某个星某个时刻最近的双行根(可能返回null)
  96. /// </summary>
  97. /// <param name="satCode">卫星编号</param>
  98. /// <param name="sigTime">时刻</param>
  99. /// <returns></returns>
  100. public static async Task<XlInfo> GetLatestAsync(int satCode, DateTime sigTime)
  101. {
  102. try
  103. {
  104. satCode = 40892;
  105. sigTime = new DateTime(2024, 10, 09, 23, 59, 53);
  106. //GetLatestAsync方法在索引列上使用了加减运算会导致索引失效,运算变慢,这个函数是对GetLatestAsyncOld的优化
  107. //老的SQL很慢
  108. //SELECT min(abs(JULIANDAY(TimeUTC)-JULIANDAY('2024-10-09 23:59:53'))) as NearDaySpan,* FROM XlInfo where satcode=40892
  109. using (RHDWContext db = new RHDWContext())
  110. {
  111. string sql = $"select * from XlInfo where SatCode={satCode} and TimeUTC>='{sigTime:yyyy-MM-dd HH:mm:ss}' order by TimeUTC limit 1";
  112. var max = await db.Database.SqlQuery<XlInfo>(sql).FirstOrDefaultAsync();
  113. sql = $"select * from XlInfo where SatCode={satCode} and TimeUTC<='{sigTime:yyyy-MM-dd HH:mm:ss}' order by TimeUTC desc limit 1";
  114. var min = await db.Database.SqlQuery<XlInfo>(sql).FirstOrDefaultAsync();
  115. if (max == null && min == null)
  116. return null;
  117. else if (max != null && min == null)
  118. return max;
  119. else if (max == null && min != null)
  120. return min;
  121. else
  122. {
  123. //谁离得更接近就用谁
  124. var val1 = Math.Abs((max.TimeUTC - sigTime).TotalSeconds);
  125. var val2 = Math.Abs((min.TimeUTC - sigTime).TotalSeconds);
  126. if (val1 < val2)
  127. return max;
  128. else
  129. return min;
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Serilog.Log.Error(ex, $"获取卫星[{satCode}]--{sigTime:yyyyMMddHHmmss}时刻附近的双行根数出错!");
  136. return null;
  137. }
  138. }
  139. }
  140. }