<?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.');

function tj() {
    global $cfg_dbprefix, $nxsql;
    $lctime1 = date("H:i:s", mktime(0, 0, 0));
    $lctime2 = date("H:i:s", mktime(0, 5, 0));
    $nowtime = date("H:i:s");
    $today = date("Y-m-d", time());
    $zuoday = date("Y-m-d", strtotime('-1 day'));
    $query = "SELECT COUNT(*) FROM ${cfg_dbprefix}statistics_01 WHERE DATE(time)='$zuoday';";
    if ($result = $nxsql->oneRowVal($query)) {
        return 1;
    }
    $query = "SELECT COUNT(DISTINCT DATE(time)) FROM ${cfg_dbprefix}counter;";
    $result = $nxsql->oneRowVal($query);
    if ($result <= 1) {
        return 2;
    }
    $query = "SELECT DISTINCT DATE(time) FROM ${cfg_dbprefix}counter;";
    if ($result = $nxsql->query($query)) {
        while ($row = $result->fetch_row()) {
            if ($row[0] != $today) {
                $mouday = $row[0];
                $hour_UV = array();
                $hour_IP = array();
                $query = "SELECT COUNT(DISTINCT ip,sw,sh,ua),COUNT(DISTINCT ip),EXTRACT(HOUR FROM time) FROM ${cfg_dbprefix}counter WHERE DATE(time)='$mouday' GROUP BY EXTRACT(HOUR FROM time);";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $hour_UV[$row1[2]] = $row1[0];
                        $hour_IP[$row1[2]] = $row1[1];
                    }
                    $result1->close();
                }
                $hour_NUV = array();
                $query = "SELECT COUNT(DISTINCT ip,sw,sh,ua),EXTRACT(HOUR FROM time) FROM ${cfg_dbprefix}counter WHERE asn=asn1 AND DATE(time)='$mouday' GROUP BY EXTRACT(HOUR FROM time);";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $hour_NUV[$row1[1]] = $row1[0];
                    }
                    $result1->close();
                }
                $hour_P = array();
                $hour_PV = array();
                $hour_SHOW = array();
                $query = "SELECT COUNT(DISTINCT ur),COUNT(DISTINCT ip,sw,sh,ua,time),COUNT(*),EXTRACT(HOUR FROM time) FROM ${cfg_dbprefix}counter WHERE ac=1 AND DATE(time)='$mouday' GROUP BY EXTRACT(HOUR FROM time);";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $hour_P[$row1[3]] = $row1[0];
                        $hour_PV[$row1[3]] = $row1[1];
                        $hour_SHOW[$row1[3]] = $row1[2];
                    }
                    $result1->close();
                }
                $hour_NPV = array();
                $hour_NSHOW = array();
                $query = "SELECT COUNT(DISTINCT c.ip,c.sw,c.sh,c.ua,c.time),COUNT(c.id),EXTRACT(HOUR FROM c.time) FROM ${cfg_dbprefix}counter AS c INNER JOIN ${cfg_dbprefix}gg AS g ON g.id=c.ggid WHERE g.type=0 AND c.ac=1 AND c.ggwh<9 AND DATE(c.time)='$mouday' GROUP BY EXTRACT(HOUR FROM c.time);";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $hour_NPV[$row1[2]] = $row1[0];
                        $hour_NSHOW[$row1[2]] = $row1[1];
                    }
                    $result1->close();
                }
                $hour_CK = array();
                $query = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time),EXTRACT(HOUR FROM time) FROM ${cfg_dbprefix}counter WHERE ac=2 AND DATE(time)='$mouday' GROUP BY EXTRACT(HOUR FROM time);";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $hour_CK[$row1[1]] = $row1[0];
                    }
                    $result1->close();
                }
                $query = "INSERT INTO `${cfg_dbprefix}statistics_01` (time,hour,num01,num02,num03,num04,num05,num06,num07,num08,num09) VALUES ";
                for ($i = 0; $i <= 23; $i++) {
                    empty($hour_UV[$i]) && $hour_UV[$i] = 0;
                    empty($hour_IP[$i]) && $hour_IP[$i] = 0;
                    empty($hour_NUV[$i]) && $hour_NUV[$i] = 0;
                    empty($hour_P[$i]) && $hour_P[$i] = 0;
                    empty($hour_PV[$i]) && $hour_PV[$i] = 0;
                    empty($hour_SHOW[$i]) && $hour_SHOW[$i] = 0;
                    empty($hour_NPV[$i]) && $hour_NPV[$i] = 0;
                    empty($hour_NSHOW[$i]) && $hour_NSHOW[$i] = 0;
                    empty($hour_CK[$i]) && $hour_CK[$i] = 0;
                    $i && $query .= ",";
                    $query .= "('$mouday','$i','$hour_UV[$i]','$hour_IP[$i]','$hour_NUV[$i]','$hour_P[$i]','$hour_PV[$i]','$hour_SHOW[$i]','$hour_NPV[$i]','$hour_NSHOW[$i]','$hour_CK[$i]')";
                }
                $query1 = "SELECT COUNT(DISTINCT ip,sw,sh,ua),COUNT(DISTINCT ip) FROM ${cfg_dbprefix}counter WHERE DATE(time)='$mouday';";
                $result1 = $nxsql->oneRowVal($query1);
                $num_UV = $result1[0];
                $num_IP = $result1[1];
                $query1 = "SELECT COUNT(DISTINCT ip,sw,sh,ua) FROM ${cfg_dbprefix}counter WHERE asn=asn1 AND DATE(time)='$mouday';";
                $result1 = $nxsql->oneRowVal($query1);
                $num_NUV = $result1;
                $query1 = "SELECT COUNT(DISTINCT ur),COUNT(DISTINCT ip,sw,sh,ua,time),COUNT(*) FROM ${cfg_dbprefix}counter WHERE ac=1 AND DATE(time)='$mouday';";
                $result1 = $nxsql->oneRowVal($query1);
                $num_P = $result1[0];
                $num_PV = $result1[1];
                $num_SHOW = $result1[2];
                $query1 = "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 g.type=0 AND c.ac=1 AND c.ggwh<9 AND DATE(c.time)='$mouday';";
                $result1 = $nxsql->oneRowVal($query1);
                $num_NPV = $result1[0];
                $num_NSHOW = $result1[1];
                $query1 = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) FROM ${cfg_dbprefix}counter WHERE ac=2 AND DATE(time)='$mouday';";
                $result1 = $nxsql->oneRowVal($query1);
                $num_CK = $result1;
                $query .= ",('$mouday','24','$num_UV','$num_IP','$num_NUV','$num_P','$num_PV','$num_SHOW','$num_NPV','$num_NSHOW','$num_CK');";
                $query1 = "SELECT COUNT(*) FROM ${cfg_dbprefix}statistics_01 WHERE DATE(time)='$mouday';";
                $result1 = $nxsql->oneRowVal($query1);
                if ($result1 == 0) {
                    $nxsql->query($query);
                }
                $query = "SELECT DISTINCT ggid FROM ${cfg_dbprefix}counter WHERE DATE(time)='$mouday';";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $ggid = $row1[0];
                        $query2 = "SELECT COUNT(DISTINCT ip,sw,sh,ua),COUNT(DISTINCT ip) FROM ${cfg_dbprefix}counter WHERE ggid='$ggid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_UV = $result2[0];
                        $num_IP = $result2[1];
                        $query2 = "SELECT COUNT(DISTINCT ip,sw,sh,ua) FROM ${cfg_dbprefix}counter WHERE asn=asn1 AND ggid='$ggid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_NUV = $result2;
                        $query2 = "SELECT COUNT(DISTINCT ur),COUNT(DISTINCT ip,sw,sh,ua,time),COUNT(*) FROM ${cfg_dbprefix}counter WHERE ac=1 AND ggid='$ggid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_P = $result2[0];
                        $num_PV = $result2[1];
                        $num_SHOW = $result2[2];
                        $query2 = "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 g.type=0 AND c.ac=1 AND c.ggwh<9 AND c.ggid='$ggid' AND DATE(c.time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_NPV = $result2[0];
                        $num_NSHOW = $result2[1];
                        $query2 = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) FROM ${cfg_dbprefix}counter WHERE ac=2 AND ggid='$ggid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_CK = $result2;
                        $query2 = "SELECT COUNT(*) FROM ${cfg_dbprefix}statistics_02 WHERE AND ggid='$ggid' DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        if ($result2 == 0) {
                            $query2 = "INSERT INTO ${cfg_dbprefix}statistics_02 (gg_id,date,num01,num02,num03,num04,num05,num06,num07,num08,num09) VALUES ('$ggid','$mouday','$num_UV','$num_IP','$num_NUV','$num_P','$num_PV','$num_SHOW','$num_NPV','$num_NSHOW','$num_CK');";
                            $nxsql->query($query2);
                        }
                    }
                    $result1->close();
                }
                $query = "SELECT DISTINCT ggwid FROM ${cfg_dbprefix}counter WHERE DATE(time)='$mouday';";
                if ($result1 = $nxsql->query($query)) {
                    while ($row1 = $result1->fetch_row()) {
                        $ggwid = $row1[0];
                        $query2 = "SELECT COUNT(DISTINCT ip,sw,sh,ua),COUNT(DISTINCT ip) FROM ${cfg_dbprefix}counter WHERE ggwid='$ggwid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_UV = $result2[0];
                        $num_IP = $result2[1];
                        $query2 = "SELECT COUNT(DISTINCT ip,sw,sh,ua) FROM ${cfg_dbprefix}counter WHERE asn=asn1 AND ggwid='$ggwid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_NUV = $result2;
                        $query2 = "SELECT COUNT(DISTINCT ur),COUNT(DISTINCT ip,sw,sh,ua,time),COUNT(*) FROM ${cfg_dbprefix}counter WHERE ac=1 AND ggwid='$ggwid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_P = $result2[0];
                        $num_PV = $result2[1];
                        $num_SHOW = $result2[2];
                        $query2 = "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 g.type=0 AND c.ac=1 AND c.ggwh<9 AND c.ggwid='$ggwid' AND DATE(c.time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_NPV = $result2[0];
                        $num_NSHOW = $result2[1];
                        $query2 = "SELECT COUNT(DISTINCT ip,ggwid,ggid,ggsn,ggsn1,ggsn2,ur,fr,sw,sh,ua,time) FROM ${cfg_dbprefix}counter WHERE ac=2 AND ggwid='$ggwid' AND DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        $num_CK = $result2;
                        $query2 = "SELECT COUNT(*) FROM ${cfg_dbprefix}statistics_02 WHERE AND ggwid='$ggwid' DATE(time)='$mouday';";
                        $result2 = $nxsql->oneRowVal($query2);
                        if ($result2 == 0) {
                            $query2 = "INSERT INTO ${cfg_dbprefix}statistics_02 (ggw_id,date,num01,num02,num03,num04,num05,num06,num07,num08,num09) VALUES ('$ggwid','$mouday','$num_UV','$num_IP','$num_NUV','$num_P','$num_PV','$num_SHOW','$num_NPV','$num_NSHOW','$num_CK');";
                            $nxsql->query($query2);
                        }
                    }
                    $result1->close();
                }
            }
        }
        if ($nowtime < $lctime2 && $nowtime > $lctime1) {
            $sql = "TRUNCATE TABLE ${cfg_dbprefix}counter;";
            $nxsql->query($sql);
        } else {
            $sql = "DELETE FROM ${cfg_dbprefix}counter WHERE DATE(time)<>'$today';";
            $nxsql->query($sql);
        }
        $result->close();
    }
    return 3;
}
