/// <summary> /// /// </summary> /// <param name="nickName"></param> /// <returns></returns> public bool CheckNickIsUsed(string nickName,int userId=0) { bool result = false; using (AnanEntities db = new AnanEntities()) { User user = null; if (userId == 0) { user = (from u in db.User where u.NickName == nickName select u).FirstOrDefault<User>(); if (user != null && user.ID > 0) { result = true; } } else { user = (from u in db.User where u.NickName == nickName && u.ID!=userId select u).FirstOrDefault<User>(); if (user != null && user.ID > 0) { result = true; } } } return result; }
public bool ChangeTradeStatus(int tradeId,int newStatus) { bool result = false; using (AnanEntities db = new AnanEntities()) { Trade trade = (from t in db.Trade where t.ID == tradeId select t).FirstOrDefault<Trade>(); if (trade == null) { throw new Exception("此订单不存在"); } if (trade.Status == 7) { throw new Exception("此订单已经取消,不能修改状态"); } if (trade.Status == 1 && (newStatus == 3 || newStatus == 4 || newStatus ==5)) { throw new Exception("此订单没有付款,不能设为正在配货,已经发货,订单完成"); } trade.Status = (short)newStatus; db.SaveChanges(); result = true; } return result; }
public bool CancelTrade(int trade_id, int user_id) { bool result = false; using (AnanEntities db = new AnanEntities()) { Trade trade=(from td in db.Trade where td.ID==trade_id select td).FirstOrDefault<Trade>(); if (trade == null) { throw new Exception("此订单不存在"); } if (trade.UserID != user_id) { throw new Exception("您不能取消别人的订单"); } if (trade.Status != 0) { throw new Exception("订单状态不是初始状态,无法取消,请联系客户取消。"); } //7 means cancel trade trade.Status = 7; db.SaveChanges(); result = true; } return result; }
public void CreateImage(Image img) { using (AnanEntities db = new AnanEntities()) { img.ProductID = 0; if(img.Path==null) { img.Path=""; } db.Image.Add(img); db.SaveChanges(); } }
/// <summary> /// /// </summary> /// <param name="email"></param> /// <returns></returns> public bool CheckEmailIsRegistered(string email) { bool result = false; using (AnanEntities db = new AnanEntities()) { User user=(from u in db.User where u.Email==email select u).FirstOrDefault<User>(); if (user != null && user.ID > 0) { result = true; } } return result; }
public CorpInfo GetCorpInfo() { CorpInfo info = null; using (AnanEntities db = new AnanEntities()) { List<CorpInfo> infos = (from ci in db.CorpInfo orderby ci.ID descending select ci).ToList<CorpInfo>(); if (infos.Count > 0) { info = infos[0]; } } return info; }
public void SetCorpInfo(CorpInfo info) { using (AnanEntities db = new AnanEntities()) { List<CorpInfo> infos = (from ci in db.CorpInfo orderby ci.ID descending select ci).ToList<CorpInfo>(); if (infos.Count > 0) { info.ID = infos[0].ID; UpdateProperties(infos[0], info); } else { db.CorpInfo.Add(info); } db.SaveChanges(); } }
public Category CreateCategory(int parentId,string name) { Category category = new Category(); if (string.IsNullOrEmpty(name)) { throw new Exception(""); } category.Name = name; category.ParentID = parentId; category.Created = anan.web.Util.DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); if(this.CurrentUser!=null) category.UserID = this.CurrentUser.ID; category.Enabled = true; using (AnanEntities db = new AnanEntities()) { db.Category.Add(category); db.SaveChanges(); } return category; }
public List<Product> SearchProducts(string keyWords, int pageIndex, int pageSize, out int total) { List<Product> products = null; total = 0; if (pageIndex == 0) { pageIndex = 1; } if (pageSize == 0) { pageSize = 15; } using (AnanEntities db = new AnanEntities()) { var p = from pdt in db.Product select pdt; if (!string.IsNullOrEmpty(keyWords)) { p=p.Where(a=>a.Title.Contains(keyWords)); } total = p.Count(); products = p.OrderByDescending(a => a.Created).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList<Product>(); } return products; }
public bool IsCategoryExist(int parentId,string name) { bool result = false; using (AnanEntities db = new AnanEntities()) { List<Category> cates = (from cate in db.Category where cate.ParentID == parentId && cate.Name == name select cate).ToList<Category>(); if (cates.Count > 0) { result = true; } } return result; }
public List<Trade_Status> GetStatus() { List<Trade_Status> status = null; using (AnanEntities db = new AnanEntities()) { status = (from s in db.Trade_Status orderby s.ID ascending select s).ToList<Trade_Status>(); } return status; }
public void DeleteImage(int imageId) { using (AnanEntities db = new AnanEntities()) { Image image = (from i in db.Image where i.ID == imageId select i).FirstOrDefault<Image>(); if (image != null) { db.Image.Remove(image); } db.SaveChanges(); } }
public bool SubmitOrder(int user_id,int address_id, List<Product> products,string desc) { bool result = false; if (products != null && address_id > 0) { using (AnanEntities db = new AnanEntities()) { Trade trade = new Trade(); trade.AddressID = address_id; trade.Amount = 0; trade.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); trade.ID = 0; trade.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); trade.PaidTime = 0; trade.Status = 1; trade.UserID = user_id; if (!string.IsNullOrEmpty(desc)) { trade.Description = desc; } db.Trade.Add(trade); db.SaveChanges(); double amount = 0; foreach (Product product in products) { Trade_Order order = new Trade_Order(); order.Amount = product.Price * product.Quantity; order.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); order.Price = (double)product.Price; order.Quantity = (int)product.Quantity; order.ProductID = product.ID; order.Status = 0; order.TradeID = trade.ID; db.Trade_Order.Add(order); amount += (double)(product.Price * product.Quantity); } trade.Amount = amount; db.SaveChanges(); result = true; } } return result; }
/// <summary> /// /// </summary> /// <param name="user"></param> /// <returns></returns> public bool UpdateUser(User user) { bool result = false; using (AnanEntities db = new AnanEntities()) { User dbUser=(from u in db.User where u.ID==user.ID select u).FirstOrDefault<User>(); base.UpdateProperties(dbUser, user); db.SaveChanges(); result = true; } return result; }
public bool UpdateProduct(Product product, int[] images) { bool result = false; using (AnanEntities db = new AnanEntities()) { Product old = (from p in db.Product where p.ID == product.ID select p).FirstOrDefault<Product>(); List<Image> imgs = null; List<int> newImg = new List<int>(); if (old != null) { product.Created = old.Created; base.UpdateProperties(old, product); db.SaveChanges(); imgs=(from i in db.Image where i.ProductID==product.ID select i).ToList<Image>(); for (int i = 0; i < images.Length; i++) { bool found = false; foreach (Image im in imgs) { if (im.ID == images[i]) { found = true; break; } } if (!found) { newImg.Add(images[i]); } } int[] ids = newImg.ToArray<int>(); List<Image> igs = (from im in db.Image where ids.Contains(im.ID) select im).ToList<Image>(); foreach (Image i in igs) { i.ProductID = product.ID; } db.SaveChanges(); result = true; } } return result; }
public Image GetImage(int image_id) { Image image = null; using (AnanEntities db = new AnanEntities()) { image=(from i in db.Image where i.ID==image_id select i).FirstOrDefault<Image>(); } return image; }
public List<Category> GetCategories() { List<Category> categories = null; AnanEntities db = new AnanEntities(); try { categories = (from category in db.Category orderby category.ID ascending orderby category.Enabled descending select category).ToList<Category>(); return categories; } catch { } finally { if (db != null) { db.Dispose(); } } return categories; }
public bool CreateAddress(int userId,int pid,int cid,int did,string addr,string person,string phone) { bool result = false; User_Address address = new User_Address(); address.ProvinceID = pid; address.CityID = cid; address.DistrictID = did; address.Address = addr; address.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); address.Person = person; address.Phone = phone; address.UserID = userId; using (AnanEntities db = new AnanEntities()) { db.User_Address.Add(address); db.SaveChanges(); result = true; } return result; }
public List<BProduct> GetBProducts(int cid, int pageIndex, int pageSize, out int total) { List<BProduct> products = null; total = 0; total = 0; if (pageIndex == 0) { pageIndex = 1; } if (pageSize == 0) { pageSize = 30; } using (AnanEntities db = new AnanEntities()) { var p = from pdt in db.Product select pdt; if (cid > 0) { int[] children = (from c in db.Category where c.ParentID == cid select c.ID).ToArray<int>(); if (children != null && children.Length > 0) { p = p.Where(a=>children.Contains(a.CategoryID)); } else { p = p.Where(a=>a.CategoryID==cid); } } total = p.Count(); var ps = from pdt in p select new BProduct { Product=pdt, Category=(from c in db.Category where pdt.CategoryID==c.ID select c).FirstOrDefault<Category>(), Created_By=(from u in db.User where u.ID==pdt.UserID select u).FirstOrDefault<User>() }; products = ps.OrderByDescending(a => a.Product.ShowFront).OrderByDescending(a => a.Product.Created).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList<BProduct>(); } return products; }
public BProduct GetBProduct(int id) { BProduct product = null; using (AnanEntities db = new AnanEntities()) { product = (from pdt in db.Product where pdt.ID == id select new BProduct { Product = pdt, Category = (from cate in db.Category where pdt.CategoryID == cate.ID select cate).FirstOrDefault<Category>(), Created_By = (from u in db.User where u.ID == pdt.UserID select u).FirstOrDefault<User>(), }).FirstOrDefault<BProduct>(); if (product != null) { product.Images = (from img in db.Image where img.ProductID == id select img).ToList<Image>(); } } return product; }
public bool DeleteImage(int image_id,string serverRoot) { bool result = false; Image image = null; using (AnanEntities db = new AnanEntities()) { image = (from i in db.Image where i.ID == image_id select i).FirstOrDefault<Image>(); if (image != null) { string fullPath = serverRoot+image.Path.Replace("/",@"\"); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); db.Image.Remove(image); db.SaveChanges(); result = true; } } } return result; }
public bool UpdateCategoryVisibility(int category_id) { bool result = false; using (AnanEntities db = new AnanEntities()) { Category cate = (from ca in db.Category where ca.ID == category_id select ca).FirstOrDefault<Category>(); if (cate != null) { if ((bool)cate.Enabled) { cate.Enabled = false; } else { cate.Enabled = true; } db.SaveChanges(); result = true; } } return result; }
public void UpdateImage(Image img) { using (AnanEntities db = new AnanEntities()) { Image image = (from i in db.Image where i.ID == img.ID select i).FirstOrDefault<Image>(); if (image != null) { image.ProductID = img.ProductID; image.Path = img.Path; } db.SaveChanges(); } }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="password"></param> /// <returns></returns> public User Login(string name, string password) { User user = null; AnanEntities db = new AnanEntities(); try { string md5Password = Encrypt.MD5(password); user = (from u in db.User where u.Email == name select u).FirstOrDefault<User>(); if (user == null) { throw new Exception("此用户不存在"); } if (user.Password != md5Password) { throw new Exception("密码错误"); } } catch(Exception ex) { throw ex; } finally { if (db != null) { db.Dispose(); } } return user; }
public Product GetProduct(int productId) { Product pdt = null; using (AnanEntities db = new AnanEntities()) { pdt=(from p in db.Product where p.ID==productId select p).FirstOrDefault<Product>(); } return pdt; }
/// <summary> /// /// </summary> /// <param name="productId"></param> /// <returns></returns> public List<Image> GetProductImages(int productId) { List<Image> images = null; using (AnanEntities db = new AnanEntities()) { images=(from img in db.Image where img.ProductID==productId select img).ToList<Image>(); } return images; }
public bool CreateProduct(Product product,int[] images) { bool result = false; using (AnanEntities db = new AnanEntities()) { db.Product.Add(product); db.SaveChanges(); if (product.ID>0 && images != null) { List<Image> imgs = (from im in db.Image where images.Contains(im.ID) select im).ToList<Image>(); foreach (Image i in imgs) { i.ProductID = product.ID; } db.SaveChanges(); result = true; } } return result; }
/// <summary> /// /// </summary> /// <param name="cid"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="total"></param> /// <returns></returns> public List<Product> GetProducts(int cid, int pageIndex, int pageSize, out int total) { List<Product> products = null; total = 0; if (pageIndex == 0) { pageIndex = 1; } if (pageSize == 0) { pageSize = 15; } using (AnanEntities db = new AnanEntities()) { var p = from pdt in db.Product select pdt; p = p.Where(a => a.ShowFront == 1); if (cid > 0) { int[] children = (from c in db.Category where c.ParentID == cid select c.ID).ToArray<int>(); if (children != null && children.Length > 0) { p=p.Where(a=>children.Contains(a.CategoryID)); } else { p=p.Where(a=>a.CategoryID==cid); } } total = p.Count(); products = p.OrderByDescending(a=>a.Created).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList<Product>(); } return products; }
public List<Product> GetNewProducts() { List<Product> products = null; using (AnanEntities db = new AnanEntities()) { var p = from pdt in db.Product where pdt.ShowFront==1 && pdt.IsNew==1 select pdt; products = p.OrderByDescending(a=>a.Created).Take(4).ToList<Product>(); } return products; }
public bool DeleteCategory(int categoryId) { bool result = false; using (AnanEntities db = new AnanEntities()) { Category c = (from cate in db.Category where cate.ID == categoryId select cate).FirstOrDefault<Category>(); if (c != null) { db.Category.Remove(c); db.SaveChanges(); result = true; } } return result; }