<?php

namespace App\Console\Channel;

use App\Libs\Utils;
use Illuminate\Console\Command;
use App\Services\Channel\ChannelUserService;
use Illuminate\Support\Facades\DB;

class RegisterChannel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'channel:register {nickname} {account} {password} {--channel_type=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建站点';

    private $channelUserService;

    public function __construct(
        ChannelUserService $channelUserService
    )
    {
        parent::__construct();
        $this->channelUserService = $channelUserService;
    }

    public function handle()
    {
        // 传参
        $nickname  = trim($this->argument('nickname'));
        $account  = trim($this->argument('account'));
        $password = trim($this->argument('password'));
        $channel_type = trim($this->option('channel_type'));

        if (!$nickname || !$account || !$password) {
            dd('参数不对,请确认后重试');
        }
        if (!$channel_type) $channel_type = 'RECHARGE';

        try {
            DB::beginTransaction();
            // 注册站点用户
            $res = $this->channelUserService->register($account, $password);
            $channel_user_id = DB::table('channel_users')->where('account', $account)->value('id');
            if (!$channel_user_id) {
                DB::rollBack();
                Utils::throwError('1002:注册站点用户失败');
            }

            // 获取下一个站点id
            $distribution_channel_id = DB::table('distribution_channels')->insertGetId([
                'name'                  => $channel_type == 'RECHARGE' ? '掌维充送制' : '掌维会员制',
                'channel_type'          => $channel_type,
                'pay_merchant_id'       => 1,
                'nickname'              => $nickname,
                'channel_user_id'       => $channel_user_id,
                'book_charge_type'      => 'HYBRID',
                'book_coin'             => 1250,
                'chapter_coin'          => 60,
                'book_calculate_price_type' => 'all',
                'chapter_calculate_price_type'  => 'bywords',
                'created_at'            => date('Y-m-d H:i:s'),
                'updated_at'            => date('Y-m-d H:i:s'),
            ]);
            if (!$distribution_channel_id) {
                DB::rollBack();
                Utils::throwError('1002:站点创建失败');
            }

            // 创建模板
            $template_id = DB::table('channel_templates')->insertGetId([
                'distribution_channel_id'   => $distribution_channel_id,
                'template_name'             => '默认模板',
                'template_type'             => 1,
                'user_scope'                => 1,
                'orders'                    => 0,
                'is_enable'                 => 1,
                'template_cate'             => $channel_type == 'RECHARGE' ? 1 : 2,
                'created_at'                => date('Y-m-d H:i:s'),
                'updated_at'                => date('Y-m-d H:i:s'),
            ]);
            if (!$template_id) {
                DB::rollBack();
                Utils::throwError('1002:默认模版创建失败');
            }

            $boolen = DB::table('distribution_channels')->where('id', $distribution_channel_id)->update([
                'pay_id'        => $template_id,
                'updated_at'    => date('Y-m-d H:i:s')
            ]);
            if (!$boolen) {
                DB::rollBack();
                Utils::throwError('1002:更新站点模板失败');
            }

            if ($channel_type == 'PERIOD') {
                $products = [
                    [
                        'type'              => 'WEEK',
                        'name'              => '周卡',
                        'name_desc'         => '周卡',
                        'price'             => '9.9',
                        'price_desc'        => '',
                        'angle_sign_text'   => '',
                        'given'             => 0,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 1,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'MONTH',
                        'name'              => '月卡',
                        'name_desc'         => '月卡(限时优惠)',
                        'price'             => '18.8',
                        'price_desc'        => '原价:￥30',
                        'angle_sign_text'   => '',
                        'given'             => 0,
                        'is_default'        => 1,
                        'is_enabled'        => 1,
                        'sequence'          => 2,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'QUARTER',
                        'name'              => '季卡',
                        'name_desc'         => '季卡(限时优惠)',
                        'price'             => '25',
                        'price_desc'        => '原价:￥50',
                        'angle_sign_text'   => '',
                        'given'             => 0,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 3,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'RECHARGE',
                        'name'              => '年卡',
                        'name_desc'         => '年卡(限时优惠)',
                        'price'             => '68',
                        'price_desc'        => '原价:￥120',
                        'angle_sign_text'   => '',
                        'given'             => 0,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 4,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                ];
            }else {
                $products = [
                    [
                        'type'              => 'RECHARGE',
                        'name'              => '19.9元',
                        'name_desc'         => '',
                        'price'             => '19.9',
                        'price_desc'        => '',
                        'angle_sign_text'   => '',
                        'given'             => 0,
                        'is_default'        => 1,
                        'is_enabled'        => 1,
                        'sequence'          => 1,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'RECHARGE',
                        'name'              => '39.9元',
                        'name_desc'         => '',
                        'price'             => '39.9',
                        'price_desc'        => '增1900书币',
                        'angle_sign_text'   => '热',
                        'given'             => 1900,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 2,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'RECHARGE',
                        'name'              => '59.9元',
                        'name_desc'         => '',
                        'price'             => '59.9',
                        'price_desc'        => '增3900书币',
                        'angle_sign_text'   => '多送39',
                        'given'             => 3900,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 3,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'WEEK',
                        'name'              => '周卡',
                        'name_desc'         => '周卡',
                        'price'             => '29.9',
                        'price_desc'        => '',
                        'angle_sign_text'   => '一周所有书免费看',
                        'given'             => 0,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 4,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'MONTH',
                        'name'              => '月卡',
                        'name_desc'         => '月卡',
                        'price'             => '49.9',
                        'price_desc'        => '',
                        'angle_sign_text'   => '一个月所有书免费看',
                        'given'             => 0,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 5,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                    [
                        'type'              => 'RECHARGE',
                        'name'              => '年卡',
                        'name_desc'         => '年卡',
                        'price'             => '89.9',
                        'price_desc'        => '',
                        'angle_sign_text'   => '一年所有书免费看',
                        'given'             => 0,
                        'is_default'        => 0,
                        'is_enabled'        => 1,
                        'sequence'          => 6,
                        'template_type'     => $template_id,
                        'template_id'       => $template_id,
                        'created_at'        => date('Y-m-d H:i:s'),
                    ],
                ];
            }

            $boolen2 = DB::table('products')->insert($products);
            if (!$boolen2) {
                DB::rollBack();
                Utils::throwError('1002:充值档位创建失败');
            }

        }catch (\Exception $e) {
            DB::rollBack();
            dd($e->getMessage());
        }

        DB::commit();
        dd('创建站点成功!');
    }
}
