package com.elephant.framework.galaxy.aroad.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.elephant.framework.galaxy.aroad.module.system.dto.DeptTree;
import com.elephant.framework.galaxy.aroad.module.system.entity.Dept;
import com.elephant.framework.galaxy.aroad.module.system.entity.DeptRelation;
import com.elephant.framework.galaxy.aroad.module.system.mapper.DeptMapper;
import com.elephant.framework.galaxy.aroad.module.system.service.DeptRelationService;
import com.elephant.framework.galaxy.aroad.module.system.service.DeptService;
import com.elephant.framework.galaxy.aroad.util.SecurityUtils;
import com.elephant.framework.galaxy.aroad.util.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@Service
@AllArgsConstructor
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    private final DeptRelationService deptRelationService;

    /**
     * 添加信息部门
     *
     * @param dept 部门
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDept(Dept dept) {
        Dept deptSave = new Dept();
        BeanUtils.copyProperties(dept, deptSave);
        this.save(deptSave);
        deptRelationService.insertDeptRelation(deptSave);
        return Boolean.TRUE;
    }


    /**
     * 删除部门
     *
     * @param id 部门 ID
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeptById(Integer id) {
        //级联删除部门
        List<Integer> idList = deptRelationService
                .list(Wrappers.<DeptRelation>query().lambda()
                        .eq(DeptRelation::getAncestor, id))
                .stream()
                .map(DeptRelation::getDescendant)
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(idList)) {
            this.removeByIds(idList);
        }

        //删除部门级联关系
        deptRelationService.deleteAllDeptRealtion(id);
        return Boolean.TRUE;
    }

    /**
     * 更新部门
     *
     * @param dept 部门信息
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeptById(Dept dept) {
        //更新部门状态
        this.updateById(dept);
        //更新部门关系
        DeptRelation relation = new DeptRelation();
        relation.setAncestor(dept.getParentId());
        relation.setDescendant(dept.getDeptId());
        deptRelationService.updateDeptRealtion(relation);
        return Boolean.TRUE;
    }

    /**
     * 查询全部部门树
     *
     * @return 树
     */
    @Override
    public List<DeptTree> selectTree() {
        return getDeptTree(this.list(Wrappers.emptyWrapper()));
    }

    /**
     * 查询用户部门树
     *
     * @return
     */
    @Override
    public List<DeptTree> getUserTree() {
        Integer deptId = SecurityUtils.getUser().getDeptId();
        List<Integer> descendantIdList = deptRelationService
                .list(Wrappers.<DeptRelation>query().lambda()
                        .eq(DeptRelation::getAncestor, deptId))
                .stream().map(DeptRelation::getDescendant)
                .collect(Collectors.toList());

        List<Dept> deptList = baseMapper.selectBatchIds(descendantIdList);
        return getDeptTree(deptList);
    }

    /**
     * 构建部门树
     *
     * @param depts 部门
     * @return
     */
    private List<DeptTree> getDeptTree(List<Dept> depts) {
        List<DeptTree> treeList = depts.stream()
                .filter(dept -> !dept.getDeptId().equals(dept.getParentId()))
                .map(dept -> {
                    DeptTree node = new DeptTree();
                    node.setId(dept.getDeptId());
                    node.setParentId(dept.getParentId());
                    node.setName(dept.getName());
                    return node;
                }).collect(Collectors.toList());
        return TreeUtil.bulid(treeList, 0);
    }

}
