XlRepository.cs 5.3 KB

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