<?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.group.dao.impl.ResourcePageCollectDaoImpl">

    <resultMap type="com.pcloud.book.group.entity.ResourcePageCollect" id="ResourcePageCollectMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="resourcePageId" column="resource_page_id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="officialAccountsId" column="official_accounts_id" jdbcType="INTEGER"/>
        <result property="collectState" column="collect_state" jdbcType="INTEGER"/>
        <result property="isNew" column="is_new" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="ResourcePageCollectMap">
        select
          id, resource_page_id, wechat_user_id, official_accounts_id, collect_state, is_new, create_time
        from book.resource_page_collect
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_collect(resource_page_id, wechat_user_id, official_accounts_id, collect_state, is_new, create_time)
        values (#{resourcePageId}, #{wechatUserId}, #{officialAccountsId}, #{collectState}, #{isNew}, now())
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.resource_page_collect
        <set>
            <if test="resourcePageId != null">
                resource_page_id = #{resourcePageId},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="officialAccountsId != null">
                official_accounts_id = #{officialAccountsId},
            </if>
            <if test="collectState != null">
                collect_state = #{collectState},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.resource_page_collect where id = #{id}
    </delete>

    <select id="getCollectRecordByUser" resultMap="ResourcePageCollectMap" parameterType="map">
        select
          id, resource_page_id, wechat_user_id, official_accounts_id, collect_state, create_time
        from book.resource_page_collect
        where resource_page_id = #{resourcePageId}
        and wechat_user_id = #{wechatUserId}
        and collect_state = 1
        limit 1
    </select>
    

    <select id="getUserIsNotNew" parameterType="long" resultType="integer">
        select
          COUNT(1)
        from book.resource_page_collect
        where
        wechat_user_id = #{wechatUserId}
        and is_new = 0
    </select>

</mapper>