<?php

namespace app\common\model\Home;

use app\common\model\TeachersStrength\Teacher as ModelTeacher;
use app\common\model\ExaminationInformation\InfoArticle as ModelinfoArticle;
use app\common\model\Works\Works as ModelWorks;
use app\common\model\Flow\Flow as ModelFlow;
use app\common\model\ContactUs\Signup as ModelSignup;
use app\common\model\ContactUs\LeaveMessage as ModelLeaveMessage;

class Home
{
    /**
     * 获取主页数据
     */
    public static function getData(): array
    {
        //今日日期时间
        $today = date('Y-m-d');
        //今年各个年份(时间戳)数据数量关联数组
        $monthly_data = self::buildMonthsTsOfThisYearData();

        //数据返回
        return [
            'data_preview' => [
                'teacher' => self::getTodayYesterdayNumContrastInfo(ModelTeacher::class, $today),
                'info_article' => self::getTodayYesterdayNumContrastInfo(ModelinfoArticle::class, $today),
                'works' => self::getTodayYesterdayNumContrastInfo(ModelWorks::class, $today),
                'flow' => self::getTodayYesterdayNumContrastInfo(ModelFlow::class, $today)
            ],
            'statistics' => [
                'signup' => self::getMonthlysDataStatistics(ModelSignup::class, $monthly_data),
                'leave_message' => self::getMonthlysDataStatistics(ModelLeaveMessage::class, $monthly_data),
            ]
        ];
    }

    /**
     * 获取月份数据统计
     * 
     * @param string $model 模型层命名空间
     * @param array $monthly_data 今年各个年份(时间戳)数据数量关联数组
     * 
     */
    private static  function getMonthlysDataStatistics(string $model, array $monthly_data): array
    {
        //模型层实例
        $model = new $model();

        //新增时间字段名
        $create_time_field = self::buildCreateTimeField($model);

        //数据查询
        $select = $model->field([$create_time_field])->select()->toArray();

        //各个月份数据统计
        foreach ($select as $value) {
            $create_monthly_time_ts = strtotime(date('Y-m', strtotime($value[$create_time_field])));
            foreach ($monthly_data as $monthly_time_ts => &$monthly_time_ts_value) {
                if ($monthly_time_ts == $create_monthly_time_ts) {
                    $monthly_time_ts_value++;
                    break;
                }
            };
        };

        //最终数据
        $data = array_values($monthly_data);

        return  $data;
    }

    /**
     * 获取今昨日新增数量对比信息
     * 
     * @param string $model 模型层命名空间
     * @param string $today 今日日期时间
     * 
     * @return array [
     *                  'count'  总数
     *                  'percentage' 今昨升降百分比数
     *               ]
     */
    private static  function getTodayYesterdayNumContrastInfo(string $model, string $today): array
    {
        //模型层实例
        $model = new $model();

        //新增时间字段名
        $create_time_field = self::buildCreateTimeField($model);

        //数据查询
        $data = $model->field([$create_time_field])->select()->toArray();

        //今日总数获取
        $today_total_count = count($data);

        //昨日总数获取
        $yesterday_total_count = 0;
        foreach ($data as $value) {
            $create_time_ts = strtotime($value[$create_time_field]);
            if ($create_time_ts < strtotime($today)) {
                $yesterday_total_count++;
            }
        };

        //上升百分比计算
        $dividend =  ($today_total_count - $yesterday_total_count);
        $divisor =  $yesterday_total_count == 0 ? 1 : $yesterday_total_count;
        $percentage = round(($dividend /  $divisor) * 100, 2);

        return [
            'count' => $today_total_count,
            'percentage' => $percentage,
        ];
    }

    /**
     * 构建新增时间字段
     * 
     * @param \think\Model $model 模型实例
     */
    private static function buildCreateTimeField(\think\Model $model): string
    {
        //获取表名
        $table_name = $model->getTable();
        //新增字段名
        $create_time_field = "{$table_name}_create_time";

        return  $create_time_field;
    }

    /**
     * 构建今年月份(时间戳)关联数组
     * 
     */
    private static function buildMonthsTsOfThisYearData(): array
    {
        //今年年份
        $year = date('Y');

        //月份(时间戳)数组构建
        $monthly_data = [];
        for ($i = 1; $i <= 12; $i++) {
            $monthly_data[strtotime("{$year}-$i")] = 0;
        }
        return  $monthly_data;
    }
}
