AppConfigRegionModel.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
  2. using Azylee.Core.DataUtils.CollectionUtils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
  8. {
  9. /// <summary>
  10. /// 配置块模型
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public class AppConfigRegionModel<T> where T : IAppConfigItemModel
  14. {
  15. public List<T> Items { get; set; }
  16. public AppConfigRegionModel()
  17. {
  18. this.Items = new List<T>();
  19. }
  20. public bool HasItems()
  21. {
  22. return Ls.Ok(Items);
  23. }
  24. public void AddItem(T item, bool overwrite = false)
  25. {
  26. bool repeat = false;
  27. for (int i = 0; i < Items.Count; i++)
  28. {
  29. if (Items[i].GetUniqueName() == item.GetUniqueName())
  30. {
  31. if (overwrite) { Items[i] = item; }
  32. repeat = true;
  33. }
  34. }
  35. if (!repeat) { this.Items.Add(item); }
  36. }
  37. public void RemoveItem(string name)
  38. {
  39. for (int i = 0; i < Items.Count; i++)
  40. {
  41. if (Items[i].GetUniqueName() == name)
  42. {
  43. Items.RemoveAt(i);
  44. break;
  45. }
  46. }
  47. }
  48. public T GetItem(string name)
  49. {
  50. for (int i = 0; i < Items.Count; i++)
  51. {
  52. if (Items[i].GetUniqueName() == name)
  53. {
  54. return Items[i];
  55. }
  56. }
  57. return default(T);
  58. }
  59. public void OrderByNumber()
  60. {
  61. Items = Items.OrderBy(x => x.GetOrderNumber()).ThenBy(x => x.GetUniqueName()).ToList();
  62. }
  63. public List<string> GetNames()
  64. {
  65. List<string> rs = new List<string>();
  66. foreach (var item in Items)
  67. {
  68. rs.Add(item.GetUniqueName());
  69. }
  70. return rs;
  71. }
  72. }
  73. }