Set<RoleType> interalOrgSet = new HashSet<RoleType>( );
CollectionUtils.addAll(interalOrgSet, RoleType.internalOrg());
if( isInsert ){ //新增数据
PartyRole fromRole = null ; //下级隶属关系
List<PartyRole> partyRoles = new ArrayList<PartyRole>( newRoleTypes.size() );
for( RoleType item:newRoleTypes ){
PartyRole newRole = new PartyRole(party,item);
partyRoles.add( newRole );
if( interalOrgSet.contains( item ) )
fromRole = newRole;
}
//partyRoleManager.save(party, newRoleTypes );
partyRoleManager.save( partyRoles );
if( party.getParent() == null || party.getParent().getId() == null )
return;
PartyRole toRole = null; //上级隶属关系
List<PartyRole> toRoles = partyRoleManager.list( party.getParent() );
for( PartyRole role:toRoles ){
if( RoleType.PARENT_ORG.equals( role.getType() ) ){
toRole = role;
break;
}
if( interalOrgSet.contains( role.getType() ) )
toRole = role;
}
if( fromRole != null && toRole != null ){
PartyRelationship pr = new PartyRelationship(
RelationshipType.ORG_ROLLUP,fromRole,toRole );
partyRelationshipManager.save( pr );
}
}else{ //修改数据
RoleType type = types[0];
for( RoleType item:types ){
if( !RoleType.PARENT_ORG.equals(item))
type = item;
}
PartyRole newFromRole = null;
List<PartyRole> savedPartyRoles = partyRoleManager.list(
party, RoleType.internalOrg() );
if( savedPartyRoles != null && !savedPartyRoles.isEmpty()){
//savedPartyRoles.size()>1 数据有问题
newFromRole = savedPartyRoles.get(0);
savedPartyRoles.get(0).setType( type );
}else{
//用于数据容错,正常情况不会执行
newFromRole = new PartyRole(party,type);
savedPartyRoles.add( newFromRole );
}
partyRoleManager.save(savedPartyRoles);
if( party.getParent() != null ){
PartyRole newToRole = null;
RoleType[] allowedSelectTypes = RoleType.internalOrg();
RoleType[] paramRoleTypes = new RoleType[allowedSelectTypes.length+1];
paramRoleTypes[allowedSelectTypes.length] = RoleType.PARENT_ORG;
System.arraycopy( allowedSelectTypes,0, paramRoleTypes, 0,allowedSelectTypes.length);
List<PartyRole> newToRoles = partyRoleManager.list(