예제 #1
0
        /// <summary>
        /// 保存表单实体,和表单实体对象
        /// </summary>
        /// <param name="db"></param>
        public int UpdateFormInstance(BizDataContext db)
        {
            SysFormInstance fi = db.FindById <SysFormInstance>(this.FormInstanceId);

            if (fi == null)
            {
                throw new Exception("表单实例不存在");
            }

            if (fi.State != (int)FormInstanceState.Approving)
            {
                fi.State = (int)FormInstanceState.New; //2013-10-8 zhumin 重新保存后状态改为新增,审核中除外
            }
            fi.UpdateUserId = this.BasePage.LoginUserID;
            fi.UpdateTime   = DateTime.Now;

            //收集实体字段的值,创建实体对象
            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  = db.FindById(es.EntityType, this.ObjectId);
                List <string> cols = new List <string>();
                foreach (var p in valueDict)
                {
                    if (obj.GetPropertyValue(p.Key) != p.Value)
                    {
                        obj.SetPropertyConvertValue(p.Key, p.Value);
                        cols.Add(p.Key);
                    }
                }

                db.UpdatePartial(obj, cols);
            }
            else //自定义实体
            {
                entity = this.DataHelper.FindById <SysEntity>(this.EntityId);
                db.DynamicUpdate(entity, this.ObjectId, valueDict);
            }

            db.UpdatePartial(fi, p => new { p.State, p.UpdateTime, p.UpdateUserId });
            return(fi.FormInstanceId);
        }