<?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.PushDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.Push">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="push_type" property="pushType" jdbcType="INTEGER"/>
        <result column="push_time" property="pushTime" jdbcType="VARCHAR"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="week_day" property="weekDay" jdbcType="INTEGER"/>
        <result column="push_plan_id" property="pushPlanId" jdbcType="BIGINT"/>
        <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_type, push_time, start_time, end_time, week_day, push_plan_id, 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
        where is_delete=0 and id = #{id,jdbcType=BIGINT}
    </select>

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

    <insert id="insert" parameterType="com.pcloud.book.push.entity.Push" useGeneratedKeys="true" keyProperty="id">
        insert into push
        <trim prefix="(" suffix=")" suffixOverrides=",">
            push_type,
            push_time,
            start_time,
            end_time,
            week_day,
            push_plan_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{pushType,jdbcType=INTEGER},
            #{pushTime,jdbcType=VARCHAR},
            #{startTime,jdbcType=TIMESTAMP},
            #{endTime,jdbcType=TIMESTAMP},
            #{weekDay,jdbcType=INTEGER},
            #{pushPlanId,jdbcType=BIGINT},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.push.entity.Push">
        update push
        <set>
            <if test="pushType != null">
                push_type = #{pushType,jdbcType=INTEGER},
            </if>
            <if test="pushTime != null">
                push_time = #{pushTime,jdbcType=VARCHAR},
            </if>
            <if test="startTime != null">
                start_time = #{startTime,jdbcType=TIMESTAMP},
            </if>
            <if test="endTime != null">
                end_time = #{endTime,jdbcType=TIMESTAMP},
            </if>
            <if test="weekDay != null">
                week_day = #{weekDay,jdbcType=INTEGER},
            </if>
            <if test="pushPlanId != null">
                push_plan_id = #{pushPlanId,jdbcType=BIGINT},
            </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>

    <!--删除群发-->
    <update id="deleteByPushId" parameterType="map">
        update push set
        is_delete = 1,
        update_user=#{partyId},
        update_time=now()
        where id = #{pushId} and is_delete = 0
    </update>

    <!--查询群发消息集合-->
    <select id="getTimingPushList" parameterType="map" resultType="com.pcloud.book.push.dto.PushDTO">
        select t.id,
        t.push_type pushType,
        t.push_time pushTime,
        t.is_delete isDelete,
        t.create_user createUser,
        t.create_time createTime,
        t.update_user updateUser,
        t.update_time updateTime,
        COUNT(DISTINCT t1.book_id) bookCount,
        COUNT(DISTINCT t1.book_group_qrcode_id) groupCount
        FROM
        push t
        LEFT JOIN push_group t1 ON t.id = t1.push_id
        WHERE
        t.is_delete = 0
        AND t1.is_delete = 0
        AND (t.push_type=2 and t.push_time>NOW() or t.push_type=3)
        AND t.create_user=#{partyId}
        group by t.id
        order by t.create_time desc
    </select>

    <!--根据id查询-->
    <select id="getByPushId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push
        where is_delete=0
        and id=#{pushId}
    </select>

    <!--修改群发时间-->
    <update id="updatePushTime" parameterType="com.pcloud.book.push.entity.Push">
        update push set
        push_time= #{pushTime},
        push_type= #{pushType},
        update_user=#{updateUser},
        update_time=now()
        where id = #{id} and is_delete = 0
    </update>

    <!--根据计划id查询-->
    <select id="getByPushPlanId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push
        where is_delete=0 and push_plan_id=#{pushPlanId}
    </select>

    <!--根据计划id集合查询-->
    <select id="getByPushPlanIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push
        where is_delete=0 and push_plan_id in
        <foreach collection="pushPlanIds" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!--根据创建人获取计划式群发-->
    <select id="getPlanPushListPartyId" parameterType="Long" resultMap="BaseResultMap">
        select
        t1.id,
        t1.push_type,
        t1.push_time,
        t1.start_time,
        t1.end_time,
        t1.week_day,
        t1.push_plan_id,
        t1.is_delete,
        t1.create_user,
        t1.create_time,
        t1.update_user,
        t1.update_time
        from push t1
        inner join push_plan t2 on t1.push_plan_id=t2.id
        where t2.create_user=#{partyId}
    </select>
</mapper>