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

    <resultMap type="com.pcloud.book.group.entity.ResourcePageCourse" id="ResourcePageCourseMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="serveId" column="serve_id" jdbcType="INTEGER"/>
        <result property="serveName" column="serve_name" jdbcType="VARCHAR"/>
        <result property="serveType" column="serve_type" jdbcType="VARCHAR"/>
        <result property="typeCode" column="type_code" jdbcType="VARCHAR"/>
        <result property="typeName" column="type_name" jdbcType="VARCHAR"/>
        <result property="channelId" column="channel_id" jdbcType="INTEGER"/>
        <result property="agentId" column="agent_id" jdbcType="INTEGER"/>
        <result property="proLabelId" column="pro_label_id" jdbcType="INTEGER"/>
        <result property="depLableId" column="dep_lable_id" jdbcType="INTEGER"/>
        <result property="purLabelId" column="pur_label_id" jdbcType="INTEGER"/>
        <result property="browseCount" column="browse_count" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="ResourcePageCourseMap">
        select
          id, serve_id, serve_name, serve_type, type_code,type_name, channel_id, agent_id, pro_label_id, dep_lable_id, pur_label_id, browse_count, create_time
        from book.resource_page_course
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_course(serve_id, serve_name, serve_type, type_code, type_name, channel_id,
        agent_id, pro_label_id, dep_lable_id, pur_label_id, browse_count, create_time)
        values (#{serveId}, #{serveName}, #{serveType}, #{typeCode}, #{typeName}, #{channelId},
         #{agentId}, #{proLabelId}, #{depLableId}, #{purLabelId}, #{browseCount}, now())
    </insert>

    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_course(serve_id, serve_name, serve_type, type_code, type_name,
        channel_id, agent_id, pro_label_id, dep_lable_id, pur_label_id, browse_count, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.serveId}, #{item.serveName}, #{item.serveType}, #{item.typeCode},#{item.typeName},
            #{item.channelId}, #{item.agentId}, #{item.proLabelId}, #{item.depLableId}, #{item.purLabelId},
            #{item.browseCount}, NOW())
        </foreach>
    </insert>

    <delete id="deleteAll">
        delete from book.resource_page_course where id >0
    </delete>

    <!--通过主键修改数据-->
    <update id="update">
        update book.resource_page_course
        <set>
            <if test="serveId != null">
                serve_id = #{serveId},
            </if>
            <if test="serveName != null and serveName != ''">
                serve_name = #{serveName},
            </if>
            <if test="serveType != null and serveType != ''">
                serve_type = #{serveType},
            </if>
            <if test="typeCode != null and typeCode != ''">
                type_code = #{typeCode},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId},
            </if>
            <if test="proLabelId != null">
                pro_label_id = #{proLabelId},
            </if>
            <if test="depLableId != null">
                dep_lable_id = #{depLableId},
            </if>
            <if test="purLabelId != null">
                pur_label_id = #{purLabelId},
            </if>
            <if test="browseCount != null">
                browse_count = #{browseCount},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        where id = #{id}
    </update>

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

    <select id="listCourseByGradeLabelId" resultType="com.pcloud.book.group.dto.ResourcePageCourseDTO" parameterType="map">
        SELECT
            c.serve_id serveId,
            IFNULL(m.serve_name, c.serve_name) serveName,
            c.serve_type serveType,
            c.type_code typeCode,
            c.channel_id channelId,
            c.type_name typeName,
            c.pro_label_id proLabelId,
            c.dep_lable_id depLabelId,
            c.pur_label_id purLabelId,
            c.browse_count browseCount,
            m.cover_img servePic
        FROM
            resource_page_course c
        INNER JOIN resource_page_grade_label g ON c.dep_lable_id = g.dep_label_id
        LEFT JOIN resource_recom_manage m ON c.serve_id = m.serve_id
        AND m.source_from = #{sourceFrom}
        AND c.type_code = m.type_code
        WHERE
            g.user_grade_label_id = #{userGradeLabelId}
        AND IFNULL(m.show_state, 1) = 1
        GROUP BY
            c.serve_id
        ORDER BY
            c.browse_count DESC
    </select>

    <select id="getGradeByGradeLabelId" resultType="long" parameterType="long">
        SELECT
            user_grade_label_id
        FROM
            resource_page_grade_label
        WHERE
            gra_label_id = #{graLabelId}
        LIMIT 1
    </select>

    <select id="getGradeByDepLabelId" parameterType="long" resultType="long">
        SELECT
            user_grade_label_id
        FROM
            resource_page_grade_label
        WHERE
            dep_label_id = #{depLabelId}
        LIMIT 1
    </select>

    <select id="listCourse4Pcloud" resultType="com.pcloud.book.group.dto.ResourcePageCourseDTO" parameterType="map">
        SELECT
            c.serve_id serveId,
            c.serve_type serveType,
            c.type_code typeCode,
            c.type_name typeName,
            c.pro_label_id proLabelId,
            c.dep_lable_id depLabelId,
            c.pur_label_id purLabelId,
            c.browse_count browseCount,
            m.cover_img servePic,
            IFNULL(m.serve_name, c.serve_name) serveName,
            IFNULL(m.show_state, 1) showState
        FROM
        resource_page_course c
        LEFT JOIN resource_recom_manage m ON c.serve_id = m.serve_id
        AND m.source_from = #{sourceFrom}
        AND c.type_code = m.type_code
        WHERE
        1 = 1
        <if test="query != null">
        AND IFNULL(m.serve_name, c.serve_name) LIKE CONCAT("%", #{query}, "%")
        </if>
        GROUP BY
        c.serve_id
        ORDER BY
        c.browse_count DESC
    </select>

</mapper>