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

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletThirdResourcesChoose">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="resource_url" property="resourceUrl" jdbcType="VARCHAR"/>
        <result column="resource_name" property="resourceName" jdbcType="VARCHAR"/>
        <result column="resource_source" property="resourceSource" jdbcType="VARCHAR"/>
        <result column="channel_name" property="channelName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="upate_time" property="upateTime" jdbcType="TIMESTAMP"/>
        <result column="resource_number" property="resourceNumber" jdbcType="VARCHAR"/>
    </resultMap>


    <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.applet.dto.AppletThirdResourcesStaticDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="resource_number" property="resourceNumber" jdbcType="VARCHAR"/>
        <result column="resource_url" property="resourceUrl" jdbcType="VARCHAR"/>
        <result column="resource_name" property="resourceName" jdbcType="VARCHAR"/>
        <result column="resource_source" property="resourceSource" jdbcType="VARCHAR"/>
        <result column="channel_name" property="channelName" jdbcType="VARCHAR"/>
        <result column="date" property="date" jdbcType="DATE"/>
        <result column="stage_str" property="stageStr" jdbcType="VARCHAR"/>
        <result column="stage" property="stage" jdbcType="VARCHAR"/>
        <result column="channel_remark" property="channelRemark" jdbcType="VARCHAR"/>
        <result column="push_uv" property="pushUv" jdbcType="INTEGER"/>
        <result column="get_qr_code_uv" property="getQrCodeUv" jdbcType="INTEGER"/>
        <result column="press_qr_code_uv" property="pressQrCodeUv" jdbcType="INTEGER"/>
        <result column="join_chatroom_count" property="joinChatroomCount" jdbcType="INTEGER"/>
        <result column="join_chatroom_rate" property="joinChatroomRate" jdbcType="VARCHAR"/>
        <result column="stay_user_count" property="stayUserCount" jdbcType="INTEGER"/>
        <result column="share_rate" property="shareRate" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="resource_type" property="resourceType" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, resource_url, resource_name, resource_source, channel_name, create_time, upate_time, resource_number
    </sql>

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

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_third_resources_choose
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_third_resources_choose(
            resource_url, 
            resource_name, 
            resource_source, 
            channel_name, 
            create_time, 
            upate_time
        ) VALUES (
            #{resourceUrl, jdbcType=VARCHAR}, 
            #{resourceName, jdbcType=VARCHAR}, 
            #{resourceSource, jdbcType=VARCHAR}, 
            #{channelName, jdbcType=VARCHAR}, 
            #{createTime, jdbcType=TIMESTAMP}, 
            #{upateTime, jdbcType=TIMESTAMP}
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO applet_third_resources_choose (
        resource_url,
        resource_name,
        resource_source,
        channel_name,
        create_time,
        upate_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.resourceUrl, jdbcType=VARCHAR},
            #{item.resourceName, jdbcType=VARCHAR},
            #{item.resourceSource, jdbcType=VARCHAR},
            #{item.channelName, jdbcType=VARCHAR},
            #{item.createTime, jdbcType=TIMESTAMP},
            #{item.upateTime, jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_third_resources_choose
        <set>
            <if test="resourceUrl != null and resourceUrl != ''">
                resource_url = #{resourceUrl},
            </if>
            <if test="resourceName != null and resourceName != ''">
                resource_name = #{resourceName},
            </if>
            <if test="resourceSource != null and resourceSource != ''">
                resource_source = #{resourceSource},
            </if>
            <if test="channelName != null and channelName != ''">
                channel_name = #{channelName},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="upateTime != null">
                upate_time = #{upateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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

    <select id="getListThirdResourcesStatic" resultMap="BaseResultMap4DTO" parameterType="map">
        SELECT
        m.id,
        m.resource_number,
        m.resource_url,
        m.resource_name,
        m.resource_source,
        m.channel_name,
        n.stage_str,
        n.stage,
        n.channel_remark,
        m.resource_type,
        n.push_uv,
        n.get_qr_code_uv,
        n.press_qr_code_uv,
        n.join_chatroom_count,
        n.stay_user_count
        FROM
        applet_third_resources_choose m
        LEFT JOIN (
        SELECT
        b.stage_str,
        b.stage,
        b.channel_remark,
        b.channel_name,
        sum(push_uv) push_uv,
        sum(get_qr_code_uv) get_qr_code_uv,
        sum(press_qr_code_uv) press_qr_code_uv,
        sum(join_chatroom_count) join_chatroom_count,
        sum(stay_user_count) stay_user_count
        FROM
        applet_third_resources_static b
        <where>
            <if test="queryDate!=null">
                and b.date = #{queryDate}
            </if>
        </where>
        GROUP BY
        b.channel_name
        ) n ON m.channel_name= n.channel_name
        <where>
            <if test="source!=null">
                m.resource_source = #{source}
            </if>
            <if test="keyValue!=null">
                and
                (
                m.resource_name like concat("%", #{keyValue}, "%")
                or
                m.resource_number like concat("%", #{keyValue}, "%")
                )
            </if>
        </where>
    </select>


</mapper>