コード例 #1
0
        public async Task <IActionResult> GetAll([FromQuery] DonNhapHangParams userParams)
        {
            try
            {
                var result = await _repo.GetAll(userParams);

                var resultToReturn = _mapper.Map <IEnumerable <DonNhapHangForListDto> >(result);

                Response.AddPagination(result.CurrentPage, result.PageSize, result.TotalCount, result.TotalPages);

                return(StatusCode(200, new SuccessResponseDto
                {
                    Message = "Lấy danh sách tất cả các " + _entityName + " thành công!",
                    Result = new SuccessResponseResultWithMultipleDataDto
                    {
                        Data = resultToReturn,
                        TotalItems = _repo.GetTotalItems(),
                        TotalPages = _repo.GetTotalPages(),
                        PageNumber = userParams.PageNumber,
                        PageSize = userParams.PageSize,
                        StatusStatistics = _repo.GetStatusStatistics(userParams)
                    }
                }));
            }
            catch (Exception e)
            {
                return(StatusCode(500, new FailedResponseDto
                {
                    Message = "Lấy danh sách tất cả các " + _entityName + " thất bại!",
                    Result = new FailedResponseResultDto
                    {
                        Errors = e
                    }
                }));
            }
        }
コード例 #2
0
        public async Task <PagedList <DonNhapHang> > GetAll(DonNhapHangParams userParams)
        {
            var result    = _context.DanhSachDonNhapHang.Include(x => x.ChiTietDonNhapHang).Include(x => x.NhaCungCap).Include(x => x.NhanVien).AsQueryable();
            var sortField = userParams.SortField;
            var sortOrder = userParams.SortOrder;

            var maDonNhapHang       = userParams.MaDonNhapHang;
            var maNhaCungCap        = userParams.MaNhaCungCap;
            var maNhanVien          = userParams.MaNhanVien;
            var ngayGiaoHangBatDau  = userParams.NgayGiaoHangBatDau;
            var ngayGiaoHangKetThuc = userParams.NgayGiaoHangKetThuc;
            var ngayLapBatDau       = userParams.NgayLapBatDau;
            var ngayLapKetThuc      = userParams.NgayLapKetThuc;
            var ghiChu             = userParams.GhiChu;
            var thanhTienBatDau    = userParams.ThanhTienBatDau;
            var thanhTienKetThuc   = userParams.ThanhTienKetThuc;
            var daThanhToanBatDau  = userParams.DaThanhToanBatDau;
            var daThanhToanKetThuc = userParams.DaThanhToanKetThuc;
            var tinhTrang          = userParams.TinhTrang;

            var thoiGianTaoBatDau      = userParams.ThoiGianTaoBatDau;
            var thoiGianTaoKetThuc     = userParams.ThoiGianTaoKetThuc;
            var thoiGianCapNhatBatDau  = userParams.ThoiGianCapNhatBatDau;
            var thoiGianCapNhatKetThuc = userParams.ThoiGianCapNhatKetThuc;
            var trangThai = userParams.TrangThai;
            var daXoa     = userParams.DaXoa;

            // DonNhapHang
            if (!string.IsNullOrEmpty(maDonNhapHang))
            {
                result = result.Where(x => x.MaDonNhapHang.ToLower().Contains(maDonNhapHang.ToLower()));
            }

            if (!string.IsNullOrEmpty(maNhaCungCap))
            {
                result = result.Where(x => x.MaNhaCungCap.ToLower().Contains(maNhaCungCap.ToLower()));
            }

            if (!string.IsNullOrEmpty(maNhanVien))
            {
                result = result.Where(x => x.MaNhanVien.ToLower().Contains(maNhanVien.ToLower()));
            }

            if (!string.IsNullOrEmpty(ghiChu))
            {
                result = result.Where(x => x.MaNhanVien.ToLower().Contains(ghiChu.ToLower()));
            }

            if (!string.IsNullOrEmpty(tinhTrang))
            {
                result = result.Where(x => x.TinhTrang.ToLower().Contains(tinhTrang.ToLower()));
            }

            if (ngayGiaoHangBatDau.GetHashCode() != 0 && ngayGiaoHangKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.NgayGiaoHang >= ngayGiaoHangBatDau && x.NgayGiaoHang <= ngayGiaoHangKetThuc);
            }

            if (ngayLapBatDau.GetHashCode() != 0 && ngayLapKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.NgayLap >= ngayLapBatDau && x.NgayLap <= ngayLapKetThuc);
            }

            if (thanhTienBatDau > 0 && thanhTienKetThuc > 0)
            {
                result = result.Where(x => x.ThanhTien >= thanhTienBatDau && x.ThanhTien <= thanhTienKetThuc);
            }

            if (daThanhToanBatDau > 0 && daThanhToanKetThuc > 0)
            {
                result = result.Where(x => x.DaThanhToan >= daThanhToanBatDau && x.DaThanhToan <= daThanhToanKetThuc);
            }

            // Base
            if (thoiGianTaoBatDau.GetHashCode() != 0 && thoiGianTaoKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.ThoiGianTao >= thoiGianTaoBatDau && x.ThoiGianTao <= thoiGianTaoKetThuc);
            }

            if (thoiGianCapNhatBatDau.GetHashCode() != 0 && thoiGianCapNhatKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.ThoiGianCapNhat >= thoiGianCapNhatBatDau && x.ThoiGianCapNhat <= thoiGianCapNhatKetThuc);
            }

            if (trangThai == -1 || trangThai == 1)
            {
                result = result.Where(x => x.TrangThai == trangThai);
            }

            if (daXoa == 0 || daXoa == 1)
            {
                result = result.Where(x => x.DaXoa == daXoa);
            }


            if (ngayGiaoHangBatDau.GetHashCode() != 0 && ngayGiaoHangKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.NgayGiaoHang >= ngayGiaoHangKetThuc && x.NgayGiaoHang <= ngayGiaoHangKetThuc);
            }

            if (!string.IsNullOrEmpty(sortField) && !string.IsNullOrEmpty(sortOrder))
            {
                switch (sortField)
                {
                case "MaDonNhapHang":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.MaDonNhapHang);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.MaDonNhapHang);
                    }
                    break;

                case "MaNhaCungCap":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.MaNhaCungCap);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.MaNhaCungCap);
                    }
                    break;

                case "MaNhanVien":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.MaNhanVien);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.MaNhanVien);
                    }
                    break;

                case "NgayGiaoHang":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.NgayGiaoHang);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.NgayGiaoHang);
                    }
                    break;

                case "NgayLap":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.NgayLap);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.NgayLap);
                    }
                    break;

                case "ThoiGianTao":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.ThoiGianTao);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.ThoiGianTao);
                    }
                    break;

                case "ThoiGianCapNhat":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.ThoiGianCapNhat);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.ThoiGianCapNhat);
                    }
                    break;

                case "TinhTrang":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.TinhTrang);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.TinhTrang);
                    }
                    break;

                case "TrangThai":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.TrangThai);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.TrangThai);
                    }
                    break;

                default:
                    result = result.OrderByDescending(x => x.ThoiGianTao);
                    break;
                }
            }

            _totalItems = result.Count();
            _totalPages = (int)Math.Ceiling((double)_totalItems / (double)userParams.PageSize);

            return(await PagedList <DonNhapHang> .CreateAsync(result, userParams.PageNumber, userParams.PageSize));
        }
