| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | 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 com.moral.mapper.AccountMapper;
|
| | | import com.moral.service.AccountService;
|
| | | 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.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;
|
| | |
|
| | |
| | | public class OrganizationServiceImpl implements OrganizationService {
|
| | |
|
| | | @Resource
|
| | | private AccountMapper accountMapper;
|
| | |
|
| | | @Resource
|
| | | private OrganizationMapper organizationMapper;
|
| | |
|
| | | @Resource
|
| | | private OrganizationRelationMapper organizationRelationMapper;
|
| | |
|
| | | private static Class ENTITY_CLASS = Organization.class;
|
| | | private static Class ENTITY_CLASS = Organization.class;
|
| | |
|
| | | @Override
|
| | | public Set<Integer> getChildOrganizationIds(Integer orgId){
|
| | | Set<Integer> orgIds = new HashSet<Integer>();
|
| | | orgIds.add(orgId);
|
| | | OrganizationRelation relation = new OrganizationRelation();
|
| | | relation.setParentId(orgId);
|
| | | Organization organization = organizationMapper.selectByPrimaryKey(orgId);
|
| | | if (IS_DELETE_FALSE.equals(organization.getIsDelete())) {
|
| | | List<OrganizationRelation> organizationRelations = organizationRelationMapper.select(relation);
|
| | | for (OrganizationRelation organizationRelation : organizationRelations) {
|
| | | Set<Integer> organizationIds = getChildOrganizationIds(organizationRelation.getChildId());
|
| | | orgIds.addAll(organizationIds);
|
| | | }
|
| | | }
|
| | | return orgIds;
|
| | | public Set<Integer> getChildOrganizationIds(Integer orgId) {
|
| | | Set<Integer> orgIds = new HashSet<Integer>();
|
| | | orgIds.add(orgId);
|
| | | OrganizationRelation relation = new OrganizationRelation();
|
| | | relation.setParentId(orgId);
|
| | | Organization organization = organizationMapper.selectByPrimaryKey(orgId);
|
| | | if (IS_DELETE_FALSE.equals(organization.getIsDelete())) {
|
| | | List<OrganizationRelation> organizationRelations = organizationRelationMapper.select(relation);
|
| | | for (OrganizationRelation organizationRelation : organizationRelations) {
|
| | | Set<Integer> organizationIds = getChildOrganizationIds(organizationRelation.getChildId());
|
| | | orgIds.addAll(organizationIds);
|
| | | }
|
| | | }
|
| | | return orgIds;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Organization> getOrganizationsByAreaName(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("areaName"), "param.is.null");
|
| | | List<Organization> organizations = organizationMapper.getOrganizationsByAreaName(parameters);
|
| | | return organizations;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Organization> getOrganizationsByAreaName(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("areaName"), "param.is.null");
|
| | | List<Organization> organizations = organizationMapper.getOrganizationsByAreaName(parameters);
|
| | | return organizations;
|
| | | }
|
| | | @Override
|
| | | public PageBean queryByPageBean(PageBean pageBean) {
|
| | | Example example = ExampleUtil.generateExample(ENTITY_CLASS, pageBean);
|
| | | List<Example.Criteria> criteriaList = example.getOredCriteria();
|
| | | if (criteriaList != null && criteriaList.size() > 0) {
|
| | | for (Example.Criteria cri : criteriaList) {
|
| | | cri.andNotEqualTo("isDelete", Constants.IS_DELETE_TRUE);
|
| | | }
|
| | | } else {
|
| | | example.or().andNotEqualTo("isDelete", Constants.IS_DELETE_TRUE);
|
| | | }
|
| | | if (pageBean.getPageSize() > 0) {
|
| | | PageHelper.startPage(pageBean.getPageIndex(), pageBean.getPageSize());
|
| | | }
|
| | | if (example.getOrderByClause() == null || example.getOrderByClause().isEmpty()) {
|
| | | example.setOrderByClause("create_time desc");
|
| | | }
|
| | | List<Organization> organizationList = organizationMapper.selectWithAreaNameByExample(example);
|
| | | return new PageBean(organizationList);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public PageBean queryByPageBean(PageBean pageBean){
|
| | | Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
|
| | | List<Example.Criteria> criteriaList = example.getOredCriteria();
|
| | | if(criteriaList!=null&&criteriaList.size()>0){
|
| | | for(Example.Criteria cri : criteriaList){
|
| | | cri.andNotEqualTo("isDelete",Constants.IS_DELETE_TRUE);
|
| | | }
|
| | | }else {
|
| | | example.or().andNotEqualTo("isDelete",Constants.IS_DELETE_TRUE);
|
| | | }
|
| | | if(pageBean.getPageSize()>0){
|
| | | PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize());
|
| | | }
|
| | | if(example.getOrderByClause() == null || example.getOrderByClause().isEmpty()) {
|
| | | example.setOrderByClause("create_time desc");
|
| | | }
|
| | | List<Organization> organizationList = organizationMapper.selectWithAreaNameByExample(example);
|
| | | return new PageBean(organizationList);
|
| | | }
|
| | | @Override
|
| | | public void addOrModify(Organization organization){
|
| | | try{
|
| | | if(organization.getId()==null){
|
| | | //新建数据,默认为未删除状态
|
| | | organization.setIsDelete(Constants.IS_DELETE_FALSE);
|
| | | organizationMapper.insertSelective(organization);
|
| | | }else{
|
| | | organizationMapper.updateByPrimaryKey(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;
|
| | | }
|
| | | }
|
| | | @Transient
|
| | | private void addOrModifyOrgRelation(Organization organization){
|
| | | OrganizationRelation relation = new OrganizationRelation();
|
| | | relation.setChildId(organization.getId());
|
| | | List<OrganizationRelation> relations = organizationRelationMapper.select(relation);
|
| | | if(CollectionUtils.isEmpty(relations)){
|
| | | relation.setParentId(organization.getParentId());
|
| | | organizationRelationMapper.insert(relation);
|
| | | }else{
|
| | | OrganizationRelation relationOfSelect = relations.get(0);
|
| | | //当关系发生修改时,更新关系
|
| | | if(organization.getParentId() !=null &&
|
| | | !organization.getParentId().equals(relationOfSelect.getParentId())
|
| | | ) {
|
| | | relationOfSelect.setParentId(organization.getParentId());
|
| | | organizationRelationMapper.updateByPrimaryKey(relationOfSelect);
|
| | | }else if(organization.getParentId() ==null){
|
| | | //ParentI为null,删除该条记录
|
| | | organizationRelationMapper.deleteByPrimaryKey(relationOfSelect.getId());
|
| | | @Override
|
| | | public void addOrModify(Organization organization) {
|
| | | try {
|
| | | if (organization.getId() == null) {
|
| | | //新建数据,默认为未删除状态
|
| | | organization.setIsDelete(Constants.IS_DELETE_FALSE);
|
| | | organizationMapper.insertSelective(organization);
|
| | | } else {
|
| | | organizationMapper.updateByPrimaryKey(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));
|
| | | }
|
| | | //删除多余关系
|
| | | if(relations.size()>1){
|
| | | relations.remove(0);
|
| | | List<Integer> relationIds = relations.stream().map(
|
| | | element->{
|
| | | return element.getId();
|
| | | }
|
| | | ).collect(Collectors.toList());
|
| | | Example example = new Example(OrganizationRelation.class);
|
| | | example.or().andIn("id",relationIds);
|
| | | organizationRelationMapper.deleteByExample(example);
|
| | | }
|
| | | }
|
| | | }
|
| | | @Override
|
| | | public void deleteByIds(Integer... ids) {
|
| | | Organization organization = new Organization();
|
| | | organization.setIsDelete(Constants.IS_DELETE_TRUE);
|
| | | if(ids!=null&&ids.length>0){
|
| | | if(ids.length==1){
|
| | | organization.setId(ids[0]);
|
| | | organizationMapper.updateByPrimaryKeySelective(organization);
|
| | | }else{
|
| | | Example example = new Example(ENTITY_CLASS);
|
| | | example.or().andIn("id", Arrays.asList(ids));
|
| | | organizationMapper.updateByExampleSelective(organization,example);
|
| | | }
|
| | | } else {
|
| | | throw new BusinessException("id of organization can't equal to it's parentId,organization:" + JSON.toJSONString(organization));
|
| | | }
|
| | | } catch (Exception ex) {
|
| | | throw ex;
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | |
| | | @Override
|
| | | public List<Organization> getOrganizationsByName(String name) {
|
| | | ValidateUtil.notEmpty(name, "param.is.null");
|
| | | Example example = new Example(Organization.class);
|
| | | Criteria criteria = example.createCriteria();
|
| | | @Transient
|
| | | private void addOrModifyOrgRelation(Organization organization) {
|
| | | OrganizationRelation relation = new OrganizationRelation();
|
| | | relation.setChildId(organization.getId());
|
| | | List<OrganizationRelation> relations = organizationRelationMapper.select(relation);
|
| | | if (CollectionUtils.isEmpty(relations)) {
|
| | | if (!ObjectUtils.isEmpty(organization.getParentId())) {
|
| | | relation.setParentId(organization.getParentId());
|
| | | organizationRelationMapper.insert(relation);
|
| | | }
|
| | | } else {
|
| | | OrganizationRelation relationOfSelect = relations.get(0);
|
| | | //当关系发生修改时,更新关系
|
| | | if (organization.getParentId() != null &&
|
| | | !organization.getParentId().equals(relationOfSelect.getParentId())
|
| | | ) {
|
| | | relationOfSelect.setParentId(organization.getParentId());
|
| | | organizationRelationMapper.updateByPrimaryKey(relationOfSelect);
|
| | | } else if (organization.getParentId() == null) {
|
| | | //ParentI为null,删除该条记录
|
| | | organizationRelationMapper.deleteByPrimaryKey(relationOfSelect.getId());
|
| | | }
|
| | | //删除多余关系
|
| | | if (relations.size() > 1) {
|
| | | relations.remove(0);
|
| | | List<Integer> relationIds = relations.stream().map(
|
| | | element -> {
|
| | | return element.getId();
|
| | | }
|
| | | ).collect(Collectors.toList());
|
| | | Example example = new Example(OrganizationRelation.class);
|
| | | example.or().andIn("id", relationIds);
|
| | | organizationRelationMapper.deleteByExample(example);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andLike("name", "%" + name + "%");
|
| | | example.or().andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andCondition("getPY(name) like ", "%" + name + "%");
|
| | | @Override
|
| | | public void deleteByIds(Integer... ids) {
|
| | | Organization organization = new Organization();
|
| | | organization.setIsDelete(Constants.IS_DELETE_TRUE);
|
| | | if (ids != null && ids.length > 0) {
|
| | | if (ids.length == 1) {
|
| | | organization.setId(ids[0]);
|
| | | organizationMapper.updateByPrimaryKeySelective(organization);
|
| | | } else {
|
| | | Example example = new Example(ENTITY_CLASS);
|
| | | example.or().andIn("id", Arrays.asList(ids));
|
| | | organizationMapper.updateByExampleSelective(organization, example);
|
| | | }
|
| | |
|
| | | List<Organization> organizations = organizationMapper.selectByExample(example);
|
| | | return organizations;
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Organization getOrganizationById(int id) {
|
| | | Example example = new Example(ENTITY_CLASS);
|
| | | example.or().andEqualTo("id",id);
|
| | | List<Organization> organizationList = organizationMapper.selectWithAreaNameByExample(example);
|
| | | return organizationList.size()>0?organizationList.get(0):null;
|
| | | }
|
| | | @Override
|
| | | public List<Organization> getOrganizationsByName(String name) {
|
| | | ValidateUtil.notEmpty(name, "param.is.null");
|
| | | Example example = new Example(Organization.class);
|
| | | Criteria criteria = example.createCriteria();
|
| | |
|
| | | criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andLike("name", "%" + name + "%");
|
| | | example.or().andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andCondition("getPY(name) like ", "%" + name + "%");
|
| | |
|
| | | List<Organization> organizations = organizationMapper.selectByExample(example);
|
| | | return organizations;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Organization getOrganizationById(int id) {
|
| | | Example example = new Example(ENTITY_CLASS);
|
| | | example.or().andEqualTo("id", id);
|
| | | List<Organization> organizationList = organizationMapper.selectWithAreaNameByExample(example);
|
| | | return organizationList.size() > 0 ? organizationList.get(0) : null;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Organization getFatherOrg(int childId) {
|
| | | OrganizationRelation relation = new OrganizationRelation();
|
| | | relation.setChildId(childId);
|
| | | List<OrganizationRelation> relations = organizationRelationMapper.select(relation);
|
| | | if (!CollectionUtils.isEmpty(relations)){
|
| | | if (!CollectionUtils.isEmpty(relations)) {
|
| | | OrganizationRelation relationOfSelect = relations.get(0);
|
| | | if(relationOfSelect.getParentId()!=null){
|
| | | if (relationOfSelect.getParentId() != null) {
|
| | | return organizationMapper.selectByPrimaryKey(relationOfSelect.getParentId());
|
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Organization> getOrganizationList(String organizationName) {
|
| | | List<Organization> organizationList = organizationMapper.getOrganizationList(organizationName);
|
| | | return organizationList;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Organization getOrganizationByAccountId(Integer accountId) {
|
| | | Map<String, Object> organizationId = accountMapper.getOrganizationIdByAccountId(accountId);
|
| | | int orgId = (int) organizationId.get("organization_id");
|
| | | Organization org = getOrganizationById(orgId);
|
| | | return org;
|
| | | }
|
| | | }
|