<?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.microservices.pms.journals.mapper.PmsJournalsMapper">
    
    <resultMap type="PmsJournals" id="PmsJournalsResult">
        <result property="id"    column="id"    />
        <result property="journalized_id"    column="journalized_id"    />
        <result property="journalized_type"    column="journalized_type"    />
        <result property="user_id"    column="user_id"    />
        <result property="notes"    column="notes"    />
        <result property="created_on"    column="created_on"    />
        <result property="private_notes"    column="private_notes"    />
        <result property="parent_id"    column="parent_id"    />
        <result property="comments_count"    column="comments_count"    />
        <result property="reply_id"    column="reply_id"    />
        <result property="review_id"    column="review_id"    />
        <result property="commit_id"    column="commit_id"    />
        <result property="diff"    column="diff"    />
        <result property="line_code"    column="line_code"    />
        <result property="path"    column="path"    />
        <result property="attachment_identifiers" column="attachment_identifiers"/>
        <result property="state"    column="state"    />
        <result property="resolve_at"    column="resolve_at"    />
        <result property="resolveer_id"    column="resolveer_id"    />
        <result property="need_respond"    column="need_respond"    />
        <result property="updated_on"    column="updated_on"    />
    </resultMap>

    <sql id="selectPmsJournalsVo">
        select id,
               journalized_id,
               journalized_type,
               user_id,
               notes,
               created_on,
               private_notes,
               parent_id,
               comments_count,
               reply_id,
               review_id,
               commit_id,
               diff,
               line_code,
               path,
               attachment_identifiers,
               state,
               resolve_at,
               resolveer_id,
               need_respond,
               updated_on
        from pms_journals
    </sql>

    <select id="selectPmsJournalsList" parameterType="PmsJournals" resultMap="PmsJournalsResult">
        <include refid="selectPmsJournalsVo"/>
        <where>  
            <if test="journalized_id != null "> and journalized_id = #{journalized_id}</if>
            <if test="journalized_type != null  and journalized_type != ''"> and journalized_type = #{journalized_type}</if>
             and parent_id is NULL
        </where>
        order by created_on asc
    </select>
    
    <select id="selectPmsJournalsById" parameterType="Long" resultMap="PmsJournalsResult">
        <include refid="selectPmsJournalsVo"/>
        where id = #{id}
    </select>
    <select id="selectChildrenPmsJournalsList" resultMap="PmsJournalsResult">
        <include refid="selectPmsJournalsVo"/>
        where parent_id =#{parentId}
        order by created_on asc
    </select>

    <insert id="insertPmsJournals" parameterType="PmsJournals" useGeneratedKeys="true" keyProperty="id">
        insert into pms_journals
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="journalized_id != null">journalized_id,</if>
            <if test="journalized_type != null and journalized_type != ''">journalized_type,</if>
            <if test="user_id != null">user_id,</if>
            <if test="notes != null">notes,</if>
            <if test="created_on != null">created_on,</if>
            <if test="private_notes != null">private_notes,</if>
            <if test="parent_id != null">parent_id,</if>
            <if test="comments_count != null">comments_count,</if>
            <if test="reply_id != null">reply_id,</if>
            <if test="review_id != null">review_id,</if>
            <if test="commit_id != null">commit_id,</if>
            <if test="diff != null">diff,</if>
            <if test="line_code != null">line_code,</if>
            <if test="path != null">path,</if>
            <if test="attachment_identifiers != null">attachment_identifiers,</if>
            <if test="state != null">state,</if>
            <if test="resolve_at != null">resolve_at,</if>
            <if test="resolveer_id != null">resolveer_id,</if>
            <if test="need_respond != null">need_respond,</if>
            <if test="updated_on != null">updated_on,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="journalized_id != null">#{journalized_id},</if>
            <if test="journalized_type != null and journalized_type != ''">#{journalized_type},</if>
            <if test="user_id != null">#{user_id},</if>
            <if test="notes != null">#{notes},</if>
            <if test="created_on != null">#{created_on},</if>
            <if test="private_notes != null">#{private_notes},</if>
            <if test="parent_id != null">#{parent_id},</if>
            <if test="comments_count != null">#{comments_count},</if>
            <if test="reply_id != null">#{reply_id},</if>
            <if test="review_id != null">#{review_id},</if>
            <if test="commit_id != null">#{commit_id},</if>
            <if test="diff != null">#{diff},</if>
            <if test="line_code != null">#{line_code},</if>
            <if test="path != null">#{path},</if>
            <if test="attachment_identifiers != null">#{attachment_identifiers},</if>
            <if test="state != null">#{state},</if>
            <if test="resolve_at != null">#{resolve_at},</if>
            <if test="resolveer_id != null">#{resolveer_id},</if>
            <if test="need_respond != null">#{need_respond},</if>
            <if test="updated_on != null">#{updated_on},</if>
         </trim>
    </insert>

    <update id="updatePmsJournals" parameterType="PmsJournals">
        update pms_journals
        <trim prefix="SET" suffixOverrides=",">
            <if test="journalized_id != null">journalized_id = #{journalized_id},</if>
            <if test="journalized_type != null and journalized_type != ''">journalized_type = #{journalized_type},</if>
            <if test="user_id != null">user_id = #{user_id},</if>
            <if test="notes != null">notes = #{notes},</if>
            <if test="created_on != null">created_on = #{created_on},</if>
            <if test="private_notes != null">private_notes = #{private_notes},</if>
            <if test="parent_id != null">parent_id = #{parent_id},</if>
            <if test="comments_count != null">comments_count = #{comments_count},</if>
            <if test="reply_id != null">reply_id = #{reply_id},</if>
            <if test="review_id != null">review_id = #{review_id},</if>
            <if test="commit_id != null">commit_id = #{commit_id},</if>
            <if test="diff != null">diff = #{diff},</if>
            <if test="line_code != null">line_code = #{line_code},</if>
            <if test="path != null">path = #{path},</if>
            <if test="attachment_identifiers != null">attachment_identifiers = #{attachment_identifiers},</if>
            <if test="state != null">state = #{state},</if>
            <if test="resolve_at != null">resolve_at = #{resolve_at},</if>
            <if test="resolveer_id != null">resolveer_id = #{resolveer_id},</if>
            <if test="need_respond != null">need_respond = #{need_respond},</if>
            <if test="updated_on != null">updated_on = #{updated_on},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsJournalsById" parameterType="Long">
        delete from pms_journals where id = #{id}
    </delete>

    <delete id="deletePmsJournalsByIds" parameterType="String">
        delete from pms_journals where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsJournalsByTypeAndJournalId">
        delete
        from pms_journals
        where journalized_id = #{journalId}
          and journalized_type = #{type}
    </delete>
</mapper>