AppConfigRegionModel.cs 1.9 KB

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