public IActionResult EditEntityMap(Guid entitymapid) { var headEntityMap = _entityMapFinder.FindById(entitymapid); EditEntityMapModel model = new EditEntityMapModel { EntityMapId = entitymapid, EntityId = headEntityMap.TargetEntityId, TargetEntityMetaData = _entityFinder.FindById(headEntityMap.TargetEntityId), SourceEntityMetaData = _entityFinder.FindById(headEntityMap.SourceEntityId), Attributes = _attributeFinder.Query(n => n .Where(f => f.EntityId == headEntityMap.TargetEntityId && f.Name.NotIn(AttributeDefaults.SystemAttributes)).Sort(s => s.SortAscending(f => f.Name))), SolutionId = SolutionId.Value, //单据头 HeadEntityMap = headEntityMap, RelationShipName = headEntityMap.RelationShipName, HeadAttributeMap = _attributeMapFinder.Query(n => n.Where(f => f.EntityMapId == headEntityMap.EntityMapId)), SourceEntityId = headEntityMap.SourceEntityId, MapType = headEntityMap.MapType }; model.HeadRelationShip = _relationShipFinder.FindByName(model.RelationShipName); //单据体 var childEntityMap = _entityMapFinder.FindByParentId(headEntityMap.EntityMapId); if (childEntityMap != null) { model.ChildEntityMap = childEntityMap; model.ChildRelationShipName = childEntityMap.RelationShipName; model.ChildTargetEntityMetaData = _entityFinder.FindById(childEntityMap.TargetEntityId); model.ChildSourceEntityMetaData = _entityFinder.FindById(childEntityMap.SourceEntityId); model.ChildAttributeMap = _attributeMapFinder.Query(n => n.Where(f => f.EntityMapId == childEntityMap.EntityMapId)); model.ChildTargetEntityId = childEntityMap.TargetEntityId; model.ChildSourceEntityId = childEntityMap.SourceEntityId; } return(View(model)); }
private Guid CreateFromMap_Copy(EntityMap entityMap, Guid sourceRecordId) { var headTargetEntityMeta = _entityFinder.FindById(entityMap.TargetEntityId); var headTargetAttributes = _attributeFinder.FindByEntityId(entityMap.TargetEntityId); //引用源实体的字段 var headRelationShipMeta = _relationShipFinder.FindByName(entityMap.RelationShipName); var refAttr = headTargetAttributes.Find(n => n.AttributeId == headRelationShipMeta.ReferencingAttributeId); if (entityMap.MapType == MapType.CopyOne) { //查询是否已生成记录 QueryExpression query_target = new QueryExpression(headTargetEntityMeta.Name, _languageId); query_target.ColumnSet.AddColumn(headTargetAttributes.Find(n => n.TypeIsPrimaryKey()).Name); query_target.Criteria.AddCondition(refAttr.Name, ConditionOperator.Equal, sourceRecordId); var existsRecord = _dataFinder.Retrieve(query_target); if (existsRecord.NotEmpty()) { OnException(_loc["entitymap_copyone_error"]); return(Guid.Empty); } } Guid newId = Guid.Empty; //源记录 var sourceRecord = _dataFinder.RetrieveById(entityMap.SourceEnttiyName, sourceRecordId); if (sourceRecord.IsEmpty()) { OnException(_loc["notfound_record"]); return(Guid.Empty); } //单据头 var attributeMaps = _attributeMapFinder.Query(n => n.Where(f => f.EntityMapId == entityMap.EntityMapId)); if (attributeMaps.IsEmpty()) { OnException(_loc["entitymap_emptyheadattributemap"]); return(Guid.Empty); } //单据体 var childEntityMap = _entityMapFinder.FindByParentId(entityMap.EntityMapId); //单据头字段元数据 var headSourceAttributes = _attributeFinder.FindByEntityId(entityMap.SourceEntityId); //新增单据头信息 Entity headEntity = new Entity(entityMap.TargetEnttiyName); foreach (var attrMap in attributeMaps) { if (!headSourceAttributes.Exists(n => n.AttributeId == attrMap.SourceAttributeId) || !headTargetAttributes.Exists(n => n.AttributeId == attrMap.TargetAttributeId)) { continue; } var attr = headTargetAttributes.Find(n => n.AttributeId == attrMap.TargetAttributeId); if (attr == null) { continue; } var value = sourceRecord[attrMap.SourceAttributeName]; if (value == null && attrMap.DefaultValue.IsNotEmpty()) { value = attrMap.DefaultValue; } headEntity.SetAttributeValue(attr.Name, sourceRecord.WrapAttributeValue(_entityFinder, attr, value)); } //关联来源单据ID headEntity.SetAttributeValue(refAttr.Name, sourceRecord.WrapAttributeValue(_entityFinder, refAttr, sourceRecord.GetIdValue())); try { _organizationDataProvider.BeginTransaction(); InternalOnMap(sourceRecord, headEntity, OperationStage.PreOperation, headTargetEntityMeta, headTargetAttributes); newId = _dataCreater.Create(headEntity); //新增单据体信息 if (childEntityMap != null) { var childAttributeMaps = _attributeMapFinder.Query(n => n.Where(f => f.EntityMapId == childEntityMap.EntityMapId)); if (childAttributeMaps.NotEmpty()) { var childTargetEntityMeta = _entityFinder.FindById(childEntityMap.TargetEntityId); var childTargetAttributesMeta = _attributeFinder.FindByEntityId(childEntityMap.TargetEntityId); var childSourceAttributesMeta = _attributeFinder.FindByEntityId(childEntityMap.SourceEntityId); var childRelationShips = childEntityMap.RelationShipName.SplitSafe(","); //源单据体与源单据头的关系 var childSourceRelationShipMeta = _relationShipFinder.FindByName(childRelationShips[0]); //目标单据体与目标单据头的关系 var childTargetRelationShipMeta = _relationShipFinder.FindByName(childRelationShips[1]); //源单据体数据 QueryExpression query_source = new QueryExpression(childEntityMap.SourceEnttiyName, _languageId); query_source.ColumnSet.AllColumns = true; var refKey = headSourceAttributes.Find(n => n.AttributeId == childSourceRelationShipMeta.ReferencedAttributeId).Name; query_source.Criteria.AddCondition(refKey, ConditionOperator.Equal, sourceRecordId); var childSourceRecords = _dataFinder.RetrieveAll(query_source); if (childSourceRecords.NotEmpty()) { //引用单据头的字段 var headRefAttr = childTargetAttributesMeta.Find(n => n.AttributeId == childTargetRelationShipMeta.ReferencingAttributeId); //引用源单据体的字段 var refSourceAttr = childTargetAttributesMeta.Find(n => n.ReferencedEntityId.HasValue && n.ReferencedEntityId.Value == childEntityMap.SourceEntityId); foreach (var item in childSourceRecords) { //目标单据体 Entity childTargetRecord = new Entity(childEntityMap.TargetEnttiyName); foreach (var attrMap in childAttributeMaps) { if (!childSourceAttributesMeta.Exists(n => n.AttributeId == attrMap.SourceAttributeId) || !childTargetAttributesMeta.Exists(n => n.AttributeId == attrMap.TargetAttributeId)) { continue; } var attr = childTargetAttributesMeta.Find(n => n.AttributeId == attrMap.TargetAttributeId); if (attr == null) { continue; } var value = item[attrMap.SourceAttributeName]; if (value == null && attrMap.DefaultValue.IsNotEmpty()) { value = attrMap.DefaultValue; } childTargetRecord.SetAttributeValue(attrMap.TargetAttributeName, sourceRecord.WrapAttributeValue(_entityFinder, attr, value)); } //关联来源单据体记录ID if (refSourceAttr != null) { childTargetRecord.SetAttributeValue(refSourceAttr.Name, sourceRecord.WrapAttributeValue(_entityFinder, refSourceAttr, item.GetIdValue())); } //单据头ID childTargetRecord.SetAttributeValue(headRefAttr.Name, sourceRecord.WrapAttributeValue(_entityFinder, headRefAttr, newId)); _dataCreater.Create(childTargetRecord); } } } } _organizationDataProvider.CommitTransaction(); InternalOnMap(sourceRecord, headEntity, OperationStage.PostOperation, headTargetEntityMeta, headTargetAttributes); } catch (Exception e) { _organizationDataProvider.RollBackTransaction(); newId = Guid.Empty; OnException(e); } return(newId); }