package com.tuoheng.admin.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Dept;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DeptMapperTest {

    @Autowired
    private DeptMapper deptMapper;

    @Test
    public void testSelectAllChildListByPid() {
        String id = "a52c59563756fb96879b54ecb9b173f1";

        String sql = buildSql();

//        List<Dept>  deptList = deptMapper.selectAllChildListById(id);
//        for (Dept dept : deptList) {
//            System.out.println(dept);
//        }

        List<String>  deptIdList = deptMapper.selectAllChildListById(id);
        for (String deptId : deptIdList) {
            System.out.println(deptId);
        }
    }

    @Test
    public void testSelectAllChildListByPid2() {
        List<Dept> deptList = new ArrayList<>();
        Dept dept = new Dept();
        dept.setId("32b8cbc4570250b67294f46c76a6c694");
        dept.setPid("0");

        deptList.add(dept);
        queryChildDeptList(deptList, dept);

        for (Dept d : deptList) {
            System.out.println(d.getId() + "-" + d.getPid() + "-" + d.getName());
        }
    }

    /**
     * 递归查询子部门信息
     *  1、递归算法，效率比较低，考虑时间数据量不会太多，后期可优化
     *  2、另一方案，通过数据库查询，根据pid，查询该部门下所有子部门及子子部门数据
     */
    private void queryChildDeptList(List<Dept> deptList, Dept parentDept) {
        List<Dept> childDeptlist = deptMapper.selectList(new LambdaQueryWrapper<Dept>()
                                    .eq(Dept::getTenantId, 0)
                                    .eq(Dept::getPid, parentDept.getId())
                                    .eq(Dept::getMark, 1));

        for (Dept dept : childDeptlist ) {
            queryChildDeptList(deptList, dept);
        }
        deptList.addAll(childDeptlist);
    }

    private String buildSql() {
        String sql = "select id, tenant_id, name, code, fullname, type, pid, sort, note, create_user, create_time, update_user, update_time, mark\n" +
                "            from (\n" +
                "                    select t1.id, t1.tenant_id, t1.name, t1.code, t1.fullname, t1.type, t1.pid, t1.sort, t1.note, t1.create_user, t1.create_time, t1.update_user, t1.update_time, t1.mark,\n" +
                "                    IF(FIND_IN_SET(pid, @pids) > 0, @pids <![CDATA[ := ]]> CONCAT(@pids, ',',id), 0) AS ischild\n" +
                "                    from (select id,pid,name from th_dept t where t.mark = 1) t1,\n" +
                "                    (select @pids <![CDATA[ := ]]> '32b8cbc4570250b67294f46c76a6c694') t2\n" +
                "            ) t3\n" +
                "            where ischild like CONCAT('%', '32b8cbc4570250b67294f46c76a6c694', '%')";
        return sql;
    }

}
