using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using YiSha.Data.Repository; using YiSha.Entity.OrganizationManage; using YiSha.Enum; using YiSha.Model; using YiSha.Model.Param.OrganizationManage; using YiSha.Util; using YiSha.Util.Extension; using YiSha.Util.Model; namespace YiSha.Service.OrganizationManage { public partial class DepartmentService { private IRepositoryFactory _baseRepository; public DepartmentService(IRepositoryFactory baseRepository) { _baseRepository = baseRepository; } #region 获取数据 public async Task> GetListPartial(DepartmentListParam param) { var expression = ListFilterPartial(param); var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression); return list.OrderBy(p => p.DepartmentSort).ToList(); } public async Task GetEntityPartial(int id) { return await _baseRepository.BaseRepository(dbConnectType).FindEntity(id); } public async Task GetMaxSortPartial() { object result = await _baseRepository.BaseRepository(dbConnectType).FindObject("SELECT MAX(DepartmentSort) FROM SysDepartment"); int sort = result.ParseToInt(); sort++; return sort; } /// /// 部门名称是否存在 /// /// ///// public bool ExistDepartmentNamePartial(DepartmentEntity entity) { var expression = LinqExtensions.True(); expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No); if (entity.Id.IsNullOrZero()) { expression = expression.And(t => t.DepartmentName == entity.DepartmentName); } else { expression = expression.And(t => t.DepartmentName == entity.DepartmentName && t.Id != entity.Id); } return _baseRepository.BaseRepository(dbConnectType).IQueryable(expression).Count() > 0 ? true : false; } /// /// 是否存在子部门 /// /// /// public bool ExistChildrenDepartmentPartial(int id) { var expression = LinqExtensions.True(); expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No); expression = expression.And(t => t.ParentId == id); return _baseRepository.BaseRepository(dbConnectType).IQueryable(expression).Count() > 0 ? true : false; } #endregion #region 提交数据 public async Task SaveFormPartial(DepartmentEntity entity) { if (entity.Id.IsNullOrZero()) { await entity.Create(); await _baseRepository.BaseRepository(dbConnectType).Insert(entity); } else { await entity.Modify(); await _baseRepository.BaseRepository(dbConnectType).Update(entity); } } #endregion #region 私有方法 private Expression> ListFilterPartial(DepartmentListParam param) { var expression = LinqExtensions.True(); expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No); if (param != null) { if (!param.DepartmentName.IsEmpty()) { expression = expression.And(t => t.DepartmentName.Contains(param.DepartmentName)); } } return expression; } #endregion } }