XlRepository.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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).ToListAsync();
  24. return res;
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. Serilog.Log.Error(ex, "加载星历信息异常");
  30. return null;
  31. }
  32. }
  33. /// <summary>
  34. /// 获取某个星某个时刻最近的双行根(可能返回null)
  35. /// </summary>
  36. /// <param name="satCode">卫星编号</param>
  37. /// <param name="sigTime">时刻</param>
  38. /// <returns></returns>
  39. public static async Task<XlInfo> GetLatestAsync(int satCode, DateTime sigTime)
  40. {
  41. var sql = $"SELECT min(abs(JULIANDAY(TimeUTC)-JULIANDAY('{sigTime:yyyy-MM-dd HH:mm:ss}')))" +
  42. $" as NearDaySpan,* FROM XlInfo where satcode={satCode}";
  43. try
  44. {
  45. using (RHDWContext db = new RHDWContext())
  46. {
  47. var res= await db.XlInfos.SqlQuery(sql).FirstOrDefaultAsync();
  48. return res;
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53. Serilog.Log.Error(ex, $"获取卫星[{satCode}]--{sigTime:yyyyMMddHHmmss}时刻附近的双行根数出错!Sql={sql}");
  54. return null;
  55. }
  56. }
  57. }
  58. }