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. var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart");
  25. if (!Directory.Exists(dir)) return list;
  26. var yearDirs = Directory.EnumerateDirectories(dir).OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name));//年目录,倒叙排列
  27. foreach (var yearDir in yearDirs)
  28. {
  29. //每一天的db文件,倒序排列
  30. var dayFiles = Directory.EnumerateFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, yearDir), "*.db").OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name.Substring(0, 4)));
  31. foreach (var dayFile in dayFiles)
  32. {
  33. using (RHDWPartContext db = RHDWPartContext.GetContext(dayFile))
  34. {
  35. list.AddRange(await db.XlInfos.ToListAsync());
  36. if (maxCount > 0 && list.Count >= maxCount)
  37. return list;
  38. }
  39. }
  40. }
  41. return list;
  42. }
  43. catch (Exception ex)
  44. {
  45. Serilog.Log.Error(ex, "加载星历信息异常");
  46. return null;
  47. }
  48. }
  49. /// <summary>
  50. /// 获取某个星某个时刻最近的双行根(可能返回null)
  51. /// </summary>
  52. /// <param name="satCode">卫星编号</param>
  53. /// <param name="timeBj">时刻</param>
  54. /// <returns></returns>
  55. public static async Task<XlInfo> GetLatestAsync(int satCode, DateTime timeBj)
  56. {
  57. try
  58. {
  59. var list = await GetAllAsync(0);
  60. if (!list.Any()) return null;
  61. var minSec = list.Min(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds));
  62. var find = list.Find(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. }