DepartmentService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using YiSha.Data.Repository;
  8. using YiSha.Entity.OrganizationManage;
  9. using YiSha.Enum;
  10. using YiSha.Model;
  11. using YiSha.Model.Param.OrganizationManage;
  12. using YiSha.Util;
  13. using YiSha.Util.Extension;
  14. using YiSha.Util.Model;
  15. namespace YiSha.Service.OrganizationManage
  16. {
  17. public partial class DepartmentService
  18. {
  19. private IRepositoryFactory _baseRepository;
  20. public DepartmentService(IRepositoryFactory baseRepository)
  21. {
  22. _baseRepository = baseRepository;
  23. }
  24. #region 获取数据
  25. public async Task<List<DepartmentEntity>> GetListPartial(DepartmentListParam param)
  26. {
  27. var expression = ListFilterPartial(param);
  28. var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression);
  29. return list.OrderBy(p => p.DepartmentSort).ToList();
  30. }
  31. public async Task<DepartmentEntity> GetEntityPartial(int id)
  32. {
  33. return await _baseRepository.BaseRepository(dbConnectType).FindEntity<DepartmentEntity>(id);
  34. }
  35. public async Task<int> GetMaxSortPartial()
  36. {
  37. object result = await _baseRepository.BaseRepository(dbConnectType).FindObject("SELECT MAX(DepartmentSort) FROM SysDepartment");
  38. int sort = result.ParseToInt();
  39. sort++;
  40. return sort;
  41. }
  42. /// <summary>
  43. /// 部门名称是否存在
  44. /// </summary>
  45. /// <param name="entity"></param>
  46. ///// <returns></returns>
  47. public bool ExistDepartmentNamePartial(DepartmentEntity entity)
  48. {
  49. var expression = LinqExtensions.True<DepartmentEntity>();
  50. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  51. if (entity.Id.IsNullOrZero())
  52. {
  53. expression = expression.And(t => t.DepartmentName == entity.DepartmentName);
  54. }
  55. else
  56. {
  57. expression = expression.And(t => t.DepartmentName == entity.DepartmentName && t.Id != entity.Id);
  58. }
  59. return _baseRepository.BaseRepository(dbConnectType).IQueryable(expression).Count() > 0 ? true : false;
  60. }
  61. /// <summary>
  62. /// 是否存在子部门
  63. /// </summary>
  64. /// <param name="id"></param>
  65. /// <returns></returns>
  66. public bool ExistChildrenDepartmentPartial(int id)
  67. {
  68. var expression = LinqExtensions.True<DepartmentEntity>();
  69. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  70. expression = expression.And(t => t.ParentId == id);
  71. return _baseRepository.BaseRepository(dbConnectType).IQueryable(expression).Count() > 0 ? true : false;
  72. }
  73. #endregion
  74. #region 提交数据
  75. public async Task SaveFormPartial(DepartmentEntity entity)
  76. {
  77. if (entity.Id.IsNullOrZero())
  78. {
  79. await entity.Create();
  80. await _baseRepository.BaseRepository(dbConnectType).Insert(entity);
  81. }
  82. else
  83. {
  84. await entity.Modify();
  85. await _baseRepository.BaseRepository(dbConnectType).Update(entity);
  86. }
  87. }
  88. #endregion
  89. #region 私有方法
  90. private Expression<Func<DepartmentEntity, bool>> ListFilterPartial(DepartmentListParam param)
  91. {
  92. var expression = LinqExtensions.True<DepartmentEntity>();
  93. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  94. if (param != null)
  95. {
  96. if (!param.DepartmentName.IsEmpty())
  97. {
  98. expression = expression.And(t => t.DepartmentName.Contains(param.DepartmentName));
  99. }
  100. }
  101. return expression;
  102. }
  103. #endregion
  104. }
  105. }