<?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.user.mapper.EduParentMapper">
  <resultMap id="parentInfo" type="com.yida.data.user.vo.ParentInfoVO">
    <result property="parentId" column="parentId"/>
    <result property="parentWxId" column="parentWxId"/>
    <result property="parentYidaAppId" column="parentYidaAppId"/>
    <result property="wxPublicOpenId" column="wx_public_open_id"/>
    <result property="mobile" column="mobile"/>
    <collection property="studentList" ofType="com.yida.data.user.vo.StudentInfoVO">
      <result property="name" column="name"/>
      <result property="studentId" column="studentId"/>
      <result property="studentWxId" column="studentWxId"/>
      <result property="relation" column="relation"/>
    </collection>
  </resultMap>

  <resultMap id="parentStudentMap" type="com.yida.data.common.core.entity.user.EduParent">
    <id column="id" property="id"/>
    <collection property="studentList" ofType="com.yida.data.common.core.entity.user.EduStudent">
      <result property="sectionName" column="sSectionName"/>
      <result property="relation" column="relationName"/>
    </collection>
  </resultMap>
  <select id="getParentWithStudent" resultMap="parentInfo">
    select a.id parentId,a.wx_id parentWxId,a.yida_app_user_id parentYidaAppId,a.wx_public_open_id
    ,a.mobile
    , d.id studentId,d.wx_id studentWxId,d.stu_name name,c.name relation
    from edu_parent a left join edu_parent_student b on a.id=b.parent_id
    left join edu_parent_type c
    on b.parent_type_id = c.id
    left join (select * from edu_student where del_flag=0 and school_id=#{schoolId})d on b.student_id=d.id
    where
    a.school_id=#{schoolId}
    <if test="mobile != null and mobile != ''">
      and a.mobile=#{mobile}
    </if>
    <if test="wxId != null and wxId != ''">
      and a.wx_id=#{wxId}
    </if>
  </select>

  <select id="listParentByClass" resultType="com.yida.data.user.vo.ParentInfoVO">
    select c.id parentId,c.wx_id parentWxId,c.mobile,GROUP_CONCAT(a.stu_name,d.`name`)
    parentName,c.yida_app_user_id parentYidaAppId
    from edu_student a
    inner join edu_parent_student b on a.id = b.student_id
    left join edu_parent c on b.parent_id = c.id
    left join edu_parent_type d on b.parent_type_id = d.id
    where a.class_id in
    <foreach collection="classIds" open="(" item="item" separator="," close=")">
      #{item}
    </foreach>
    <if test="parentName != null and parentName != ''">
      and( a.stu_name like concat('%',#{parentName},'%')
      or (d.name like concat('%',#{parentName},'%')))
    </if>
    group by c.id,c.wx_id,c.mobile
    order by parentName
  </select>

  <select id="listParentByStudent" resultType="com.yida.data.common.core.entity.user.EduParent">
    select a.*
    from edu_parent a left join edu_parent_student b on a.id=b.parent_id
    where
    b.student_id in
    <foreach collection="studentId" item="item" close=")" open="(" separator=",">
      #{item}
    </foreach>
  </select>

  <select id="listParentWithStudentBySchool" resultMap="parentStudentMap">
    select a.id,e.DEPT_NAME sSectionName,d.name relationName
    from edu_parent a inner join edu_parent_student b on a.id=b.parent_id
    left join edu_student c on b.student_id=c.id and c.del_flag=0
    left join edu_parent_type d on b.parent_type_id=d.id
    left join edu_user_dept e on c.section_id=e.DEPT_ID
    where a.school_id in
    <foreach collection="schoolIds" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </select>
</mapper>