<?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.PushRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.PushRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="push_id" property="pushId" jdbcType="BIGINT"/>
        <result column="push_time" property="pushTime" jdbcType="BIGINT"/>
        <result column="push_type" property="pushType" jdbcType="INTEGER"/>
        <result column="push_status" property="pushStatus" jdbcType="INTEGER"/>
        <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>

    <resultMap id="BaseResultDTOMap" type="com.pcloud.book.push.dto.PushRecordDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="push_id" property="pushId" jdbcType="BIGINT"/>
        <result column="push_time" property="pushTime" jdbcType="BIGINT"/>
        <result column="push_type" property="pushType" jdbcType="INTEGER"/>
        <result column="push_status" property="pushStatus" jdbcType="INTEGER"/>
        <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"/>
        <result column="group_count" property="groupCount" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, push_id, push_time, push_type, push_status, 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_record
        where id = #{id,jdbcType=BIGINT}
    </select>

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

    <insert id="insert" parameterType="com.pcloud.book.push.entity.PushRecord" useGeneratedKeys="true" keyProperty="id">
        insert into push_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            push_id,
            push_time,
            push_type,
            push_status,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{pushId,jdbcType=BIGINT},
            #{pushTime,jdbcType=VARCHAR},
            #{pushType,jdbcType=INTEGER},
            #{pushStatus,jdbcType=INTEGER},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.push.entity.PushRecord">
        update push_record
        <set>
            <if test="pushId != null">
                push_id = #{pushId,jdbcType=BIGINT},
            </if>
            <if test="pushTime != null">
                push_time = #{pushTime,jdbcType=VARCHAR},
            </if>
            <if test="pushType != null">
                push_type = #{pushType,jdbcType=INTEGER},
            </if>
            <if test="pushStatus != null">
                push_status = #{pushStatus,jdbcType=INTEGER},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=BIGINT},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--更新记录状态-->
    <update id="updateStateById" parameterType="map" >
        update push_record set
        push_status = #{pushStatus}
        where id=#{pushRecordId}
    </update>

    <!--获取群发记录集合-->
    <select id="getPushRecordList" parameterType="map" resultMap="BaseResultDTOMap">
        SELECT
        t.id,
        t.push_id,
        t.push_time,
        t.push_type,
        t.push_status,
        t.is_delete,
        t.create_user,
        t.create_time,
        t.update_user,
        t.update_time,
        COUNT(DISTINCT t1.book_group_qrcode_id ) group_count
        FROM
        push_record t
        LEFT JOIN push_group t1 ON t.push_id = t1.push_id
        WHERE
        t.is_delete = 0
        AND t.create_user = #{partyId}
        GROUP BY
        t.id
        order by t.create_time desc
    </select>
</mapper>