package com.mx.cneeds.server.user.service.impl;

import com.mx.cneeds.common.annotation.DataFilter;
import com.mx.cneeds.common.pager.Constant;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.pager.Query;
import com.mx.cneeds.server.dao.SysDeptDao;
import com.mx.cneeds.server.entity.SysDeptEntity;
import com.mx.cneeds.server.user.service.SysDeptService;
import org.springframework.stereotype.Service;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {


    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysDeptEntity> queryListByPage(Map<String, Object> params){
        long curPage = 1;
        long limit = 10;

        if(params.get(Constant.PAGE) != null){
            curPage = Long.parseLong((String)params.get(Constant.PAGE));
        }
        if(params.get(Constant.LIMIT) != null){
            limit = Long.parseLong((String)params.get(Constant.LIMIT));
        }

        params.put(Constant.PAGE,curPage);
        params.put(Constant.LIMIT,limit);
        Long startIndex = curPage>1? 0 : (curPage-1)*limit;
        params.put("startIndex",startIndex);
        return baseMapper.queryListByPage(params);
    }

    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysDeptEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public Long queryDeptCount(Map<String, Object> params) {
        Long count = baseMapper.queryCount(params);
        return count;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDeptEntity> page = this.page(
                new Query<SysDeptEntity>().getPage(params),
                new QueryWrapper<SysDeptEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Long> getSubDeptIdList(Long deptId){
        //部门及子部门ID列表
        List<Long> deptIdList = new ArrayList<>();

        //获取子部门ID
        List<Long> subIdList = queryDetpIdList(deptId);
        getDeptTreeList(subIdList, deptIdList);

        return deptIdList;
    }

    @Override
    public List<Long> queryDetpIdList(Long parentId) {
        return baseMapper.queryDetpIdList(parentId);
    }


    @Override
//    @Transactional
    public List<SysDeptEntity> queryDeptIdsBydeptName(Set<Long> Ids) {
        return baseMapper.selectList(new QueryWrapper<SysDeptEntity>()
                .select("dept_id","name").in("dept_id",Ids));
    }



    /**
     * 递归
     */
    private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
        for(Long deptId : subIdList){
            List<Long> list = queryDetpIdList(deptId);
            if(list.size() > 0){
                getDeptTreeList(list, deptIdList);
            }

            deptIdList.add(deptId);
        }
    }

}
