1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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>();
- var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart");
- if (!Directory.Exists(dir)) return list;
- var yearDirs = Directory.EnumerateDirectories(dir).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 mainXl = list.Where(w => w.SatCode == satCode);
- var minSec = mainXl.Min(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds));
- var find = mainXl.FirstOrDefault(p => Math.Abs((p.TimeBJ - timeBj).TotalSeconds) == minSec);
- return find;
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, $"获取卫星[{satCode}]--{timeBj:yyyyMMddHHmmss}时刻附近的双行根数出错!");
- return null;
- }
- }
- }
- }
|