123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using DW5S.Entity;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Caching.Memory;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace DW5S.Repostory
- {
- /// <summary>
- /// 双行根星历仓储
- /// </summary>
- public class XlRepository : Repository<XlInfo, int>
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="ctx"></param>
- public XlRepository(SqliteContext ctx)
- : base(ctx)
- {
- }
- /// <summary>
- /// 获取双行根数总条数
- /// </summary>
- /// <param name="satCode"></param>
- /// <returns></returns>
- public async Task<int> GetCount(int? satCode)
- {
- var queryable = base.dbSet.AsQueryable();
- if (satCode != null)
- queryable = queryable.Where(p => p.SatCode == satCode);
- var count = await queryable.CountAsync();
- return count;
- }
- public async Task<IEnumerable<SatInfo>> GetAllSat()
- {
- var list = IocContainer.Cache.Get<IEnumerable<SatInfo>>(CacheKeys.AllXlSat);
- if (list != null)
- {
- return list;
- }
- var data = await dbSet.GroupBy(p => p.SatCode).Select(p => p.FirstOrDefault()).ToListAsync();
- var sats = data.Select(p => new SatInfo()
- {
- SatCode = p.SatCode,
- SatName = p.SatName,
- SatLon = p.Lon,
- }).ToList();
- IocContainer.Cache.Set(CacheKeys.AllXlSat, sats, TimeSpan.FromDays(1));
- return sats;
- }
- /// <summary>
- /// 获取数据库双行根数
- /// </summary>
- /// <param name="currentPage">当前页(从1开始)</param>
- /// <param name="pageSize">每页显示的条数</param>
- /// <param name="satCode">卫星编号</param>
- /// <returns></returns>
- public async Task<List<XlInfo>> GetPageAsync(int currentPage, int pageSize, int? satCode)
- {
- try
- {
- int skip = (currentPage - 1) * pageSize;
- var queryable = base.dbSet.AsQueryable();
- if (satCode != null)
- queryable = queryable.Where(p => p.SatCode == satCode);
- var res = await queryable.OrderByDescending(p => p.TimeUTC)
- .ThenByDescending(p => p.Lon).Skip(skip).Take(pageSize).ToListAsync();
- return res;
- }
- catch (Exception ex)
- {
- throw new Exception("分页星历信息查询异常", ex);
- }
- }
- /// <summary>
- /// 获取某个星某个时刻最近的双行根
- /// </summary>
- /// <param name="satCode">卫星编号</param>
- /// <param name="sigTime">时刻</param>
- /// <returns></returns>
- public async Task<XlInfo> GetLatestAsync(int satCode, DateTime sigTime)
- {
- try
- {
- var max = await dbSet.Where(p => p.SatCode == satCode && p.TimeUTC >= sigTime)
- .OrderBy(p => p.TimeUTC).FirstOrDefaultAsync();
- var min = await dbSet.Where(p => p.SatCode == satCode && p.TimeUTC <= sigTime)
- .OrderByDescending(p => p.TimeUTC).FirstOrDefaultAsync();
- if (max == null && min == null)
- {
- throw new Exception($"系统缺少编号{satCode}的卫星星历!");
- }
- else
- {
- XlInfo xlInfo = null;
- if (max != null && min == null)
- {
- xlInfo = max;
- }
- else if (max == null && min != null)
- {
- xlInfo = min;
- }
- else
- {
- var val1 = Math.Abs((max.TimeUTC - sigTime).TotalSeconds);
- var val2 = Math.Abs((min.TimeUTC - sigTime).TotalSeconds);
- if (val1 < val2)
- xlInfo = max;
- else
- xlInfo = min;
- }
- int days = AppConfigHelper.Get("XlWarningDays", 5);
- if (Math.Abs((sigTime - xlInfo.TimeUTC).TotalDays) > days)
- {
- IocContainer.Logger.Warning($"编号{satCode}卫星TLE星历(发布时刻:{xlInfo.TimeUTC:yyyyMMdd})和信号时刻({sigTime:yyyyMMdd})相差过大");
- }
- IocContainer.Logger.Information($"卫星{satCode}使用发布时刻为{xlInfo.TimeUTC:yyyyMMddHHmmss}的星历进行推算");
- return xlInfo;
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"获取卫星[{satCode}]--{sigTime:yyyyMMddHHmmss}时刻附近的双行根数出错!", ex);
- }
- }
- }
- }
|