<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\MerchantsUserModel;
use app\model\MerchantStatisModel;
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'] = (float)$item['game_profit'];
                $item['bet_amount'] = (float)$item['bet_amount'];
                $item['total_winning_score'] = (float)$item['total_winning_score'];
                $item['commission_amount'] = (float)bcmul((string)$item['game_profit'], (string)$GLOBALS['CommissionRate'], 2);
                $item['platform_fee'] = (float)bcmul((string)$item['bet_amount'], (string)$GLOBALS['CommissionRate'], 2);
                $item['buy_free_bet'] = intval($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['login_users'] = 0;
            $result['register_users'] = 0;            
            $playerData = MerchantsUserModel::getPlayerStatistics($userInfo['merchant_id'], $filters);
            if ($playerData) {
                foreach ($playerData as $key => $row) {
                    if ($key == 'register') {
                        $result['register_users'] = array_sum(array_column($row, 'count'));
                    }
                    if ($key == 'login') {
                        $result['login_users'] = array_sum(array_column($row, 'count'));
                    }
                }
            }
            
            // 格式化数据
            $result['rtp'] = calculateRTP($result['total_winning_score'], $result['bet_amount']);
            $result['date_stage'] = implode(' ~ ', $filters);
            $result['game_profit'] = (float)$result['game_profit'];
            $result['bet_amount'] = (float)$result['bet_amount'];
            $result['total_winning_score'] = (float)$result['total_winning_score'];
            $result['commission_amount'] = (float)bcmul((string)$result['game_profit'], (string)$GLOBALS['CommissionRate'], 2);
            $result['platform_fee'] = (float)bcmul((string)$result['bet_amount'], (string)$GLOBALS['CommissionRate'], 2);
            $result['buy_free_bet'] = intval($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['login_users'] = 0;
            $result['register_users'] = 0;            
            $playerData = MerchantsUserModel::getPlayerStatistics($userInfo['merchant_id']);
            if ($playerData) {
                foreach ($playerData as $key => $row) {
                    if ($key == 'register') {
                        $result['register_users'] = array_sum(array_column($row, 'count'));
                    }
                    if ($key == 'login') {
                        $result['login_users'] = array_sum(array_column($row, 'count'));
                    }
                }
            }            
            // 格式化数据
            $result['rtp'] = calculateRTP($result['total_winning_score'], $result['bet_amount']);
            $result['game_profit'] = (float)$result['game_profit'];
            $result['bet_amount'] = (float)$result['bet_amount'];
            $result['total_winning_score'] = (float)$result['total_winning_score'];
            $result['commission_amount'] = (float)bcmul((string)$result['game_profit'], (string)$GLOBALS['CommissionRate'], 2);
            $result['platform_fee'] = (float)bcmul((string)$result['bet_amount'], (string)$GLOBALS['CommissionRate'], 2);
            $result['buy_free_bet'] = intval($result['buy_free_bet_count'] ?? 0);
            
            return json_success($result, '获取成功');
        } catch (\Exception $e) {
            return json_error([], '获取商户每日数据失败：' . $e->getMessage());
        }
    }
            
}