<?php

namespace app\command;

use app\model\Card;
use app\model\CardIssue;
use app\model\Member;
use app\model\MemberAccount;
use app\model\Order;
use app\model\PayDetail;
use support\Db;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CardGenerateCommand extends Command
{

    protected static $defaultName = 'CardGenerateCommand';
    protected static $defaultDescription = 'CardGenerateCommand';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::OPTIONAL, '生成卡号');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $issue = CardIssue::where('card_issue_id', 5)->first();
            $cards = [];
            for ($i = 0; $i < $issue->card_issue_num; $i++) {
                $cardId = $issue->card_issue_serial_prefix . str_pad($issue->card_issue_serial_begin + $i, $issue->card_issue_serial_length - strlen($issue->card_issue_serial_prefix), '0', STR_PAD_LEFT);
                $cards[] = [
                    'card_id' => $cardId,
                    'join_card_main_id' => $issue->join_issue_card_main_id,
                    'join_card_issue_id' => $issue->card_issue_id,
                    'card_status' => 'INIT',
                    'card_cdkey' => random_string(8, 'number'),
                    'card_addtimes' => time()
                ];
            }

            Card::insert($cards);

            return self::SUCCESS;
        } catch (\Exception $e) {
            dump($e->getMessage());
            return self::SUCCESS;
        }

    }
}