public IQueryable <TariffViewModel> Paging(TariffCriteria criteria, int page, int size, out int rowsCount)
        {
            ICurrentUser _user       = PermissionExtention.GetUserMenuPermission(currentUser, Menu.settingTariff);
            var          rangeSearch = PermissionExtention.GetPermissionRange(currentUser.UserMenuPermission.List);

            if (rangeSearch == PermissionRange.None)
            {
                rowsCount = 0;
                return(null);
            }

            List <TariffViewModel> results = null;
            // var list = Query(criteria);
            IQueryable <TariffViewModel> list = QueryPermission(criteria, rangeSearch);

            if (list == null)
            {
                rowsCount = 0;
                return(null);
            }
            list      = list.OrderByDescending(x => x.DatetimeModified);
            rowsCount = list.ToList().Count;
            if (size > 1)
            {
                if (page < 1)
                {
                    page = 1;
                }
                results = list.Skip((page - 1) * size).Take(size).ToList();
            }
            return(results.AsQueryable());
        }
        //[Authorize]
        public IActionResult Paging(TariffCriteria criteria, int page, int size)
        {
            var data   = tariffService.Paging(criteria, page, size, out int rowCount);
            var result = new { data, totalItems = rowCount, page, size };

            return(Ok(result));
        }
        private IQueryable <TariffViewModel> QueryPermission(TariffCriteria criteria, PermissionRange range)
        {
            var list = Query(criteria);

            if (list == null)
            {
                return(null);
            }
            IQueryable <TariffViewModel> data = null;

            switch (range)
            {
            case PermissionRange.Owner:
                data = list.Where(x => x.UserCreated == currentUser.UserID).AsQueryable();
                break;

            case PermissionRange.Group:
                data = list.Where(x => x.UserCreated == currentUser.UserID ||
                                  x.setTariff.GroupId == currentUser.GroupId &&
                                  x.setTariff.DepartmentId == currentUser.DepartmentId &&
                                  x.setTariff.OfficeId == currentUser.OfficeID &&
                                  x.setTariff.CompanyId == currentUser.CompanyID).AsQueryable();
                break;

            case PermissionRange.Department:
                data = list.Where(x => x.UserCreated == currentUser.UserID || x.setTariff.DepartmentId == currentUser.DepartmentId && x.setTariff.OfficeId == currentUser.OfficeID &&
                                  x.setTariff.CompanyId == currentUser.CompanyID).AsQueryable();
                break;

            case PermissionRange.Office:
                data = list.Where(x => x.UserCreated == currentUser.UserID || x.setTariff.OfficeId == currentUser.OfficeID && x.setTariff.CompanyId == currentUser.CompanyID).AsQueryable();
                break;

            case PermissionRange.Company:
                data = list.Where(x => x.UserCreated == currentUser.UserID || x.setTariff.CompanyId == currentUser.CompanyID).AsQueryable();
                break;

            case PermissionRange.All:
                data = list.AsQueryable();
                break;

            default:
                break;
            }
            return(data);
        }
        public List <TariffViewModel> Query(TariffCriteria criteria)
        {
            var tariff  = GetAllTariff();
            var partner = catPartnerRepo.Get();

            var query = from t in tariff
                        join p in partner on t.CustomerId equals p.Id into partnerdata
                        from p in partnerdata.DefaultIfEmpty()
                        join s in partner on t.SupplierId equals s.Id into supplierdata
                        from s in supplierdata.DefaultIfEmpty()
                        select new { t, CustomerName = p != null ? p.ShortName : null, SupplierName = s != null ? s.ShortName : null };

            query = query.Where(x =>
                                ((x.t.TariffName ?? "").IndexOf(criteria.Name ?? "", StringComparison.OrdinalIgnoreCase)) >= 0 &&
                                (x.t.CustomerId ?? "").IndexOf(criteria.CustomerID ?? "", StringComparison.OrdinalIgnoreCase) >= 0 &&
                                (x.t.TariffType == criteria.TariffType || string.IsNullOrEmpty(criteria.TariffType)) &&
                                (x.t.ServiceMode == criteria.ServiceMode || string.IsNullOrEmpty(criteria.ServiceMode)) &&
                                (x.t.SupplierId == criteria.SupplierID || string.IsNullOrEmpty(criteria.SupplierID)) &&
                                (x.t.OfficeId == criteria.OfficeId || criteria.OfficeId == Guid.Empty) &&
                                (x.t.Status == criteria.Status || criteria.Status == null));
            if (criteria.DateType == "CreateDate" && criteria.ToDate.HasValue && criteria.FromDate.HasValue)
            {
                query = query.Where(x =>
                                    (x.t.DatetimeCreated.HasValue && x.t.DatetimeCreated.Value.Date >= criteria.FromDate.Value.Date && x.t.DatetimeCreated.Value.Date <= criteria.ToDate.Value.Date));
            }
            else if (criteria.DateType == "EffectiveDate" && criteria.ToDate.HasValue && criteria.FromDate.HasValue)
            {
                query = query.Where(x =>
                                    (x.t.EffectiveDate.Date >= criteria.FromDate && x.t.EffectiveDate <= criteria.ToDate));
            }
            else if (criteria.DateType == "ModifiedDate" && criteria.ToDate.HasValue && criteria.FromDate.HasValue)
            {
                query = query.Where(x =>
                                    (x.t.DatetimeModified.HasValue && x.t.DatetimeModified.Value.Date >= criteria.FromDate.Value.Date && x.t.DatetimeModified.Value.Date <= criteria.ToDate.Value.Date));
            }
            else if (criteria.DateType == "ExpiredDate")
            {
                query = query.Where(x =>
                                    (x.t.ExpiredDate.Date >= criteria.FromDate && x.t.ExpiredDate.Date <= criteria.ToDate));
            }
            else
            {
                query = query.Where(x =>
                                    (((x.t.DatetimeCreated.HasValue && x.t.DatetimeCreated.Value.Date >= criteria.FromDate) &&
                                      (x.t.DatetimeCreated.Value.Date <= criteria.ToDate)) &&
                                     ((x.t.EffectiveDate.Date >= criteria.FromDate) &&
                                      (x.t.EffectiveDate.Date <= criteria.ToDate)) &&
                                     ((x.t.DatetimeModified.HasValue && x.t.DatetimeModified.Value.Date >= criteria.FromDate) &&
                                      (x.t.DatetimeModified.Value.Date <= criteria.ToDate)) &&
                                     ((x.t.ExpiredDate.Date >= criteria.FromDate) &&
                                      (x.t.ExpiredDate.Date <= criteria.ToDate)) || criteria.FromDate == null || criteria.ToDate == null)
                                    );


                //|| ((x.t.EffectiveDate.Date >= criteria.FromDate)
                //&& (x.t.EffectiveDate.Date <= criteria.ToDate))

                //|| ((x.t.DatetimeModified.HasValue && x.t.DatetimeModified.Value.Date >= criteria.FromDate)
                //&& (x.t.DatetimeModified.Value.Date <= criteria.ToDate))

                ////|| ((x.t.DatetimeModified.HasValue ? x.t.DatetimeModified.Value.Date >= criteria.FromDate || criteria.FromDate == null : x.t.DatetimeModified >= criteria.FromDate || criteria.FromDate == null)
                ////&& (x.t.DatetimeModified.HasValue ? x.t.DatetimeModified.Value.Date <= criteria.ToDate || criteria.ToDate == null : x.t.DatetimeModified <= criteria.ToDate || criteria.ToDate == null))

                //|| ((x.t.ExpiredDate.Date >=  criteria.FromDate.Value.Date)
                //&& (x.t.ExpiredDate.Date <= criteria.ToDate.Value.Date)) || criteria.FromDate == null || criteria.ToDate == null);
            }

            if (query == null)
            {
                return(null);
            }
            query = query.ToArray().OrderByDescending(x => x.t.DatetimeModified).AsQueryable();
            List <TariffViewModel> results = new List <TariffViewModel>();

            foreach (var item in query)
            {
                var tariffView = mapper.Map <TariffViewModel>(item.t);
                tariffView.CustomerName = item.CustomerName != null?item.CustomerName.ToString() : null;

                tariffView.SupplierName = item.SupplierName != null?item.SupplierName.ToString() : null;

                results.Add(tariffView);
            }
            return(results);
        }
        public IActionResult Get(TariffCriteria criteria)
        {
            var results = tariffService.Query(criteria);

            return(Ok(results));
        }