package easydo.technology.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import easydo.technology.system.domain.SysDept;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 部门 Mapper 接口
 * </p>
 *
 * @author fr
 * @since 2022-01-26
 */
@Repository
public interface SysDeptMapper extends BaseMapper<SysDept> {
    @Select("SELECT\n" +
            "    ID.LEVELs,\n" +
            "    DATA.*\n" +
            "FROM\n" +
            "    (\n" +
            "    SELECT\n" +
            "        @ids AS _ids,\n" +
            "        ( SELECT @ids := GROUP_CONCAT( dept_id ) FROM sys_dept WHERE FIND_IN_SET(pid, @ids ) ) AS cids,\n" +
            "        @l := @l + 1 AS LEVELs\n" +
            "    FROM\n" +
            "        sys_dept,\n" +
            "        ( SELECT @ids := #{id}, @l := 0 ) b\n" +
            "    WHERE\n" +
            "        @ids IS NOT NULL\n" +
            "    ) ID,\n" +
            "    sys_dept DATA\n" +
            "WHERE\n" +
            "    FIND_IN_SET( DATA.dept_id, ID._ids )\n" +
            "ORDER BY\n" +
            "   dept_sort")
    List<SysDept> selectChilds(int id);

}
