<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\modules\stats\repository;

use app\repository\BaseRepository;
use think\facade\Db;

/**
 * Stats Repository
 * @package Applications\sample\modules\stats\repository
 */
class StatsRepository extends BaseRepository
{
    /**
     * 获取概览指标
     * @return array
     */
    public function overview(): array
    {
        return [
            'articles' => [
                'total' => (int)Db::name('sample_article')->count(),
                'published' => (int)Db::name('sample_article')->where('status', 1)->count(),
                'disabled' => (int)Db::name('sample_article')->where('status', 0)->count(),
            ],
            'categories' => [
                'total' => (int)Db::name('sample_category')->count(),
                'enabled' => (int)Db::name('sample_category')->where('status', 1)->count(),
                'disabled' => (int)Db::name('sample_category')->where('status', 0)->count(),
            ],
            'orders' => [
                'total' => (int)Db::name('sample_order')->count(),
                'paid' => (int)Db::name('sample_order')->where('status', 1)->count(),
                'pending' => (int)Db::name('sample_order')->where('status', 0)->count(),
            ],
            'income' => [
                'paid_amount' => (float)Db::name('sample_order')->where('status', 1)->sum('amount'),
            ],
        ];
    }

    /**
     * 获取待办提醒
     * @return array
     */
    public function todo(): array
    {
        return [
            'pending_orders' => (int)Db::name('sample_order')->where('status', 0)->count(),
            'disabled_articles' => (int)Db::name('sample_article')->where('status', 0)->count(),
            'disabled_categories' => (int)Db::name('sample_category')->where('status', 0)->count(),
        ];
    }

    /**
     * 获取趋势数据
     * @param int $days
     * @return array
     */
    public function trend(int $days = 7): array
    {
        $days = max(1, $days);
        $endDate = date('Y-m-d');
        $startDate = date('Y-m-d', strtotime('-' . ($days - 1) . ' days'));

        $rows = Db::name('sample_order')
            ->field("DATE(create_time) AS stat_date, COUNT(*) AS order_count, SUM(CASE WHEN status = 1 THEN amount ELSE 0 END) AS paid_amount")
            ->whereBetweenTime('create_time', $startDate . ' 00:00:00', $endDate . ' 23:59:59')
            ->group('stat_date')
            ->select()
            ->toArray();

        $statMap = [];
        foreach ($rows as $row) {
            $dateKey = (string)($row['stat_date'] ?? '');
            $statMap[$dateKey] = [
                'order_count' => (int)($row['order_count'] ?? 0),
                'paid_amount' => (float)($row['paid_amount'] ?? 0),
            ];
        }

        $dates = [];
        $orderCounts = [];
        $paidAmounts = [];
        for ($i = $days - 1; $i >= 0; $i--) {
            $dateKey = date('Y-m-d', strtotime('-' . $i . ' days'));
            $dates[] = $dateKey;
            $orderCounts[] = (int)($statMap[$dateKey]['order_count'] ?? 0);
            $paidAmounts[] = (float)($statMap[$dateKey]['paid_amount'] ?? 0);
        }

        return [
            'dates' => $dates,
            'order_counts' => $orderCounts,
            'paid_amounts' => $paidAmounts,
        ];
    }
}
