<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\GameBetGameModel;
use app\model\GameBetOrderModel;
use app\model\MerchantStatisModel;
use app\common\CommonUtils;
use think\facade\Request;

class MerchantStatis extends BaseController
{
    /**
     * 获取商户每日数据
     */
    public function Daily()
    {
        $userInfo = $this->request->userInfo;
        
        // 获取查询参数
        $page = Request::get('page', 1, 'intval');
        $limit = Request::get('limit', 10, 'intval');

        // 筛选条件
        $filters = [
            // 时间筛选
            'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
            'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
        ];
        
        try {
            // 获取商户每日数据
            $result = MerchantStatisModel::getMerchantDailyList(
                $userInfo['merchant_id'],
                $page,
                $limit,
                $filters,                
            );

            // 格式化数据
            foreach ($result['list'] as &$item) {
                $item['game_profit'] = CommonUtils::convertBalance($item['game_profit'], false);
                $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'], false);
                $item['commission_amount'] = CommonUtils::convertBalance($item['game_profit'] * 0.08, false);
                $item['platform_fee'] = CommonUtils::convertBalance($item['bet_amount'] * 0.08, false);
                $item['buy_free_bet'] = $item['buy_free_bet_count'] ?? 0;
            }
            
            return json_success($result, '获取成功');
        } catch (\Exception $e) {
            return json_error([], '获取商户每日数据失败：' . $e->getMessage());
        }
    }

    /*
     * 获取商户时间阶段数据统计
     */
    public function Stage()
    {
        $userInfo = $this->request->userInfo;
        
        // 筛选条件
        $filters = [
            // 时间筛选
            'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
            'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
        ];

        try {
            // 获取商户每日数据
            $result = MerchantStatisModel::getMerchantHistory($userInfo['merchant_id'], $filters);
            
            // 格式化数据
            $result['rtp'] = !is_null($result["rtp"]) ? $result["rtp"] : 0;
            $result['date_stage'] = implode(' ~ ', $filters);
            $result['game_profit'] = CommonUtils::convertBalance($result['game_profit'], false);
            $result['bet_amount'] = CommonUtils::convertBalance($result['bet_amount'], false);
            $result['commission_amount'] = CommonUtils::convertBalance($result['game_profit'] * 0.08, false);
            $result['platform_fee'] = CommonUtils::convertBalance($result['bet_amount'] * 0.08, false);
            $result['buy_free_bet'] = $result['buy_free_bet_count'] ?? 0;
            
            return json_success($result, '获取成功');
        } catch (\Exception $e) {
            return json_error([], '获取商户每日数据失败：' . $e->getMessage());
        }
    }    
            
    /**
     * 获取商户历史数据统计
     */
    public function History()
    {
        $userInfo = $this->request->userInfo;
        
        try {
            // 获取商户每日数据
            $result = MerchantStatisModel::getMerchantHistory($userInfo['merchant_id']);
            
            // 格式化数据
            $result['game_profit'] = CommonUtils::convertBalance($result['game_profit'], false);
            $result['bet_amount'] = CommonUtils::convertBalance($result['bet_amount'], false);
            $result['commission_amount'] = CommonUtils::convertBalance($result['game_profit'] * 0.08, false);
            $result['platform_fee'] = CommonUtils::convertBalance($result['bet_amount'] * 0.08, false);
            $result['buy_free_bet'] = $result['buy_free_bet_count'] ?? 0;
            
            return json_success($result, '获取成功');
        } catch (\Exception $e) {
            return json_error([], '获取商户每日数据失败：' . $e->getMessage());
        }
    }
            
}