<?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.ytpm.raisepig.dao.AppUserMapper">
    <insert id="addOne" parameterType="com.ytpm.app.model.YtDyzUser">
        insert into yt_dyz_user
        (
         user_id,
         nick_name,
         head_img,
         registry_time,
         last_login_time,
         last_login_ip,
         login_days,
         total_video,
         total_income,
         red_packet_balance,
         red_packet_amount,
         points_balance,
         points_total,
         withdraw_total,
         sign_days,
         user_status,
         risk_reason,
         wx_open_id,
         ditch_id,
         platform_id,
         app_id,
         power,
         phone,
         device_id
        )
        values
        (
         #{userId},
         #{nickName},
         #{headImg},
         #{registryTime},
         #{lastLoginTime},
         #{lastLoginIp},
         #{loginDays},
         #{totalVideo},
         #{totalIncome},
         #{redPacketBalance},
         #{redPacketAmount},
         #{pointsBalance},
         #{pointsTotal},
         #{withdrawTotal},
         #{signDays},
         #{userStatus},
         #{riskReason},
         #{wxOpenId},
         #{ditchId},
         #{platformId},
         #{appId},
         #{power},
         #{phone},
         #{deviceId}
        )
    </insert>
    <insert id="addPowerRecord">
        insert into yt_dyz_power_record
        (
         record_id, user_id, type, remark, add_time
        )
        values
        (
         #{recordId},#{userId},#{type},#{remark},#{addTime}
        )
    </insert>
    <insert id="saveAppConfig">
        insert into yt_app_default_config
        (
         config_name,
         open_id,
         secret,
         app_id,
         app_key,
         app_type
        )
        values
        (
         #{configName},
         #{openId},
         #{secret},
         #{appId},
         #{appKey},
         #{appType}
        )
    </insert>
    <update id="addOnePower">
        update yt_dyz_user set power = power+1 where user_id = #{userId}
    </update>
    <update id="subOnePower">
        update yt_dyz_user set power = power-1 where user_id = #{userId}
    </update>
    <update id="updateUser" parameterType="com.ytpm.app.model.YtDyzUser">
        update yt_dyz_user
        <set>
            <if test="nickName != null">
                nick_name = #{nickName},
            </if>
            <if test="phone != null">
                phone = #{phone},
            </if>
            <if test="deviceId != null">
                device_id = #{deviceId},
            </if>
            <if test="headImg != null">
                head_img = #{headImg},
            </if>
            <if test="power != null">
                power = #{power},
            </if>
            <if test="lastLoginTime != null">
                last_login_time = #{lastLoginTime},
            </if>
            <if test="lastLoginIp != null">
                last_login_ip = #{lastLoginIp},
            </if>
            <if test="loginDays != null">
                login_days = #{loginDays},
            </if>
            <if test="totalVideo != null">
                total_video = #{totalVideo},
            </if>
            <if test="totalIncome != null">
                total_income = #{totalIncome},
            </if>
            <if test="redPacketBalance != null">
                red_packet_balance = #{redPacketBalance},
            </if>
            <if test="redPacketAmount != null">
                red_packet_amount = #{redPacketAmount},
            </if>
            <if test="pointsBalance != null">
                points_balance = #{pointsBalance},
            </if>
            <if test="pointsTotal != null">
                points_total = #{pointsTotal},
            </if>
            <if test="withdrawTotal != null">
                withdraw_total = #{withdrawTotal},
            </if>
            <if test="signDays != null">
                sign_days = #{signDays},
            </if>
            <if test="userStatus != null">
                user_status = #{userStatus},
            </if>
            <if test="riskReason != null">
                risk_reason = #{riskReason},
            </if>
            <if test="wxOpenId != null">
                wx_open_id = #{wxOpenId},
            </if>
            <if test="platformId != null">
                platform_id = #{platformId}
            </if>
        </set>
        where user_id = #{userId}
    </update>
    <delete id="delByAppId">
        delete from yt_app_default_config where app_id = #{appId}
    </delete>
    <select id="getYtAppUser" resultType="com.ytpm.app.model.YtDyzUser">
        select
           user_id, app_id,phone,device_id, ditch_id, nick_name,head_img, power, registry_time, last_login_time, last_login_ip, login_days, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, risk_reason, wx_open_id, platform_id
        from yt_dyz_user
        where wx_open_id = #{openid}
        and ditch_id = #{ditchId}
    </select>
    <select id="queryAll" resultType="com.ytpm.app.view.YtAppUserListView">
        select
            user_id,app_id,phone,device_id, ditch_id, nick_name,head_img, power, registry_time, last_login_time, last_login_ip, login_days, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, risk_reason, wx_open_id, platform_id
        from yt_dyz_user
        where 1 = 1
        <if test="userId != null and userId !=''">
            and user_id = #{userId}
        </if>
        <if test="nickName != null and nickName !=''">
            and nick_name like concat('%',#{nickName},'%')
        </if>
        <if test="ditchId != null">
            and ditch_id = #{ditchId}
        </if>
        <if test="userStatus != null">
            and user_status = #{userStatus}
        </if>
        <if test="registryTimeBegin != null">
            and DATE_FORMAT(registry_time, '%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{registryTimeBegin}, '%Y-%m-%d')
        </if>
        <if test="registryTimeEnd != null">
            and DATE_FORMAT(registry_time, '%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(#{registryTimeEnd}, '%Y-%m-%d')
        </if>
        <if test="lastLoginTimeBegin != null">
            and DATE_FORMAT(last_login_time, '%Y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{lastLoginTimeBegin}, '%Y-%m-%d')
        </if>
        <if test="lastLoginTimeEnd != null">
            and DATE_FORMAT(last_login_time, '%Y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(#{lastLoginTimeEnd}, '%Y-%m-%d')
        </if>
        <if test="appIds != null and appIds != ''">
            and app_id in
                <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
                    #{item}
                </foreach>
        </if>
        order by user_id desc
    </select>
    <select id="selectPrimaryKey" resultType="com.ytpm.app.model.YtDyzUser">
        select
            user_id,app_id,phone,device_id, ditch_id, head_img, nick_name, registry_time, last_login_time, last_login_ip, login_days, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, risk_reason, wx_open_id, platform_id, power
        from yt_dyz_user
        where user_id = #{userId}
    </select>
    <resultMap id="UserMap" type="com.ytpm.app.model.YtDyzUser">
        <id column="user_id" property="userId" />
        <result column="head_img" property="headImg" />
        <result column="nick_name" property="nickName" />
        <result column="phone" property="phone" />
        <result column="device_id" property="deviceId" />
        <result column="registry_time" property="registryTime" />
        <result column="last_login_time" property="lastLoginTime" />
        <result column="last_login_ip" property="lastLoginIp" />
        <result column="login_days" property="loginDays" />
        <result column="total_video" property="totalVideo" />
        <result column="total_income" property="totalIncome" />
        <result column="red_packet_balance" property="redPacketBalance" />
        <result column="red_packet_amount" property="redPacketAmount" />
        <result column="points_balance" property="pointsBalance" />
        <result column="points_total" property="pointsTotal" />
        <result column="withdraw_total" property="withdrawTotal" />
        <result column="sign_days" property="signDays" />
        <result column="user_status" property="userStatus" />
        <result column="risk_reason" property="riskReason" />
        <result column="wx_open_id" property="wxOpenId" />
        <result column="ditch_id" property="ditchId" />
        <result column="app_id" property="appId" />
        <result column="platform_id" property="platformId" />
        <result column="power" property="power" />
        <collection property="loginRecordList" ofType="com.ytpm.app.model.YtDyzLoginRecord">
            <result column="record_id" property="recordId" />
            <result column="user_id" property="userId" />
            <result column="login_time" property="loginTime" />
            <result column="device_brand" property="deviceBrand" />
            <result column="device_model" property="deviceModel" />
            <result column="login_ip" property="loginIp" />
            <result column="operator" property="operator" />
            <result column="ip_addr" property="ipAddr" />
        </collection>
    </resultMap>
    <select id="getUserList" resultMap="UserMap">
        select
            du.user_id,
            du.head_img,
            du.nick_name,
            du.phone,
            du.device_id,
            du.registry_time,
            du.last_login_time,
            du.last_login_ip,
            du.login_days,
            du.total_video,
            du.total_income,
            du.red_packet_balance,
            du.red_packet_amount,
            du.points_balance,
            du.points_total,
            du.withdraw_total,
            du.sign_days,
            du.user_status,
            du.risk_reason,
            du.wx_open_id,
            du.ditch_id,
            du.app_id,
            du.platform_id,
            du.power,
            lr.record_id,
            lr.login_time,
            lr.device_brand,
            lr. device_model,
            lr.login_ip,
            lr.operator,
            lr.ip_addr
        from yt_dyz_user du
        left join yt_dyz_login_record lr on du.user_id = lr.user_id
        <where>
            <if test="userIds != null and userIds != ''">
                and du.user_id in
                <foreach collection="userIds.split(',')" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="loginTimeBegin != null">
                and du.last_login_time <![CDATA[>=]]> #{loginTimeBegin}
            </if>
            <if test="loginTimeEnd != null">
                and du.last_login_time <![CDATA[<=]]> #{loginTimeEnd}
            </if>
        </where>
        group by du.user_id
    </select>

    <select id="queryAllByTime" resultType="com.ytpm.app.model.YtDyzUser">
        SELECT
        user_id,app_id,phone,device_id, nick_name, head_img, power, registry_time,
        last_login_time, last_login_ip, login_days, total_video,
        total_income, red_packet_balance, red_packet_amount,
        points_balance, points_total, withdraw_total, sign_days,
        user_status, risk_reason, wx_open_id, platform_id
        FROM yt_dyz_user
        <where>
            <if test="startTime != null">
                AND registry_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND registry_time &lt;= #{endTime}
            </if>
            <choose>
                <when test="appIdList == null or appIdList.isEmpty()">
                    AND 1 = 0
                </when>
                <otherwise>
                    AND app_id IN
                    <foreach collection="appIdList" item="item"
                             open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </otherwise>
            </choose>
        </where>
        ORDER BY registry_time DESC
    </select>
    <select id="countUsersByHour" resultType="com.ytpm.app.view.HourCountView">
        SELECT HOUR(registry_time) AS hour, COUNT(*) AS count
        FROM yt_dyz_user
        WHERE registry_time BETWEEN #{startTime} AND #{endTime}
        <choose>
            <when test="appIdList == null or appIdList.isEmpty()">
                AND 1 = 0
            </when>
            <otherwise>
                AND app_id IN
                <foreach collection="appIdList" item="item"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </otherwise>
        </choose>
        GROUP BY HOUR(registry_time)
    </select>

    <select id="queryTodayBanned" resultType="com.ytpm.app.model.YtDyzUser">
        select user_id from yt_dyz_user
        where registry_time BETWEEN #{startTime} AND #{endTime}
        and user_id in
        <foreach collection="userIdList" item="item"
                 open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="getDefaultConfig" resultType="com.ytpm.app.view.WxDefaultConfig">
        select
            config_id, config_name, open_id appId, secret, app_id platformAppId, app_key platformAppSecret, app_type,user_path,login_path,ad_path,answer_path,power_path
        from yt_app_default_config
        where app_type = #{appType}
    </select>
    <select id="getSecretByAppId" resultType="java.lang.String">
        select
           secret
        from yt_app_default_config
        where app_id = #{appId}
    </select>
    <select id="queryByOpenid" resultType="com.ytpm.app.model.YtDyzUser">
        select
            user_id,phone,device_id, head_img, nick_name, registry_time, last_login_time, last_login_ip, login_days, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, risk_reason, wx_open_id, ditch_id, app_id, platform_id, power
        from yt_dyz_user
        where wx_open_id = #{openid}
    </select>
    <select id="getByDeviceId" resultType="java.lang.String">
        select
            platform_id
        from yt_dyz_user
        where device_id = #{deviceId}
        and wx_open_id = #{openid}
        limit 1
    </select>
    <select id="getConfigByIds" resultType="com.ytpm.app.view.WxDefaultConfig">
        select
            config_id, config_name, open_id, secret, app_id, app_key, app_type,user_path,login_path,ad_path,answer_path,power_path
        from yt_app_default_config
        where app_id in
        <foreach collection="appIds.split(',')" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getAdCount" resultType="java.lang.Integer">
        select
            sum(total_video)
        from yt_dyz_user
        where app_id in
        <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getRevenueCount" resultType="java.math.BigDecimal">
        select
        sum(total_income)
        from yt_dyz_user
        where app_id in
        <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getAppRanking" resultType="com.ytpm.middle.view.AppRankingListVO">
        SELECT
            u.app_id,
            sum( ar.revenue ) totalRevenue
        FROM
            yt_dyz_ad_record ar
            JOIN yt_dyz_user u ON ar.user_id = u.user_id
        <where>
            <if test="sortBy != null">
                <choose>
                    <when test="sortBy == 1">
                        DATE_FORMAT( ar.finish_time, '%Y-%m-%d' ) = CURRENT_DATE()
                    </when>
                    <when test=" sortBy == 2">
                        YEARWEEK( ar.finish_time, 1 ) = YEARWEEK(now(),1)
                    </when>
                    <otherwise>
                        DATE_FORMAT( ar.finish_time, '%Y-%m' ) = DATE_FORMAT(NOW(), '%Y-%m')
                    </otherwise>
                </choose>
            </if>
        </where>
        GROUP BY
            u.app_id
        ORDER BY
            sum( ar.revenue ) DESC
        LIMIT #{limit}
    </select>
    <select id="getUserRanking" resultType="com.ytpm.middle.view.UserRankingListVO">
        SELECT
            u.user_id,
            u.nick_name,
            u.head_img,
            u.total_video,
            sum( ar.revenue ) totalIncome
        FROM
            yt_dyz_ad_record ar
                JOIN yt_dyz_user u ON ar.user_id = u.user_id
        <where>
            <if test="sortBy != null">
                <choose>
                    <when test="sortBy == 1">
                        DATE_FORMAT( ar.finish_time, '%Y-%m-%d' ) = CURRENT_DATE()
                    </when>
                    <when test=" sortBy == 2">
                        YEARWEEK( ar.finish_time, 1 ) = YEARWEEK(now(),1)
                    </when>
                    <otherwise>
                        DATE_FORMAT( ar.finish_time, '%Y-%m' ) = DATE_FORMAT(NOW(), '%Y-%m')
                    </otherwise>
                </choose>
            </if>
        </where>
        GROUP BY ar.user_id
        ORDER BY
            sum( ar.revenue ) DESC
        limit #{limit}
    </select>
    <select id="countUserByAppIds" resultType="java.lang.Integer">
        select
            count(user_id)
        from yt_dyz_user
        where app_id in
        <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="countRevenueByAppIds" resultType="java.math.BigDecimal">
        select
            sum(total_income)
        from yt_dyz_user
        where app_id in
        <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="countBannedByAppIds" resultType="java.lang.Integer">
        select
            count(user_id)
        from yt_dyz_user
        where user_status > 1 and app_id in
        <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="countRegistryUser" resultType="java.lang.Integer">
        select
        count(user_id)
        from yt_dyz_user
        where app_id = #{appId}
        <if test="type != null and type ==1">
            and DATE(registry_time) = DATE(now())
        </if>
        <if test="type != null and type ==2">
            and DATE(registry_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type ==3">
            and DATE_FORMAT(registry_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        </if>
    </select>
    <select id="countLoginUser" resultType="java.lang.Integer">
        select
        count(user_id)
        from yt_dyz_user
        where app_id = #{appId}
        <if test="type != null and type ==1">
            and DATE(last_login_time) = DATE(now())
        </if>
        <if test="type != null and type ==2">
            and DATE(last_login_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type ==3">
            and DATE_FORMAT(last_login_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        </if>
    </select>
    <select id="countRegistryHour" resultType="com.ytpm.middle.view.AppUserHourVO">
        select
            DATE_FORMAT(registry_time,'%Y-%m-%d %H:00:00') as `time`,
            count(user_id) `count`
        from yt_dyz_user
        where app_id = #{appId}
        <if test="type != null and type ==1">
            and DATE(registry_time) = DATE(now())
        </if>
        <if test="type != null and type ==2">
            and DATE(registry_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type ==3">
            and DATE_FORMAT(registry_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        </if>
        group by `time`
    </select>
    <select id="countLoginHour" resultType="com.ytpm.middle.view.AppUserHourVO">
        select
        DATE_FORMAT(last_login_time,'%Y-%m-%d %H:00:00') as `time`,
        count(user_id) `count`
        from yt_dyz_user
        where app_id = #{appId}
        <if test="type != null and type ==1">
            and DATE(last_login_time) = DATE(now())
        </if>
        <if test="type != null and type ==2">
            and DATE(last_login_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type ==3">
            and DATE_FORMAT(last_login_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
        </if>
        group by `time`
    </select>
    <select id="queryByUserIds" resultType="com.ytpm.app.model.YtDyzUser">
        select
            user_id, head_img, nick_name, registry_time, last_login_time, last_login_ip, login_days, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, risk_reason, wx_open_id, ditch_id, app_id, platform_id, power, phone, device_id
        from yt_dyz_user
        where user_id in
        <foreach collection="userIds.split(',')" separator="," item="item"  open="(" close=")">
            #{item}
        </foreach>
    </select>
    <resultMap id="UserAdMap" type="com.ytpm.agent.view.AgentAuditCheckVO">
        <id column="user_id" property="userId" />
        <result column="app_id" property="appId" />
        <result column="nick_name" property="nickName" />
        <collection property="adRecordList" ofType="com.ytpm.app.model.YtDyzAdRecord">
            <result column="record_id" property="recordId" />
            <result column="placement_id" property="placementId" />
            <result column="ad_source_id" property="adSourceId" />
            <result column="ad_source_type" property="adSourceType" />
            <result column="ad_source_index" property="adSourceIndex" />
            <result column="revenue" property="revenue" />
            <result column="ecpm" property="ecpm" />
            <result column="network_form_id" property="networkFormId" />
            <result column="network_name" property="networkName" />
            <result column="network_placement_id" property="networkPlacementId" />
            <result column="finish_time" property="finishTime" />
            <result column="begin_time" property="beginTime" />
        </collection>
    </resultMap>
    <select id="queryTodayUserAd" resultMap="UserAdMap">
        select
            u.user_id,
            u.app_id,
            u.nick_name,
            ar.record_id,
            ar.placement_id,
            ar.ad_source_id,
            ar.ad_source_type,
            ar.ad_source_index,
            ar.revenue,
            ar.ecpm,
            ar.network_form_id,
            ar.network_name,
            ar.network_placement_id,
            ar.finish_time,
            ar.begin_time
        from yt_dyz_ad_record ar
        join yt_dyz_user u on ar.user_id = u.user_id
        where ar.ad_source_type = #{adSourceType} and ar.app_id = #{appId} and ar.user_id in
        <foreach collection="userIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
        and DATE_FORMAT(ar.finish_time, '%Y-%m-%d') = current_date()
    </select>
    <select id="getMonthRegistryUser" resultType="com.ytpm.app.model.YtDyzUser">
        select
        user_id, head_img, nick_name, registry_time, last_login_time, last_login_ip, login_days, total_video, total_income, red_packet_balance, red_packet_amount, points_balance, points_total, withdraw_total, sign_days, user_status, risk_reason, wx_open_id, ditch_id, app_id, platform_id, power, phone, device_id
        from yt_dyz_user
        where app_id in
        <foreach collection="appIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
        <if test="type != null and type == 1">
            and DATE_FORMAT(registry_time, '%Y-%m') = DATE_FORMAT(now(),'%Y-%m')
        </if>
        <if test="type != null and type == 2">
            and DATE_FORMAT(last_login_time, '%Y-%m') = DATE_FORMAT(now(),'%Y-%m')
        </if>
    </select>
    <update id="unlockUser">
        update yt_dyz_user
        set user_status = 1
        where user_status > 1
        and user_id in
        <foreach collection="userIds.split(',')" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>
</mapper>