using DW5S.Entity;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
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
{
///
/// 双行根星历仓储
///
public class XlRepository : Repository
{
public XlRepository(OracleContext ctx)
: base(ctx)
{
}
///
/// 获取双行根数总条数
///
///
///
public async Task 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> GetAllSat()
{
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();
return sats;
}
///
/// 获取数据库双行根数
///
/// 当前页(从1开始)
/// 每页显示的条数
/// 卫星编号
///
public async Task> 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);
}
}
///
/// 获取某个星某个时刻最近的双行根(可能返回null)
///
/// 卫星编号
/// 时刻
///
public async Task 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)
return null;
else if (max != null && min == null)
return max;
else if (max == null && min != null)
return min;
else
{
//谁离得更接近就用谁
var val1 = Math.Abs((max.TimeUTC - sigTime).TotalSeconds);
var val2 = Math.Abs((min.TimeUTC - sigTime).TotalSeconds);
if (val1 < val2)
return max;
else
return min;
}
}
catch (Exception ex)
{
throw new Exception($"获取卫星[{satCode}]--{sigTime:yyyyMMddHHmmss}时刻附近的双行根数出错!", ex);
}
}
}
}