<?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.schoolinout.system.mapper.ParentMapper">

    <resultMap type="Parent" id="ParentResult">
        <result property="id" column="id"/>
        <result property="parentName" column="parent_name"/>
        <result property="parentPhone" column="parent_phone"/>
        <result property="studentIds" column="student_ids"/>
        <result property="relation" column="relation"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectParentVo">
        select id,
               parent_name,
               parent_phone,
               relation,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tb_school_parent
    </sql>

    <select id="selectParentList" parameterType="Parent" resultMap="ParentResult">
        select p.id,
        p.parent_name,
        p.parent_phone,
        p.relation,
        p.del_flag,
        p.create_by,
        p.create_time,
        p.update_by,
        p.update_time,
        GROUP_CONCAT(sp.student_id SEPARATOR ',') AS student_ids
        from tb_school_parent p
        LEFT JOIN tb_school_student_parent_relation sp ON p.id = sp.parent_id
        <where>
            <if test="parentName != null  and parentName != ''">and parent_name like concat('%', #{parentName}, '%')
            </if>
            <if test="parentPhone != null  and parentPhone != ''">and parent_phone = #{parentPhone}</if>
            <if test="relation != null  and relation != ''">and relation = #{relation}</if>
        </where>
    </select>

    <select id="selectParentById" parameterType="Long" resultMap="ParentResult">
        select p.id,
               p.parent_name,
               p.parent_phone,
               p.relation,
               p.del_flag,
               p.create_by,
               p.create_time,
               p.update_by,
               p.update_time,
               GROUP_CONCAT(sp.student_id SEPARATOR ',') AS student_ids
        from tb_school_parent p
                 LEFT JOIN tb_school_student_parent_relation sp ON p.id = sp.parent_id
        where p.id = #{id}
    </select>

    <insert id="insertParent" parameterType="Parent" useGeneratedKeys="true" keyProperty="id">
        insert into tb_school_parent
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentName != null and parentName != ''">parent_name,</if>
            <if test="parentPhone != null and parentPhone != ''">parent_phone,</if>
            <if test="relation != null and relation != ''">relation,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentName != null and parentName != ''">#{parentName},</if>
            <if test="parentPhone != null and parentPhone != ''">#{parentPhone},</if>
            <if test="relation != null and relation != ''">#{relation},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateParent" parameterType="Parent">
        update tb_school_parent
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentName != null and parentName != ''">parent_name = #{parentName},</if>
            <if test="parentPhone != null and parentPhone != ''">parent_phone = #{parentPhone},</if>
            <if test="relation != null and relation != ''">relation = #{relation},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteParentById" parameterType="Long">
        delete
        from tb_school_parent
        where id = #{id}
    </delete>

    <delete id="deleteParentByIds" parameterType="String">
        delete from tb_school_parent where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>