public DiscountCreateDTO GetModel(int id) { using (var db = new SqlSugarClient(Connection)) { DiscountCreateDTO model = null; R_Discount data = db.Queryable <R_Discount>().InSingle(id); if (data != null) { model = new DiscountCreateDTO() { Id = data.Id, Area = data.R_Area_Id, Restaurant = data.R_Restaurant_Id, Market = data.R_Market_Id, Name = data.Name, IsEnable = data.IsEnable, StartDate = data.StartDate, EndDate = data.EndDate }; model.CyxmZkCp = db.Queryable <R_DiscountCategory>(). Where(p => p.R_Discount_Id == data.Id).ToList(); } return(model); } }
public ActionResult Edit(DiscountCreateDTO req) { Response res = new Response(); if (ModelState.IsValid) { try { if (req.Id > 0) { res.Data = _discountRepository.Update(req); } else { var currentUser = OperatorProvider.Provider.GetCurrent(); req.CompanyId = currentUser.CompanyId.ToInt(); res.Data = _discountRepository.Create(req); } } catch (Exception ex) { res.Data = false; res.Message = ex.Message; } } else { res.Data = false; res.Message = string.Join(",", ModelState .SelectMany(ms => ms.Value.Errors) .Select(e => e.ErrorMessage)); } return(Json(res, JsonRequestBehavior.AllowGet)); }
public bool Create(DiscountCreateDTO req) { using (var db = new SqlSugarClient(Connection)) { bool result = true; try { db.BeginTran(); R_Discount model = new R_Discount() { R_Restaurant_Id = req.Restaurant, R_Area_Id = req.Area, R_Market_Id = req.Market, IsEnable = req.IsEnable, Name = req.Name, StartDate = req.StartDate, EndDate = req.EndDate, R_Company_Id = req.CompanyId }; var newId = db.Insert(model); if (req.CyxmZkCp != null) { var list = req.CyxmZkCp.Where(p => p.Id == 0).ToList(); list.ForEach(p => p.R_Discount_Id = Convert.ToInt32(newId)); //插入之前先判断是否存在,只插入不存在的 foreach (var item in list) { //查询是否存在 List <R_DiscountCategory> data = db.Queryable <R_DiscountCategory>() .Where(p => p.R_Discount_Id == Convert.ToInt32(newId)) .Where(p => p.R_Category_Id == item.R_Category_Id) .ToList(); //插入不存在的 if (data.Count <= 0) { db.Insert(item); } } //db.InsertRange<R_DiscountCategory>(list); } db.CommitTran(); } catch (Exception) { db.RollbackTran(); } return(result); } }