<?php
namespace app\models\system;

use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

class SystemAwardHistory extends BaseModel {

    use ModelTrait;

    public static function InsertNew($activity, $result, $order_num, $winner_num, $total_paid, $diff_paid, $profit, $rate)
    {
        self::create([
            'activity' => $activity,
            'result' => $result,
            'ts' => time(),
            'order_num' => $order_num,
            'winner_num' => $winner_num,
            'total_paid' => $total_paid,
            'diff_paid' => $diff_paid,
            'profit' => $profit,
            'rate' => $rate,
        ]);
    }

    /**
     * 获取开奖结果
     * 
     * @return array
     */
    public static function getList($activity, $startTime, $stopTime, $page, $limit)
    {
        $model = new self();
        $model = $model::where('1=1');
        if ($activity) {
            $model = $model->where('activity', 'in', $activity);
        }
        if ($startTime) {
            $model = $model->where('ts', '>=', $startTime);
        }

        if ($stopTime) {
            $model = $model->where('ts', '<=', $stopTime);
        }

        if ($page && $limit) {
            $model = $model->page(intval($page), intval($limit));
        }

        $model = $model->field('FROM_UNIXTIME(ts, "%Y-%m-%d %H:%i:%s") as ts, activity, result')->order('ts desc');
        return $model->select()->toArray();
    }
}