<?php


namespace app\models\system;


use app\models\merchant\Merchant;
use app\models\store\StoreOrder;
use app\models\user\User;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class SystemDailyReport extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'system_daily_report';

    use ModelTrait;

    /**
     * 记录昨天的记录
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function recordYesterdayReport()
    {
        if (self::where('report_date', date('Y-m-d', strtotime('-1day')))->find()) {
            return;
        }
        $add = [
            'report_date' => date('Y-m-d', strtotime('-1day')),
            'add_time' => time(),
            'today_reg' => User::whereDay('add_time', 'yesterday')->count(),
            'today_visit' => User::whereDay('last_time', 'yesterday')->count(),
            'today_order' => StoreOrder::whereDay('add_time', 'yesterday')->count(),
            'today_order_money' => StoreOrder::where('paid', 1)
                ->where('is_del', 0)
                ->where('refund_status', 0)
                ->whereDay('pay_time', 'yesterday')
                ->sum('pay_price'),];
        self::create($add);
    }
}