package com.tuoheng.admin.service.dept.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.conver.DeptConverMapper;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.code.dept.QueryDeptTreeListCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.dept.DeptTreeVo;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  获取当前部门树形列表业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-19
 */
@Slf4j
@Service
public class QueryListTreeByDeptIdService {

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 获取部门树形列表
     *
     * @return
     */
    public JsonResult getListTree(String deptId) {
        List<DeptTreeVo> deptTreeVoList = new ArrayList<>();
        User user = CurrentUserUtil.getUserInfo();
        // 查询当前部门下所有有效的部门数据
        List<String> deptIdList = deptMapper.selectAllChildListById(user.getDeptId());
        List<Dept> deptList = deptMapper.selectListByIdList(deptIdList);
        //  找出当前部门
        Dept currentDept = deptList.stream()
                .filter(item -> deptId.equals(item.getId()))
                .findAny()
                .orElse(null);
        deptList.removeIf(item -> (item.getPid().equals(currentDept.getPid()) && !item.getId().equals(deptId)));

        if (CollectionUtil.isEmpty(deptList)) {
            log.info("获取部门列表为空");
            return JsonResult.error(QueryDeptTreeListCodeEnum.DEPT_LIST_IS_NULL.getCode(), QueryDeptTreeListCodeEnum.DEPT_LIST_IS_NULL.getMsg());
        }
        List<DeptTreeVo> deptTreeVoListTmp = DeptConverMapper.INSTANCE.deptListToDeptVoList(deptList);
        Map<String, DeptTreeVo> deptVoMap = new HashMap<>();
        for (DeptTreeVo deptTreeVo : deptTreeVoListTmp) {
            deptVoMap.put(deptTreeVo.getId(), deptTreeVo);
        }
        for (DeptTreeVo deptTreeVo : deptTreeVoListTmp) {
            DeptTreeVo child = deptTreeVo;
            if (currentDept.getId().equals(child.getId())) {
                deptTreeVoList.add(deptTreeVo);
            } else {
                DeptTreeVo parent = deptVoMap.get(child.getPid());
                if (ObjectUtil.isNotNull(parent)) {
                    deptTreeVo.setPname(parent.getName());
                    parent.getChildren().add(child);
                }
            }
        }
        if (ObjectUtil.isNotNull(currentDept)) {
            // 从list中移除上级部门
            Dept finalCurrentDept = currentDept;
            deptTreeVoList.removeIf(item -> item.getId().equals(finalCurrentDept.getPid()));
        }
        return JsonResult.success(deptTreeVoList);
    }

}
