<?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.EduParentStudentMapper">
  <select id="listParentStudent" resultType="com.yida.data.common.core.entity.user.EduParentStudent">
    select eps.*,ept.name parent_type_name
    from edu_parent_student eps
    left join edu_parent_type ept on eps.parent_type_id=ept.id
    where eps.parent_id=#{parentId}
  </select>

  <select id="listOnlyOneChild" resultType="com.yida.data.common.core.entity.user.EduParent">
    select distinct ep.id,ep.wx_id
    from edu_parent_student a left join edu_parent ep on a.parent_id = ep.id
    where a.parent_id in (
    select eps.parent_id
    from edu_parent_student eps
    where student_id=#{studentId})
    group by a.parent_id
    having count(1)=1
  </select>

  <select id="getRelationByParentAndStudent"
    resultType="com.yida.data.common.core.entity.user.EduParentStudent">
    select a.*,ept.name parentTypeName
    from edu_parent_student a left join edu_parent_type ept on a.parent_type_id = ept.id
    where a.parent_id=#{parentId} and a.student_id=#{studentId} limit 1
  </select>

  <delete id="delNotExistRaltion">
    DELETE
    FROM edu_parent_student
    WHERE student_id NOT IN (SELECT id FROM edu_student WHERE del_flag = 0)
       OR parent_id NOT IN (
      SELECT id
      FROM edu_parent
      )
  </delete>
</mapper>