<?php


namespace App\Http\Controllers\API\BlindBox;

use App\Http\Controllers\API\Redis\CheckArrivalCl;
use App\Models\BlindBoxList;
use App\Models\UserWallet;
use Illuminate\Support\Facades\Log;

class OpenPrizeController
{
    /**
     * 盲盒开奖
     */
    public function useOpenPrize($item)
    {
        # 已购买人数
        $purchased = json_decode($item->purchased);
        # 可中奖人数
        $winner_num = $item->winner_num;
        # 获取盲盒价格
        $price = BlindBoxList::find($item->id);
        if ($purchased && count($purchased) && count($purchased) >= $item->min_participants_num) {
            # 计算购买者应有的中奖几率
            #平均值
            $average_value = 100 / count($purchased);
            $max = 100;
            # 没有邀请的购买者人数
            $not_invite_num = 0;
            foreach ($purchased as $values) {
                # 获取各自的邀请人数
                if ($values->invite_num) {
                    # 不为0的
                    $values->rate = $average_value + ($values->invite_num * $values->invite_up_rate);
                    $max -= $values->rate;
                } else {
                    $not_invite_num += 1;
                }
            }
            # 循环结束后,$max剩下的人平分
            $not_invite_rate = $max / $not_invite_num;
            foreach ($purchased as $values) {
                if (!$values->invite_num) {
                    $values->rate = $not_invite_rate;
                }
            }

            # 开奖 -
            $while_status = 1;
            $winner_list = json_decode($item->winner_list);
            while ($while_status) {
                # 直到抽奖人数达标
                if ($winner_list && count($winner_list) >= $winner_num) {
                    $while_status = 0;
                    continue;
                }
                $res = self::get_prize($purchased);
                if ($res) {
                    if ($winner_list && count($winner_list)) {
                        foreach ($winner_list as $win_item) {
                            if ($win_item == $res) {
                                continue;
                            } else {
                                # 没有重复中奖--push
                                $winner_list[] = $res;
                                break;
                            }
                        }
                    } else {
                        # 为空创建一个数组
                        $list = [];
                        $list[] = $res;
                        $winner_list = $list;
                    }
                } else {
                    $while_status = 0;
                    Log::error('未取到中奖地址');
                }
                $while_status += 1;
                if ($while_status == 100) {
                    $while_status = 0;
                    Log::error('本次开奖已经循环一百次，请查看问题');
                }
            }
            # 更新数据
            BlindBoxList::query()->where('id', $item->id)->update(['winner_list' => json_encode($winner_list),
                'purchased' => json_encode($purchased)]);
            # 开奖完成--先更新订单状态

            foreach ($winner_list as $winner) {
                UserWallet::query()->where('blind_box_id', $item->id)->where('user_address', $winner)
                    ->update(['winner_status' => 1]);
                # 中奖转账
                app(CheckArrivalCl::class)->WinnerTransfer($winner, $item->id, ($price->price * 3), 1);
            }
        } else if ($purchased && count($purchased) && count($purchased) < $item->min_participants_num) {
            # 不能开奖的盲盒--退款
            foreach ($purchased as $pur_item) {
                app(CheckArrivalCl::class)->WinnerTransfer($pur_item->address, $item->id, $price->price, 2);
            }
        }
    }

    public function get_prize($list)
    {
        $result = '';
        //概率数组的总概率精度
        $proSum = 100;
        //概率数组循环
        foreach ($list as $item) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $item->rate) {
                $result = $item->address;
                break;
            } else {
                $proSum -= $item->rate;
            }
        }
        return $result;
    }

}
