<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class AdminScore extends Model
{
    const TABLE = 'agent.dbo.admin_score';
    protected $table = self::TABLE;

    public $timestamps = false;


    protected $guarded = [];

    // 添加记录
    public static function add_score($admin, $change_score, $type)
    {

        switch ($type) {
            case 1:
                $before_score = $admin->lottery_amount;

                // 减少管理员金额
                $after_score = $before_score - $change_score;
                $admin_data = ['lottery_amount' => $after_score];
                break;
            case 2:
                $before_score = $admin->recharge_amount;

                // 减少管理员金额
                $after_score = $before_score - $change_score;
                $admin_data = ['recharge_amount' => $after_score];
                break;
            default:
                return;
        }


        // 添加记录
        $data = [
            'before_score' => $before_score,
            'after_score' => $after_score,
            'change_score' => $change_score,
            'admin_id' => $admin->id,
            'type' => $type,
            'created_at' => date('Y-m-d H:i:s')
        ];
        self::create($data);

        DB::table('agent.dbo.admin_users')->where('id', $admin->id)->update($admin_data);
    }
}
