public async Task <int> Count(KpiItemFilter filter) { IQueryable <KpiItemDAO> KpiItems = DataContext.KpiItem.AsNoTracking(); KpiItems = DynamicFilter(KpiItems, filter); return(await KpiItems.CountAsync()); }
public async Task <List <KpiItem> > List(KpiItemFilter filter) { if (filter == null) { return(new List <KpiItem>()); } IQueryable <KpiItemDAO> KpiItemDAOs = DataContext.KpiItem.AsNoTracking(); KpiItemDAOs = DynamicFilter(KpiItemDAOs, filter); KpiItemDAOs = DynamicOrder(KpiItemDAOs, filter); List <KpiItem> KpiItems = await DynamicSelect(KpiItemDAOs, filter); return(KpiItems); }
public async Task <List <KpiItem> > List(KpiItemFilter KpiItemFilter) { try { List <KpiItem> KpiItems = await UOW.KpiItemRepository.List(KpiItemFilter); DateTime Now = StaticParams.DateTimeNow; foreach (var KpiItem in KpiItems) { if (KpiItem.KpiPeriodId < 113) { DateTime periodToDateTime = new DateTime(Convert.ToInt32(KpiItem.KpiYearId), Convert.ToInt32(KpiItem.KpiPeriodId - 100), 1).AddMonths(1).AddSeconds(-1); if (Now > periodToDateTime) { KpiItem.ReadOnly = true; } } else if (KpiItem.KpiPeriodId < 204) { var firstMonthOfQuarter = (KpiItem.KpiPeriodId - 200) * 3 - 2; DateTime periodToDateTime = new DateTime(Convert.ToInt32(KpiItem.KpiYearId), Convert.ToInt32(firstMonthOfQuarter), 1).AddMonths(3).AddSeconds(-1); if (Now > periodToDateTime) { KpiItem.ReadOnly = true; } } } return(KpiItems); } catch (Exception ex) { if (ex.InnerException == null) { await Logging.CreateSystemLog(ex, nameof(KpiItemService)); throw new MessageException(ex); } else { await Logging.CreateSystemLog(ex.InnerException, nameof(KpiItemService)); throw new MessageException(ex.InnerException); } } }
public async Task <int> CountAppUser(AppUserFilter AppUserFilter, IdFilter KpiYearId, IdFilter KpiPeriodId) { try { KpiItemFilter KpiItemFilter = new KpiItemFilter { Skip = 0, Take = int.MaxValue, KpiYearId = KpiYearId, KpiPeriodId = KpiPeriodId, Selects = KpiItemSelect.Id | KpiItemSelect.Employee }; var KpiItems = await UOW.KpiItemRepository.List(KpiItemFilter); var AppUserIds = KpiItems.Select(x => x.EmployeeId).ToList(); if (AppUserFilter.Id == null) { AppUserFilter.Id = new IdFilter(); } AppUserFilter.Id.NotIn = AppUserIds; var count = await UOW.AppUserRepository.Count(AppUserFilter); return(count); } catch (Exception ex) { await UOW.Rollback(); if (ex.InnerException == null) { await Logging.CreateSystemLog(ex, nameof(KpiItemService)); throw new MessageException(ex); } else { await Logging.CreateSystemLog(ex.InnerException, nameof(KpiItemService)); throw new MessageException(ex.InnerException); } } }
public async Task <bool> ValidateId(KpiItem KpiItem) { KpiItemFilter KpiItemFilter = new KpiItemFilter { Skip = 0, Take = 10, Id = new IdFilter { Equal = KpiItem.Id }, Selects = KpiItemSelect.Id }; int count = await UOW.KpiItemRepository.Count(KpiItemFilter); if (count == 0) { KpiItem.AddError(nameof(KpiItemValidator), nameof(KpiItem.Id), ErrorCode.IdNotExisted); } return(count == 1); }
public async Task <int> Count(KpiItemFilter KpiItemFilter) { try { int result = await UOW.KpiItemRepository.Count(KpiItemFilter); return(result); } catch (Exception ex) { if (ex.InnerException == null) { await Logging.CreateSystemLog(ex, nameof(KpiItemService)); throw new MessageException(ex); } else { await Logging.CreateSystemLog(ex.InnerException, nameof(KpiItemService)); throw new MessageException(ex.InnerException); } } }
private IQueryable <KpiItemDAO> DynamicFilter(IQueryable <KpiItemDAO> query, KpiItemFilter filter) { if (filter == null) { return(query.Where(q => false)); } query = query.Where(q => !q.DeletedAt.HasValue); if (filter.CreatedAt != null) { query = query.Where(q => q.CreatedAt, filter.CreatedAt); } if (filter.UpdatedAt != null) { query = query.Where(q => q.UpdatedAt, filter.UpdatedAt); } if (filter.Id != null) { query = query.Where(q => q.Id, filter.Id); } if (filter.OrganizationId != null) { if (filter.OrganizationId.Equal != null) { OrganizationDAO OrganizationDAO = DataContext.Organization .Where(o => o.Id == filter.OrganizationId.Equal.Value).FirstOrDefault(); query = query.Where(q => q.Organization.Path.StartsWith(OrganizationDAO.Path)); } if (filter.OrganizationId.NotEqual != null) { OrganizationDAO OrganizationDAO = DataContext.Organization .Where(o => o.Id == filter.OrganizationId.NotEqual.Value).FirstOrDefault(); query = query.Where(q => !q.Organization.Path.StartsWith(OrganizationDAO.Path)); } if (filter.OrganizationId.In != null) { List <OrganizationDAO> OrganizationDAOs = DataContext.Organization .Where(o => o.DeletedAt == null && o.StatusId == 1).ToList(); List <OrganizationDAO> Parents = OrganizationDAOs.Where(o => filter.OrganizationId.In.Contains(o.Id)).ToList(); List <OrganizationDAO> Branches = OrganizationDAOs.Where(o => Parents.Any(p => o.Path.StartsWith(p.Path))).ToList(); List <long> Ids = Branches.Select(o => o.Id).ToList(); query = query.Where(q => Ids.Contains(q.OrganizationId)); } if (filter.OrganizationId.NotIn != null) { List <OrganizationDAO> OrganizationDAOs = DataContext.Organization .Where(o => o.DeletedAt == null && o.StatusId == 1).ToList(); List <OrganizationDAO> Parents = OrganizationDAOs.Where(o => filter.OrganizationId.NotIn.Contains(o.Id)).ToList(); List <OrganizationDAO> Branches = OrganizationDAOs.Where(o => Parents.Any(p => o.Path.StartsWith(p.Path))).ToList(); List <long> Ids = Branches.Select(o => o.Id).ToList(); query = query.Where(q => !Ids.Contains(q.OrganizationId)); } } if (filter.KpiYearId != null) { query = query.Where(q => q.KpiYearId, filter.KpiYearId); } if (filter.KpiPeriodId != null) { query = query.Where(q => q.KpiPeriodId, filter.KpiPeriodId); } if (filter.StatusId != null) { query = query.Where(q => q.StatusId, filter.StatusId); } if (filter.AppUserId != null) { query = query.Where(q => q.EmployeeId, filter.AppUserId); } if (filter.CreatorId != null) { query = query.Where(q => q.CreatorId, filter.CreatorId); } query = OrFilter(query, filter); return(query); }
private async Task <List <KpiItem> > DynamicSelect(IQueryable <KpiItemDAO> query, KpiItemFilter filter) { List <KpiItem> KpiItems = await query.Select(q => new KpiItem() { Id = filter.Selects.Contains(KpiItemSelect.Id) ? q.Id : default(long), OrganizationId = filter.Selects.Contains(KpiItemSelect.Organization) ? q.OrganizationId : default(long), KpiYearId = filter.Selects.Contains(KpiItemSelect.KpiYear) ? q.KpiYearId : default(long), KpiPeriodId = filter.Selects.Contains(KpiItemSelect.KpiPeriod) ? q.KpiPeriodId : default(long), StatusId = filter.Selects.Contains(KpiItemSelect.Status) ? q.StatusId : default(long), EmployeeId = filter.Selects.Contains(KpiItemSelect.Employee) ? q.EmployeeId : default(long), CreatorId = filter.Selects.Contains(KpiItemSelect.Creator) ? q.CreatorId : default(long), Creator = filter.Selects.Contains(KpiItemSelect.Creator) && q.Creator != null ? new AppUser { Id = q.Creator.Id, Username = q.Creator.Username, DisplayName = q.Creator.DisplayName, Address = q.Creator.Address, Email = q.Creator.Email, Phone = q.Creator.Phone, Department = q.Creator.Department, OrganizationId = q.Creator.OrganizationId, StatusId = q.Creator.StatusId, Avatar = q.Creator.Avatar, SexId = q.Creator.SexId, Birthday = q.Creator.Birthday, } : null, Employee = filter.Selects.Contains(KpiItemSelect.Employee) && q.Employee != null ? new AppUser { Id = q.Employee.Id, Username = q.Employee.Username, DisplayName = q.Employee.DisplayName, Address = q.Employee.Address, Email = q.Employee.Email, Phone = q.Employee.Phone, Department = q.Employee.Department, OrganizationId = q.Employee.OrganizationId, StatusId = q.Employee.StatusId, Avatar = q.Employee.Avatar, SexId = q.Employee.SexId, Birthday = q.Employee.Birthday, } : null, KpiPeriod = filter.Selects.Contains(KpiItemSelect.KpiPeriod) && q.KpiPeriod != null ? new KpiPeriod { Id = q.KpiPeriod.Id, Code = q.KpiPeriod.Code, Name = q.KpiPeriod.Name, } : null, KpiYear = filter.Selects.Contains(KpiItemSelect.KpiYear) && q.KpiYear != null ? new KpiYear { Id = q.KpiYear.Id, Code = q.KpiYear.Code, Name = q.KpiYear.Name, } : null, Organization = filter.Selects.Contains(KpiItemSelect.Organization) && q.Organization != null ? new Organization { Id = q.Organization.Id, Code = q.Organization.Code, Name = q.Organization.Name, ParentId = q.Organization.ParentId, Path = q.Organization.Path, Level = q.Organization.Level, StatusId = q.Organization.StatusId, Phone = q.Organization.Phone, Email = q.Organization.Email, Address = q.Organization.Address, } : null, Status = filter.Selects.Contains(KpiItemSelect.Status) && q.Status != null ? new Status { Id = q.Status.Id, Code = q.Status.Code, Name = q.Status.Name, } : null, CreatedAt = q.CreatedAt, UpdatedAt = q.UpdatedAt, }).ToListAsync(); return(KpiItems); }
private IQueryable <KpiItemDAO> DynamicOrder(IQueryable <KpiItemDAO> query, KpiItemFilter filter) { switch (filter.OrderType) { case OrderType.ASC: switch (filter.OrderBy) { case KpiItemOrder.Id: query = query.OrderBy(q => q.Id); break; case KpiItemOrder.Organization: query = query.OrderBy(q => q.Organization.Name); break; case KpiItemOrder.KpiYear: query = query.OrderBy(q => q.KpiYearId); break; case KpiItemOrder.KpiPeriod: query = query.OrderBy(q => q.KpiPeriodId); break; case KpiItemOrder.Status: query = query.OrderBy(q => q.StatusId); break; case KpiItemOrder.Employee: query = query.OrderBy(q => q.Employee.DisplayName); break; case KpiItemOrder.Creator: query = query.OrderBy(q => q.Creator.DisplayName); break; } break; case OrderType.DESC: switch (filter.OrderBy) { case KpiItemOrder.Id: query = query.OrderByDescending(q => q.Id); break; case KpiItemOrder.Organization: query = query.OrderByDescending(q => q.Organization.Name); break; case KpiItemOrder.KpiYear: query = query.OrderByDescending(q => q.KpiYearId); break; case KpiItemOrder.KpiPeriod: query = query.OrderByDescending(q => q.KpiPeriodId); break; case KpiItemOrder.Status: query = query.OrderByDescending(q => q.StatusId); break; case KpiItemOrder.Employee: query = query.OrderByDescending(q => q.Employee.DisplayName); break; case KpiItemOrder.Creator: query = query.OrderByDescending(q => q.Creator.DisplayName); break; } break; } query = query.Skip(filter.Skip).Take(filter.Take); return(query); }
private IQueryable <KpiItemDAO> OrFilter(IQueryable <KpiItemDAO> query, KpiItemFilter filter) { if (filter.OrFilter == null || filter.OrFilter.Count == 0) { return(query); } IQueryable <KpiItemDAO> initQuery = query.Where(q => false); foreach (KpiItemFilter KpiItemFilter in filter.OrFilter) { IQueryable <KpiItemDAO> queryable = query; if (KpiItemFilter.OrganizationId != null) { if (KpiItemFilter.OrganizationId.Equal != null) { OrganizationDAO OrganizationDAO = DataContext.Organization .Where(o => o.Id == KpiItemFilter.OrganizationId.Equal.Value).FirstOrDefault(); queryable = queryable.Where(q => q.Organization.Path.StartsWith(OrganizationDAO.Path)); } if (KpiItemFilter.OrganizationId.NotEqual != null) { OrganizationDAO OrganizationDAO = DataContext.Organization .Where(o => o.Id == KpiItemFilter.OrganizationId.NotEqual.Value).FirstOrDefault(); queryable = queryable.Where(q => !q.Organization.Path.StartsWith(OrganizationDAO.Path)); } if (KpiItemFilter.OrganizationId.In != null) { List <OrganizationDAO> OrganizationDAOs = DataContext.Organization .Where(o => o.DeletedAt == null && o.StatusId == 1).ToList(); List <OrganizationDAO> Parents = OrganizationDAOs.Where(o => KpiItemFilter.OrganizationId.In.Contains(o.Id)).ToList(); List <OrganizationDAO> Branches = OrganizationDAOs.Where(o => Parents.Any(p => o.Path.StartsWith(p.Path))).ToList(); List <long> Ids = Branches.Select(o => o.Id).ToList(); queryable = queryable.Where(q => Ids.Contains(q.OrganizationId)); } if (KpiItemFilter.OrganizationId.NotIn != null) { List <OrganizationDAO> OrganizationDAOs = DataContext.Organization .Where(o => o.DeletedAt == null && o.StatusId == 1).ToList(); List <OrganizationDAO> Parents = OrganizationDAOs.Where(o => KpiItemFilter.OrganizationId.NotIn.Contains(o.Id)).ToList(); List <OrganizationDAO> Branches = OrganizationDAOs.Where(o => Parents.Any(p => o.Path.StartsWith(p.Path))).ToList(); List <long> Ids = Branches.Select(o => o.Id).ToList(); queryable = queryable.Where(q => !Ids.Contains(q.OrganizationId)); } } if (KpiItemFilter.AppUserId != null) { queryable = queryable.Where(q => q.EmployeeId, KpiItemFilter.AppUserId); } if (KpiItemFilter.UserId != null) { if (KpiItemFilter.UserId.Equal != null) { AppUserDAO AppUserDAO = DataContext.AppUser.Where(p => p.Id == KpiItemFilter.UserId.Equal).FirstOrDefault(); OrganizationDAO OrganizationDAO = DataContext.Organization .Where(o => o.Id == AppUserDAO.OrganizationId).FirstOrDefault(); queryable = queryable.Where(q => q.Employee.Organization.Path.StartsWith(OrganizationDAO.Path)); } if (KpiItemFilter.UserId.NotEqual != null) { AppUserDAO AppUserDAO = DataContext.AppUser.Where(p => p.Id == KpiItemFilter.UserId.NotEqual).FirstOrDefault(); OrganizationDAO OrganizationDAO = DataContext.Organization .Where(o => o.Id != AppUserDAO.OrganizationId).FirstOrDefault(); queryable = queryable.Where(q => !q.Employee.Organization.Path.StartsWith(OrganizationDAO.Path)); } } //if (KpiItemFilter.KpiYearId != null) // queryable = queryable.Where(q => q.KpiYearId, KpiItemFilter.KpiYearId); //if (KpiItemFilter.KpiPeriodId != null) // queryable = queryable.Where(q => q.KpiPeriodId, KpiItemFilter.KpiPeriodId); //if (KpiItemFilter.StatusId != null) // queryable = queryable.Where(q => q.StatusId, KpiItemFilter.StatusId); //if (KpiItemFilter.CreatorId != null) // queryable = queryable.Where(q => q.CreatorId, KpiItemFilter.CreatorId); initQuery = initQuery.Union(queryable); } return(initQuery); }
public async Task <KpiItemFilter> ToFilter(KpiItemFilter filter) { if (filter.OrFilter == null) { filter.OrFilter = new List <KpiItemFilter>(); } if (CurrentContext.Filters == null || CurrentContext.Filters.Count == 0) { return(filter); } List <Organization> Organizations = await OrganizationService.List(new OrganizationFilter { Skip = 0, Take = int.MaxValue, Selects = OrganizationSelect.ALL, OrderBy = OrganizationOrder.Id, OrderType = OrderType.ASC }); foreach (var currentFilter in CurrentContext.Filters) { KpiItemFilter subFilter = new KpiItemFilter(); filter.OrFilter.Add(subFilter); List <FilterPermissionDefinition> FilterPermissionDefinitions = currentFilter.Value; foreach (FilterPermissionDefinition FilterPermissionDefinition in FilterPermissionDefinitions) { if (FilterPermissionDefinition.Name == nameof(subFilter.OrganizationId)) { var organizationIds = FilterOrganization(Organizations, FilterPermissionDefinition.IdFilter); IdFilter IdFilter = new IdFilter { In = organizationIds }; subFilter.OrganizationId = FilterBuilder.Merge(subFilter.OrganizationId, IdFilter); } if (FilterPermissionDefinition.Name == nameof(subFilter.AppUserId)) { subFilter.AppUserId = FilterBuilder.Merge(subFilter.AppUserId, FilterPermissionDefinition.IdFilter); } if (FilterPermissionDefinition.Name == nameof(CurrentContext.UserId) && FilterPermissionDefinition.IdFilter != null) { if (FilterPermissionDefinition.IdFilter.Equal.HasValue && FilterPermissionDefinition.IdFilter.Equal.Value == CurrentUserEnum.IS.Id) { if (subFilter.UserId == null) { subFilter.UserId = new IdFilter { } } ; subFilter.UserId.Equal = CurrentContext.UserId; } if (FilterPermissionDefinition.IdFilter.Equal.HasValue && FilterPermissionDefinition.IdFilter.Equal.Value == CurrentUserEnum.ISNT.Id) { if (subFilter.UserId == null) { subFilter.UserId = new IdFilter { } } ; subFilter.UserId.NotEqual = CurrentContext.UserId; } } } } return(filter); } } }