XlCache.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.Repostory.EFContext;
  9. using XdCxRhDW.Repostory.Model;
  10. namespace XdCxRhDW.Repostory
  11. {
  12. public static class XlCache
  13. {
  14. /// <summary>
  15. /// 获取数据库所有双行根数
  16. /// </summary>
  17. /// <param name="maxCount">最大条数.超过此条数时不再继续查询,为0则不限制条数</param>
  18. /// <returns></returns>
  19. public static async Task<List<XlInfo>> GetAllAsync(int maxCount = 2000)
  20. {
  21. try
  22. {
  23. List<XlInfo> list = new List<XlInfo>();
  24. if (!Directory.Exists("DbPart")) return list;
  25. var yearDirs = Directory.EnumerateDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart")).OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name));//年目录,倒叙排列
  26. foreach (var yearDir in yearDirs)
  27. {
  28. //每一天的db文件,倒序排列
  29. var dayFiles = Directory.EnumerateFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, yearDir), "*.db").OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name.Substring(0, 4)));
  30. foreach (var dayFile in dayFiles)
  31. {
  32. using (RHDWPartContext db = RHDWPartContext.GetContext(dayFile))
  33. {
  34. list.AddRange(await db.XlInfos.ToListAsync());
  35. if (maxCount > 0 && list.Count >= maxCount)
  36. return list;
  37. }
  38. }
  39. }
  40. return list;
  41. }
  42. catch (Exception ex)
  43. {
  44. Serilog.Log.Error(ex, "加载星历信息异常");
  45. return null;
  46. }
  47. }
  48. /// <summary>
  49. /// 获取某个星某个时刻最近的双行根(可能返回null)
  50. /// </summary>
  51. /// <param name="satCode">卫星编号</param>
  52. /// <param name="timeBj">时刻</param>
  53. /// <returns></returns>
  54. public static async Task<XlInfo> GetLatestAsync(int satCode, DateTime timeBj)
  55. {
  56. try
  57. {
  58. var list = await GetAllAsync(0);
  59. if (!list.Any()) return null;
  60. var minSec = list.Min(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds));
  61. var find = list.Find(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds) == minSec);
  62. return find;
  63. }
  64. catch (Exception ex)
  65. {
  66. Serilog.Log.Error(ex, $"获取卫星[{satCode}]--{timeBj:yyyyMMddHHmmss}时刻附近的双行根数出错!");
  67. return null;
  68. }
  69. }
  70. }
  71. }