XlRepository.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using DW5S.Entity;
  2. using Microsoft.AspNetCore.Mvc.RazorPages;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Caching.Memory;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace DW5S.Repostory
  13. {
  14. /// <summary>
  15. /// 双行根星历仓储
  16. /// </summary>
  17. public class XlRepository : Repository<XlInfo, int>
  18. {
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. /// <param name="ctx"></param>
  23. public XlRepository(SqliteContext ctx)
  24. : base(ctx)
  25. {
  26. }
  27. /// <summary>
  28. /// 获取双行根数总条数
  29. /// </summary>
  30. /// <param name="satCode"></param>
  31. /// <returns></returns>
  32. public async Task<int> GetCount(int? satCode)
  33. {
  34. var queryable = base.dbSet.AsQueryable();
  35. if (satCode != null)
  36. queryable = queryable.Where(p => p.SatCode == satCode);
  37. var count = await queryable.CountAsync();
  38. return count;
  39. }
  40. public async Task<IEnumerable<SatInfo>> GetAllSat()
  41. {
  42. var list = IocContainer.Cache.Get<IEnumerable<SatInfo>>(CacheKeys.AllXlSat);
  43. if (list != null)
  44. {
  45. return list;
  46. }
  47. var data = await dbSet.GroupBy(p => p.SatCode).Select(p => p.FirstOrDefault()).ToListAsync();
  48. var sats = data.Select(p => new SatInfo()
  49. {
  50. SatCode = p.SatCode,
  51. SatName = p.SatName,
  52. SatLon = p.Lon,
  53. }).ToList();
  54. IocContainer.Cache.Set(CacheKeys.AllXlSat, sats, TimeSpan.FromDays(1));
  55. return sats;
  56. }
  57. /// <summary>
  58. /// 获取数据库双行根数
  59. /// </summary>
  60. /// <param name="currentPage">当前页(从1开始)</param>
  61. /// <param name="pageSize">每页显示的条数</param>
  62. /// <param name="satCode">卫星编号</param>
  63. /// <returns></returns>
  64. public async Task<List<XlInfo>> GetPageAsync(int currentPage, int pageSize, int? satCode)
  65. {
  66. try
  67. {
  68. int skip = (currentPage - 1) * pageSize;
  69. var queryable = base.dbSet.AsQueryable();
  70. if (satCode != null)
  71. queryable = queryable.Where(p => p.SatCode == satCode);
  72. var res = await queryable.OrderByDescending(p => p.TimeUTC)
  73. .ThenByDescending(p => p.Lon).Skip(skip).Take(pageSize).ToListAsync();
  74. return res;
  75. }
  76. catch (Exception ex)
  77. {
  78. throw new Exception("分页星历信息查询异常", ex);
  79. }
  80. }
  81. /// <summary>
  82. /// 获取某个星某个时刻最近的双行根
  83. /// </summary>
  84. /// <param name="satCode">卫星编号</param>
  85. /// <param name="sigTime">时刻</param>
  86. /// <returns></returns>
  87. public async Task<XlInfo> GetLatestAsync(int satCode, DateTime sigTime)
  88. {
  89. try
  90. {
  91. var max = await dbSet.Where(p => p.SatCode == satCode && p.TimeUTC >= sigTime)
  92. .OrderBy(p => p.TimeUTC).FirstOrDefaultAsync();
  93. var min = await dbSet.Where(p => p.SatCode == satCode && p.TimeUTC <= sigTime)
  94. .OrderByDescending(p => p.TimeUTC).FirstOrDefaultAsync();
  95. if (max == null && min == null)
  96. {
  97. throw new Exception($"系统缺少编号{satCode}的卫星星历!");
  98. }
  99. else
  100. {
  101. XlInfo xlInfo = null;
  102. if (max != null && min == null)
  103. {
  104. xlInfo = max;
  105. }
  106. else if (max == null && min != null)
  107. {
  108. xlInfo = min;
  109. }
  110. else
  111. {
  112. var val1 = Math.Abs((max.TimeUTC - sigTime).TotalSeconds);
  113. var val2 = Math.Abs((min.TimeUTC - sigTime).TotalSeconds);
  114. if (val1 < val2)
  115. xlInfo = max;
  116. else
  117. xlInfo = min;
  118. }
  119. int days = AppConfigHelper.Get("XlWarningDays", 5);
  120. if (Math.Abs((sigTime - xlInfo.TimeUTC).TotalDays) > days)
  121. {
  122. IocContainer.Logger.Warning($"编号{satCode}卫星TLE星历(发布时刻:{xlInfo.TimeUTC:yyyyMMdd})和信号时刻({sigTime:yyyyMMdd})相差过大");
  123. }
  124. IocContainer.Logger.Information($"卫星{satCode}使用发布时刻为{xlInfo.TimeUTC:yyyyMMddHHmmss}的星历进行推算");
  125. return xlInfo;
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. throw new Exception($"获取卫星[{satCode}]--{sigTime:yyyyMMddHHmmss}时刻附近的双行根数出错!", ex);
  131. }
  132. }
  133. }
  134. }