<?php

namespace App\Models;

class Prediction extends BaseModel
{
    protected $table = 'prediction';
    protected $fillable = ['issue_no', 'size', 'odd_or_even', 'is_valid', 'winning_numbers'];


    //预测
    static function prediction($issueNo)
    {
        $size = mt_rand(0, 1);
        $oddOrEven = mt_rand(0, 1);
        return static::create([
            'issue_no' => $issueNo,
            'size' => $size,
            'odd_or_even' => $oddOrEven
        ]);
    }

    //预测结果
    static function result($issueNo, $size, $oddOrEven, $winningNumbers)
    {
        $data = static::where('issue_no', $issueNo)->first();
        if (!$data) $data = static::prediction($issueNo);
        $size = $size == '大' ? 1 : 0;
        $oddOrEven = $oddOrEven == '双' ? 1 : 0;
        $data->is_valid = 0;
        if ($data->size == $size || $data->odd_or_even == $oddOrEven) {
            $data->is_valid = 1;
        }
        $data->winning_numbers = $winningNumbers;
        $data->save();

    }


    function getWinningNumbersAttribute($value)
    {
        if (!empty($value)) {
            $value = explode(',', $value);
            $value = array_map('intval', $value);
            $value[] = array_sum($value);
            return $value;
        }
        return [];
    }


}