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

    <resultMap type="com.pcloud.book.applet.entity.AppletRecordSevenDay" id="AppletRecordSevenDayMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="recordType" column="record_type" jdbcType="INTEGER"/>
        <result property="fromId" column="from_id" jdbcType="INTEGER"/>
        <result property="fromName" column="from_name" jdbcType="VARCHAR"/>
        <result property="typeCode" column="type_code" jdbcType="VARCHAR"/>
        <result property="linkUrl" column="link_url" jdbcType="VARCHAR"/>
        <result property="proLabelId" column="pro_label_id" jdbcType="INTEGER"/>
        <result property="depLabelId" column="dep_label_id" jdbcType="INTEGER"/>
        <result property="purLabelId" column="pur_label_id" jdbcType="INTEGER"/>
        <result property="clickCount" column="click_count" jdbcType="INTEGER"/>
        <result property="userCount" column="user_count" jdbcType="INTEGER"/>
        <result property="sourceType" column="source_type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="AppletRecordSevenDayMap">
        select
          id, record_type, from_id, from_name, type_code, link_url, pro_label_id, dep_label_id, pur_label_id,click_count, user_count, source_type, create_time
        from book.applet_record_seven_day
        where id = #{id}
    </select>


    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_record_seven_day(record_type, from_id, from_name, type_code, link_url, pro_label_id, dep_label_id, pur_label_id, click_count, user_count, source_type, create_time)
        values (#{recordType}, #{fromId}, #{fromName}, #{typeCode}, #{linkUrl}, #{proLabelId}, #{depLabelId}, #{purLabelId}, #{clickCount}, #{userCount}, #{sourceType},now())
    </insert>

    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_record_seven_day(record_type, from_id, from_name,
        type_code, link_url, pro_label_id, dep_label_id,
        pur_label_id, click_count, user_count, source_type, create_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.recordType}, #{item.fromId}, #{item.fromName},
            #{item.typeCode}, #{item.linkUrl}, #{item.proLabelId}, #{item.depLabelId},
            #{item.purLabelId}, #{item.clickCount}, #{item.userCount}, #{item.sourceType}, now())
        </foreach>
    </insert>


    <!--通过主键修改数据-->
    <update id="update">
        update book.applet_record_seven_day
        <set>
            <if test="recordType != null">
                record_type = #{recordType},
            </if>
            <if test="fromId != null">
                from_id = #{fromId},
            </if>
            <if test="fromName != null and fromName != ''">
                from_name = #{fromName},
            </if>
            <if test="typeCode != null and typeCode != ''">
                type_code = #{typeCode},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
            <if test="proLabelId != null">
                pro_label_id = #{proLabelId},
            </if>
            <if test="depLabelId != null">
                dep_label_id = #{depLabelId},
            </if>
            <if test="purLabelId != null">
                pur_label_id = #{purLabelId},
            </if>
            <if test="userCount != null">
                user_count = #{userCount},
            </if>
            <if test="sourceType != null">
                source_type = #{sourceType},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        where id = #{id}
    </update>

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

    <delete id="deleteBySourceType" parameterType="integer">
        delete from book.applet_record_seven_day where source_type = #{sourceType}
    </delete>

    <select id="getResourceByGrade4H5" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletRecordDTO">
        SELECT
            a.record_type recordType,
            a.from_id fromId,
            IFNULL(m.serve_name, a.from_name) fromName,
            a.link_url linkUrl,
            a.type_code typeCode,
            a.click_count clickCount,
            a.user_count userCount,
            m.cover_img servePic
        FROM
            applet_record_seven_day a
        INNER JOIN resource_page_grade_label g ON a.dep_label_id = g.dep_label_id
        LEFT JOIN resource_recom_manage m ON A.from_id = m.serve_id
        AND m.source_from = #{sourceFrom}
        AND a.type_code = m.type_code
        WHERE
            a.source_type = #{sourceType}
        AND IFNULL(m.show_state, 1) = 1
        <if test="gradeLabelIds != null and gradeLabelIds.size>0">
            AND g.user_grade_label_id IN
            <foreach collection="gradeLabelIds" separator="," index="index" item="item" close=")" open="(">
                ${item}
            </foreach>
        </if>
        GROUP BY
            a.record_type,
            a.from_id
        ORDER BY
        <if test="gradeLabelIds != null and gradeLabelIds.size>0">
            FIELD(g.user_grade_label_id
            <foreach collection="gradeLabelIds" item="item" open=" " separator=" " close=" ">
                ,${item}
            </foreach>
            ),
        </if>
            a.user_count DESC
        <if test="limit != null">
            limit #{limit}
        </if>
    </select>

    <select id="getList4Pcloud" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletRecordDTO">
        SELECT
            a.record_type recordType,
            a.from_id fromId,
            IFNULL(m.serve_name, a.from_name) fromName,
            a.link_url linkUrl,
            a.type_code typeCode,
            a.click_count clickCount,
            a.user_count userCount,
            m.cover_img servePic,
            IFNULL(m.show_state, 1) serveShowState
        FROM
            applet_record_seven_day a
        LEFT JOIN resource_recom_manage m ON A.from_id = m.serve_id
        AND m.source_from = #{sourceFrom}
        AND a.type_code = m.type_code
        WHERE 1=1
        <if test="sourceTypeList != null and sourceTypeList.size>0">
            AND a.source_type IN
            <foreach collection="sourceTypeList" index="index" separator="," item="item" close=")" open="(">
                ${item}
            </foreach>
        </if>
        <if test="query != null">
            AND IFNULL(m.serve_name, a.from_name) LIKE CONCAT("%", #{query}, "%")
        </if>
        GROUP BY
            a.record_type,
            a.from_id
        ORDER BY
            a.user_count DESC
    </select>

</mapper>