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

    <resultMap type="com.pcloud.book.applet.entity.AppletLinkClick" id="AppletLinkClickMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="typeId" column="type_id" jdbcType="INTEGER"/>
        <result property="fromId" column="from_id" jdbcType="INTEGER"/>
        <result property="recordType" column="record_type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_link_click(wechat_user_id, type_id, from_id, record_type, create_time, create_date)
        values (#{wechatUserId}, #{typeId}, #{fromId}, #{recordType}, NOW(),NOW())
    </insert>

    <select id="getListThirdResourcesAnalysis" parameterType="map"
            resultType="com.pcloud.book.applet.dto.AppletThirdResourcesDTO">
        SELECT
        id,
        resource_number number,
        resource_type type,
        resource_name name,
        app_Id appletsId,
        resource_source source,
        resource_img_url resourceImgUrl,
        resource_url url,
        remark,
        create_time createTime,
        update_time updateTime,
        creator,
        updator
        FROM
        applet_third_resources n
        WHERE
        1 = 1
        <if test="keyValue != null">
            AND (n.resource_number LIKE CONCAT("%",#{keyValue},"%") or n.resource_name LIKE CONCAT("%",#{keyValue},"%"))
        </if>
        <if test="source != null">
            AND n.resource_source = #{source}
        </if>
        <if test="resourcesType != null">
            AND n.resource_type = #{resourcesType}
        </if>
        GROUP BY
        n.id
        ORDER BY
        n.update_time DESC
    </select>

    <select id="mapPVUV" parameterType="map" resultType="com.pcloud.book.applet.dto.PvuvDTO">
        SELECT
        from_id fromId,
        COUNT(1) pv,
        -- COUNT(DISTINCT wechat_user_id,create_date) uv
        COUNT(DISTINCT wechat_user_id) uv
        FROM applet_link_click
        WHERE type_id = #{typeId}
        AND record_type = #{recordType}
        AND from_id in
        <foreach collection="list" item="item" index="index" separator="," close=")" open="(">
            #{item}
        </foreach>
        <if test="intervalDaya != null">
            AND date_format(create_time ,'%Y-%m-%d' ) between DATE_SUB(CURDATE(),INTERVAL #{intervalDaya} DAY) and DATE_SUB(CURDATE(),INTERVAL 1
            DAY)
        </if>
        GROUP BY from_id
    </select>

    <select id="mapPVUVByMonth" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletChartDateDTO">
        SELECT b.createDate,IFNULL(aa.pv,0) pv,IFNULL(aa.uv,0) uv from
		(SELECT
				create_date createDate,
        COUNT(1) pv,
        COUNT(DISTINCT wechat_user_id) uv
        FROM applet_link_click
        WHERE  type_id = #{typeId}
        AND record_type =  #{recordType}
		and from_id=  #{newsId}
        AND create_date between date_add(CURDATE(), interval -1 month) and DATE_SUB(CURDATE(),INTERVAL 1 DAY)
        GROUP BY create_date
		) aa
		RIGHT JOIN
			(select a.Date as createDate from
				(select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date from
					(select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
			  cross join
					(select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
				cross join
					(select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
				) a
			where a.Date between DATE_SUB(CURDATE(), INTERVAL 30 DAY) and DATE_SUB(CURDATE(),INTERVAL 1 DAY))b ON b.createDate = aa.createDate
			ORDER BY b.createDate asc
    </select>

    <select id="mapCouponUseCount" parameterType="map" resultType="com.pcloud.book.applet.dto.PvuvDTO">
        SELECT
        from_id fromId,
        COUNT(DISTINCT wechat_user_id) count
        FROM applet_link_click
        WHERE type_id=2
        AND record_type=3
        AND from_id in
        <foreach collection="list" item="item" index="index" separator="," close=")" open="(">
            #{item}
        </foreach>
        GROUP BY from_id
    </select>

    <select id="getThirdGroup" parameterType="map" resultType="com.pcloud.book.applet.dto.ExportClickDTO">
         SELECT
	from_id id,
	count(*) clickCount,
	b.NAME resourceName
    FROM
	`applet_link_click` a
    LEFT JOIN pcloud_group_activity b ON a.from_id = b.id
    WHERE
	type_id = 4
	AND record_type = 1
    AND DATE_FORMAT(create_date,"%Y-%m-%d") >= #{startDate}
    AND DATE_FORMAT(create_date,"%Y-%m-%d") &lt;= #{endDate}
    GROUP BY
	from_id
    ORDER BY
	clickCount DESC
    LIMIT #{count}
    </select>

    <select id="getNormalGroup" parameterType="map" resultType="com.pcloud.book.applet.dto.ExportClickDTO">
        SELECT
	from_id id,
	count(*) clickCount,
	b.classify resourceName
    FROM
	applet_link_click a
    LEFT JOIN book_group_classify b ON a.from_id = b.id
    WHERE
	type_id = 3
    AND record_type = 1
    AND DATE_FORMAT(create_date,"%Y-%m-%d") >= #{startDate}
    AND DATE_FORMAT(create_date,"%Y-%m-%d") &lt;= #{endDate}
    GROUP BY
	from_id
    ORDER BY
	clickCount DESC
    LIMIT #{count}
    </select>

    <select id="getNews" parameterType="map" resultType="com.pcloud.book.applet.dto.ExportClickDTO">
        SELECT
	from_id id,
	count(*) clickCount,
	n.news_name resourceName,
	n.source source,
	c.classify rightsSettingName
    FROM
	applet_link_click a
    LEFT JOIN applet_news n ON a.from_id = n.id
    LEFT JOIN rights_setting_classify c ON n.rights_classify_id = c.id
    WHERE
	type_id = 1
    AND record_type = 1
    AND DATE_FORMAT(create_date,"%Y-%m-%d") >= #{startDate}
    AND DATE_FORMAT(create_date,"%Y-%m-%d") &lt;= #{endDate}
    GROUP BY
	from_id
    ORDER BY
	clickCount DESC
	LIMIT #{count}
    </select>



</mapper>