<?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.salpa.subject.mapper.SubjectMapper">

    <resultMap type="com.salpa.subject.domain.MonitorSubject" id="MonitorSubjectResult">
        <id     property="id"            column="id"             />
        <result property="subjectName"          column="subject_name"           />
        <result property="subjectDescription"   column="subject_description"    />
        <result property="deleted"              column="deleted"                />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="getList" resultMap="MonitorSubjectResult">
        SELECT id,subject_name,subject_description,deleted,create_at FROM monitor_subject WHERE deleted = 0
        <if test="subjectName != null and subjectName != ''">
            AND subject_name like concat('%', #{subjectName}, '%')
        </if>
    </select>

    <select id="getById" resultMap="MonitorSubjectResult">
        SELECT id,subject_name,subject_description,deleted,create_at FROM monitor_subject WHERE id = #{id}
    </select>

    <select id="checkSubjectNameUnique" resultType="int">
        SELECT count(1) from monitor_subject where subject_name = #{subjectName} and deleted = 0 limit 1
    </select>

    <select id="selectCount" resultType="int">
        SELECT COUNT(*) FROM monitor_project WHERE subject_id = #{id} AND deleted = 0
    </select>

    <insert id="addSubject" parameterType="MonitorSubject">
        insert into monitor_subject(
        <if test="subjectName != null and subjectName != ''">subject_name,</if>
        <if test="subjectDescription != null and subjectDescription != ''">subject_description,</if>
        deleted
        )values(
        <if test="subjectName != null and subjectName != ''">#{subjectName},</if>
        <if test="subjectDescription != null and subjectDescription != ''">#{subjectDescription},</if>
        0
        )
    </insert>

    <update id="updateSubject">
        UPDATE monitor_subject SET subject_name = #{subjectName},subject_description = #{subjectDescription}
        WHERE id = #{id}
    </update>

    <update id="deleteSubject">
        UPDATE monitor_subject SET deleted = 1 WHERE id = #{id}
    </update>
</mapper>