<?php

namespace app\controller;

use app\BaseController;
use app\model\GameControlAutoRTPConfigModel;
use think\facade\Request;

class GameControlAutoRTPConfig extends BaseController
{
    /**
     * 更新自动RTP配置
     */
    public function updateAutoRTPConfig()
    {

        $float_rate = Request::post('float_rate',0, 'intval');
        $new_user_number = Request::post('new_user_number',0, 'intval');
        $rtp_kill_data = Request::post('rtp_kill_data',[]);

        if( empty($float_rate) || empty($new_user_number) || empty($rtp_kill_data)){
            return json_error([],'参数错误');
        }

        if(count($rtp_kill_data) > 12) {
            return  json_error([],'玩家RTP区间不能超过10个');
        }


        $userInfo = $this->getUserInfo();


        $info = GameControlAutoRTPConfigModel::where(['app_id' => $userInfo['merchant_id']])->find();
        if(empty($info)){
            return json_error([],'参数错误');
        }
        $info->float_rate = bcmul( $float_rate , 100) * 1;
        $info->new_user_number = $new_user_number;


        foreach ($rtp_kill_data as $idx => $item) {
            $item['new'] = ($item['new'] ?? 0) * 100;
            $item['old'] = ($item['old'] ?? 0) * 100;
            $rtp_kill_data[$idx] = $item;
        }
        $info->rtp_kill_data = json_encode( $rtp_kill_data);
        $info->save();

        return json_success([]);
    }

    /**
     * 获取自动RTP配置
     * @return \think\response\Json
     */
    public function getAutoRTPConfigInfo()
    {
        $userInfo = $this->getUserInfo();
        $autoRtpConfig = GameControlAutoRTPConfigModel::getAutoRTPConfigInfo($userInfo['merchant_id']);
        if(empty($autoRtpConfig)){
            return json_success([
                'total' => 0,
                'list' => []
            ]);
        }
        return json_success([
            'total' => 1,
            'list' => [$autoRtpConfig]
        ]);
    }

    /**
     * 更新自动RTP配置状态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateAutoRTPStatus()
    {
        $status = Request::post("status", 0, "trim");
        $id = Request::post("id", 0, "trim");
        if(empty($id)){
            return json_error([],'参数错误');
        }
        $info = GameControlAutoRTPConfigModel::find($id);
        if(empty($info)){
            return json_error([],'参数错误');
        }
        $info->status = $status;
        $info->save();
        return json_success();
    }


}