示例#1
0
        public ActionResult AddIngredientIngredient(RecipeIngredientIngredientViewModels data)
        {
            List <string> listUpdate            = new List <string>();
            List <RecipeProductModels> listInfo = new List <RecipeProductModels>();

            data.ListItem = data.ListItem.Where(x => x.IsSelect).ToList();
            int type = 0;

            foreach (var item in data.ListItem)
            {
                RecipeIngredientModels model = new RecipeIngredientModels
                {
                    MixtureIngredientId = data.Id,
                    UOMId = item.BaseUOM,

                    IngredientId = item.IngredientId,
                    Usage        = item.Usage,
                    BaseUsage    = item.Usage
                };

                double BaseUsage = _IngredientFactory.GetUsageUOMForIngredient(model.IngredientId, model.UOMId, ref type);
                if (type != 0)
                {
                    model.BaseUsage = (BaseUsage * model.Usage);
                }

                var itemDb = _factory.CheckInsertIngredient(model);
                if (itemDb == null) //Insert
                {
                    if (item.IsSelect)
                    {
                        model.CreatedBy   = CurrentUser.UserId;
                        model.CreatedDate = DateTime.Now;
                        model.UpdatedBy   = CurrentUser.UserId;
                        model.UpdatedDate = DateTime.Now;
                        string Id = "";
                        _factory.InsertRecipeIngredient(model, ref Id);
                        listUpdate.Add(Id);
                    }
                }
                else //Update
                {
                    if (item.IsSelect)
                    {
                        model.UpdatedBy   = CurrentUser.UserId;
                        model.UpdatedDate = DateTime.Now;
                        model.Id          = itemDb.Id;
                        _factory.UpdateRecipeIngredient(model);
                        listUpdate.Add(itemDb.Id);
                    }
                    //else
                    //{
                    //    model.UpdatedBy = CurrentUser.UserId;
                    //    model.UpdatedDate = DateTime.Now;
                    //    model.Id = itemDb.Id;
                    //    _factory.DeleteRecipeIngredient(model);
                    //}
                }
            }
            //===========
            //Delete
            _factory.DeleteListIdRecipeIngredient(data.Id, listUpdate);
            //Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }