SysDeptRelationServiceImpl.java 2.8 KB
Newer Older
pig_冷冷's avatar
pig_冷冷 已提交
1
/*
2
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
3
 *
4 5 6
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
7
 *
8 9 10 11 12 13 14
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
pig_冷冷's avatar
pig_冷冷 已提交
15 16 17 18 19 20 21 22 23 24 25
 */

package com.pig4cloud.pig.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.entity.SysDept;
import com.pig4cloud.pig.admin.api.entity.SysDeptRelation;
import com.pig4cloud.pig.admin.mapper.SysDeptRelationMapper;
import com.pig4cloud.pig.admin.service.SysDeptRelationService;
26
import lombok.RequiredArgsConstructor;
pig_冷冷's avatar
pig_冷冷 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2019/2/1
 */
@Service
42
@RequiredArgsConstructor
43 44 45
public class SysDeptRelationServiceImpl extends ServiceImpl<SysDeptRelationMapper, SysDeptRelation>
		implements SysDeptRelationService {

pig_冷冷's avatar
pig_冷冷 已提交
46 47 48 49 50 51 52 53
	private final SysDeptRelationMapper sysDeptRelationMapper;

	/**
	 * 维护部门关系
	 * @param sysDept 部门
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
54
	public void saveDeptRelation(SysDept sysDept) {
55 56 57
		// 增加部门关系表
		List<SysDeptRelation> relationList = sysDeptRelationMapper.selectList(
				Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getDescendant, sysDept.getParentId()))
58 59 60 61 62 63
			.stream()
			.map(relation -> {
				relation.setDescendant(sysDept.getDeptId());
				return relation;
			})
			.collect(Collectors.toList());
pig_冷冷's avatar
pig_冷冷 已提交
64 65 66 67
		if (CollUtil.isNotEmpty(relationList)) {
			this.saveBatch(relationList);
		}

68
		// 自己也要维护到关系表中
pig_冷冷's avatar
pig_冷冷 已提交
69 70 71 72 73 74 75 76 77 78 79
		SysDeptRelation own = new SysDeptRelation();
		own.setDescendant(sysDept.getDeptId());
		own.setAncestor(sysDept.getDeptId());
		sysDeptRelationMapper.insert(own);
	}

	/**
	 * 通过ID删除部门关系
	 * @param id
	 */
	@Override
80
	public void removeDeptRelationById(Long id) {
pig_冷冷's avatar
pig_冷冷 已提交
81 82 83 84 85 86 87 88
		baseMapper.deleteDeptRelationsById(id);
	}

	/**
	 * 更新部门关系
	 * @param relation
	 */
	@Override
89
	@Transactional(rollbackFor = Exception.class)
90
	public void updateDeptRelation(SysDeptRelation relation) {
91 92
		baseMapper.deleteDeptRelations(relation);
		baseMapper.insertDeptRelations(relation);
pig_冷冷's avatar
pig_冷冷 已提交
93 94 95
	}

}