<?php
namespace Admin\Api;

use Admin\Common\Api;
use Admin\Domain\StatReport as StatReportDomain;

/**
 * 统计报表
 * @dogstar 20210316
 */
class StatReport extends Api {

    public function getRules() {
        return array(
            'getAppDailyStatReport' => array(
                'statAppKey' => array('name' => 'stat_app_key', 'default' => '', 'desc' => '为空时表示全部汇总'),
                'startDay' => array('name' => 'start_day', 'default' => '', 'desc' => '默认为当前月初'),
                'endDay' => array('name' => 'end_day', 'default' => '', 'desc' => '默认为当天'),
            ),
            'getAppStatReport' => array(
                'statAppKey' => array('name' => 'stat_app_key', 'default' => '', 'desc' => '为空时表示全部汇总'),
                'startDay' => array('name' => 'start_day', 'default' => '', 'desc' => '默认为当前月初'),
                'endDay' => array('name' => 'end_day', 'default' => '', 'desc' => '默认为当天'),
            ),
        );
    }

    /**
     * 汇总报表
     */
    public function getAppDailyStatReport() {
        $rs = array();

        $statAppKey = $this->statAppKey;
        $startDay = $this->startDay ? date('Y-m-d', strtotime($this->startDay)) : date('Y-m-d', strtotime('30 day ago'));
        $endDay = $this->endDay ? date('Y-m-d', strtotime($this->endDay)) : date('Y-m-d');

        $domain = new StatReportDomain();
        $report = $domain->getAppDailyStatReport($statAppKey, $startDay, $endDay);

        // 压入今天的，好对比
        $today = date('Y-m-d');
        $todayReport = $domain->calOneAppDailyStat($today, $statAppKey);
        $todayReport['stat_day'] = $today;

        $rs['report'] = array_merge(array($todayReport), $report);

        $yesterday = date('Y-m-d', strtotime($endDay) - 86400);
        $statDataYesterday = $domain->getActiveAppDailyStatReport($yesterday);
        $rs['report_yesterday'] = $statDataYesterday;

        return $rs;
    }

    /**
     * 应用统计
     */
    public function getAppStatReport() {
        $rs = array();

        $statAppKey = $this->statAppKey;
        $startDay = $this->startDay ? date('Y-m-d', strtotime($this->startDay)) : date('Y-m-d', strtotime('30 day ago'));
        $endDay = $this->endDay ? date('Y-m-d', strtotime($this->endDay)) : date('Y-m-d');

        $domain = new StatReportDomain();
        $report = $domain->getAppDailyStatReport($statAppKey, $startDay, $endDay);

        $rs['report'] = $report;

        return $rs;
    }
}
