<?php

/*
 * ----------------------------------------------------
 * 程序名称: 牛叉广告管理系统(NiuXams)
 * 程序功能: 实现专业的广告投放、管理和统计等管理系统！
 * 程序开发: 牛叉软件(NiuXSoft.com)
 * 版权所有: [NiuXams] (C)2013-2099 NiuXSoft.com
 * 官方网站: www.niuxsoft.com  Email: niuxsoft@163.com
 * ----------------------------------------------------
 * 使用条款:
 * 1.为构建创新型经济社会,请尊重保护知识产权！
 * 2.该软件个人非商业用途免费使用.
 * 3.免费使用禁止修改版权信息和官方推广链接.
 * 4.禁止任何衍生版本.禁止任何非法修改源代码贩卖.
 * ----------------------------------------------------
 */

defined('NIUX_INCLUDED') or exit('Access Denied.');
$cfg_autoTj && tj();
$ggid = empty($_POST['ggid']) ? (empty($_GET['ggid']) ? 0 : $_GET['ggid']) : $_POST['ggid'];
is_int($ggid) || $ggid >= 0 || die('非法ID输入！');
$ggwid = empty($_POST['ggwid']) ? (empty($_GET['ggwid']) ? 0 : $_GET['ggwid']) : $_POST['ggwid'];
is_int($ggwid) || $ggwid >= 0 || die('非法ID输入！');
$title = ($ggwid ? '广告位id：' . $ggwid : '广告id：' . $ggid) . ' 统计 ' . date("Y-m-d");
$id_q = ($ggwid ? "ggwid='$ggwid'" : "ggid='$ggid'");
$query = "SELECT COUNT(DISTINCT ip,sw,sh,ua),COUNT(DISTINCT ip) FROM ${cfg_dbprefix}counter WHERE $id_q AND DATE(time)=CURDATE();";
$result = $nxsql->oneRowVal($query);
$num_UV = $result[0];
$num_IP = $result[1];
$query = "SELECT COUNT(DISTINCT ip,sw,sh,ua) FROM ${cfg_dbprefix}counter WHERE $id_q AND asn=asn1 AND DATE(time)=CURDATE();";
$result = $nxsql->oneRowVal($query);
$num_NUV = $result;
$query = "SELECT COUNT(DISTINCT ur),COUNT(DISTINCT ip,sw,sh,ua,time),COUNT(*) FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE();";
$result = $nxsql->oneRowVal($query);
$num_P = $result[0];
$num_PV = $result[1];
$num_SHOW = $result[2];
$query = "SELECT COUNT(DISTINCT c.ip,c.sw,c.sh,c.ua,c.time),COUNT(c.id) FROM ${cfg_dbprefix}counter AS c INNER JOIN ${cfg_dbprefix}gg AS g ON g.id=c.ggid WHERE c.$id_q AND g.type=0 AND c.ac=1 AND c.ggwh<9 AND DATE(c.time)=CURDATE();";
$result = $nxsql->oneRowVal($query);
$num_NPV = $result[0];
$num_NSHOW = $result[1];
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE();";
$result = $nxsql->oneRowVal($query);
$num_CK = $result;
$top_ur_s = '';
$query = "SELECT COUNT(*) AS Number,ur FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY ur ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_ur_s .= '<tr><td><a href="' . htmlspecialchars($row[1]) . '" title="' . htmlspecialchars($row[1]) . '" target="_blank">' . substr(htmlspecialchars($row[1]), -75, 75) . '</a></td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_ur_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,ur FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY ur ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_ur_c .= '<tr><td><a href="' . htmlspecialchars($row[1]) . '" title="' . htmlspecialchars($row[1]) . '" target="_blank">' . substr(htmlspecialchars($row[1]), -75, 75) . '</a></td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_fr_s = '';
$query = "SELECT COUNT(*) AS Number,fr FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY fr ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_fr_s .= '<tr><td>' . ($row[1] ? '<a href="' . htmlspecialchars($row[1]) . '" title="' . htmlspecialchars($row[1]) . '" target="_blank">' . substr(htmlspecialchars($row[1]), -75, 75) . '</a>' : '直接访问') . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_fr_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,fr FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY fr ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_fr_c .= '<tr><td>' . ($row[1] ? '<a href="' . htmlspecialchars($row[1]) . '" title="' . htmlspecialchars($row[1]) . '" target="_blank">' . substr(htmlspecialchars($row[1]), -75, 75) . '</a>' : '直接访问') . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_ggw_s = '';
$query = "SELECT COUNT(*) AS Number,ggwid FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY ggwid ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_ggw_s .= '<tr><td>' . $cfg_forum . '-' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_ggw_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,ggwid FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY ggwid ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_ggw_c .= '<tr><td>' . $cfg_forum . '-' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_gg_s = '';
$query = "SELECT COUNT(*) AS Number,ggid FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY ggid ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_gg_s .= '<tr><td>' . $cfg_thread . '-' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_gg_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,ggid FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY ggid ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_gg_c .= '<tr><td>' . $cfg_thread . '-' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_sc_s = '';
$query = "SELECT COUNT(*) AS Number,sw,sh FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY sw,sh ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_sc_s .= '<tr><td>' . $row[1] . '*' . $row[2] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_sc_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,sw,sh FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY sw,sh ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_sc_c .= '<tr><td>' . $row[1] . '*' . $row[2] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_b_s = '';
$query = "SELECT COUNT(*) AS Number,bn,bv FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY bn,bv ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_b_s .= '<tr><td>' . $row[1] . '：' . $row[2] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_b_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,bn,bv FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY bn,bv ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_b_c .= '<tr><td>' . $row[1] . '：' . $row[2] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_os_s = '';
$query = "SELECT COUNT(*) AS Number,os FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY os ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_os_s .= '<tr><td>' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_os_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,os FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY os ORDER BY Number DESC LIMIT 10;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_os_c .= '<tr><td>' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_la_s = '';
$query = "SELECT COUNT(*) AS Number,la FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=1 AND DATE(time)=CURDATE() GROUP BY la ORDER BY Number DESC LIMIT 5;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_la_s .= '<tr><td>' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$top_la_c = '';
$query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) AS Number,la FROM ${cfg_dbprefix}counter WHERE $id_q AND ac=2 AND DATE(time)=CURDATE() GROUP BY la ORDER BY Number DESC LIMIT 5;";
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $top_la_c .= '<tr><td>' . $row[1] . '</td><td>' . $row[0] . '</td></tr>';
    }
    $result->close();
}
$id_q = ($ggwid ? "ggw_id='$ggwid'" : "gg_id='$ggid'");
$list = '';
$query = "SELECT COUNT(*),SUM(num01),SUM(num02),SUM(num03),SUM(num04),SUM(num05),SUM(num06),SUM(num07),SUM(num08),SUM(num09) FROM ${cfg_dbprefix}statistics_02 WHERE $id_q;";
if ($result = $nxsql->oneRowVal($query)) {
    $list .= '<tr>';
    $list .= '<td class="ui-widget-content tc">合计' . $result[0] . '天</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[1] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[3] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . ($result[1] - $result[3]) . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[2] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[4] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[5] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[6] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[7] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[8] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . $result[9] . '</td>';
    $list .= '<td class="ui-widget-content tc">' . round($result[9] / ($result[6] ? $result[6] : 1), 4) * 100 . '%</td>';
    $list .= '<td class="ui-widget-content tc">' . round($result[9] / ($result[1] ? $result[1] : 1), 4) * 100 . '%</td>';
    $list .= '</tr>';
}
$desc = empty($_GET['desc']) ? 'DESC' : '';
$limit = empty($_GET['limit']) ? 7 : (intval($_GET['limit']) ? abs(intval($_GET['limit'])) : 7);
$page = empty($_GET['page']) ? 1 : (intval($_GET['page']) ? abs(intval($_GET['page'])) : 1);
$search = empty($_GET['search']) ? '' : substr(insql($_GET['search']), 0, 255);
$searchSql = $search ? " AND (date LIKE '%{$search}%' OR id LIKE '%{$search}%')" : '';
$order = empty($_GET['order']) ? 'date' : substr(insql($_GET['order']), 0, 8);
$query = "SELECT COUNT(id) FROM ${cfg_dbprefix}statistics_02 WHERE $id_q$searchSql;";
$total = $nxsql->oneRowVal($query);
$pages = ceil($total / $limit);
$page > $pages && $page = $pages;
$begin = $page * $limit - $limit;
$query = "SELECT * FROM ${cfg_dbprefix}statistics_02 WHERE $id_q$searchSql ORDER BY $order $desc LIMIT $begin, $limit;";
if ($total) {
    if ($result = $nxsql->query($query)) {
        while ($row = $result->fetch_row()) {
            $list .= '<tr>';
            $list .= '<td class="ui-widget-content tc">' . $row[3] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[4] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[6] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . ($row[4] - $row[6]) . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[5] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[7] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[8] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[9] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[10] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[11] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . $row[12] . '</td>';
            $list .= '<td class="ui-widget-content tc">' . round($row[12] / ($row[9] ? $row[9] : 1), 4) * 100 . '%</td>';
            $list .= '<td class="ui-widget-content tc">' . round($row[12] / ($row[4] ? $row[4] : 1), 4) * 100 . '%</td>';
            $list .= '</tr>';
        }
        $result->close();
    }
}
$subPageNums = $page < 1000 ? 10 : ($page < 10000 ? 7 : 5);
$subPageLink = 'tj_gg_id.php?desc=' . ($desc ? '' : 1) . '&ggid=' . $ggid . '&ggwid=' . $ggwid . '&limit=' . $limit . '&order=' . $order . '&search=' . $search . '&page=';
$fenye = new Fenye($total, $limit, $page, $subPageNums, $subPageLink, 6);
