<?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.cku.oa.trainschool.dao.GroomerSchoolMailDao">

    <sql id="groomerSchoolMailColumns">
		a.id AS "id",
		a.school_id AS "schoolId",
		a.release_date AS "releaseDate",
		a.release_num AS "releaseNum",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		b.school_name_cn AS "schoolNameCn",
		b.address AS "traingInstitution.address"
	</sql>

    <sql id="groomerSchoolMailJoins">
		LEFT JOIN training_institution b ON a.school_id = b.id
	</sql>

    <select id="get" resultType="GroomerSchoolMail">
        SELECT
        <include refid="groomerSchoolMailColumns"/>
        FROM groomer_school_mail a
        <include refid="groomerSchoolMailJoins"/>
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="GroomerSchoolMail">
        SELECT
        <include refid="groomerSchoolMailColumns"/>
        FROM groomer_school_mail a
        <include refid="groomerSchoolMailJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="schoolId != null and schoolId != ''">
                AND a.school_id = #{schoolId}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.release_date DESC
            </otherwise>
        </choose>
    </select>

    <select id="findAllList" resultType="GroomerSchoolMail">
        SELECT
        <include refid="groomerSchoolMailColumns"/>
        FROM groomer_school_mail a
        <include refid="groomerSchoolMailJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <insert id="insert">
		INSERT INTO groomer_school_mail(
			id,
			school_id,
			release_date,
			release_num,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{schoolId},
			#{releaseDate},
			#{releaseNum},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>

    <update id="update">
		UPDATE groomer_school_mail SET 	
			release_date = #{releaseDate},
			release_num = #{releaseNum},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>

    <update id="delete">
		UPDATE groomer_school_mail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

</mapper>