<?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.recommend.dao.impl.ResourceRecommendDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.recommend.entity.ResourceRecommend">
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="product_or_app_id" property="productOrAppId" jdbcType="INTEGER"/>
        <result column="server_type" property="serverType" jdbcType="VARCHAR"/>
        <result column="resource_limit_type" property="resourceLimitType" jdbcType="INTEGER"/>
        <result column="recommend_list" property="recommendList" jdbcType="VARCHAR"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        adviser_id, product_or_app_id, server_type, resource_limit_type, recommend_list, update_time        
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM resource_recommend
        WHERE adviser_id = #{adviserId}
    </select>
    
    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM resource_recommend
    </select>

    <insert id="insert" keyProperty="adviserId" useGeneratedKeys="true">
        INSERT INTO resource_recommend(
            recommend_list,
            update_time
        ) VALUES (
            #{recommendList, jdbcType=VARCHAR},
            #{updateTime, jdbcType=TIMESTAMP}
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="adviserId" useGeneratedKeys="true">
        INSERT INTO resource_recommend (
            recommend_list,
            update_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.recommendList, jdbcType=VARCHAR},
            #{item.updateTime, jdbcType=TIMESTAMP}
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE resource_recommend
        <set>
            <if test="recommendList != null and recommendList != ''">
                recommend_list = #{recommendList},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE adviser_id = #{adviserId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM resource_recommend WHERE adviser_id = #{adviserId}
    </delete>

    <select id="getServeRecommend" parameterType="map" resultType="String">
        select recommend_list
        from resource_recommend
        where product_or_app_id=#{serveId}
        and server_type=#{serveType}
        and resource_limit_type=#{fromType}
        and adviser_id=#{adviserId}
        limit 1
    </select>

</mapper>