/// <summary> /// Lấy ra danh sách tên sản phẩm theo keyword /// </summary> /// <param name="keyword"></param> /// <returns></returns> public static List <string> ListName(string keyword) { using (var db = new BANDONGHOEntities()) { return(db.SANPHAMs.Where(n => n.TENSP.Contains(keyword)).Select(n => n.TENSP).ToList()); } }
public static bool CheckNumberProduct(int id, int soluong) { BANDONGHOEntities db = new BANDONGHOEntities(); SANPHAM sp = db.SANPHAMs.Where(s => s.MASP == id).SingleOrDefault(); return(sp.SOLUONG >= soluong); }
public SANPHAM GetProductByID(int id) { using (var db = new BANDONGHOEntities()) { return(db.SANPHAMs.Find(id)); } }
// Phương thức thêm một đơn hàng public static void AddBill(CusInfoViewModel model, int idKhachHang) { // lấy ra id lớn nhất BANDONGHOEntities db = new BANDONGHOEntities(); DONHANG don = (from dh in db.DONHANGs orderby dh.MADH descending select dh).Take(1).SingleOrDefault(); int id = don == null ? 0:don.MADH; id += 1; // tạo mới đơn hàng DONHANG donhang = new DONHANG { MADH = id, MAKH = idKhachHang, DIACHIGIAO = model.DiaChiGiao, SDT = model.Sdt, MOTA = model.MoTa, TONGTIEN = model.cart.TotalMoney(), TRANGTHAI = "0", NGAYDAT = DateTime.Now, NGAYGIAO = DateTime.Now.AddDays(7) }; db.DONHANGs.Attach(donhang); db.DONHANGs.Add(donhang); db.SaveChanges(); // thêm danh sách chi tiết đơn hàng AddListDetailBill(model.cart, id); }
public static List <ProductViewModel> GetListNewProducts() { List <ProductViewModel> result = new List <ProductViewModel>(); // Lấy danh sách sản phẩm List <SANPHAM> ListNewProducts = null; using (var db = new BANDONGHOEntities()) { ListNewProducts = (from sp in db.SANPHAMs orderby sp.MASP descending select sp).ToList(); } // Lấy promotion của sản phẩm foreach (SANPHAM sp in ListNewProducts) { int Promotion = PromotionService.GetPromotion(sp.MASP); ProductViewModel productViewModel = new ProductViewModel { Product = sp, Promotion = Promotion }; result.Add(productViewModel); } return(result); }
// phương thức thêm sản phẩm public void AddProduct(int id, int soluong) { BANDONGHOEntities db = new BANDONGHOEntities(); // Lấy sản phẩm từ CSDL var sanpham = db.SANPHAMs.Find(id); if (sanpham == null) { return; } // Tìm sản phẩm trong giỏ hàng // xem đã được add vào chưa var item = (from i in Products where i.Product.MASP == sanpham.MASP select i).SingleOrDefault(); // nếu sp có trong cart rồi thì cập nhật số lượng if (item != null) { item.Quantity += soluong; } // nếu sản phẩm chưa có thì thêm mới else { // lấy khuyến mãi int Promotion = PromotionService.GetPromotion(id); Products.Add(new CartItem { Product = sanpham, Quantity = soluong, Promotion = Promotion }); } }
public int Login(string name, string pass) { BANDONGHOEntities db = new BANDONGHOEntities(); var taikhoan = db.TAIKHOANs.SingleOrDefault(x => x.TENDN == name); if (taikhoan == null) { return(0); } else { if (taikhoan.TRANGTHAI == false) { return(-2); } else { if (taikhoan.MATKHAU == pass) { if (taikhoan.MALOAITK == "LK00001") { return(1); } else { return(-1); } } else { return(-2); } } } }
public SANPHAM GetProductByID(int id) { using (var db = new BANDONGHOEntities()) { return(db.SANPHAMs.Where(n => n.MASP == id).FirstOrDefault()); } }
public void UpdateOrder(OrderViewModel orderViewModel, double?totalMoney) { using (var db = new BANDONGHOEntities()) { var result = db.DONHANGs.Where(n => n.MADH == orderViewModel.mahd).FirstOrDefault(); if (result != null) { try { result.TRANGTHAI = orderViewModel.tinhtrang; result.DIACHIGIAO = orderViewModel.diachi; result.SDT = orderViewModel.sodt; result.NGAYDAT = DateTime.Parse(orderViewModel.ngaymua); result.NGAYGIAO = DateTime.Parse(orderViewModel.ngaygiao); result.TONGTIEN = totalMoney; db.DONHANGs.AddOrUpdate(result); db.SaveChanges(); } catch (Exception) { throw; } } } }
public static SANPHAM LoadDetailProduct(int Id) { SANPHAM res = null; BANDONGHOEntities db = new BANDONGHOEntities(); res = db.SANPHAMs.Find(Id); return(res); }
/// <summary> /// category[donghonam, donghonu, null], pageIndex: số trang hiện tại, pageSize: số phần tử trong 1 trang /// </summary> /// <param name="category"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public static IEnumerable <SANPHAM> LoadProductCategory(string category, ref int totalRecord, int pageIndex = 1, int pageSize = 8) { IEnumerable <SANPHAM> ListProductCategory = null; BANDONGHOEntities db = new BANDONGHOEntities(); if (category == "tat-ca") { totalRecord = (from sp in db.SANPHAMs orderby sp.MASP descending select sp).Count(); try { ListProductCategory = (from sp in db.SANPHAMs orderby sp.MASP descending select sp).Skip((pageIndex - 1) * pageSize).Take(pageSize); } catch (Exception e) { } } if (category == "dong-ho-nam") { totalRecord = (from sp in db.SANPHAMs where sp.MALOAISP == "LP00001" orderby sp.MASP descending select sp).Count(); try { ListProductCategory = (from sp in db.SANPHAMs where sp.MALOAISP == "LP00001" orderby sp.MASP descending select sp).Skip((pageIndex - 1) * pageSize).Take(pageSize); } catch (Exception e) { } } if (category == "dong-ho-nu") { totalRecord = (from sp in db.SANPHAMs where sp.MALOAISP == "LP00002" orderby sp.MASP descending select sp).Count(); try { ListProductCategory = (from sp in db.SANPHAMs where sp.MALOAISP == "LP00002" orderby sp.MASP descending select sp).Skip((pageIndex - 1) * pageSize).Take(pageSize); } catch (Exception e) { } } return(ListProductCategory); }
public static SANPHAM Find(int id) { SANPHAM result = null; using (var db = new BANDONGHOEntities()) { result = db.SANPHAMs.Find(id); } return(result); }
public static string GetTag(int Id) { string res = ""; BANDONGHOEntities db = new BANDONGHOEntities(); // lấy ra mã thương hiệu int trademark = db.SANPHAMs.Find(Id).MATH.Value; // lấy ra tên thương hiệu res = db.THUONGHIEUx.Find(trademark).TENTH; return(res); }
public static List <SANPHAM> GetListProductsSelling() { List <SANPHAM> ListProductsSelling = null; // nghiệp vụ // * Nếu ngày lấy danh sách trước ngày 15 hàng tháng thì sẽ lấy danh sách tháng trước // * Lấy ra danh sách sản phẩm bán chạy trong tháng đó // * Nếu danh sách lấy ra là null thì trả về danh sách 3 sản phẩm mới nhất int Month = DateTime.Today.Month; int Year = DateTime.Today.Year; if (DateTime.Today.Day < 15) { if (Month - 1 == 0) { Year -= 1; Month = 12; } else { Month -= 1; } } BANDONGHOEntities db = new BANDONGHOEntities(); try { ListProductsSelling = (from sp in db.SANPHAMs let totalQuantity = (from ct in db.CHITIETDONHANGs join dh in db.DONHANGs on ct.MADH equals dh.MADH where sp.MASP == ct.MASP && dh.NGAYDAT.Value.Month == Month && dh.NGAYDAT.Value.Year == Year select ct.SOLUONG).Sum() where totalQuantity > 0 orderby totalQuantity descending select sp).Take(3).ToList(); } catch (Exception e) { } if (ListProductsSelling == null || ListProductsSelling.ToList().Count < 3) { List <SANPHAM> lstListNewProduct = new List <SANPHAM>(); foreach (ProductViewModel sp in GetListNewProducts().Take(3)) { lstListNewProduct.Add(sp.Product); } return(lstListNewProduct); } List <SANPHAM> lsp = ListProductsSelling.ToList(); return(ListProductsSelling); }
//public void GetMaKH(string MAKH) //{ // BANDONGHOEntities db = new BANDONGHOEntities(); // // Lấy mã khách hàng lớn nhất // KHACHHANG kh = (from KH in db.KHACHHANGs // orderby KH.MAKH // select KH).SingleOrDefault(); // if (kh == null) // { // MAKH = ID_DEFAULT; // } // else // { // // cắt 2 ký tự đầu // int numberID; // if (!Int32.TryParse(MAKH.Substring(2), out numberID)) // { // MAKH = ID_DEFAULT; // } // else // { // numberID++; // string newID = "KH"; // for (int i = 0; i < 5 - numberID.ToString().Length; i++) // { // newID += "0"; // } // newID += numberID.ToString(); // MAKH = newID; // } // } //} //public void GetMaTK(string MATK) //{ // BANDONGHOEntities db = new BANDONGHOEntities(); // // Lấy mã tài khoản lớn nhất // TAIKHOAN tk = (from TK in db.TAIKHOANs // orderby TK.MATK // select TK).SingleOrDefault(); // if (tk == null) // { // MATK = TK_DEFAULT; // } // else // { // // cắt 2 ký tự đầu // int numberID; // if (!Int32.TryParse(MATK.Substring(2), out numberID)) // { // MATK = TK_DEFAULT; // } // else // { // numberID++; // string newID = "TK"; // for (int i = 0; i < 5 - numberID.ToString().Length; i++) // { // newID += "0"; // } // newID += numberID.ToString(); // MATK = newID; // } // } //} public void RegisterAccount(RegisterViewModel register) { int makh, matk; BANDONGHOEntities db = new BANDONGHOEntities(); // Lấy mã tài khoản lớn nhất TAIKHOAN tk = (from TK in db.TAIKHOANs orderby TK.MATK descending select TK).FirstOrDefault(); if (tk == null) { matk = 1; } else { int numberTK = tk.MATK; numberTK++; matk = numberTK; } // Lấy mã khách hàng lớn nhất KHACHHANG kh = (from KH in db.KHACHHANGs orderby KH.MAKH descending select KH).FirstOrDefault(); if (kh == null) { makh = 1; } else { int numberKH = kh.MAKH; numberKH++; makh = numberKH; } //Tạo mới tài khoản TAIKHOAN account = new TAIKHOAN { TENDN = register.Account, MATKHAU = Encryptor.MD5Hash(register.Password), MALOAITK = "LK00002", NGAYDANGKY = DateTime.Now, TRANGTHAI = true }; db.TAIKHOANs.Add(account); db.SaveChanges(); // Tạo mới khách hàng KHACHHANG customer = new KHACHHANG { MATK = matk, TENKH = register.FirstName + register.LastName, DIACHI = register.Address, EMAIL = register.Email, SDT = register.Phone, GIOITINH = register.Sex }; db.KHACHHANGs.Add(customer); db.SaveChanges(); }
public bool isExistAccount(string account) { using (var db = new BANDONGHOEntities()) { TAIKHOAN taikhoan = (from tk in db.TAIKHOANs where tk.TENDN.Equals(account) select tk).SingleOrDefault(); if (taikhoan != null) { return(true); } return(false); } }
// Phương thức thêm chi tiết đơn hàng private static void AddListDetailBill(Cart cart, int id) { BANDONGHOEntities db = new BANDONGHOEntities(); foreach (var item in cart.GetList()) { CHITIETDONHANG ct = new CHITIETDONHANG { MADH = id, MASP = item.Product.MASP, SOLUONG = item.Quantity }; db.CHITIETDONHANGs.Add(ct); db.SaveChanges(); } }
public bool isExistAccount(string account) { // get tk => tk.TENDN == account // if tk == null return true ? falsea BANDONGHOEntities db = new BANDONGHOEntities(); TAIKHOAN taikhoan = (from tk in db.TAIKHOANs where tk.TENDN.Equals(account) select tk).SingleOrDefault(); if (taikhoan != null) { return(true); } return(false); }
public bool CheckAccount(LoginViewModel login) { string matKhauMD5 = GetMD5(login.Pass); // tìm trong CSDL using (var db = new BANDONGHOEntities()) { TAIKHOAN taikhoan = (from tk in db.TAIKHOANs where tk.TENDN.Equals(login.Id) && tk.MATKHAU == matKhauMD5 select tk).FirstOrDefault(); if (taikhoan != null) { return(true); } } return(false); }
public void InsertDetailOrder(int mahd, int masp, int soluong) { using (var db = new BANDONGHOEntities()) { CHITIETDONHANG chitietdonhang = new CHITIETDONHANG { MADH = mahd, MASP = masp, SOLUONG = soluong }; try { db.CHITIETDONHANGs.Add(chitietdonhang); db.SaveChanges(); } catch (Exception) { throw; } } }
public bool Register(RegisterViewModel register) { using (var db = new BANDONGHOEntities()) { try { TAIKHOAN tk = new TAIKHOAN { TENDN = register.Username, MATKHAU = GetMD5(register.Password), NGAYDANGKY = DateTime.Now, TRANGTHAI = true, MALOAITK = "LK00002" }; db.TAIKHOANs.Add(tk); db.SaveChanges(); return(true); } catch { return(false); } } }
public static int GetPromotion(int id) { int result = 0; using (var db = new BANDONGHOEntities()) { List <int> listValue = (from ct in db.CHITIETKMs join km in db.KHUYENMAIs on ct.MAKM equals km.MAKM where ct.MASP == id && DateTime.Compare(km.NGAYBD.Value, DateTime.Today) <= 0 && DateTime.Compare(DateTime.Today, km.NGAYKT.Value) <= 0 select ct.PHANTRAMKM.Value).ToList(); if (listValue != null && listValue.Count >= 1) { result = listValue.Max(); } } return(result); }
// Phương thức Lấy về danh sách sản phẩm rẻ nhất // @number là số lượng sản phẩm lấy về public List <SANPHAM> GetSaveProducts(int number) { List <SANPHAM> Result = new List <SANPHAM>(); // Mã Linq lấy về danh sách sản phẩm sắp xếp theo Giá IEnumerable <SANPHAM> sanpham = null; using (var db = new BANDONGHOEntities()) { sanpham = (from sp in db.SANPHAMs orderby sp.DONGIA descending select sp); // Cắt ra @number phần tử if (sanpham != null) { Result = sanpham.Take(number).ToList(); } } return(Result); }
/// <summary> /// Service load tất cả sản phẩm /// </summary> /// <returns></returns> public static List <ProductViewModel> LoadProductAll() { BANDONGHOEntities db = new BANDONGHOEntities(); List <ProductViewModel> result = new List <ProductViewModel>(); IEnumerable <SANPHAM> LoadProductAll = new List <SANPHAM>(); LoadProductAll = (from sp in db.SANPHAMs orderby sp.MASP descending select sp); // Lấy promotion của sản phẩm foreach (SANPHAM sp in LoadProductAll) { int Promotion = PromotionService.GetPromotion(sp.MASP); ProductViewModel productViewModel = new ProductViewModel { Product = sp, Promotion = Promotion }; result.Add(productViewModel); } return(result); }
// phương thức cập nhật public void UpdateProduct(int id, int soluong) { BANDONGHOEntities db = new BANDONGHOEntities(); // Tìm sản phẩm từ CSDL var sanpham = db.SANPHAMs.Find(id); if (sanpham == null) { return; } // Tìm sản phẩm xem có trong giỏ hàng chưa var item = (from i in Products where i.Product.MASP == sanpham.MASP select i).SingleOrDefault(); // nếu có thì cập nhật số lượng if (item != null) { item.Quantity = soluong; } }
// phương thức xóa sản phẩm public void RemoveProduct(int id) { BANDONGHOEntities db = new BANDONGHOEntities(); // Tìm sản phẩm từ CSDL var sanpham = db.SANPHAMs.Find(id); if (sanpham == null) { return; } // tìm xem sản phẩm đó đã có trong giỏ hàng chưa var item = (from i in Products where i.Product.MASP == sanpham.MASP select i).SingleOrDefault(); // Nếu có rồi thì remove sản phẩm đi if (item != null) { Products.Remove(item); } }
public static IEnumerable <SANPHAM> LoadListProductRelative(int Id) { SANPHAM product = LoadDetailProduct(Id); if (product == null) { List <SANPHAM> lstListNewProduct = new List <SANPHAM>(); foreach (ProductViewModel sp in ProductService.GetListNewProducts().Take(3)) { lstListNewProduct.Add(sp.Product); } return(lstListNewProduct); } // nếu ID là có IEnumerable <SANPHAM> res = null; BANDONGHOEntities db = new BANDONGHOEntities(); res = (from sp in db.SANPHAMs where sp.MATH == product.MATH select sp); if (res != null && res.ToList().Count < 3) { // lấy những sản phẩm cùng thương hiệu int amount = res.ToList().Count; List <SANPHAM> lsp = new List <SANPHAM>(); foreach (var item in res) { lsp.Add(item); } // lấy những sản phẩm mới // IEnumerable<SANPHAM> NewProducts = ; foreach (var item in ProductService.GetListNewProducts().Take(3 - amount)) { lsp.Add(item.Product); } return(lsp); } return(res); }
public void DeleteDetailOrder(OrderViewModel orderViewModel) { using (var db = new BANDONGHOEntities()) { var result = db.CHITIETDONHANGs.Where(n => n.MADH == orderViewModel.mahd); if (result != null) { try { foreach (var item in result) { db.CHITIETDONHANGs.Remove(item); } db.SaveChanges(); } catch (Exception) { throw; } } } }
public void UpdateQuantityProduct(int masp, int soluong, bool kiemtra) { using (var db = new BANDONGHOEntities()) { int soluongmoi = HandleQuantityProduct(masp, soluong, kiemtra); var result = db.SANPHAMs.Where(n => n.MASP == masp).FirstOrDefault(); if (result != null) { try { result.SOLUONG = soluongmoi; db.SANPHAMs.AddOrUpdate(result); db.SaveChanges(); } catch (Exception) { throw; } } } }
internal static List <ProductViewModel> GetListProductRelative(int idTrademark) { List <ProductViewModel> result = new List <ProductViewModel>(); // Lấy danh sách sản phẩm List <SANPHAM> ListProducts = new List <SANPHAM>(); using (var db = new BANDONGHOEntities()) { ListProducts = (from sp in db.SANPHAMs where sp.MATH == idTrademark orderby sp.MASP descending select sp).ToList(); } foreach (SANPHAM sp in ListProducts) { int Promotion = PromotionService.GetPromotion(sp.MASP); ProductViewModel productViewModel = new ProductViewModel { Product = sp, Promotion = Promotion }; result.Add(productViewModel); } return(result); }