package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.core.api.APIResponse;
import com.ruoyi.system.domain.resp.RespTreeSelect;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：zhangbaoyu
 * @date ：Created in 2020-01-14 17:24
 */
@Service
@Transactional
public class BusinessSysDeptService {

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private SysDeptMapper deptMapper;


    /**
     * 前端select
     *
     * @return
     */
    public APIResponse treeSelect() {
        SysDept sysDept = new SysDept();
        QueryWrapper<SysDept> qw = new QueryWrapper(sysDept);
        qw.orderByAsc("sort");
        List<SysDept> list = sysDeptService.list(qw);
        SysDept tree = new SysDept();
        tree.setDeptId(-1L);
        this.converterTree(list,tree);
        List<SysDept> children = tree.getChildren();
        List<RespTreeSelect> respTreeSelects = children.stream().map(RespTreeSelect::new).collect(Collectors.toList());
        return APIResponse.toAPIResponse(respTreeSelects);
    }

    private static Map<Long, List<SysDept>> subTreeMap = new HashMap<>();
    private List<SysDept> children;
    /**
     * 树转换
     * @param sysDeptList
     */
    public void converterTree(List<SysDept> sysDeptList,SysDept tree) {
        subTreeMap.clear();
        for (SysDept menu : sysDeptList) {
            List<SysDept> subTreeList = subTreeMap.get(menu.getParentId());
            if (null == subTreeList) {
                subTreeList = new LinkedList<>();
                subTreeMap.put(menu.getParentId(),subTreeList);
            }
            subTreeList.add(menu);
        }

        this.children = treeDepth(tree).getChildren();
        if(this.children.isEmpty()){
            this.children = sysDeptList;
        }
    }
    private SysDept treeDepth(SysDept sysDept) {
        Long deptId = sysDept.getDeptId();
        List<SysDept> childMenuList = subTreeMap.get(deptId);
        if(null != childMenuList){
            List<SysDept> childList = Lists.newLinkedList();
            for (SysDept childMenu : childMenuList) {
                SysDept child = treeDepth(childMenu);
                childList.add(child);
            }
            sysDept.setChildren(childList);
            return sysDept;
        }else{
            sysDept.setChildren(Lists.newLinkedList());
            return sysDept;
        }
    }



    /**
     * 前端select
     *
     * @return
     */
    public List<RespTreeSelect> campusList() {
        SysDept sysDept = new SysDept();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysDept.setTenantId(loginUser.getNowTenantId());
        sysDept.setDeptType("2");
        List<SysDept> list = deptMapper.selectDeptList(sysDept);
        return list.stream().map(RespTreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 校区选择
     * 如果拥有全部校区权限，返回全部校区 部分校区
     * 否二 返回 部分校区
     *
     * @return
     */
    public List<RespTreeSelect> campusSelect() {

        List<RespTreeSelect> list = Lists.newArrayList();

        list.add(new RespTreeSelect(9L,"全部校区",null));
        list.add(new RespTreeSelect(0L,"部分校区",null));

        return list;
    }


}
