public Response DeleteDynamicView([FromBody] TemplateApiModel model)
 {
     try
     {
         var item = _templateDetailsService.CreateQuery().SingleOrDefault(o => o.ParrentID == model.ParrentLayout && o.PartialID == model.PartialID && o.TemplateID == model.Record);
         if (item != null)
         {
             var listPro = _propertyService.GetItemByParentID(item.ID);
             if (listPro != null)
             {
                 _propertyService.CreateQuery().RemoveRange(listPro);
                 _propertyService.CreateQuery().Complete();
             }
             _templateDetailsService.CreateQuery().Remove(item);
             _templateDetailsService.CreateQuery().Complete();
         }
         _logs.WriteLogsInfo(Newtonsoft.Json.JsonConvert.SerializeObject(model));
         return(new Response(200, "Delete Success", null));
     }
     catch (Exception ex)
     {
         _logs.WriteLogsError("DeleteDynamicView", ex);
         return(new Response(500, ex.Message, null));
     }
 }
        public Response UpdateTemplate(TemplateApiModel model)
        {
            try
            {
                // kiểm tra thông tin truyền vào.
                var form = HttpContext.Request.Form;
                switch (model.Command)
                {
                case "UpdateInfoItemDynamic":
                    UpdateInfoItemDynamic(model, form);
                    break;

                case "UpdateInfoStatic":
                    #region UpdateInfoStatic
                    UpdateInfoStatic(model, form);
                    #endregion
                    break;

                case "AddNewItemDynamic":
                    AddNewItemDynamic(model);
                    break;

                default: break;
                }
                _logs.WriteLogsInfo(Newtonsoft.Json.JsonConvert.SerializeObject(model) + "\r\n" +
                                    Newtonsoft.Json.JsonConvert.SerializeObject(form) + "\r\n");
                return(new Response(200, "Success", model));
            }
            catch (Exception ex)
            {
                _logs.WriteLogsError("UpdateTemplate", Newtonsoft.Json.JsonConvert.SerializeObject(model), ex);
                return(new Response(500, ex.Message, null));
            }
        }
        private void UpdateInfoStatic(TemplateApiModel model, IFormCollection form)
        {
            var item = _templateDetailsService.CreateQuery().SelectFirst(o => o.IsDynamic == false &&
                                                                         o.IsBody == false &&
                                                                         o.PartialID == model.PartialID &&
                                                                         o.TemplateID == model.Record);

            if (item == null)
            {
                item = new SysTemplateDetailsEntity()
                {
                    IsBody      = false,
                    IsDynamic   = false,
                    CModule     = model.CModule,
                    PartialID   = model.PartialID,
                    LayoutName  = model.LayoutName,
                    TemplateID  = model.Record,
                    TypeView    = model.TypeView,
                    ParrentID   = string.Empty,
                    PartialView = model.PartialView
                };
                _templateDetailsService.Save(item);
            }
            int detailsTemplateID = item.ID;

            foreach (var key in form.Keys)
            {
                if (model.GetType().GetMember(key).Count() > 0)
                {
                    continue;
                }
                else
                {
                    var itemProperties = _propertyService.CreateQuery().SelectFirst(o => o.TemplateDetailID == detailsTemplateID && o.Name == key && o.PartialID == model.PartialID);
                    if (itemProperties == null)
                    {
                        itemProperties = new SysPropertyEntity()
                        {
                            PartialID        = item.PartialID,
                            Name             = key,
                            TemplateDetailID = detailsTemplateID
                        };
                    }
                    var data = form[key];
                    if (!string.IsNullOrEmpty(data))
                    {
                        itemProperties.Value = data;
                        _propertyService.Save(itemProperties);
                    }
                }
            }
        }
        private void UpdateInfoItemDynamic(TemplateApiModel model, IFormCollection form)
        {
            var item = _templateDetailsService.CreateQuery().SelectFirst(o => o.IsDynamic == true &&
                                                                         o.IsBody == false &&
                                                                         o.ParrentID == model.ParrentLayout &&
                                                                         o.PartialID == model.PartialID &&
                                                                         o.TemplateID == model.Record);

            if (item == null)
            {
                return;
            }
            else
            {
                if (!string.IsNullOrEmpty(model.PartialView))
                {
                    var newITem = item;
                    newITem.PartialView = model.PartialView;
                    _templateDetailsService.CreateQuery().Update(item, newITem);
                    _templateDetailsService.CreateQuery().Complete();
                }
            }
            int detailsTemplateID = item.ID;

            foreach (var key in form.Keys)
            {
                if (model.GetType().GetMember(key).Count() > 0)
                {
                    continue;
                }
                else
                {
                    var itemProperties = _propertyService.CreateQuery().SelectFirst(o => o.TemplateDetailID == detailsTemplateID && o.Name == key && o.PartialID == model.PartialID);
                    if (itemProperties == null)
                    {
                        itemProperties = new SysPropertyEntity()
                        {
                            PartialID        = item.PartialID,
                            Name             = key,
                            TemplateDetailID = detailsTemplateID
                        };
                    }
                    var data = form[key];
                    if (!string.IsNullOrEmpty(data))
                    {
                        itemProperties.Value = data;
                        _propertyService.Save(itemProperties);
                    }
                }
            }
        }
        private void AddNewItemDynamic(TemplateApiModel model)
        {
            if (model.CModule.ToLower() == "renderbody")
            {
                model.IsBody = true;
            }
            var item = new SysTemplateDetailsEntity()
            {
                CModule    = model.CModule,
                IsBody     = model.IsBody,
                LayoutName = model.LayoutName + "(" + model.Name + ")",
                TypeView   = model.TypeView,
                TemplateID = model.Record,
                IsDynamic  = true,
                ParrentID  = model.ParrentLayout,
                PartialID  = Guid.NewGuid().ToString()
            };

            _templateDetailsService.Save(item);
        }