<?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.fs.bdAdv.mapper.FsAdvSemMapper">

    <resultMap type="FsAdvSem" id="FsAdvSemResult">
        <result property="id"    column="id"    />
        <result property="imei"    column="imei"    />
        <result property="idfa"    column="idfa"    />
        <result property="oaid"    column="oaid"    />
        <result property="androidId"    column="android_id"    />
        <result property="akey"    column="akey"    />
        <result property="aType"    column="a_type"    />
        <result property="aValue"    column="a_value"    />
        <result property="joinType"    column="join_type"    />
        <result property="ip"    column="ip"    />
        <result property="ua"    column="ua"    />
        <result property="osVersion"    column="os_version"    />
        <result property="model"    column="model"    />
        <result property="osType"    column="os_type"    />
        <result property="deviceId"    column="device_id"    />
        <result property="aid"    column="aid"    />
        <result property="pid"    column="pid"    />
        <result property="uid"    column="uid"    />
        <result property="clickId"    column="click_id"    />
        <result property="callbackUrl"    column="callback_url"    />
        <result property="advType"    column="adv_type"    />
        <result property="cmdType"    column="cmd_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="userId"    column="user_id"    />
    </resultMap>

    <sql id="selectFsAdvSemVo">
        select id, imei, idfa, oaid,user_id, android_id, akey, a_type, a_value, join_type, ip, ua, os_version, model, os_type, device_id, aid, pid, uid, click_id, callback_url, adv_type,cmd_type, create_time, update_time from fs_adv_sem
    </sql>

    <select id="selectFsAdvSemList" parameterType="FsAdvSem" resultMap="FsAdvSemResult">
        <include refid="selectFsAdvSemVo"/>
        <where>
            <if test="imei != null  and imei != ''"> and imei = #{imei}</if>
            <if test="idfa != null  and idfa != ''"> and idfa = #{idfa}</if>
            <if test="oaid != null  and oaid != ''"> and oaid = #{oaid}</if>
            <if test="androidId != null  and androidId != ''"> and android_id = #{androidId}</if>
            <if test="akey != null  and akey != ''"> and akey = #{akey}</if>
            <if test="aType != null  and aType != ''"> and a_type = #{aType}</if>
            <if test="aValue != null  and aValue != ''"> and a_value = #{aValue}</if>
            <if test="joinType != null  and joinType != ''"> and join_type = #{joinType}</if>
            <if test="ip != null  and ip != ''"> and ip = #{ip}</if>
            <if test="ua != null  and ua != ''"> and ua = #{ua}</if>
            <if test="osVersion != null  and osVersion != ''"> and os_version = #{osVersion}</if>
            <if test="model != null  and model != ''"> and model = #{model}</if>
            <if test="osType != null "> and os_type = #{osType}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="aid != null  and aid != ''"> and aid = #{aid}</if>
            <if test="pid != null  and pid != ''"> and pid = #{pid}</if>
            <if test="uid != null  and uid != ''"> and uid = #{uid}</if>
            <if test="clickId != null  and clickId != ''"> and click_id = #{clickId}</if>
            <if test="callbackUrl != null  and callbackUrl != ''"> and callback_url = #{callbackUrl}</if>
            <if test="advType != null "> and adv_type = #{advType}</if>
        </where>
    </select>

    <select id="selectFsAdvSemById" parameterType="Long" resultMap="FsAdvSemResult">
        <include refid="selectFsAdvSemVo"/>
        where id = #{id}
    </select>

    <select id="selectFsAdvSemStatisticsByDayVo" parameterType="com.fs.bdAdv.param.FsAdvSemStatisticsByDayParam" resultType="com.fs.bdAdv.vo.FsAdvSemStatisticsByDayVo">
        SELECT * FROM (
            SELECT
            DATE(create_time) AS date,
            adv_type,
            COUNT(CASE WHEN cmd_type = 0 THEN 1 END) AS activation_count,
            COUNT(CASE WHEN cmd_type = 1 THEN 1 END) AS registration_count,
            COUNT(CASE WHEN cmd_type = 4 THEN 1 END) AS jump_count,
            COUNT(CASE WHEN cmd_type = 5 THEN 1 END) AS binding_count,
            COUNT(*) AS total_count,
            ROUND(COUNT(CASE WHEN cmd_type = 0 THEN 1 END) / COUNT(*) * 100, 2) AS activation_rate,
            ROUND(COUNT(CASE WHEN cmd_type = 1 THEN 1 END) / COUNT(*) * 100, 2) AS registration_rate,
            ROUND(COUNT(CASE WHEN cmd_type = 4 THEN 1 END) / COUNT(*) * 100, 2) AS jump_rate,
            ROUND(COUNT(CASE WHEN cmd_type = 5 THEN 1 END) / COUNT(*) * 100, 2) AS binding_rate
                FROM
                    fs_adv_sem
                WHERE
                    create_time IS NOT NULL
                    <if test="advType != null">
                        and adv_type = #{advType}
                    </if>
                GROUP BY
                    DATE(create_time) ,adv_type
                ORDER BY
                    DATE(create_time)) a
            <where>
                <if test="totalCountMax != null">
                    a.total_count &lt;= #{totalCountMax}
                </if>
                <if test="totalCountMin != null">
                    a.total_count &gt;= #{totalCountMin}
                </if>
                <if test="activationCountMax != null">
                    a.activation_count &lt;= #{activationCountMax}
                </if>
                <if test="activationCountMin != null">
                    a.activation_count &gt;= #{activationCountMin}
                </if>
                <if test="registrationCountMax != null">
                    a.registration_count &lt;= #{registrationCountMax}
                </if>
                <if test="registrationCountMin != null">
                    a.registration_count &gt;= #{registrationCountMin}
                </if>
                <if test="jumpCountMax != null">
                    a.jump_count &lt;= #{jumpCountMax}
                </if>
                <if test="jumpCountMin != null">
                    a.jump_count &gt;= #{jumpCountMin}
                </if>
                <if test="bindingCountMax != null">
                    a.binding_count &lt;= #{bindingCountMax}
                </if>
                <if test="bindingCountMin != null">
                    a.binding_count &gt;= #{bindingCountMin}
                </if>
                <if test="activationRateMax != null">
                    a.activation_rate &lt;= #{activationRateMax}
                </if>
                <if test="activationRateMin != null">
                    a.activation_rate &gt;= #{activationRateMin}
                </if>
                <if test="registrationRateMax != null">
                    a.registration_rate &lt;= #{registrationRateMax}
                </if>
                <if test="registrationRateMin != null">
                    a.registration_rate &gt;= #{registrationRateMin}
                </if>
                <if test="jumpRateMax != null">
                    a.jump_rate &lt;= #{jumpRateMax}
                </if>
                <if test="jumpRateMin != null">
                    a.jump_rate &gt;= #{jumpRateMin}
                </if>
                <if test="bindingRateMax != null">
                    a.binding_rate &lt;= #{bindingRateMax}
                </if>
                <if test="bindingRateMin != null">
                    a.binding_rate &gt;= #{bindingRateMin}
                </if>
                <if test="dateMax != null and dateMax != ''">
                    and date_format(a.date,'%y%m%d') &lt;= date_format(#{dateMax},'%y%m%d')
                </if>
                <if test="dateMin != null and dateMin != ''">
                    and date_format(a.date,'%y%m%d') &gt;= date_format(#{dateMin},'%y%m%d')
                </if>
            </where>
            ${params.dataScope}
    </select>

    <insert id="insertFsAdvSem" parameterType="FsAdvSem" useGeneratedKeys="true" keyProperty="id">
        insert into fs_adv_sem
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="imei != null">imei,</if>
            <if test="idfa != null">idfa,</if>
            <if test="oaid != null">oaid,</if>
            <if test="androidId != null">android_id,</if>
            <if test="akey != null">akey,</if>
            <if test="aType != null">a_type,</if>
            <if test="aValue != null">a_value,</if>
            <if test="joinType != null">join_type,</if>
            <if test="ip != null">ip,</if>
            <if test="ua != null">ua,</if>
            <if test="osVersion != null">os_version,</if>
            <if test="model != null">model,</if>
            <if test="osType != null">os_type,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="aid != null">aid,</if>
            <if test="pid != null">pid,</if>
            <if test="uid != null">uid,</if>
            <if test="clickId != null">click_id,</if>
            <if test="callbackUrl != null">callback_url,</if>
            <if test="advType != null">adv_type,</if>
            <if test="cmdType != null">cmd_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="userId != null">user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="imei != null">#{imei},</if>
            <if test="idfa != null">#{idfa},</if>
            <if test="oaid != null">#{oaid},</if>
            <if test="androidId != null">#{androidId},</if>
            <if test="akey != null">#{akey},</if>
            <if test="aType != null">#{aType},</if>
            <if test="aValue != null">#{aValue},</if>
            <if test="joinType != null">#{joinType},</if>
            <if test="ip != null">#{ip},</if>
            <if test="ua != null">#{ua},</if>
            <if test="osVersion != null">#{osVersion},</if>
            <if test="model != null">#{model},</if>
            <if test="osType != null">#{osType},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="aid != null">#{aid},</if>
            <if test="pid != null">#{pid},</if>
            <if test="uid != null">#{uid},</if>
            <if test="clickId != null">#{clickId},</if>
            <if test="callbackUrl != null">#{callbackUrl},</if>
            <if test="advType != null">#{advType},</if>
            <if test="cmdType != null">#{cmdType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="userId != null">#{userId},</if>
         </trim>
    </insert>

    <update id="updateFsAdvSem" parameterType="FsAdvSem">
        update fs_adv_sem
        <trim prefix="SET" suffixOverrides=",">
            <if test="imei != null">imei = #{imei},</if>
            <if test="idfa != null">idfa = #{idfa},</if>
            <if test="oaid != null">oaid = #{oaid},</if>
            <if test="androidId != null">android_id = #{androidId},</if>
            <if test="akey != null">akey = #{akey},</if>
            <if test="aType != null">a_type = #{aType},</if>
            <if test="aValue != null">a_value = #{aValue},</if>
            <if test="joinType != null">join_type = #{joinType},</if>
            <if test="ip != null">ip = #{ip},</if>
            <if test="ua != null">ua = #{ua},</if>
            <if test="osVersion != null">os_version = #{osVersion},</if>
            <if test="model != null">model = #{model},</if>
            <if test="osType != null">os_type = #{osType},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="aid != null">aid = #{aid},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="uid != null">uid = #{uid},</if>
            <if test="clickId != null">click_id = #{clickId},</if>
            <if test="callbackUrl != null">callback_url = #{callbackUrl},</if>
            <if test="advType != null">adv_type = #{advType},</if>
            <if test="cmdType != null">cmd_type = #{cmdType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="userId != null">user_id = #{userId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFsAdvSemById" parameterType="Long">
        delete from fs_adv_sem where id = #{id}
    </delete>

    <delete id="deleteFsAdvSemByIds" parameterType="String">
        delete from fs_adv_sem where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
