PublicTypeLogic.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using CZFW.Framework.Logic;
  4. using CZKJ.GBRS2.Entity;
  5. using CZKJ.GBRS2.Interface;
  6. namespace CZKJ.GBRS2.Logic
  7. {
  8. public class PublicTypeLogic : LogicBase<PublicTypeEntity>, IPublicType
  9. {
  10. public IList<PublicTypeEntity> GetChildList(int? parentId)
  11. {
  12. var typeList = new List<PublicTypeEntity>();
  13. var queryable = Queryable;
  14. if (parentId.HasValue)
  15. {
  16. queryable = queryable.Where(x => x.ParentId == parentId);
  17. if (queryable.Count() > 0)
  18. {
  19. typeList.AddRange(GetList(queryable.ToList()));
  20. }
  21. else
  22. {
  23. typeList.Add(Queryable.FirstOrDefault(x => x.Id == parentId));
  24. }
  25. }
  26. else
  27. {
  28. typeList.AddRange(queryable.ToList());
  29. }
  30. return typeList;
  31. }
  32. IList<PublicTypeEntity> list = new List<PublicTypeEntity>();
  33. private IList<PublicTypeEntity> GetList(IList<PublicTypeEntity> typelist)
  34. {
  35. foreach (var item in typelist)
  36. {
  37. var count = Queryable.Count(x => x.ParentId == item.Id);
  38. if (count == 0)
  39. {
  40. list.Add(item);
  41. }
  42. else
  43. {
  44. GetList(Queryable.Where(x => x.ParentId == item.Id).ToList());
  45. }
  46. }
  47. return list;
  48. }
  49. public IList<PublicTypeEntity> GetParentList()
  50. {
  51. var list = Queryable.ToList();
  52. var typeList = new List<PublicTypeEntity>();
  53. foreach (var item in list)
  54. {
  55. var count = list.Count(x => x.ParentId == item.Id);
  56. if (count > 0)
  57. {
  58. typeList.Add(item);
  59. }
  60. }
  61. return typeList;
  62. }
  63. public IList<PublicTypeEntity> GetList(int? parentId, string keywords, int pageIndex, int pageSize, out int rowsCount)
  64. {
  65. var queryable = Queryable;
  66. if (parentId.HasValue)
  67. {
  68. queryable = queryable.Where(x => x.ParentId == parentId);
  69. }
  70. if (!string.IsNullOrWhiteSpace(keywords))
  71. {
  72. queryable = queryable.Where(x => x.Name.Contains(keywords));
  73. }
  74. rowsCount = queryable.Count();
  75. var result = from a in queryable
  76. join b in DbContext.Set<PublicTypeEntity>() on a.ParentId equals b.Id into temp
  77. from c in temp.DefaultIfEmpty()
  78. select new
  79. {
  80. Id = a.Id,
  81. Name = a.Name,
  82. Description = a.Description,
  83. ParentName = c == null ? "" : c.Name,
  84. SortOrder = a.SortOrder,
  85. ParentId = a.ParentId
  86. };
  87. return result.OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList().Select(x => new PublicTypeEntity
  88. {
  89. Id = x.Id,
  90. Name = x.Name,
  91. ParentId = x.ParentId,
  92. Description = x.Description,
  93. ParentName = x.ParentName,
  94. SortOrder = x.SortOrder
  95. }).ToList();
  96. }
  97. }
  98. }