Esempio n. 1
0
        /// <summary>
        /// 保存多语言的文本信息
        /// </summary>
        /// <param name="dataService">当前的数据服务</param>
        /// <param name="e">数据实体对象</param>
        /// <param name="t">业务实体对象</param>
        public void SaveLanguages(EFAuditDataService <TEntity> dataService, TEntity e, TModel t)
        {
            var me = e as IMultiLanguage;

            if (me == null)
            {
                return;
            }
            ModelRule modelRule  = ModelRule.Get <TModel>();
            string    entityType = typeof(TEntity).Name;
            var       langs      = ResHelper.GetUsedCultureNames().Select(l => l.ToLower()).ToArray();

            if (me.LangTexts == null)
            {
                me.LangTexts = dataService.GetContext().Set <Sys_DataLanguage>().Where(d => d.BillId == e.Id && d.BillType == entityType).ToList();
            }
            var sysLangs = me.LangTexts;

            foreach (var rule in modelRule.SingleRules.Where(r => r.DataType == ExtDataType.MultiLanguage))
            {
                //先处理主文本框name不带语言后缀的表单值,表示是当前语言
                string langVal     = HttpContext.Current.Request.Form[rule.Name];
                string currentLang = ResHelper.CurrentCultureName.ToLower();
                var    langEntity  = sysLangs.FirstOrDefault(l => l.Language == currentLang);
                if (langEntity == null)
                {
                    langEntity = new Sys_DataLanguage
                    {
                        BillId   = e.Id,
                        BillType = entityType,
                        Language = currentLang,
                        Name     = rule.Name,
                        Text     = langVal
                    };
                    dataService.MarkState(langEntity, EntityState.Added);
                }

                else if (langVal != langEntity.Text)
                {
                    langEntity.Text = langVal;
                    dataService.MarkState(langEntity, EntityState.Modified);
                }

                RefHelper.SetValue(e, rule.Name, langVal);

                //再处理下拉列表中的文本框name带语言后缀的表单值
                foreach (var lang in langs.Where(l => l != currentLang))
                {
                    langVal    = HttpContext.Current.Request.Form[rule.Name + "-" + lang];
                    langEntity = sysLangs.FirstOrDefault(l => l.Language == lang);
                    if (langEntity == null)
                    {
                        langEntity = new Sys_DataLanguage
                        {
                            BillId   = e.Id,
                            BillType = entityType,
                            Language = lang,
                            Name     = rule.Name,
                            Text     = langVal
                        };
                        dataService.MarkState(langEntity, EntityState.Added);
                    }
                    else if (langVal != langEntity.Text)
                    {
                        langEntity.Text = langVal;
                        dataService.MarkState(langEntity, EntityState.Modified);
                    }
                }
            }
        }
 protected virtual void MarkState(object t, EntityState state)
 {
     _innerDataProvider.MarkState(t, state);
 }