コード例 #1
0
ファイル: ViewAgent.cs プロジェクト: haositongxue/avia
        /// <summary>
        /// 保存系统模板
        /// </summary>
        /// <param name="template"></param>
        /// <param name="models"></param>
        /// <returns></returns>
        public bool SaveTemplateInfo(ViewTemplate template, int[] models)
        {
            if (string.IsNullOrEmpty(template.Preview))
            {
                return(this.FaildMessage("请上传预览图"));
            }
            if (string.IsNullOrEmpty(template.Name))
            {
                return(this.FaildMessage("请输入模板名称"));
            }

            bool isNew = template.ID == 0;

            using (DbExecutor db = NewExecutor(IsolationLevel.ReadUncommitted))
            {
                if (template.ID == 0)
                {
                    template.AddIdentity(db);
                }
                else
                {
                    template.Update(db, t => t.Name, t => t.Preview);
                }

                //# 得到当前平台下所有的视图
                List <ViewSetting> views     = db.ReadList <ViewSetting>(t => t.Platform == template.Platform);
                List <ViewModel>   modelList = new List <ViewModel>();
                foreach (int modelId in models)
                {
                    ViewModel model = db.ReadInfo <ViewModel>(t => t.ID == modelId);
                    if (model == null)
                    {
                        db.Rollback();
                        return(this.FaildMessage($"模型ID{modelId}不存在"));
                    }
                    ViewTemplateConfig config = isNew ? null :
                                                config = db.ReadInfo <ViewTemplateConfig>(t => t.TemplateID == template.ID && t.ViewID == model.ViewID);
                    if (config != null && config.ModelID != modelId)
                    {
                        config.ModelID = modelId;
                        config.Update(db, t => t.ModelID);
                    }
                    else if (config == null)
                    {
                        config = new ViewTemplateConfig()
                        {
                            TemplateID = template.ID,
                            ViewID     = model.ViewID,
                            ModelID    = model.ID
                        };
                        config.Add(db);
                    }
                    modelList.Add(model);
                }

                ViewSetting view = views.FirstOrDefault(t => !modelList.Any(p => p.ViewID == t.ID));
                if (view != null)
                {
                    db.Rollback();
                    return(this.FaildMessage($"视图{view.Name}未选则模型"));
                }

                db.Commit();
            }

            return(this.AccountInfo.Log(LogType.View, $"保存系统模板 {template.Platform}/{template.Name}"));
        }