<?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.ServeCollectDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.ServeCollect">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="INTEGER"/>
        <result column="serve_name" property="serveName" jdbcType="VARCHAR"/>
        <result column="serve_code" property="serveCode" jdbcType="INTEGER"/>
        <result column="serve_type_code" property="serveTypeCode" jdbcType="VARCHAR"/>
        <result column="serve_type_name" property="serveTypeName" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="is_delete" property="isDelete" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="collect_day" property="collectDay" jdbcType="DATE"/>
        <result column="pic_url" property="picUrl" jdbcType="VARCHAR"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
    </resultMap>

    <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.applet.dto.ServeCollectDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="INTEGER"/>
        <result column="serve_name" property="serveName" jdbcType="VARCHAR"/>
        <result column="serve_code" property="serveCode" jdbcType="INTEGER"/>
        <result column="serve_type_code" property="serveTypeCode" jdbcType="VARCHAR"/>
        <result column="serve_type_name" property="serveTypeName" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="is_delete" property="isDelete" jdbcType="INTEGER"/>
        <result column="collect_day" property="collectDay" jdbcType="DATE"/>
        <result column="pic_url" property="picUrl" jdbcType="VARCHAR"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, wechat_user_id, serve_id, serve_name,serve_type, serve_code,serve_type_code ,serve_type_name, link_url, is_delete, create_time, update_time, collect_day,
        pic_url, book_id, adviser_id, channel_id
    </sql>

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

    <select id="getList" resultMap="BaseResultMap" parameterType="map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM serve_collect
        where
        wechat_user_id IN
        <foreach collection="wechatUserIds" separator="," item="item" open="(" close=")" index="index">
            ${item}
        </foreach>
        <if test="typeCode != null">
        and  serve_type_code = #{typeCode}
        </if>
        and
        is_delete = 0
        order by
        update_time desc
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO serve_collect(
            wechat_user_id, 
            serve_id,
            serve_name,
            serve_type, 
            serve_type_Code,
            serve_type_name,
            link_url,
            pic_url,
            is_delete, 
            create_time, 
            update_time, 
            collect_day,
            book_id,
            adviser_id,
            channel_id
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{serveId, jdbcType=BIGINT},
            #{serveName},
            #{serveType, jdbcType=INTEGER}, 
            #{serveTypeCode, jdbcType=VARCHAR},
            #{serveTypeName, jdbcType=VARCHAR},
            #{linkUrl, jdbcType=VARCHAR},
            #{picUrl},
            #{isDelete, jdbcType=INTEGER}, 
            now(),
            now(),
            CURDATE(),
            #{bookId, jdbcType=BIGINT},
            #{adviserId, jdbcType=BIGINT},
            #{channelId, jdbcType=BIGINT}
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO serve_collect (
        wechat_user_id,
        serve_id,
        serve_type,
        serve_code,
        link_url,
        is_delete,
        create_time,
        update_time,
        collect_day,
        book_id,
        adviser_id,
        channel_id
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.wechatUserId, jdbcType=BIGINT},
            #{item.serveId, jdbcType=BIGINT},
            #{item.serveType, jdbcType=INTEGER},
            #{item.serveCode, jdbcType=INTEGER},
            #{item.linkUrl, jdbcType=VARCHAR},
            #{item.isDelete, jdbcType=INTEGER},
            now(),
            now(),
            CURDATE(),
            #{bookId, jdbcType=BIGINT},
            #{adviserId, jdbcType=BIGINT},
            #{channelId, jdbcType=BIGINT}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE serve_collect
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId},
            </if>
            <if test="serveType != null">
                serve_type = #{serveType},
            </if>
            <if test="serveCode != null">
                serve_code = #{serveCode},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="collectDay != null">
                collect_day = #{collectDay},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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

    <update id="cancelCollect" parameterType="list">
        update
        serve_collect
        set
        is_delete = 1,
        update_time = now()
        where
        id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <update id="deleteInvalidNews" parameterType="list">
        update
        serve_collect
        set
        is_delete = 1
        where
        serve_id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and serve_type = 1
    </update>


    <select id="getList4RightsSettingByWechatUserId" parameterType="list" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        serve_collect
       where
            is_delete = 0
            and
            wechat_user_id = #{wechatUserId}
    </select>


    <select id="getList4RightsSetting" parameterType="list" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        serve_collect
        <where>
            is_delete = 0
            and
            <foreach collection="list" item="item" open="(" separator="or" close=")">
                wechat_user_id = #{item.wechatUserId}
                and
                serve_id = #{item.serveId}
                and
                serve_type = #{item.serveType}
            </foreach>
        </where>
    </select>

    <select id="getAllCollect" parameterType="map" resultType="long">
        select
        id
        from
        serve_collect
        where
        is_delete = 0
        <if test="typeCode != null">
        and serve_type_code =#{typeCode}
        </if>
        and
        wechat_user_id in
        <foreach collection="wechatUserIds" separator="," close=")" open="(" index="index" item="item">
            #{item}
        </foreach>
        order by  update_time desc
    </select>


    <select id="getList4Collect" parameterType="com.pcloud.book.applet.dto.ServeCollectDTO" resultMap="BaseResultMap4DTO">
        select
        <include refid="Base_Column_List"/>
        from
        serve_collect
        <where>
            is_delete = 0
            and
            <foreach collection="list" item="item" open="(" separator="or" close=")">
                wechat_user_id = #{item.wechatUserId}
                and
                serve_id = #{item.serveId}
                and
                serve_type = #{item.serveType}
            </foreach>
        </where>
    </select>

    <select id="getTypeNameAndCountByWechatId" parameterType="list" resultType="com.pcloud.book.applet.dto.CollectionTypeNameAndCountDTO">
        SELECT
            serve_type_code as typeCode ,
            serve_type_name as typeName ,
            COUNT(1) as count
        from
            serve_collect
        where
            wechat_user_id in
        <foreach collection="list" separator="," item="item" open="(" close=")" index="index">
            #{item}
        </foreach>
        and
            is_delete = 0
        group by
            serve_type_name,serve_type_code
    </select>
    
    <select id="getListByTypeNameAndWechatUserId" parameterType="map" resultMap="BaseResultMap">
        SELECT
             <include refid="Base_Column_List"/>
        from
            serve_collect
        where
            serve_type_name= #{typeName}
        and
            wechat_user_id = #{wechatUserId}
        and
            is_delete = 0
        order by
            update_time desc
    </select>

    <select id="isCollected" resultMap="BaseResultMap4DTO" parameterType="map">
        Select
            <include refid="Base_Column_List"/>
        from
            serve_collect
        where
            wechat_user_id = #{wechatUserId}
        and
            serve_id = #{serveId}
        and
            serve_type = #{serveType}
        and
            is_delete = 0
        LIMIT 1
    </select>

    <select id="getDistinctTypeCode" resultType="com.pcloud.book.applet.vo.CollectionTypeNameCodeClassifyVO">
        select distinct
            serve_type_code as serveTypeCode,
            serve_type as serveType
        from
            serve_collect
        where
            serve_type_code is not null
    </select>

    <update id="updateTypeName" parameterType="map">
        update
             serve_collect
        set
            serve_type_name= #{serveTypeName}
        where
            serve_type_code =#{serveTypeCode}
    </update>

    <select id="List2ProcessedData" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            serve_collect
        where
            serve_type_code is null
    </select>

    <update id="batchUpdate" parameterType="list">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update serve_collect
            <set>
                update_time = NOW(),
                <if test="item.serveTypeCode != null">
                    serve_type_code = #{item.serveTypeCode},
                </if>
                <if test="item.serveTypeName != null">
                    serve_type_name = #{item.serveTypeName}
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <select id="getCollectByUser" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        serve_collect
        where
        wechat_user_id
        in
        <foreach collection="wechatUserIds" index="index" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and
        serve_id = #{serveId}
        and
        serve_type = #{serveType}
        LIMIT 1
    </select>

    <select id="getCollectList" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    serve_collect
    where
    wechat_user_id
    in
    <foreach collection="wechatUserIds" index="index" item="item" open="(" close=")" separator=",">
        #{item}
    </foreach>
    and
    serve_id = #{serveId}
    and
    serve_type = #{serveType}
    <if test="isDelete != null">
        and is_delete = #{isDelete}
    </if>
    </select>
</mapper>