<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pig4cloud.pig.marketing.mapper.MarketingAppsMapper">


	<select id="statAppFirstMktData" resultType="com.pig4cloud.pig.marketing.api.vo.app.PageStatAppFirstMktDataVO">
		SELECT
			ip,
			MIN(create_time) AS firstTime,
			MAX(create_time) AS lastTime,
			COUNT(*) AS count,
			(SELECT utm_source FROM marketing_data
			WHERE ip = md.ip
			AND app_id = #{appId}
			<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
			<if test="endTime != null">AND create_time &lt;= #{endTime}</if>
			AND del_flag = '0'
			ORDER BY create_time DESC LIMIT 1) AS utmSource,
			(SELECT url FROM marketing_data
			WHERE ip = md.ip
			AND app_id = #{appId}
			<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
			<if test="endTime != null">AND create_time &lt;= #{endTime}</if>
			AND del_flag = '0'
			AND url IS NOT NULL
			ORDER BY create_time DESC, id DESC LIMIT 1) AS url
		FROM marketing_data md
		WHERE
			md.app_id = #{appId}
			AND md.del_flag = '0'
			<if test="startTime != null">
				AND md.create_time &gt;= #{startTime}
			</if>
			<if test="endTime != null">
				AND md.create_time &lt;= #{endTime}
			</if>
		GROUP BY md.ip
		ORDER BY lastTime DESC
	</select>
	<select id="statAppSecondMktData" resultType="com.pig4cloud.pig.marketing.api.vo.app.PageStatAppSecondMktDataVO">
		SELECT
			fingerprint,
			MIN(create_time) AS firstTime,
			MAX(create_time) AS lastTime,
			COUNT(*) AS count,

			(SELECT url FROM marketing_data
			WHERE fingerprint = md.fingerprint
			AND ip = #{ip}
			AND app_id = #{appId}
			<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
			<if test="endTime != null">AND create_time &lt;= #{endTime}</if>
			AND del_flag = '0'
			ORDER BY create_time DESC, id DESC LIMIT 1) AS url,

			(SELECT os_type FROM marketing_data
			WHERE fingerprint = md.fingerprint
			AND ip = #{ip}
			AND app_id = #{appId}
			<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
		    <if test="endTime != null">AND create_time &lt;= #{endTime}</if>
			AND del_flag = '0'
			ORDER BY create_time DESC, id DESC LIMIT 1) AS osType,

			(SELECT os_version FROM marketing_data
			WHERE fingerprint = md.fingerprint
			AND ip = #{ip}
			AND app_id = #{appId}
			<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
			<if test="endTime != null">AND create_time &lt;= #{endTime}</if>
			AND del_flag = '0'
			ORDER BY create_time DESC, id DESC LIMIT 1) AS osVersion,

			(SELECT browser FROM marketing_data
			WHERE fingerprint = md.fingerprint
			AND ip = #{ip}
			AND app_id = #{appId}
			<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
			<if test="endTime != null">AND create_time &lt;= #{endTime}</if>
			AND del_flag = '0'
			ORDER BY create_time DESC, id DESC LIMIT 1) AS browser,

		(SELECT browser_version FROM marketing_data
		WHERE fingerprint = md.fingerprint
		AND ip = #{ip}
		AND app_id = #{appId}
		<if test="startTime != null">AND create_time &gt;= #{startTime}</if>
		<if test="endTime != null">AND create_time &lt;= #{endTime}</if>
		AND del_flag = '0'
		ORDER BY create_time DESC, id DESC LIMIT 1) AS browserVersion
		FROM marketing_data md
		WHERE
			md.app_id = #{appId}
			AND md.ip = #{ip}
			AND md.del_flag = '0'
			<if test="startTime != null">
				AND md.create_time &gt;= #{startTime}
			</if>
			<if test="endTime != null">
				AND md.create_time &lt;= #{endTime}
			</if>
			AND md.fingerprint IS NOT NULL
		GROUP BY md.fingerprint
		ORDER BY lastTime DESC
	</select>
</mapper>
