<?php


namespace App\Http\logic\admin;


use App\Http\logic\api\BaseApiLogic;
use App\Models\ChannelSaveLog;
use Illuminate\Support\Facades\DB;

class ChannelLogic extends BaseApiLogic
{
    public function index()
    {
        $where = [];
        $list = DB::table('agent.dbo.channel')
            ->where($where)
            ->orderByDesc('updated_at')
            ->get();

        $names = DB::table('agent.dbo.admin_configs')->where('type', 'channel')->pluck('name', 'id');
        foreach ($list as &$value) {
            $name = [];

            if (!empty($value->open_region)) {
                $open_region = \GuzzleHttp\json_decode($value->open_region, true);
                foreach ($open_region as $val) {
                    if ($val['status'] == 1) {
                        $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
                    }
                }
            }

            $value->open_region = implode(',', $name);
        }
        return compact('list');
    }

    public function switch_control($id, $ChannelIndex, $AndroidVersion, $type)
    {
        $where = [
            ['ChannelType', '=', $id],
            ['ChannelIndex', '=', $ChannelIndex],
            ['AndroidVersion', '=', $AndroidVersion],

        ];

        $logModel = new ChannelSaveLog();
        $first = DB::connection('read')->table('QPPlatformDB.dbo.ChannelConfig')->where($where)->first();
        $admin_id = session('admin')->id;
        switch ($type) {
            case 'off': // 关闭

                $res = DB::connection('write')->table('QPPlatformDB.dbo.ChannelConfig')->where($where)->update([
                    'ReviewState' => 0,
                ]);

                // 添加日志
                $logModel->add($admin_id, $first->ChannelType, $first->ChannelIndex, 0, $first->VersionValue, $first->AndroidVersion);
                break;
            case 'on':  // 开启

                $res = DB::connection('write')->table('QPPlatformDB.dbo.ChannelConfig')->where($where)->update([
                    'ReviewState' => 1,
                ]);

                // 添加日志
                $logModel->add($admin_id, $first->ChannelType, $first->ChannelIndex, 1, $first->VersionValue, $first->AndroidVersion);
                break;

            default:
                $this->error = '类型错误';
                return false;
        }

        if (isset($res) && $res) {
            return true;
        } else {
            $this->error = '操作失败';
            return false;
        }

    }

    public function update($id)
    {
        $info = DB::table('agent.dbo.channel')->where('id', $id)->first();
        $open_region = \GuzzleHttp\json_decode($info->open_region, true);
        $names = DB::table('agent.dbo.admin_configs')->where('type', 'channel')->pluck('name', 'id');
        foreach ($open_region as &$value) {

            $value['name'] = isset($names[$value['id']]) ? $names[$value['id']] : '';

        }
        $info->open_region = $open_region;
        return compact('info');
    }

    public function channel_switch($id, $config_id, $type)
    {

        $query = DB::table('agent.dbo.channel')->where('id', $id)->first();

        $open_region = \GuzzleHttp\json_decode($query->open_region, true);
        $off = 0;
        switch ($type) {
            case 'on':
                foreach ($open_region as &$value) {
                    if ($value['id'] == $config_id) {
                        $value['status'] = 1;
                    }
                }
                break;

            case 'off':
                foreach ($open_region as &$value) {
                    if ($value['id'] == $config_id) {
                        $value['status'] = -1;
                    }
                    if ($value['status'] == 1) {
                        $off += 1;
                    }

                }
                if ($off < 1) {
                    $this->error = '至少开启一个通道';
                    return false;
                }
                break;

            default:
                $this->error = '类型错误';
                return false;
        }
        $open_region = \GuzzleHttp\json_encode($open_region);
        DB::table('agent.dbo.channel')->where('id', $id)->update(['open_region' => $open_region]);
    }

    // 渠道包配置充值和游戏
    public function rechargeGame($ChannelPackageName)
    {
        $where = [];
        !empty($ChannelPackageName) && $where[] = ['PackageName', 'like', $ChannelPackageName . '%'];
        $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelPackageName')
            ->where($where)
            ->paginate(30);

        foreach ($list as $value) {

            // 游戏名称
            $arr = DB::connection('write')->table('QPPlatformDB.dbo.ChannelGameOpen')
                ->where('Channel', $value->Channel)
                ->where('Status', 1)
                ->pluck('KindID')->toArray();
            $KindName = DB::connection('write')->table('QPPlatformDB.dbo.GameKindItem')
                ->whereIn('KindID', $arr)
                ->pluck('KindName')->toArray();
            $value->KindName = implode('，', $KindName);

            // 充值名称
            $ConfigIDs = DB::connection('write')->table('QPPlatformDB.dbo.ChannelOpenRecharge')
                ->where('Channel', $value->Channel)
                ->where('Status', 1)
                ->pluck('ConfigID')->toArray();

            $name = DB::connection('write')->table('agent.dbo.admin_configs')
                ->whereIn('id', $ConfigIDs)
                ->pluck('name')->toArray();
            $value->rechargeName = implode('，', $name);
        }


        return compact('list','ChannelPackageName');
    }
}
