/// <summary> /// 修改商品属性 /// </summary> /// <param name="model"></param> private void EditDishGoodAttr(DishGood model) { List <DishGoodAttr> goodAttrList = DishGoodAttrBLL.SingleModel.GetList($"goods_id={model.id} and attr_type_id={model.goods_type}"); if (model.attr != null) { List <DishGoodAttr> tempGoodAttrList = model.attr.Where(w => w.id > 0).ToList(); //取差集,删除差集 List <DishGoodAttr> diffGoodAttr = goodAttrList?.Except(model.attr, new DishGoodAttrComparer2()).ToList(); diffGoodAttr?.ForEach(p => { if (p.id > 0) { DishGoodAttrBLL.SingleModel.Delete(p.id); } }); tempGoodAttrList?.ForEach(item => { DishGoodAttrBLL.SingleModel.Update(item, "price,value"); }); tempGoodAttrList = model.attr.Where(w => w.id == 0).ToList(); tempGoodAttrList?.ForEach(item => { item.id = Convert.ToInt32(DishGoodAttrBLL.SingleModel.Add(item)); }); } }
public ActionResult Goods(string act = "", int id = 0, int aId = 0, int storeId = 0, int pageIndex = 0, int pageSize = 20, string kw = "", int cate_id = 0, string sortData = "") { //显示 if (string.IsNullOrEmpty(act)) { ViewModel <DishGood> vm = new ViewModel <DishGood>(); var tupleResult = DishGoodBLL.SingleModel.GetListFromTable(aId, storeId, pageIndex, pageSize, kw, cate_id); vm.DataList = tupleResult.Item1; vm.TotalCount = tupleResult.Item2; vm.PageIndex = pageIndex; vm.PageSize = pageSize; vm.aId = aId; vm.storeId = storeId; ViewBag.dishCategoryList = DishCategoryBLL.SingleModel.GetList($"aid={aId} and storeid={storeId} and state=1"); return(View(vm)); } else { //删除 if (act == "del") { if (id <= 0) { _result.msg = "参数错误"; } else { DishGood updateModel = DishGoodBLL.SingleModel.GetModel(id); if (updateModel != null) { updateModel.state = -1; bool updateResult = DishGoodBLL.SingleModel.Update(updateModel); if (updateResult) { _result.code = 1; _result.msg = "删除成功"; } else { _result.msg = "删除失败"; } } else { _result.msg = "删除失败,菜品不存在"; } } } else if (act == "sort") { bool updateResult = DishGoodBLL.SingleModel.UpdateSortBatch(sortData); _result.code = updateResult ? 1 : 0; _result.msg = updateResult ? "排序成功" : "排序失败"; } } return(Json(_result, JsonRequestBehavior.AllowGet)); }
public JsonResult Delete(DishStore store, int?productId) { if (!productId.HasValue) { return(ApiModel(message: $"参数不能为空[productId]")); } DishGood product = DishGoodBLL.SingleModel.GetModel(productId.Value); if (product == null || product.storeId != store.id) { return(ApiModel(message: $"非法操作[{product?.id}]")); } bool success = DishGoodBLL.SingleModel.DeleteProduct(product); return(ApiModel(isok: success, message: success ? "删除成功" : "删除失败")); }
public JsonResult Edit(DishStore store, int?productId, [System.Web.Http.FromBody] EditProduct edit) { if (!productId.HasValue) { return(ApiModel(message: "参数不能为空[productId]")); } if (DishGoodBLL.SingleModel.GetModel(productId.Value)?.storeId != store.id) { return(ApiModel(message: "非法操作")); } //保存编辑商品 DishGood editProduct = Common.SingleModel.ConvertToDAO(edit); editProduct.id = productId.Value; bool success = DishGoodBLL.SingleModel.Update(editProduct, "img,g_name,g_description,g_danwei,g_renqi,g_print_tag,shop_price,market_price,dabao_price,day_kucun,is_order,state,cate_id,yue_xiaoliang,update_time"); if (!success) { return(ApiModel(message: "编辑失败", data: editProduct)); } //当前商品属性 List <DishGoodAttr> currentAttr = DishGoodAttrBLL.SingleModel.GetListByProduct(productId.Value); //更新商品属性 List <DishGoodAttr> updateAttr = Common.SingleModel.ConvertToDAO(edit.Attrs?.FindAll(item => item.AttrId > 0 && currentAttr.Exists(thisItem => thisItem.id == item.Id))); //删除商品属性 List <DishGoodAttr> deleteAttr = currentAttr.FindAll(curr => edit.Attrs?.Exists(editAttr => curr.id == editAttr.Id) == false); //新增商品属性 List <DishGoodAttr> newAttr = Common.SingleModel.ConvertToDAO(edit.Attrs?.FindAll(item => item.Id == 0)); newAttr.ForEach(item => item.goods_id = productId.Value); if (!DishGoodAttrBLL.SingleModel.UpdateAttr(newAttr, updateAttr, deleteAttr)) { return(ApiModel(message: "保存商品属性失败")); } return(ApiModel(isok: success, message: success ? "编辑成功" : "编辑失败")); }
public EditProduct ConvertToDTO(DishGood DAO) { return(new EditProduct { Img = DAO.img, Name = DAO.g_name, Description = DAO.g_description, Unit = DAO.g_danwei, Hit = DAO.g_renqi, PrintTagId = DAO.g_print_tag, Price = DAO.shop_price, OriginalPrice = DAO.market_price, PackingFee = DAO.dabao_price, DailySupply = DAO.day_kucun, Sort = DAO.is_order, IsTakeOut = DAO.is_waimai == 1, Display = DAO.state == 1, CategoryId = DAO.cate_id, Sale = DAO.yue_xiaoliang, }); }
public JsonResult Detail(DishStore store, int?productId) { if (!productId.HasValue) { return(ApiModel(message: "参数不能为空[productId]")); } DishGood product = DishGoodBLL.SingleModel.GetModel(productId.Value); if (product == null || product.storeId != store.id) { return(ApiModel(message: "非法操作")); } EditProduct editProduct = Common.SingleModel.ConvertToDTO(product); List <DishGoodAttr> productAttr = DishGoodAttrBLL.SingleModel.GetListByProduct(product.id); editProduct.Attrs = Common.SingleModel.ConvertToDTO(productAttr); return(ApiModel(isok: true, message: "获取成功", data: editProduct)); }
public JsonResult Add(DishStore store, [System.Web.Http.FromBody] EditProduct edit) { DishGood newProduct = Common.SingleModel.ConvertToDAO(edit); newProduct.storeId = store.id; newProduct.aId = store.aid; int newId; bool success = int.TryParse(DishGoodBLL.SingleModel.Add(newProduct)?.ToString(), out newId) && newId > 0; if (success && edit.Attrs?.Count > 0) { List <DishGoodAttr> newAttr = Common.SingleModel.ConvertToDAO(edit.Attrs); newAttr.ForEach(attr => attr.goods_id = newId); if (!DishGoodAttrBLL.SingleModel.UpdateAttr(newAttr: newAttr)) { Delete(store, newId); return(ApiModel(message: "写入商品属性失败")); } } return(ApiModel(isok: success, message: success ? "新增成功" : "新增失败")); }
public ActionResult GoodEdit(string act = "", int id = 0, int aId = 0, int storeId = 0, DishGood model = null) { //参数验证 if (id < 0 || aId <= 0 || storeId <= 0) { _result.msg = "参数错误"; return(Json(_result, JsonRequestBehavior.AllowGet)); } //显示 if (string.IsNullOrEmpty(act)) { if (id == 0) { model = new DishGood(); } else { model = DishGoodBLL.SingleModel.GetModel(id); if (model == null) { return(Content("菜品不存在")); } } EditModel <DishGood> em = new EditModel <DishGood>(); em.DataModel = model; em.aId = aId; em.storeId = storeId; ViewBag.dishCategoryList = DishCategoryBLL.SingleModel.GetList($"aid={aId} and storeid={storeId} and state=1"); ViewBag.dishAttrTypeList = DishAttrTypeBLL.SingleModel.GetList($"aid={aId} and storeid={storeId} and state=1 and enabled=1"); ViewBag.dishPrintTagList = DishTagBLL.SingleModel.GetTagByParams(aId, storeId, 1); return(View(em)); } else { if (act == "edit") { //去除重复属性,如果有重复属性只取第一个 model.attr = model.attr?.GroupBy(p => new { p.goods_id, p.attr_id, p.value }).Select(groups => groups.First()).OrderBy(p => p.attr_id).ToList(); if (id == 0) { //添加产品 int newid = Convert.ToInt32(DishGoodBLL.SingleModel.Add(model)); _result.msg = newid > 0 ? "添加成功" : "添加失败"; _result.code = newid > 0 ? 1 : 0; //添加产品属性 model.attr?.ForEach(item => { if (!string.IsNullOrEmpty(item.value)) { item.goods_id = newid; DishGoodAttrBLL.SingleModel.Add(item); } }); } else { bool updateResult = DishGoodBLL.SingleModel.Update(model); _result.msg = updateResult ? "修改成功" : "修改失败"; _result.code = updateResult ? 1 : 0; EditDishGoodAttr(model); } } return(Json(_result, JsonRequestBehavior.AllowGet)); } }