<?php

namespace App\Models;

class RechargeChannel extends BaseModel
{

    protected $table = 'recharge_channel';
    protected $fillable = ['id','data_type','key', 'name', 'type', 'rate','min','max' ,'fixed' ,'sort'];
    protected $hidden = [];

    public function getFixedAttribute($value)
    {
        return $value ? explode(',', $value) : null; 
    }

    public static function getChannel($data_type, $type = [])
    {
        $query = self::where('status', 1)->where('data_type', $data_type);
        if ($type) {
            $query = $query->whereIn('type', $type);
        }
        $channel = $query->select(['type','name'])->get()->toArray();
        $channel = array_column($channel, null, 'type');
        return array_values($channel);
    }

    //获取充值通道
    public static function product($from = '')
    {
        $where['status'] = 1;
        $where['data_type'] = 1;
        if($from){
            $where['from'] = $from;
        }
        $list = self::where($where)->orderBy('sort', 'asc')->get()->toArray();
        return array_column($list, null, 'key');
    }

    public static function getFormatChannel($data_type, $recharge_channel_group_id = 1)
    {
        $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
        $query = self::where(['status' => 1, 'data_type' => $data_type]);
        
        $field = 'recharge_type'; //充值类型
        if ($data_type == 2) {
            //提现类型
            $field = 'withdraw_type';
        } elseif ($data_type == 3) {
            //提现类型
            $field = 'activity_type';
        } 
        $type = RechargeChannelGroup::where('id', $recharge_channel_group_id)->value($field);
        if (!$type) {
            return [];
        }
        $query = $query->whereIn('type', $type);
        
        $product = $query->orderBy('sort', 'asc')->select(['id', 'data_type','name','type', 'min','max','fixed','rate'])->get()->toArray();
        
        $list = [];
        foreach($product as $key => $pv) {
            if (isset($list[$pv['type']]['config'])) {
                $config = $list[$pv['type']]['config'];
                if (empty($config['range'])) {
                    $config['range'][] = $config;
                }
                if ($pv['min'] < $config['min']) {
                    $config['min'] = $pv['min'];
                } 
                if ($pv['max'] > $config['max']) {
                    $config['max'] = $pv['max'];
                }
                if ($pv['rate'] < $config['rate']) {
                    $config['max_rate'] = $config['rate'];
                    $config['min_rate'] = $pv['rate'];
                }
                if ($pv['rate'] > $config['rate']) {
                    $config['max_rate'] = $pv['rate'];
                }
                $config['range'][] = $pv;
            } else {
                $config = $pv;
            }
            
            $list[$pv['type']] = [
                'key' => $key,
                'label' => lang($pv['name']),
                'value' => $pv['type'],
                'config' => $config ?? [],
            ];
        }
        $list = array_column($list, null, 'key');
    
        return array_values($list);
    }
    
    //校验是否支持此提现方式
    public static function checkWithdrawChannel($type, $recharge_channel_group_id = 1) {
        $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
        $withdraw_type = RechargeChannelGroup::where('id', $recharge_channel_group_id)->value('withdraw_type');
        if (in_array($type, $withdraw_type)) {
            return self::where('data_type', 2)->where('type', $type)->first();
        } else {
            return false;
        }
    }
    
    //校验是否支持此充值方式
    public static function checkRechargeChannel($type, $recharge_channel_group_id = 1) {
        $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
        $recharge_type = RechargeChannelGroup::where('id', $recharge_channel_group_id)->value('recharge_type');
        if (in_array($type, $recharge_type)) {
            return true;
        } else {
            return false;
        }
    }
}
