<?php
namespace Admin\Domain;

use Base\Model\Member as MemberModel;
use Base\Model\AppDailyStat as AppDailyStatModel;
use Base\Model\Apps as AppsModel;
use Base\Model\Requests as RequestsModel;
use Mall\Model\Mall\Order as OrderModel;

/**
 * 报表统计
 * @dogstar 20210311
 */
class StatReport {
    /**
     * 统计全站的数据
     */
    public function calWholeAppDailyStat($statDay) {
        $app_key = '';
        $data = $this->calOneAppDailyStat($statDay, $app_key);
        $this->recordAppDailyStat($statDay, $app_key, $data);
    }

    /**
     * 分别统计每个应用的数据
     */
    public function calAllEachAppDailyStat($statDay) {
        // 取全部app_key
        $appKeyList = AppsModel::notorm()->select('id,app_key')->fetchPairs('id', 'app_key');
        foreach ($appKeyList as $app_key) {
            $data = $this->calOneAppDailyStat($statDay, $app_key);
            $this->recordAppDailyStat($statDay, $app_key, $data);
        }
    }

    /**
     * 统计一个应用的数据
     * @param string $app_key 为空时表示全部
     */
    public function calOneAppDailyStat($statDay, $app_key = '') {
        $where = array(
            'id > ?' => 0,
        );

        // 不带时间，不分应用
        $total_user = empty($app_key) ? MemberModel::model()->count($where) : 0; // 具体应用不分用户 

        $total_order = empty($app_key) ? OrderModel::model()->count($where) : 0;

        if (!empty($app_key)) {
            $where['app_key'] = $app_key;
        }

        // 带统计时间，截至时间的汇总
        $where['add_time < ?'] = $statDay . ' 23:59:59';

        $total_app = AppsModel::model()->count($where);

        // 统计流量-API，只限当天
        $whereRL = $where;
        $whereRL['request_time >= ?'] = $statDay . ' 00:00:00';
        $whereRL['request_time < ?'] = $whereRL['add_time < ?'];
        unset($whereRL['add_time < ?']);

        $app_key_visit = RequestsModel::model()->count($whereRL, 'DISTINCT app_key');
        $user_visit = RequestsModel::model()->count($whereRL, 'DISTINCT uid');
        $api_request_times = RequestsModel::model()->count($whereRL);

        $rs = [
            'total_order' => intval($total_order),
            'total_user' => intval($total_user),
            'total_app' => intval($total_app),
            'app_key_visit' => intval($app_key_visit),
            'user_visit' => intval($user_visit),
            'api_request_times' => intval($api_request_times),
        ];
        // var_dump($rs);

        return $rs;
    }

    public function recordAppDailyStat($statDay, $app_key = '', $data = array()) {
        if (empty($data)) {
            return;
        }

        $where = array('app_key' => $app_key, 'stat_day' => $statDay);
        $record = AppDailyStatModel::model()->getData($where, array(), 'id');

        if ($record) {
            AppDailyStatModel::model()->update($record['id'], $data);
        } else {
            $data = array_merge($data, $where);
            $data['add_time'] = date('Y-m-d H:i:s');
            AppDailyStatModel::model()->insert($data);
        }
    }

    public function getAppDailyStatReport($statAppKey, $startDay, $endDay) {
        // ak可选
        $statAppKey = strval($statAppKey);
        $orm = AppDailyStatModel::notorm();
        if ($statAppKey !== null) {
            $orm->where('app_key', $statAppKey);
        }
        return $orm
            ->where('stat_day >= ?', $startDay)
            ->where('stat_day <= ?', $endDay)
            ->order('id DESC')
            ->fetchAll();
    }

    public function getActiveAppDailyStatReport($day) {
        return AppDailyStatModel::notorm()->where('app_key <> ?', '')
            ->where('stat_day', $day)
            ->where('api_request_times > 0')
            ->order('api_request_times DESC')
            ->fetchAll();
    }
}
