<?php

namespace app\controller;

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

class GameImproveRtpGlobal extends BaseController
{
    public function getImproveRtpGlobalList()
    {
        $userInfo = $this->getUserInfo();
        $info = GameImproveRtpGlobalModel::find($userInfo['merchant_id'])->toArray();

        $info['evaluation_period_text'] = [
          '1' => '每日',
          '2' => '每周',
          '3' => '每月',
          '4' => '终生',
          '5' => '自定义时间'
        ][$info['evaluation_period']] ?? "";

        if(!empty($info['custom_time_start']) && !empty($info['custom_time_end'])) {
            $info['custom_time_start'] = date('Y-m-d H:i:s', $info['custom_time_start']);
            $info['custom_time_end'] = date('Y-m-d H:i:s', $info['custom_time_end']);
        }

        return json_success(['total' => 1, 'list' => [$info]]);
    }

    public function updateImproveRtpGlobalStatus()
    {
        $status = Request::param('status', '', 'trim');
        if(!in_array($status, ['0', '1'])){
            return json_error([], '参数错误');
        }
        $userInfo = $this->getUserInfo();
        $info = GameImproveRtpGlobalModel::find($userInfo['merchant_id']);
        $info->status = $status;
        $info->save();
        return json_success([],'操作成功');
    }

    public function updateImproveRtpGlobalInfo()
    {
        $userInfo = $this->getUserInfo();

        try{
            $data = Request::param([
                'net_income' => 100, // 设定玩家在游戏内盈利值达到后关闭功能
                'turnover_multiple' => 5, //达到设定流水倍数关闭提升状态
                'evaluation_period' => 4, // 系统控制的触发周期选择
                'effective_count' => 1, //周期内可被系统控制的次数
                'trigger_interval_rounds' => 300, // 每次系统控制间隔局数
            ], 'post', 'trim');
            foreach ($data as $k => $v) {
                if(empty($v)) {
                    return json_error([], '参数错误');
                }
            }
            $custom_time_start = Request::post('custom_time_start', '', 'trim');
            $custom_time_end = Request::post('custom_time_end', '', 'trim');
            if($data['evaluation_period'] == 5) {
                if(empty($custom_time_start) || empty($custom_time_end)) {
                    return json_error([], '参数错误');
                }
            }


            $info = GameImproveRtpGlobalModel::find($userInfo['merchant_id']);
            // 更新
            $info->effective_count = $data['effective_count'];
            $info->evaluation_period = $data['evaluation_period'];
            $info->net_income = $data['net_income'];
            $info->trigger_interval_rounds = $data['trigger_interval_rounds'];
            $info->turnover_multiple = $data['turnover_multiple'];
            $info->custom_time_start = !empty($custom_time_start) ?  strtotime($custom_time_start) : 0;
            $info->custom_time_end = !empty($custom_time_end) ?  strtotime($custom_time_end) : 0;


            if($info->evaluation_period != 5) {
                $info->round_status = 1;
                $info->open_status_time = time();
            }else {
                if(time() >= $info->custom_time_start) {
                    $info->round_status = 1;
                }
                if(time() >= $info->custom_time_end) {
                    $info->round_status = 2;
                }
                $info->open_status_time = $info->custom_time_start;
            }

            $info->save();

            return json_success([],'操作成功');
        }catch (\Exception $e){
            return json_error([], "更新全局养客功能错误：". $e->getMessage());
        }

    }

}