XlCache.cs 3.0 KB

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