<?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.pcloud.book.applet.dao.impl.AppletSubscribeCountDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletSubscribeCount">
        <id column="id" property="id" jdbcType="OTHER"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="OTHER"/>
        <result column="record_type" property="recordType" jdbcType="OTHER"/>
        <result column="valid_count" property="validCount" jdbcType="OTHER"/>
        <result column="create_time" property="createTime" jdbcType="OTHER"/>
        <result column="update_time" property="updateTime" jdbcType="OTHER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, wechat_user_id, record_type, valid_count, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_subscribe_count
        WHERE id = #{id}
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_subscribe_count
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_subscribe_count(
            wechat_user_id, 
            record_type,
            valid_count,
            create_time, 
            update_time
        ) VALUES (
            #{wechatUserId, jdbcType=OTHER}, 
            #{recordType, jdbcType=OTHER},
            #{validCount, jdbcType=OTHER},
            now(),
            now()
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO applet_subscribe_count (
        wechat_user_id,
        record_type,
        valid_count,
        create_time,
        update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.wechatUserId, jdbcType=OTHER},
            #{item.recordType, jdbcType=OTHER},
            #{item.validCount, jdbcType=OTHER},
            #{item.createTime, jdbcType=OTHER},
            #{item.updateTime, jdbcType=OTHER}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_subscribe_count
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="recordType != null">
                record_type = #{recordType},
            </if>
            <if test="validCount != null">
                valid_count = #{validCount},
            </if>
                update_time = now()
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM applet_subscribe_count where id = #{id}
    </delete>

    <!--通过主键软删除-->
    <delete id="softDeleteById">
        update applet_subscribe_count set is_delete  = 1, update_time = now() where id = #{id}
    </delete>

    <select id="getByUserId" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        applet_subscribe_count
        where
        wechat_user_id  = #{wechatUserId}
        <if test="recordType != null">
            and record_type = #{recordType}
        </if>
    </select>

    <select id="clearSubscribeCount" parameterType="map">
        update
        applet_subscribe_count
        set
        valid_count = 0,
        update_time = now()
        where
        wechat_user_id  = #{wechatUserId}
        <if test="recordType != null">
            and record_type = #{recordType}
        </if>
    </select>

</mapper>