<?php


namespace App\Models;


use App\Http\helper\NumConfig;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

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

    public $timestamps = false;


    protected $guarded = [];


    // 添加记录
    public static function record_add($user_id, $admin_id, $content, $score)
    {
        // 查找之前有没有配置过

        $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $user_id)->first();


        if ($query) {

            $EffectiveScore = number_float($query->EffectiveScore / NumConfig::NUM_VALUE);
            $ControlScore = number_float($query->ControlScore / NumConfig::NUM_VALUE);
            $prefix = $ControlScore < 0 ? $EffectiveScore . '/' . $ControlScore . '输' : '赢' . $EffectiveScore . '/' . $ControlScore;
            $UserControlKind = DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->where('UserID', $user_id)->get();

            $data = [];
            $GameData = (new Control())->GameData;
            $GameData = array_flip($GameData);
            foreach ($UserControlKind as $value) {

                if (!isset($GameData[$value->KindID])) {
                    continue;
                }

                $data[$GameData[$value->KindID]] = number_float($value->ControlRadian) . '%';
//                if ($value->KindID == 1005) {
//                    $data['TP'] = number_float($value->ControlRadian) . '%';
//                }
//                if ($value->KindID == 2061) {
//                    $data['TPAK47'] = number_float($value->ControlRadian) . '%';
//                }
//                if ($value->KindID == 2060) {
//                    $data['TPJOKER'] = number_float($value->ControlRadian) . '%';
//                }
//                if ($value->KindID == 2030) {
//                    $data['Rummy5'] = number_float($value->ControlRadian ) . '%';
//                }
//                if ($value->KindID == 2050) {
//                    $data['Rummy2'] = number_float($value->ControlRadian) . '%';
//                }

            }

            $str = '';
            foreach ($data as $key => $value) {

                $str .= $key . '：' . $value . '，';

            }


            $before_config = $prefix . '/' . rtrim( $str,'，');

        }

        $before_config = $before_config ?? '';
        $data = [
            'user_id' => $user_id,
            'admin_id' => $admin_id,
            'contents' => $content,
            'before_config' => $before_config,
            'created_at' => date('Y-m-d H:i:s'),
            'score' => $score
        ];

        DB::connection('write')->table('agent.dbo.control_record')->insert($data);

        // 大于5条以外的数据删除 -- 只保留最新的5条记录
        DB::connection('write')->table('agent.dbo.control_record')
            ->whereNotIn('id', function ($query) use ($user_id) {
                $query->from('agent.dbo.control_record')
                    ->selectRaw('id')
                    ->where('user_id', $user_id)
                    ->orderBy('created_at', 'desc')
                    ->limit(5);
            })->where('user_id', $user_id)
            ->delete();

    }

}