コード例 #3
0
        public object GetStatusStatistics(DonNhapHangParams userParams)
        {
            var result                 = _context.DanhSachDonNhapHang.Include(x => x.NhaCungCap).Include(x => x.NhanVien).AsQueryable();
            var sortField              = userParams.SortField;
            var sortOrder              = userParams.SortOrder;
            var keyword                = userParams.Keyword;
            var thoiGianTaoBatDau      = userParams.ThoiGianTaoBatDau;
            var thoiGianTaoKetThuc     = userParams.ThoiGianTaoKetThuc;
            var thoiGianCapNhatBatDau  = userParams.ThoiGianCapNhatBatDau;
            var thoiGianCapNhatKetThuc = userParams.ThoiGianCapNhatKetThuc;
            var trangThai              = userParams.TrangThai;
            var daXoa = userParams.DaXoa;
            var ngayGiaoHangBatDau  = userParams.NgayGiaoHangBatDau;
            var ngayGiaoHangKetThuc = userParams.NgayGiaoHangKetThuc;

            if (!string.IsNullOrEmpty(keyword))
            {
                result = result.Where(x => x.MaNhanVien.ToLower().Contains(keyword.ToLower()) || x.MaDonNhapHang.ToString() == keyword || x.MaDonNhapHang.ToString() == keyword);
            }

            if (thoiGianTaoBatDau.GetHashCode() != 0 && thoiGianTaoKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.ThoiGianTao >= thoiGianTaoBatDau && x.ThoiGianTao <= thoiGianTaoKetThuc);
            }

            if (thoiGianCapNhatBatDau.GetHashCode() != 0 && thoiGianCapNhatKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.ThoiGianCapNhat >= thoiGianCapNhatBatDau && x.ThoiGianCapNhat <= thoiGianCapNhatKetThuc);
            }

            if (trangThai == -1 || trangThai == 1)
            {
                result = result.Where(x => x.TrangThai == trangThai);
            }

            if (daXoa == 0 || daXoa == 1)
            {
                result = result.Where(x => x.DaXoa == daXoa);
            }

            if (ngayGiaoHangBatDau.GetHashCode() != 0 && ngayGiaoHangKetThuc.GetHashCode() != 0)
            {
                result = result.Where(x => x.NgayGiaoHang >= ngayGiaoHangKetThuc && x.NgayGiaoHang <= ngayGiaoHangKetThuc);
            }

            if (!string.IsNullOrEmpty(sortField) && !string.IsNullOrEmpty(sortOrder))
            {
                switch (sortField)
                {
                case "MaDonNhapHang":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.MaDonNhapHang);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.MaDonNhapHang);
                    }
                    break;

                case "MaNhaCungCap":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.MaNhaCungCap);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.MaNhaCungCap);
                    }
                    break;

                case "MaNhanVien":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.MaNhanVien);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.MaNhanVien);
                    }
                    break;

                case "NgayGiaoHang":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.NgayGiaoHang);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.NgayGiaoHang);
                    }
                    break;

                case "ThoiGianTao":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.ThoiGianTao);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.ThoiGianTao);
                    }
                    break;

                case "ThoiGianCapNhat":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.ThoiGianCapNhat);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.ThoiGianCapNhat);
                    }
                    break;

                case "TrangThai":
                    if (string.Equals(sortOrder, "ASC", StringComparison.OrdinalIgnoreCase))
                    {
                        result = result.OrderBy(x => x.TrangThai);
                    }
                    else
                    {
                        result = result.OrderByDescending(x => x.TrangThai);
                    }
                    break;

                default:
                    result = result.OrderByDescending(x => x.ThoiGianTao);
                    break;
                }
            }


            var all      = result.Count();
            var active   = result.Count(x => x.DaXoa == 0);
            var inactive = result.Count(x => x.DaXoa == 1);

            return(new
            {
                All = all,
                Active = active,
                Inactive = inactive
            });
        }