<?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="INTEGER"/>
        <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"/>
    </resultMap>

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

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

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM serve_collect
        where
        wechat_user_id = #{wechatUserId}
        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,
            link_url,
            pic_url,
            is_delete, 
            create_time, 
            update_time, 
            collect_day
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{serveId, jdbcType=BIGINT},
            #{serveName},
            #{serveType, jdbcType=INTEGER}, 
            #{serveTypeCode, jdbcType=VARCHAR},
            #{linkUrl, jdbcType=VARCHAR},
            #{picUrl},
            #{isDelete, jdbcType=INTEGER}, 
            now(),
            now(),
            CURDATE()
        ) on duplicate key
        update
        update_time = now(),
        collect_day =  CURDATE()
        <if test="isDelete != null">
            , is_delete = #{isDelete}
        </if>
    </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
        ) 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()
            )
        </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="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="list" resultType="long">
        select
        id
        from
        serve_collect
        where
        is_delete = 0
        and
        wechat_user_id = #{wechatUserId}
    </select>
</mapper>