Esempio n. 1
0
        private void ValueProcess(CategoryProperty property, ViewPropertyValue vi, List <CategoryPropertyValue> valAdd,
                                  List <CategoryPropertyValue> valUpd = null, IEnumerable <string> delIdList = null)
        {
            if (delIdList != null && delIdList.Contains(vi.Id.ToString()))
            {
                return;
            }

            if (vi.Id == Guid.Empty)
            {
                var value = new CategoryPropertyValue {
                    PropertyId = property.Id,
                    ValueName  = vi.ValueName
                };

                valAdd.Add(value);
            }
            else
            {
                var value = Resolve <ICategoryPropertyValueService>().GetSingle(vi.Id);
                if (value != null)
                {
                    value.ValueName = vi.ValueName;
                    valUpd.Add(value);
                }
            }
        }
 public ActionResult Edit([Bind(Include = "ID,CategoryPropertyId,Value,CreateDateTime,CreateUserId,UpdateDateTime,UpdateUserId")] CategoryPropertyValue categoryPropertyValue)
 {
     if (ModelState.IsValid)
     {
         db.Entry(categoryPropertyValue).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.CategoryPropertyId = new SelectList(db.CategoryProperties, "ID", "Name", categoryPropertyValue.CategoryPropertyId);
     return(View(categoryPropertyValue));
 }
        // GET: Panel/CategoryPropertyValues/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CategoryPropertyValue categoryPropertyValue = db.CategoryPropertyValues.Find(id);

            if (categoryPropertyValue == null)
            {
                return(HttpNotFound());
            }
            return(View(categoryPropertyValue));
        }
        // GET: Panel/CategoryPropertyValues/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CategoryPropertyValue categoryPropertyValue = db.CategoryPropertyValues.Find(id);

            if (categoryPropertyValue == null)
            {
                return(HttpNotFound());
            }
            ViewBag.CategoryPropertyId = new SelectList(db.CategoryProperties, "ID", "Name", categoryPropertyValue.CategoryPropertyId);
            return(View(categoryPropertyValue));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            CategoryPropertyValue categoryPropertyValue = db.CategoryPropertyValues.Find(id);

            db.CategoryPropertyValues.Remove(categoryPropertyValue);
            PropertyPropertyValues propertyPropertyValues =
                db.PropertyPropertyValueses.FirstOrDefault(
                    x => x.CategoryPropertyId == categoryPropertyValue.CategoryPropertyId &&
                    x.CategoryPropertyValueId == categoryPropertyValue.Id);

            if (propertyPropertyValues != null)
            {
                db.PropertyPropertyValueses.Remove(propertyPropertyValues);
            }
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Create([Bind(Include = "ID,CategoryPropertyId,Value,CreateDateTime,CreateUserId,UpdateDateTime,UpdateUserId")] CategoryPropertyValue categoryPropertyValue)
        {
            if (ModelState.IsValid)
            {
                //Property Create
                categoryPropertyValue.CreateDateTime = DateTime.Now;
                categoryPropertyValue.CreateUserId   = AdminLoginUserId;
                db.CategoryPropertyValues.Add(categoryPropertyValue);
                db.SaveChanges();
                //PropVal Create
                PropertyPropertyValues propertyPropertyValues = new PropertyPropertyValues();
                propertyPropertyValues.CategoryPropertyId      = categoryPropertyValue.CategoryPropertyId;
                propertyPropertyValues.CategoryPropertyValueId = categoryPropertyValue.Id;
                propertyPropertyValues.CreateDateTime          = DateTime.Now;
                propertyPropertyValues.CreateUserId            = AdminLoginUserId;
                db.PropertyPropertyValueses.Add(propertyPropertyValues);

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryPropertyId = new SelectList(db.CategoryProperties, "ID", "Name", categoryPropertyValue.CategoryPropertyId);
            return(View(categoryPropertyValue));
        }
        public ServiceResult AddOrUpdateOrDelete(CategoryProperty model, string fieldJson)
        {
            var fields = fieldJson.DeserializeJson <List <DataField> >();

            foreach (var item in fields)
            {
                if (item.SortOrder.IsNullOrEmpty() || item.FieldName.IsNullOrEmpty())
                {
                    return(ServiceResult.FailedWithMessage("属性值或者排序不能为空"));
                }
            }
            //保存属性
            var result = ServiceResult.Success;

            var find = GetByIdNoTracking(model.Id);

            if (find == null)
            {
                Add(model);
            }
            else
            {
                find.CategoryId        = model.CategoryId;
                find.Name              = model.Name;
                find.DisplayValue      = model.DisplayValue;
                find.PropertyValueJson = model.PropertyValueJson;
                find.IsSale            = model.IsSale;
                find.ControlsType      = model.ControlsType;
                find.SortOrder         = model.SortOrder;
                Update(find);
            }

            #region 属性值添加与编辑

            model = Resolve <ICategoryPropertyService>().GetSingle(model.Id, model.IsSale);
            var valueIds   = model.PropertyValues.Select(r => r.Id).ToList();
            var addList    = new List <CategoryPropertyValue>();
            var updateList = new List <CategoryPropertyValue>();

            foreach (var item in fields) //名称为空不做处理
            {
                if (!item.FieldName.IsNullOrEmpty())
                {
                    if (Guid.TryParse(item.Key, out var valueId) && valueIds.Contains(valueId))
                    {
                        //更新属性值
                        var propertyValue = model.PropertyValues.Find(r => r.Id == valueId);
                        if (propertyValue.ValueName != item.FieldName || propertyValue.SortOrder != item.SortOrder)
                        {
                            propertyValue.ValueName = item.FieldName;
                            propertyValue.SortOrder = item.SortOrder;
                            updateList.Add(propertyValue);
                        }
                    }
                    else
                    {
                        ///新增属性值
                        var propertyValue = new CategoryPropertyValue
                        {
                            ValueName  = item.FieldName,
                            SortOrder  = item.SortOrder,
                            PropertyId = model.Id
                        };
                        addList.Add(propertyValue);
                    }
                }
            }

            //修改属性值

            #endregion 属性值添加与编辑

            #region 属性值删除

            var filedKeys     = fields.Select(r => r.Key).ToList();
            var fieldKeysGuid = new List <Guid>();
            foreach (var item in filedKeys)
            {
                if (Guid.TryParse(item, out var valueId))
                {
                    fieldKeysGuid.Add(valueId);
                }
            }

            var valueDeleteIds = new List <Guid>();
            foreach (var item in model.PropertyValues)
            {
                if (!fieldKeysGuid.Contains(item.Id))
                {
                    valueDeleteIds.Add(item.Id);
                }
            }

            #endregion 属性值删除

            var context = Repository <ICategoryRepository>().RepositoryContext;
            context.BeginTransaction();
            try
            {
                if (valueDeleteIds.Count > 0)
                {
                    Resolve <ICategoryPropertyValueService>().Delete(r => valueDeleteIds.Any(e => e == r.Id));
                }

                if (updateList.Count > 0)
                {
                    foreach (var item in updateList)
                    {
                        Resolve <ICategoryPropertyValueService>().Update(item);
                    }
                }

                if (addList.Count > 0)
                {
                    Resolve <ICategoryPropertyValueService>().AddMany(addList);
                }

                context.SaveChanges();
                context.CommitTransaction();
            }
            catch (Exception ex)
            {
                context.RollbackTransaction();
                return(ServiceResult.FailedWithMessage("更新失败:" + ex.Message));
            }
            finally
            {
                context.DisposeTransaction();
            }

            return(result);
        }