<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yida.data.system.mapper.DeptMapper">
  <select id="findListByAera" resultType="com.yida.data.common.core.entity.system.Dept">
    select td.*
    from t_dept td
    <where>
      <if test="type != null">
        and td.dept_type=#{type}
      </if>
      <if test="areaIds != null and areaIds.size > 0">
        and td.area_id in
        <foreach collection="areaIds" separator="," open="(" item="area" close=")">
          #{area}
        </foreach>
      </if>
      <if test="schoolName != null and schoolName != ''">
        and td.DEPT_NAME like concat('%',#{schoolName},'%')
      </if>
    </where>
    order by ORDER_NUM
  </select>

  <select id="getSchoolAreaInfo" resultType="com.yida.data.system.vo.SchoolClassInfoVO">
    SELECT
    school.DEPT_ID schoolId,
    school.DEPT_NAME schoolName,
    town.id townId,
    town.NAME townName,
    county.id countyId,
    county.NAME countyName,
    city.id cityId,
    city.NAME cityName,
    city.parent_id provinceId,
    city.parent_name provinceName
    FROM
    t_dept school
    LEFT JOIN edu_area town ON school.area_id = town.id
    LEFT JOIN edu_area county ON town.parent_id = county.id
    LEFT JOIN edu_area city ON county.parent_id = city.id
    where school.DEPT_ID=#{schoolId}
  </select>

  <!-- 查询代理商管理的学校列表数据 -->
  <select id="listAgentSchoolPage"
    resultType="com.yida.data.system.vo.AgentSchoolSelectPageVO">
    SELECT
    d.DEPT_ID AS dept_id,
    d.DEPT_NAME AS dept_name,
    d.CREATE_TIME AS create_date,
    r.enable_status,
    r.create_name
    FROM
    t_dept d
    LEFT JOIN (
      SELECT DISTINCT
        dr.*,
        u.nickname as create_name
      FROM
        t_dept_school_relate dr
      LEFT JOIN t_user u ON dr.create_id = u.USER_ID
      WHERE
        dr.create_id = #{currentUser.userId}
      or dr.dept_id = #{currentUser.deptId}
      <if test='childIdList != null and childIdList.size > 0'>
        or dr.create_dept_id IN
        <foreach collection="childIdList" open="(" close=")" separator="," item="deptId">
          #{deptId}
        </foreach>
      </if>
    ) r ON d.DEPT_ID = r.school_id
    WHERE
    r.del_flag = 0
    <if test='dto.schoolName != null and dto.schoolName != ""'>
      AND d.DEPT_NAME LIKE CONCAT( '%', #{dto.schoolName}, '%' )
    </if>
    ORDER BY
    d.CREATE_TIME DESC
  </select>

  <!-- 查询代理商关联学校 -->
  <select id="listSchoolByAgentId"
    resultType="com.yida.data.common.core.entity.system.Dept">
    SELECT
    d.*
    FROM
    edu_agent_school_relate r
    inner JOIN t_dept d ON r.school_id = d.DEPT_ID
    WHERE
    r.del_flag = 0
    AND r.enable_status = 1
    AND r.agent_id = #{agentId}
    <if test="areaIds != null and areaIds.size > 0">
      and d.area_id in
      <foreach collection="areaIds" separator="," open="(" item="area" close=")">
        #{area}
      </foreach>
    </if>
    <if test="schoolName != null and schoolName != ''">
      and d.DEPT_NAME like concat('%',#{schoolName},'%')
    </if>
    ORDER BY
    d.CREATE_TIME DESC
  </select>
  
  <!-- 查询关联的学校信息 -->
  <select id="listSchool"
          resultType="com.yida.data.common.core.entity.system.Dept">
    SELECT
    d.*
    FROM
    (
      SELECT DISTINCT
        *
      FROM
        t_dept_school_relate
      WHERE
      create_id = #{currentUser.userId}
      or dept_id = #{currentUser.deptId}
      <if test='childIdList != null and childIdList.size > 0'>
        or create_dept_id IN
        <foreach collection="childIdList" open="(" close=")" separator="," item="deptId">
          #{deptId}
        </foreach>
      </if>
    ) r
    inner JOIN t_dept d ON r.school_id = d.DEPT_ID
    WHERE
    r.del_flag = 0
    AND r.enable_status = 1
    <if test="areaIds != null and areaIds.size > 0">
      and d.area_id in
      <foreach collection="areaIds" separator="," open="(" item="area" close=")">
        #{area}
      </foreach>
    </if>
    <if test="schoolName != null and schoolName != ''">
      and d.DEPT_NAME like concat('%',#{schoolName},'%')
    </if>
    ORDER BY
    d.CREATE_TIME DESC
  </select>
  
</mapper>