/// <summary> /// 新增表单实例,新增表单实体对象 /// </summary> public int SaveFormInstance(BizDataContext db) { SysFormInstance fi = new SysFormInstance() { FormInstanceId = db.GetNextIdentity_Int(), FormId = this.FormId, //--------------// FormDate = Convert.ToDateTime(this.lblFormDate.Text.Trim()), FormCode = this.lblFormCode.Text.Trim(), FormDescription = Server.HtmlDecode(this.lblFormDescription.Text.Trim()).Replace("<br />", "\r\n"), FormTitle = this.lblFormTitle.Text.Trim(), State = (int)FormInstanceState.New, //---------------// CreateTime = DateTime.Now, CreateUserId = this.BasePage.LoginUserID, OwnerId = this.BasePage.LoginUserID, }; #region 收集实体字段的值,创建实体对象 List <SysFormField> ffList = db.Where <SysFormField>(p => p.FormId == this.FormId); Dictionary <string, object> valueDict = new Dictionary <string, object>(); foreach (var ff in ffList) { string controlId = string.Format("ff_{0}", ff.FormFieldId); IDrisionControl control = this.BasePage.GetControlById <IDrisionControl>(controlId); if (control == null) { throw new Exception("表单控件解析出错"); } valueDict[control.FieldName] = control.GetValue(); } SysEntity entity = this.BasePage.EntityCache.FindById <SysEntity>(this.EntityId); if (entity != null) //元数据预置实体,利用EntitySchema创建对象插入 { var es = IEntitySchemaHelper.Get(this.EntityId); if (es == null) { throw new Exception("元数据实体EntitySchema找不到"); } object obj = es.CreateInstance(); int objectId = db.GetNextIdentity_Int(); obj.SetPropertyConvertValue(es.KeyName, objectId); //主键 foreach (var p in valueDict) { obj.SetPropertyConvertValue(p.Key, p.Value); } //一些默认字段 if (es.RequiredLevel() != RequireLevelEnum.PlatForm) { if (obj.GetPropertyValue(ConstFieldNames.CreateTime) == null) { obj.SetPropertyConvertValue(ConstFieldNames.CreateTime, DateTime.Now); } if (obj.GetPropertyValue(ConstFieldNames.CreateUserId) == null) { obj.SetPropertyConvertValue(ConstFieldNames.CreateUserId, this.BasePage.LoginUserID); } if (obj.GetPropertyValue(ConstFieldNames.OwnerId) == null) { obj.SetPropertyConvertValue(ConstFieldNames.OwnerId, this.BasePage.LoginUserID); } if (obj.GetPropertyValue(ConstFieldNames.State) == null) { obj.SetPropertyConvertValue(ConstFieldNames.State, 0); } if (obj.GetPropertyValue(ConstFieldNames.StateDetail) == null) { obj.SetPropertyConvertValue(ConstFieldNames.StateDetail, 0); } } db.Insert(obj); fi.ObjectId = objectId; } else //自定义实体 { entity = this.DataHelper.FindById <SysEntity>(this.EntityId); fi.ObjectId = db.DynamicInsert(entity, valueDict); } #endregion db.Insert(fi); return(fi.FormInstanceId); }