XlCache.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. public static List<XlInfo> GetAll()
  15. {
  16. try
  17. {
  18. DateTime now = DateTime.Now;
  19. List<XlInfo> xlInfos = new List<XlInfo>();
  20. for (int i = 0; i < 30; i++)
  21. {
  22. using (RHDWPartContext db = RHDWPartContext.GetContext(now.AddDays(-i)))
  23. {
  24. var items = db.XlInfos.OrderBy(p => p.SatName).OrderByDescending(p => p.TimeBJ).ToList();
  25. xlInfos.AddRange(items);
  26. }
  27. }
  28. return xlInfos;
  29. }
  30. catch (Exception ex)
  31. {
  32. Serilog.Log.Error(ex, "加载星历信息异常");
  33. return null;
  34. }
  35. }
  36. public static async Task<List<XlInfo>> GetAllAsync()
  37. {
  38. try
  39. {
  40. List<XlInfo> list = new List<XlInfo>();
  41. if (!Directory.Exists("DbPart")) return list;
  42. var yearDirs = Directory.EnumerateDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart")).OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name));//年目录,倒叙排列
  43. foreach (var yearDir in yearDirs)
  44. {
  45. //每一天的db文件,倒序排列
  46. var dayFiles = Directory.EnumerateFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, yearDir),"*.db").OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name.Substring(0, 4)));
  47. foreach (var dayFile in dayFiles)
  48. {
  49. using (RHDWPartContext db = RHDWPartContext.GetContext(dayFile))
  50. {
  51. list.AddRange(await db.XlInfos.ToListAsync());
  52. if (list.Count > 2000)
  53. return list;
  54. }
  55. }
  56. }
  57. return list;
  58. }
  59. catch (Exception ex)
  60. {
  61. Serilog.Log.Error(ex, "加载星历信息异常");
  62. return null;
  63. }
  64. }
  65. }
  66. }