<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.push.dao.impl.PushItemDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.PushItem">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="push_id" property="pushId" jdbcType="BIGINT"/>
        <result column="item_type" property="itemType" jdbcType="INTEGER"/>
        <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
        <result column="text_content" property="textContent" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="link_title" property="linkTitle" jdbcType="VARCHAR"/>
        <result column="link_description" property="linkDescription" jdbcType="VARCHAR"/>
        <result column="link_image_url" property="linkImageUrl" jdbcType="VARCHAR"/>
        <result column="image_url" property="imageUrl" jdbcType="VARCHAR"/>
        <result column="app_id" property="appId" jdbcType="BIGINT"/>
        <result column="product_id" property="productId" jdbcType="BIGINT"/>
        <result column="app_url" property="appUrl" jdbcType="VARCHAR"/>
        <result column="product_url" property="productUrl" jdbcType="VARCHAR"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="BIGINT"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, push_id, item_type, seq_num, text_content, link_url, link_title, link_description, link_image_url,
        image_url, app_id, product_id, app_url, product_url, is_delete, create_user, create_time, update_user, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from push_item
        where is_delete=0 and id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="java.lang.Long">
        update push_item set is_delete = 1
        where id = #{id,jdbcType=BIGINT} and is_delete = 0
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.push.entity.PushItem" useGeneratedKeys="true" keyProperty="id">
        insert into push_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            push_id,
            item_type,
            seq_num,
            text_content,
            link_url,
            link_title,
            link_description,
            link_image_url,
            image_url,
            app_id,
            product_id,
            app_url,
            product_url,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{pushId,jdbcType=BIGINT},
            #{itemType,jdbcType=INTEGER},
            #{seqNum,jdbcType=INTEGER},
            #{textContent,jdbcType=VARCHAR},
            #{linkUrl,jdbcType=VARCHAR},
            #{linkTitle,jdbcType=VARCHAR},
            #{linkDescription,jdbcType=VARCHAR},
            #{linkImageUrl,jdbcType=VARCHAR},
            #{imageUrl,jdbcType=VARCHAR},
            #{appId,jdbcType=BIGINT},
            #{productId,jdbcType=BIGINT},
            #{appUrl,jdbcType=VARCHAR},
            #{productUrl,jdbcType=VARCHAR},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.push.entity.PushItem">
        update push_item
        <set>
            <if test="pushId != null">
                push_id = #{pushId,jdbcType=BIGINT},
            </if>
            <if test="itemType != null">
                item_type = #{itemType,jdbcType=INTEGER},
            </if>
            <if test="seqNum != null">
                seq_num = #{seqNum,jdbcType=INTEGER},
            </if>
            <if test="textContent != null">
                text_content = #{textContent,jdbcType=VARCHAR},
            </if>
            <if test="linkUrl != null">
                link_url = #{linkUrl,jdbcType=VARCHAR},
            </if>
            <if test="linkTitle != null">
                link_title = #{linkTitle,jdbcType=VARCHAR},
            </if>
            <if test="linkDescription != null">
                link_description = #{linkDescription,jdbcType=VARCHAR},
            </if>
            <if test="linkImageUrl != null">
                link_image_url = #{linkImageUrl,jdbcType=VARCHAR},
            </if>
            <if test="imageUrl != null">
                image_url = #{imageUrl,jdbcType=VARCHAR},
            </if>
            <if test="appId != null">
                app_id = #{appId,jdbcType=BIGINT},
            </if>
            <if test="productId != null">
                product_id = #{productId,jdbcType=BIGINT},
            </if>
            <if test="appUrl != null">
                app_url = #{appUrl,jdbcType=VARCHAR},
            </if>
            <if test="productUrl != null">
                product_url = #{productUrl,jdbcType=VARCHAR},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=BOOLEAN},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=BIGINT},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.push.entity.PushItem" useGeneratedKeys="true" keyProperty="id">
        insert into push_item (
            push_id,
            item_type,
            seq_num,
            text_content,
            link_url,
            link_title,
            link_description,
            link_image_url,
            image_url,
            app_id,
            product_id,
            app_url,
            product_url,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
         (
            #{item.pushId,jdbcType=BIGINT},
            #{item.itemType,jdbcType=INTEGER},
            #{item.seqNum,jdbcType=INTEGER},
            #{item.textContent,jdbcType=VARCHAR},
            #{item.linkUrl,jdbcType=VARCHAR},
            #{item.linkTitle,jdbcType=VARCHAR},
            #{item.linkDescription,jdbcType=VARCHAR},
            #{item.linkImageUrl,jdbcType=VARCHAR},
            #{item.imageUrl,jdbcType=VARCHAR},
            #{item.appId,jdbcType=BIGINT},
            #{item.productId,jdbcType=BIGINT},
            #{item.appUrl,jdbcType=VARCHAR},
            #{item.productUrl,jdbcType=VARCHAR},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
          )
        </foreach>
    </insert>

    <!--根据pushId删除群发消息项-->
    <update id="deleteByPushId" parameterType="map">
        UPDATE
        push_item
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE push_id =#{pushId}
    </update>

    <!--删除群发消息项-->
    <update id="deleteByPushItemId" parameterType="map">
        UPDATE
        push_item
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE id =#{pushItemId}
    </update>

    <!--获取最大排序值-->
    <select id="getMaxSeqNumByPushId" parameterType="long" resultType="Integer">
        select IFNULL(max(seq_num),0) from push_item where is_delete=0 and push_id=#{pushId}
    </select>

    <!--排序-->
    <update id="sortPushItems" parameterType="map">
        update push_item
        set seq_num =
        case
        <foreach collection="pushItemIds" item="item" index="index">
            WHEN id=#{item} THEN #{index}+1
        </foreach>
        end,
        update_time=NOW(),
        update_user=#{partyId}
        where is_delete=0
        and id in
        <foreach collection="pushItemIds" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
    </update>

    <!--根据群发id集合查询-->
    <select id="getListByPushIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push_item
        where is_delete=0
        and push_id in
        <foreach collection="pushIds" item="pushId" open="("  separator="," close=")">
            #{pushId}
        </foreach>
        order by push_id asc, seq_num asc
    </select>

    <!--根据群发id获取数量-->
    <select id="getCountByPushId" parameterType="Long" resultType="Integer">
        select count(1) from push_item where is_delete=0 and push_id = #{pushId}
    </select>

    <!--根据群发id查询群发项集合-->
    <select id="getListByPushId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push_item
        where is_delete=0
        and push_id =#{pushId}
    </select>

    <!--根据群发id集合查询所有(包括删除了的)-->
    <select id="getAllListByPushIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push_item
        where
        push_id in
        <foreach collection="pushIds" item="pushId" open="("  separator="," close=")">
            #{pushId}
        </foreach>
        order by push_id asc, seq_num asc
    </select>
</mapper>