public IHttpActionResult GetAllkhachHang(KhachHangSearchList khachHangSearchList)
 {
     return(Ok(new
     {
         data = _khachHangServices.GetAll(khachHangSearchList),
         page = khachHangSearchList.page,
         size = khachHangSearchList.size,
         total = _khachHangServices.count_list
     }));
 }
Example #2
0
        public List <KhachHangList> GetAll(KhachHangSearchList khachHangSearchList)
        {
            var query = from kh in _khachhangrepository.GetAll()
                        join loaikh in _loaikhachhangrepository.GetAll() on kh.MaLoaiKH equals loaikh.MaLoaiKH
                        select new
            {
                TrangThai    = kh.TrangThai,
                MaKhachHang  = kh.MaKhachHang,
                TenLoaiKH    = loaikh.TenLoaiKH,
                TenKhachHang = kh.TenKhachHang,
                SoDienThoai  = kh.SoDienThoai,
                DiaChi       = kh.DiaChi,
                Email        = kh.Email,
                TenTaiKhoan  = kh.TenTaiKhoan,
                MaLoaiKH     = kh.MaLoaiKH
            };

            query = query.Where(p => p.TrangThai == 1);
            if (!string.IsNullOrWhiteSpace(khachHangSearchList.TaiKhoan))
            {
                query = query.Where(p => p.TenTaiKhoan.ToLower().Contains(khachHangSearchList.TaiKhoan.ToLower()));
            }
            if (!string.IsNullOrWhiteSpace(khachHangSearchList.HoTen))
            {
                query = query.Where(p => p.TenKhachHang.ToLower().Contains(khachHangSearchList.HoTen.ToLower()));
            }
            if (!string.IsNullOrWhiteSpace(khachHangSearchList.DiaChi))
            {
                query = query.Where(p => p.DiaChi.ToLower().Contains(khachHangSearchList.DiaChi.ToLower()));
            }
            if (!string.IsNullOrWhiteSpace(khachHangSearchList.SoDienThoai))
            {
                query = query.Where(p => p.SoDienThoai.ToLower().Contains(khachHangSearchList.SoDienThoai.ToLower()));
            }
            if (!string.IsNullOrWhiteSpace(khachHangSearchList.LoaiKH))
            {
                query = query.Where(p => p.MaLoaiKH.ToString().Contains(khachHangSearchList.LoaiKH));
            }
            if (!string.IsNullOrWhiteSpace(khachHangSearchList.TrangThai))
            {
                query = query.Where(p => p.TrangThai.ToString() == khachHangSearchList.TrangThai);
            }
            this.count_list = query.Count();
            query           = query.OrderBy(p => p.MaKhachHang).Skip(khachHangSearchList.size.Value * khachHangSearchList.page.Value)
                              .Take(khachHangSearchList.size.Value).OrderByDescending(prop => prop.MaKhachHang);

            List <KhachHangList> listKhachHang = new List <KhachHangList>();

            foreach (var i in query)
            {
                KhachHangList kh = new KhachHangList();
                kh.MaKhachHang  = i.MaKhachHang;
                kh.TenKhachHang = i.TenKhachHang;
                kh.SoDienThoai  = i.SoDienThoai;
                kh.DiaChi       = i.DiaChi;
                kh.Email        = i.Email;
                kh.LoaiKH       = i.TenLoaiKH;
                kh.TenTaiKhoan  = i.TenTaiKhoan;
                kh.MaLoaiKH     = i.MaLoaiKH.ToString();
                kh.TrangThai    = i.TrangThai;
                listKhachHang.Add(kh);
            }
            return(listKhachHang);
        }