<?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.zone.journals.mapper.ZoneJournalsMapper">

    <resultMap type="com.microservices.zone.journals.domain.ZoneJournals" id="ZoneJournalsResult">
        <result property="id"    column="id"    />
        <result property="journalizedId"    column="journalized_id"    />
        <result property="journalizedType"    column="journalized_type"    />
        <result property="userId"    column="user_id"    />
        <result property="notes"    column="notes"    />
        <result property="parentId"    column="parent_id"    />
        <result property="replyId"    column="reply_id"    />
        <result property="commentsCount"    column="comments_count"    />
        <result property="attachmentIdentifiers" column="attachment_identifiers"/>
        <result property="receiversLogin"    column="receivers_login"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createdOn"    column="created_on"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updatedOn"    column="updated_on"    />
    </resultMap>

    <sql id="selectZoneJournalsVo">
        select id,
               journalized_id,
               journalized_type,
               user_id,
               notes,
               parent_id,
               reply_id,
               comments_count,
               attachment_identifiers,
               receivers_login,
               del_flag,
               create_by,
               created_on,
               update_by,
               updated_on
        from zone_journals
    </sql>

    <select id="selectZoneJournalsList" parameterType="com.microservices.zone.journals.domain.ZoneJournals" resultMap="ZoneJournalsResult">
        <include refid="selectZoneJournalsVo"/>
        <where>
            <if test="journalizedId != null "> and journalized_id = #{journalizedId}</if>
            <if test="journalizedType != null  and journalizedType != ''"> and journalized_type = #{journalizedType}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            and del_flag = '0'
            and parent_id is NULL
        </where>
        order by created_on desc
    </select>

    <select id="selectZoneJournalsById" parameterType="Long" resultMap="ZoneJournalsResult">
        <include refid="selectZoneJournalsVo"/>
        where id = #{id}
    </select>

    <select id="selectChildrenZoneJournalsList" resultMap="ZoneJournalsResult">
        <include refid="selectZoneJournalsVo"/>
        where parent_id = #{parentId}
          and del_flag = '0'
        order by created_on asc
    </select>

    <select id="countByJournalized" resultType="int">
        select count(*)
        from zone_journals
        where journalized_id = #{journalizedId}
          and journalized_type = #{journalizedType}
          and del_flag = '0'
    </select>

    <insert id="insertZoneJournals" parameterType="com.microservices.zone.journals.domain.ZoneJournals" useGeneratedKeys="true" keyProperty="id">
        insert into zone_journals
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="journalizedId != null">journalized_id,</if>
            <if test="journalizedType != null and journalizedType != ''">journalized_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="notes != null and notes != ''">notes,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="replyId != null">reply_id,</if>
            <if test="commentsCount != null">comments_count,</if>
            <if test="attachmentIdentifiers != null">attachment_identifiers,</if>
            <if test="receiversLogin != null">receivers_login,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createdOn != null">created_on,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updatedOn != null">updated_on,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="journalizedId != null">#{journalizedId},</if>
            <if test="journalizedType != null and journalizedType != ''">#{journalizedType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="notes != null and notes != ''">#{notes},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="replyId != null">#{replyId},</if>
            <if test="commentsCount != null">#{commentsCount},</if>
            <if test="attachmentIdentifiers != null">#{attachmentIdentifiers},</if>
            <if test="receiversLogin != null">#{receiversLogin},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createdOn != null">#{createdOn},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updatedOn != null">#{updatedOn},</if>
        </trim>
    </insert>

    <update id="updateZoneJournals" parameterType="com.microservices.zone.journals.domain.ZoneJournals">
        update zone_journals
        <trim prefix="SET" suffixOverrides=",">
            <if test="notes != null and notes != ''">notes = #{notes},</if>
            <if test="attachmentIdentifiers != null">attachment_identifiers = #{attachmentIdentifiers},</if>
            <if test="receiversLogin != null">receivers_login = #{receiversLogin},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updatedOn != null">updated_on = #{updatedOn},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateCommentsCountById">
        UPDATE zone_journals
        SET comments_count = (
            SELECT COUNT(*)
            FROM (
                     SELECT 1
                     FROM zone_journals
                     WHERE parent_id = #{id}
                       AND del_flag = '0'
                 ) AS tmp
        )
        WHERE id = #{id}
    </update>

    <update id="updateJournalsIdByTypeAndOldId">
        update zone_journals
        set journalized_id = #{journalId}
        where journalized_type = #{type}
          and journalized_id = #{oldJournalId}
    </update>

    <update id="deleteZoneJournalsById" parameterType="Long">
        update zone_journals set del_flag = '2' where id = #{id}
    </update>

    <update id="deleteZoneJournalsByIds" parameterType="String">
        update zone_journals set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="deleteZoneJournalsByParentId">
        update zone_journals set del_flag = '2' where parent_id = #{parentId}
    </update>

    <update id="deleteZoneJournalsByTypeAndJournalId">
        update zone_journals set del_flag = '2'
        where journalized_id = #{journalId}
          and journalized_type = #{type}
    </update>
</mapper>
