123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using XdCxRhDW.Repostory.EFContext;
- using XdCxRhDW.Repostory.Model;
- namespace XdCxRhDW.Repostory
- {
- public static class XlCache
- {
- /// <summary>
- /// 获取数据库所有双行根数
- /// </summary>
- /// <param name="maxCount">最大条数.超过此条数时不再继续查询,为0则不限制条数</param>
- /// <returns></returns>
- public static async Task<List<XlInfo>> GetAllAsync(int maxCount = 2000)
- {
- try
- {
- List<XlInfo> list = new List<XlInfo>();
- if (!Directory.Exists("DbPart")) return list;
- var yearDirs = Directory.EnumerateDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart")).OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name));//年目录,倒叙排列
- foreach (var yearDir in yearDirs)
- {
- //每一天的db文件,倒序排列
- var dayFiles = Directory.EnumerateFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, yearDir), "*.db").OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name.Substring(0, 4)));
- foreach (var dayFile in dayFiles)
- {
- using (RHDWPartContext db = RHDWPartContext.GetContext(dayFile))
- {
- list.AddRange(await db.XlInfos.ToListAsync());
- if (maxCount > 0 && list.Count >= maxCount)
- return list;
- }
- }
- }
- return list;
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "加载星历信息异常");
- return null;
- }
- }
- /// <summary>
- /// 获取某个星某个时刻最近的双行根(可能返回null)
- /// </summary>
- /// <param name="satCode">卫星编号</param>
- /// <param name="timeBj">时刻</param>
- /// <returns></returns>
- public static async Task<XlInfo> GetLatestAsync(int satCode, DateTime timeBj)
- {
- try
- {
- var list = await GetAllAsync(0);
- if (!list.Any()) return null;
- var minSec = list.Min(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds));
- var find = list.Find(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds) == minSec);
- return find;
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, $"获取卫星[{satCode}]--{timeBj:yyyyMMddHHmmss}时刻附近的双行根数出错!");
- return null;
- }
- }
- }
- }
|