FavoriteManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Muchinfo.MTPClient.Data.Model;
  2. using Muchinfo.MTPClient.Infrastructure.Utilities;
  3. using Muchinfo.PC.Common.Bosn;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace Muchinfo.MTPClient.Infrastructure.Users
  8. {
  9. public class FavoriteManager
  10. {
  11. private static List<MyFavorite> _myFavorites;
  12. /// <summary>
  13. /// 初始化自选
  14. /// </summary>
  15. /// <param name="defaultFavoriteName">Default name of the favorite.</param>
  16. public static void InitializeFavorite(string defaultFavoriteName)
  17. {
  18. //本地不存在自选板块,则添加个默认"自选"
  19. var list = GetMyFavorites();
  20. if (list == null || !list.Any())
  21. {
  22. var myFavorite = new MyFavorite()
  23. {
  24. Id = 0,
  25. Name = defaultFavoriteName,
  26. ShowIndex = 0,
  27. FavoriteGoodses = null,
  28. };
  29. AddOrUpdateMyFavorites(myFavorite);
  30. }
  31. }
  32. /// <summary>
  33. /// 获取自选列表
  34. /// </summary>
  35. /// <returns>IEnumerable{MyFavorite}.</returns>
  36. public static IEnumerable<MyFavorite> GetMyFavorites()
  37. {
  38. if (_myFavorites == null)
  39. {
  40. var filePath = GetFilePath(true);
  41. if (string.IsNullOrWhiteSpace(filePath)) return null;
  42. _myFavorites = BsonHelper.LoadData<MyFavorite>(filePath);
  43. }
  44. return _myFavorites == null ? null : _myFavorites.OrderBy(z => z.ShowIndex);
  45. }
  46. /// <summary>
  47. /// 保存自选板块
  48. /// </summary>
  49. /// <param name="myFavorite">My favorite.</param>
  50. public static void AddOrUpdateMyFavorites(MyFavorite myFavorite)
  51. {
  52. if (myFavorite == null) return;
  53. var list = GetMyFavorites();
  54. var result = list == null ? new List<MyFavorite>() : list.ToList();
  55. //Id为0,则表示增加
  56. if (myFavorite.Id == 0)
  57. {
  58. myFavorite.Id = result.Count > 0 ? result.Max(z => z.Id) + 1 : 1;
  59. result.Add(myFavorite);
  60. }
  61. else
  62. {
  63. var item = result.FirstOrDefault(z => z.Id == myFavorite.Id);
  64. if (item != null)
  65. {
  66. result.Remove(item);
  67. result.Add(myFavorite);
  68. }
  69. }
  70. var filePath = GetFilePath(false);
  71. _myFavorites = result;
  72. BsonHelper.SaveData<MyFavorite>(filePath, result, null, true);
  73. }
  74. /// <summary>
  75. /// 删除自选板块
  76. /// </summary>
  77. /// <param name="myFavoriteId">My favorite identifier.</param>
  78. public static void DeleteMyFavorites(int myFavoriteId)
  79. {
  80. if (_myFavorites == null || !_myFavorites.Any()) return;
  81. //默认"自选" 不能删除
  82. if (myFavoriteId == 0) return;
  83. var item = _myFavorites.FirstOrDefault(z => z.Id == myFavoriteId);
  84. if (item == null) return;
  85. _myFavorites.Remove(item);
  86. var filePath = GetFilePath(false);
  87. BsonHelper.SaveData<MyFavorite>(filePath, _myFavorites, null, true);
  88. }
  89. /// <summary>
  90. /// 获取存储文件路径
  91. /// </summary>
  92. /// <returns>System.String.</returns>
  93. private static string GetFilePath(bool needValidate)
  94. {
  95. var filePath = Path.Combine(UserManager.UserDataFolder, "MyFavorite" + UserManager.LocalDataExtensionName);
  96. if (needValidate)
  97. {
  98. if (!File.Exists(filePath)) return string.Empty;
  99. }
  100. return filePath;
  101. }
  102. }
  103. }