public static CouponProductCollection LoadForCoupon(Int32 couponId, bool excludeGiftCerts) { if (excludeGiftCerts) { CouponProductCollection CouponProducts = new CouponProductCollection(); //CREATE THE DYNAMIC SQL TO LOAD OBJECT StringBuilder selectQuery = new StringBuilder(); selectQuery.Append("SELECT CP.ProductId"); selectQuery.Append(" FROM ac_CouponProducts CP INNER JOIN ac_Products P ON CP.ProductId=P.ProductId"); selectQuery.Append(" WHERE CP.CouponId = @couponId"); selectQuery.Append(" AND P.IsGiftCertificate = 0"); Database database = Token.Instance.Database; DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()); database.AddInParameter(selectCommand, "@couponId", System.Data.DbType.Int32, couponId); //EXECUTE THE COMMAND using (IDataReader dr = database.ExecuteReader(selectCommand)) { while (dr.Read()) { CouponProduct couponProduct = new CouponProduct(); couponProduct.CouponId = couponId; couponProduct.ProductId = dr.GetInt32(0); CouponProducts.Add(couponProduct); } dr.Close(); } return(CouponProducts); } else { return(LoadForCoupon(couponId)); } }
public static CouponProductCollection LoadForProduct(Int32 productId) { CouponProductCollection CouponProducts = new CouponProductCollection(); //CREATE THE DYNAMIC SQL TO LOAD OBJECT StringBuilder selectQuery = new StringBuilder(); selectQuery.Append("SELECT CouponId"); selectQuery.Append(" FROM ac_CouponProducts"); selectQuery.Append(" WHERE ProductId = @productId"); Database database = Token.Instance.Database; DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()); database.AddInParameter(selectCommand, "@productId", System.Data.DbType.Int32, productId); //EXECUTE THE COMMAND using (IDataReader dr = database.ExecuteReader(selectCommand)) { while (dr.Read()) { CouponProduct couponProduct = new CouponProduct(); couponProduct.ProductId = productId; couponProduct.CouponId = dr.GetInt32(0); CouponProducts.Add(couponProduct); } dr.Close(); } return(CouponProducts); }
public async Task <IHttpActionResult> PutProducts(int id, bool prd, List <int> pids) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var products = db.CouponProducts.Where(c => c.CouponId == id).ToList(); var oldids = products.Select(c => c.ProductId); foreach (var prdt in products) //删除产品 { if (!pids.Contains(prdt.ProductId)) { db.CouponProducts.Remove(prdt); } } foreach (var cid in pids) //新增产品 { if (!oldids.Contains(cid)) { CouponProduct cc = new CouponProduct(); cc.ProductId = cid; cc.CouponId = id; db.CouponProducts.Add(cc); } } try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CouponExists(id)) { return(NotFound()); } else { throw; } } var coupon = await db.Coupons.Include(c => c.Products).Include(c => c.Campuses).SingleOrDefaultAsync(c => c.Id == id); var data = new CouponVM() { CouponName = coupon.CouponName, Id = coupon.Id, OverDate = coupon.OverDate, Rule = coupon.Rule, StartDate = coupon.StartDate, State = coupon.State, Vlaue = coupon.Vlaue, campusIds = coupon.Campuses.Select(c => c.CampusID).ToList(), productIds = coupon.Products.Select(c => c.ProductId).ToList() }; return(Ok(data)); }
public static CouponProduct Load(Int32 couponId, Int32 productId) { CouponProduct couponProduct = new CouponProduct(); couponProduct.CouponId = couponId; couponProduct.ProductId = productId; couponProduct.IsDirty = false; return(couponProduct); }
public static bool Delete(Int32 couponId, Int32 productId) { CouponProduct couponProduct = new CouponProduct(); if (couponProduct.Load(couponId, productId)) { return(couponProduct.Delete()); } return(false); }
public async Task <IActionResult> Edit(string id, CouponViewModel viewModel) { #region SelectList var users = await _db.UserRepository.GetAsync(); ViewData["UsersId"] = new SelectList(users, "Id", "UserName"); var products = await _db.ProductRepository.GetAsync(); ViewData["ProductsId"] = new SelectList(products, "Id", "Name"); var categories = await _db.CategoryRepository.GetAsync(); ViewData["CategoriesId"] = new SelectList(categories, "Id", "Name"); #endregion if (ModelState.IsValid) { viewModel.Title = viewModel.Title.Trim(); var couponModel = await _db.CouponRepository.GetAsync(c => c.Title == viewModel.Title.Trim() && c.Id != id, string.Empty); if (couponModel == null) { var coupon = await _db.CouponRepository.GetAsync(id); if (coupon != null) { if (viewModel.EndDateLimit < viewModel.StartDateLimit) { ModelState.AddModelError("EndDateLimit", "تاریخ پایان باید بعد از تاریخ شروع باشد"); return(View(viewModel)); } if (viewModel.HasUserLimit && viewModel.Users.Count == 0) { ModelState.AddModelError("Users", "کاربران را وارد کنید"); return(View(viewModel)); } if (viewModel.HasProductOrCategoryLimit) { if (viewModel.ProductOrCategoryLimit == "product" && viewModel.Products.Count == 0) { ModelState.AddModelError("Products", "محصولات را وارد کنید"); return(View(viewModel)); } if (viewModel.ProductOrCategoryLimit == "category" && viewModel.Categories.Count == 0) { ModelState.AddModelError("Categories", "دسته بندی ها را وارد کنید"); return(View(viewModel)); } } #region Coupon coupon.Title = viewModel.Title.Trim(); coupon.Type = viewModel.Type == "percent" ? true : false; coupon.Value = viewModel.Type == "percent" ? viewModel.PercentDiscount : viewModel.ValueDiscount; coupon.HasCountLimit = viewModel.HasCountLimit; if (viewModel.HasCountLimit) { coupon.CountLimit = viewModel.CountLimit; } coupon.HasDateLimit = viewModel.HasDateLimit; if (viewModel.HasDateLimit) { coupon.StartDateLimit = viewModel.StartDateLimit; coupon.EndDateLimit = viewModel.EndDateLimit; } coupon.HasUserLimit = viewModel.HasUserLimit; if (viewModel.HasProductOrCategoryLimit) { if (viewModel.ProductOrCategoryLimit == "product") { coupon.HasProductLimit = true; coupon.HasCategoryLimit = false; } else { coupon.HasCategoryLimit = true; coupon.HasProductLimit = false; } } else { coupon.HasProductLimit = false; coupon.HasCategoryLimit = false; } #endregion _db.CouponRepository.Update(coupon); await _db.SaveAsync(); if (viewModel.HasUserLimit) { var oldUsers = await _db.CouponUserRepository.GetAsync(c => c.CouponId == coupon.Id, null, string.Empty); foreach (var oldUser in oldUsers) { _db.CouponUserRepository.Delete(oldUser); } foreach (var userId in viewModel.Users) { var couponUser = new CouponUser() { CouponId = coupon.Id, UserId = userId }; await _db.CouponUserRepository.AddAsync(couponUser); } await _db.SaveAsync(); } if (viewModel.HasProductOrCategoryLimit) { if (viewModel.ProductOrCategoryLimit == "product") { var oldProducts = await _db.CouponProductRepository.GetAsync(c => c.CouponId == coupon.Id, null, string.Empty); foreach (var oldProduct in oldProducts) { _db.CouponProductRepository.Delete(oldProduct); } foreach (var productId in viewModel.Products) { var couponProduct = new CouponProduct() { CouponId = coupon.Id, ProductId = productId }; await _db.CouponProductRepository.AddAsync(couponProduct); } await _db.SaveAsync(); } else { var oldCategories = await _db.CouponCategoryRepository.GetAsync(c => c.CouponId == coupon.Id, null, string.Empty); foreach (var oldCategory in oldCategories) { _db.CouponCategoryRepository.Delete(oldCategory); } foreach (var categoryId in viewModel.Categories) { var couponCategory = new CouponCategory() { CouponId = coupon.Id, CategoryId = categoryId }; await _db.CouponCategoryRepository.AddAsync(couponCategory); } await _db.SaveAsync(); } } return(Redirect("/Admin/Coupon")); } else { return(NotFound()); } } else { ModelState.AddModelError("Title", "کد تخفیف با این نام قبلا ثبت شده است"); return(View(viewModel)); } } else { return(View(viewModel)); } }
public async Task <IHttpActionResult> PutProduct(int id, ProductVM product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { return(BadRequest()); } var prd = await db.Products.FindAsync(id); if (product.State == false && prd.State == true) { prd.OverDate = DateTime.UtcNow; } prd.AccordIdList = product.AccordIdList; prd.CoverImg = product.CoverImg; prd.DiscountValue = product.DiscountValue; prd.Desc = product.Desc; prd.IsDiscountForOld = product.IsDiscountForOld; prd.IsPackage = product.IsPackage; prd.PackageIdList = product.PackageIdList; prd.Price = product.Price; prd.ProductName = product.ProductName; db.Entry(prd).State = EntityState.Modified; var newids = product.couponIds; var coupons = db.CouponProducts.Where(c => c.ProductId == product.Id).Where(c => c.Coupon.State).ToList(); var oldids = coupons.Select(c => c.CouponId); foreach (var coupon in coupons) //删除优惠券 { if (!newids.Contains(coupon.CouponId)) { db.CouponProducts.Remove(coupon); } } foreach (var cid in product.couponIds) //新增优惠券 { if (!oldids.Contains(cid)) { CouponProduct cp = new CouponProduct(); cp.CouponId = cid; cp.ProductId = id; db.CouponProducts.Add(cp); } } var newids2 = product.serviceIds; var services = db.ProductServices.Where(c => c.ProductId == product.Id).ToList(); var oldids2 = services.Select(c => c.ServiceId); foreach (var service in services) //删除服务 { if (!newids2.Contains(service.ServiceId)) { db.ProductServices.Remove(service); } } foreach (var sid in product.serviceIds) //新增服务 { if (!oldids2.Contains(sid)) { ProductService ps = new ProductService(); ps.ProductId = id; ps.ServiceId = sid; ps.Sort = 1; db.ProductServices.Add(ps); } } try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(Ok(product)); }
public static SaveResult Insert(CouponProduct couponProduct) { return(couponProduct.Save()); }
public static bool Delete(CouponProduct couponProduct) { return(couponProduct.Delete()); }
public static SaveResult Update(CouponProduct couponProduct) { return(couponProduct.Save()); }