|  |  |  | 
|---|
|  |  |  | package com.moral.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import static com.moral.common.bean.Constants.IS_DELETE_FALSE; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  | import java.util.HashSet; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.springframework.data.annotation.Transient; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.github.pagehelper.PageHelper; | 
|---|
|  |  |  | import com.moral.common.bean.Constants; | 
|---|
|  |  |  | import com.moral.common.bean.PageBean; | 
|---|
|  |  |  | import com.moral.common.exception.BusinessException; | 
|---|
|  |  |  | import com.moral.common.util.ExampleUtil; | 
|---|
|  |  |  | import com.moral.common.util.ValidateUtil; | 
|---|
|  |  |  | import com.moral.entity.Organization; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.moral.mapper.OrganizationMapper; | 
|---|
|  |  |  | import com.moral.mapper.OrganizationRelationMapper; | 
|---|
|  |  |  | import com.moral.service.OrganizationService; | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.springframework.data.annotation.Transient; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import tk.mybatis.mapper.entity.Example; | 
|---|
|  |  |  | import tk.mybatis.mapper.entity.Example.Criteria; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import static com.moral.common.bean.Constants.IS_DELETE_FALSE; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class OrganizationServiceImpl implements OrganizationService { | 
|---|
|  |  |  | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | organizationMapper.updateByPrimaryKey(organization); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //更新父组织关系 | 
|---|
|  |  |  | addOrModifyOrgRelation(organization); | 
|---|
|  |  |  | if(organization.getId()!=null && !organization.getId().equals(organization.getParentId())){ | 
|---|
|  |  |  | List<Integer> orgIds = organizationMapper.selectLowerOrgIds(organization.getId()); | 
|---|
|  |  |  | boolean isChild = orgIds.stream().anyMatch( | 
|---|
|  |  |  | item -> { | 
|---|
|  |  |  | return item!=null && item.equals(organization.getParentId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | // 非子孙组织才能设为父组织,避免闭环脏数据 | 
|---|
|  |  |  | if(!isChild){ | 
|---|
|  |  |  | //更新父组织关系 | 
|---|
|  |  |  | addOrModifyOrgRelation(organization); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | throw new BusinessException("cannot set the children as father,organization:"+ JSON.toJSONString(organization)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | throw new BusinessException("id of organization can't equal to it's parentId,organization:"+ JSON.toJSONString(organization)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch (Exception ex){ | 
|---|
|  |  |  | throw  ex; | 
|---|
|  |  |  | 
|---|
|  |  |  | relation.setChildId(organization.getId()); | 
|---|
|  |  |  | List<OrganizationRelation> relations =  organizationRelationMapper.select(relation); | 
|---|
|  |  |  | if(CollectionUtils.isEmpty(relations)){ | 
|---|
|  |  |  | relation.setParentId(organization.getParentId()); | 
|---|
|  |  |  | organizationRelationMapper.insert(relation); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(organization.getParentId())) { | 
|---|
|  |  |  | relation.setParentId(organization.getParentId()); | 
|---|
|  |  |  | organizationRelationMapper.insert(relation); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | OrganizationRelation relationOfSelect = relations.get(0); | 
|---|
|  |  |  | //当关系发生修改时,更新关系 | 
|---|