XlCache.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 mainXl = list.Where(w => w.SatCode == satCode);
  61. var minSec = mainXl.Min(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds));
  62. var find = mainXl.FirstOrDefault(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds) == minSec);
  63. return find;
  64. }
  65. catch (Exception ex)
  66. {
  67. Serilog.Log.Error(ex, $"获取卫星[{satCode}]--{timeBj:yyyyMMddHHmmss}时刻附近的双行根数出错!");
  68. return null;
  69. }
  70. }
  71. }
  72. }