Esempio n. 1
0
        public bool Put(ProductModel model)
        {
            var curruentUser = (UserBase)_workContext.CurrentUser;
            var nowTime = DateTime.Now;

            var entity = _productService.GetProductById(model.Id);
            if(entity == null)
                return false;

            var user = curruentUser;

            entity.Name = model.Name;

            entity.Spec = model.Spec;

            entity.Price = model.Price;

            entity.Upduser = user;

            entity.Updtime = nowTime;

            entity.Unit = model.Unit;

            entity.Image = model.Image;

            entity.Detail.Detail = model.Detail.Detail;
            entity.Detail.ImgUrl1 = model.Detail.ImgUrl1;
            entity.Detail.ImgUrl2 = model.Detail.ImgUrl2;
            entity.Detail.ImgUrl3 = model.Detail.ImgUrl3;
            entity.Detail.ImgUrl4 = model.Detail.ImgUrl4;
            entity.Detail.ImgUrl5 = model.Detail.ImgUrl5;

            entity.Category = _categoryService.GetCategoryById(model.Category.Id);

            entity.Status = model.Status;

            foreach (var p in model.PropertyValues)
            {
                foreach (var pv in p.PropertyValues)
                {
                    if (pv.Id != 0)
                        entity.PropertyValues.First(pp => pp.PropertyValue.Id == pv.Id).PropertyValue.Value = pv.Value;
                    else
                    {
                        var property = entity.PropertyValues.First(pp => pp.Property.Id == p.PropertyId).Property;
                        var newPropertyValue = new ProductPropertyValueEntity()
                        {
                            Addtime = nowTime,
                            Adduser = curruentUser,
                            UpdTime = nowTime,
                            UpdUser = curruentUser,
                            Property = property,
                            PropertyValue = new PropertyValueEntity
                            {
                                Addtime = nowTime,
                                Adduser = curruentUser,
                                UpdTime = nowTime,
                                UpdUser = curruentUser,
                                Value = pv.Value,
                                Property = property
                            }
                        };
                        entity.PropertyValues.Add(newPropertyValue);
                    }
                }
            //		        pv.PropertyValue.Value = model.PropertyValues.First(c => c.PropertyId == pv.Property.Id).PropertyValues.First(v=>v.Id);
            }

            if(_productService.Update(entity) != null)
                return true;
            return false;
        }
Esempio n. 2
0
        public bool Post(ProductModel model)
        {
            var user = (UserBase)_workContext.CurrentUser;
            var entity = new ProductEntity
            {

                Name = model.Name,

                Spec = model.Spec,

                Price = model.Price,

                Adduser = user,

                Addtime = DateTime.Now,

                Upduser = user,

                Updtime = DateTime.Now,

                Unit = model.Unit,

                Image = model.Image,

                Detail = new ProductDetailEntity
                {
                    Detail = model.Detail.Detail,
                    ImgUrl1 = model.Detail.ImgUrl1,
                    ImgUrl2 = model.Detail.ImgUrl2,
                    ImgUrl3 = model.Detail.ImgUrl3,
                    ImgUrl4 = model.Detail.ImgUrl4,
                    ImgUrl5 = model.Detail.ImgUrl5,
                },

                Category = _categoryService.GetCategoryById(model.Category.Id),

                Status = EnumProductStatus.OnSale,

                //				PropertyValues = model.PropertyValues.SelectMany(pv=>new ProductPropertyValueEntity
                //				{
                //				    Addtime = DateTime.Now,
                //                    Adduser = (UserBase)_workContext.CurrentUser,
                //                    UpdTime = DateTime.Now,
                //                    UpdUser = (UserBase)_workContext.CurrentUser,
                //                    Property = _propertyService.GetPropertyById(pv.PropertyId),
                //                    PropertyValue = _propertyValueService.GetOrCreatEntityWithValue(pv.PropertyValue, _propertyService.GetPropertyById(pv.PropertyId))
                //				}).ToList()

            };
            var productProperty = (from pv in model.PropertyValues
                let p = _propertyService.GetPropertyById(pv.PropertyId)
                from v in pv.PropertyValues
                select new ProductPropertyValueEntity
                {
                    Addtime = DateTime.Now,
                    Adduser = (UserBase) _workContext.CurrentUser,
                    UpdTime = DateTime.Now,
                    UpdUser = (UserBase) _workContext.CurrentUser,
                    Property = p,
                    PropertyValue = _propertyValueService.GetOrCreatEntityWithValue(v.Value, p)
                }).ToList();
            entity.PropertyValues = productProperty;
            return _productService.Create(entity).Id > 0;
        }