<?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.pangu.base.mapper.SubjectMapper">

    <resultMap id="SubjectResult" type="com.pangu.base.domain.Subject">
        <id property="subjectId" column="subject_id"/>
        <result property="subjectCode" column="subject_code"/>
        <result property="subjectName" column="subject_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectSubjectVo">
        SELECT subject_id, subject_code, subject_name, order_num, status,
               create_by, create_time, update_by, update_time, remark
        FROM pg_subject
        WHERE del_flag = '0'
    </sql>

    <select id="selectSubjectList" parameterType="com.pangu.base.domain.Subject" resultMap="SubjectResult">
        <include refid="selectSubjectVo"/>
        <if test="subjectName != null and subjectName != ''">
            AND subject_name LIKE CONCAT('%', #{subjectName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ORDER BY order_num
    </select>

    <select id="selectSubjectById" parameterType="Long" resultMap="SubjectResult">
        <include refid="selectSubjectVo"/>
        AND subject_id = #{subjectId}
    </select>

    <select id="checkSubjectNameUnique" parameterType="String" resultMap="SubjectResult">
        <include refid="selectSubjectVo"/>
        AND subject_name = #{subjectName}
        LIMIT 1
    </select>

    <insert id="insertSubject" parameterType="com.pangu.base.domain.Subject" useGeneratedKeys="true" keyProperty="subjectId">
        INSERT INTO pg_subject (
            subject_code, subject_name, order_num, status,
            create_by, create_time, del_flag, remark
        ) VALUES (
            #{subjectCode}, #{subjectName}, #{orderNum}, #{status},
            #{createBy}, #{createTime}, '0', #{remark}
        )
    </insert>

    <update id="updateSubject" parameterType="com.pangu.base.domain.Subject">
        UPDATE pg_subject
        <set>
            <if test="subjectName != null and subjectName != ''">subject_name = #{subjectName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = #{updateTime}
        </set>
        WHERE subject_id = #{subjectId}
    </update>

    <update id="deleteSubjectById" parameterType="Long">
        UPDATE pg_subject SET del_flag = '1' WHERE subject_id = #{subjectId}
    </update>

</mapper>
