Example #1
0
        /// <summary>
        /// 根据录屏结果生成实体及映射
        /// </summary>
        /// <param name="categoryID">分类ID</param>
        /// <param name="recordCollection">录屏集合</param>
        public string RecordResultGenerate(string categoryID, RecordResultCollection recordCollection)
        {
            categoryID.CheckStringIsNullOrEmpty <ArgumentNullException>("categoryID");
            recordCollection.NullCheck <ArgumentNullException>("录屏集合不能为空!");

            string masterEntityID = string.Empty;

            #region 验证
            //只验证主表,子表可以为空
            recordCollection.Any().FalseThrow("没有找到信息项,请重新填写!");

            //验证CodeName唯一性
            recordCollection.Select(p => p.EntityName).Distinct().ForEach(p =>
                                                                          DESchemaObjectAdapter.Instance.CheckCodeNameExist(categoryID, p).TrueThrow(string.Format("已存在同名[{0}]", p)));
            #endregion

            List <string> listFullName = recordCollection.Select(p => p.TempFullPath).Distinct().ToList();

            //按长度倒序排序,确保从最子级节点开始添加实体
            listFullName = listFullName.OrderByDescending(p => p.Split('/').Length).ToList();

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                listFullName.ForEach(fullName =>
                {
                    //准备数据
                    var entityRecordList = recordCollection.Where(r => r.TempFullPath.Equals(fullName));
                    var entityResultColl = new RecordResultCollection();
                    entityResultColl.CopyFrom(entityRecordList);

                    //实体入库
                    var childEntity = entityResultColl.BuildEntity(categoryID);
                    DEObjectOperations.InstanceWithoutPermissions.DoOperation(SCObjectOperationMode.Add, childEntity, null);

                    //为父实体指定“引用实体”属性值
                    if (fullName.Contains("/"))
                    {
                        string parentFullName  = fullName.Substring(0, fullName.LastIndexOf("/"));
                        string parentFieldName = fullName.Replace(parentFullName + "/", "");
                        recordCollection.First(pe => pe.TempFullPath.Equals(parentFullName) && pe.FieldName.Equals(parentFieldName)).ReferenceEntityCodeName =
                            childEntity.CodeName;
                    }

                    //映射入库
                    EntityMapping childMapping = BuildEntityMapping(childEntity);
                    DEObjectOperations.InstanceWithoutPermissions.AddEntityMapping(childMapping);

                    //最后一个被添加的实体为主实体
                    masterEntityID = childEntity.ID;
                });

                scope.Complete();

                #region 以前方法,先注释掉
                ///////////////////////////////////////////////////////////////////////////////////////
                //#region 实体定义
                ////先入子表
                //recordCollection.Where(p => !p.IsMasterTable).GroupBy(p => p.EntityName).ToList().ForEach(p =>
                //{
                //    RecordResultCollection child = new RecordResultCollection();
                //    child.CopyFrom(p.ToList());

                //    var childEntity = child.BuildEntity(categoryID);
                //    DEObjectOperations.InstanceWithoutPermissions.DoOperation(SCObjectOperationMode.Add, childEntity, null);

                //    //添加子类引用
                //    recordCollection.Add(new Dynamics.Objects.RecordResult()
                //    {
                //        EntityName = recordCollection.FirstOrDefault(m => m.IsMasterTable).EntityName,
                //        EntityDesc = recordCollection.FirstOrDefault(m => m.IsMasterTable).EntityName,
                //        IsMasterTable = true,
                //        FieldName = p.Key,
                //        FieldType = FieldTypeEnum.Collection,
                //        ReferenceEntityCodeName = childEntity.CodeName,
                //        FieldDesc = p.Key,
                //        FieldLength = 99999,
                //        SortNo = recordCollection.Count(m => m.IsMasterTable) + 1,
                //        IsStruct = p.All(f => f.EntityDesc == "STRUCTURE")
                //    });
                //});

                //// 主表
                //RecordResultCollection master = new RecordResultCollection();
                //master.CopyFrom(recordCollection.Where(p => p.IsMasterTable).ToList());
                //var masterEntity = master.BuildEntity(categoryID);

                //DEObjectOperations.InstanceWithoutPermissions.DoOperation(SCObjectOperationMode.Add, masterEntity, null);
                //#endregion

                //// 主表映射
                //EntityMapping masterMapping = BuildEntityMapping(masterEntity);
                //DEObjectOperations.InstanceWithoutPermissions.AddEntityMapping(masterMapping);

                ////子表映射
                //masterEntity.Fields.Where(p => p.FieldType == FieldTypeEnum.Collection).ForEach(f =>
                //{
                //    DynamicEntity childEntity = DEDynamicEntityAdapter.Instance.LoadByCodeName(f.ReferenceEntityCodeName) as DynamicEntity;
                //    EntityMapping childMapping = BuildEntityMapping(childEntity);

                //    DEObjectOperations.InstanceWithoutPermissions.AddEntityMapping(childMapping);
                //});

                //masterEntityID = masterEntity.ID;
                //scope.Complete();
                #endregion
            }

            return(masterEntityID);
        }