public async Task <IActionResult> GetGeneralStatistics([FromQuery] KhachHangStatisticsParams userParams) { try { var result = await _repo.GetGeneralStatistics(userParams); return(StatusCode(200, new SuccessResponseDto { Message = "Lấy dữ liệu thống kê tổng quan về " + _entityName + " thành công!", Result = new SuccessResponseResultWithSingleDataDto { Data = result } })); } catch (Exception e) { return(StatusCode(500, new FailedResponseDto { Message = "Lấy dữ liệu thống kê tổng quan về " + _entityName + " thất bại!", Result = new FailedResponseResultDto { Errors = e } })); } }
public async Task <Object> GetGeneralStatistics(KhachHangStatisticsParams userParams) { var result = _context.DanhSachKhachHang.AsQueryable(); var totalCustomers = 0; var studentCustomers = 0; var youthCustomers = 0; var adultCustomers = 0; if (userParams != null && userParams.StartingTime.GetHashCode() != 0 && userParams.EndingTime.GetHashCode() != 0) { totalCustomers = result.Count(); studentCustomers = result.Where(x => DateTime.Now.Year - x.NgaySinh.Year < 19 && x.ThoiGianTao >= userParams.StartingTime && x.ThoiGianTao <= userParams.EndingTime).Count(); youthCustomers = result.Where(x => DateTime.Now.Year - x.NgaySinh.Year >= 19 && DateTime.Now.Year - x.NgaySinh.Year < 30 && x.ThoiGianTao >= userParams.StartingTime && x.ThoiGianTao <= userParams.EndingTime).Count(); adultCustomers = result.Where(x => DateTime.Now.Year - x.NgaySinh.Year >= 30 && x.ThoiGianTao >= userParams.StartingTime && x.ThoiGianTao <= userParams.EndingTime).Count(); } else { totalCustomers = result.Count(); studentCustomers = result.Where(x => DateTime.Now.Year - x.NgaySinh.Year < 19).Count(); youthCustomers = result.Where(x => DateTime.Now.Year - x.NgaySinh.Year >= 19 && DateTime.Now.Year - x.NgaySinh.Year < 30).Count(); adultCustomers = result.Where(x => DateTime.Now.Year - x.NgaySinh.Year >= 30).Count(); } return(new { Total = totalCustomers, Student = studentCustomers, Youth = youthCustomers, Adult = adultCustomers }); }