<?php

namespace app\command;

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 NewCustomCommand extends Command
{

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

    /**
     * @return void
     */
    protected function configure()
    {
        // $this->addArgument('name', InputArgument::OPTIONAL, '余额账户转福利账户');
        $this->addArgument('name', InputArgument::OPTIONAL, '新客专享回填订单号');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $payDetails = PayDetail::where('pay_category', 'COMBINE')->get()->toArray();
        try {

            foreach ($payDetails as $payDetail) {
                if (!empty($payDetail['join_pay_object_json'])) {
                    $payObjectJson = json_decode($payDetail['join_pay_object_json'], true);
                }
                // 查订单号
                $payDetailOrder = PayDetail::where('join_pay_order_id', 'COMBINE')
                    ->where('join_pay_member_id', $payDetail['join_pay_member_id'])
                    ->first();
                if (!empty($payDetailOrder->join_pay_object_json)) {
                    $payObjectJson2 = json_decode($payDetailOrder->join_pay_object_json, true);
                    if (isset($payObjectJson2['recharge_order_id'])) {
                        continue;
                    }
                    if (isset($payObjectJson['order_id'])) {
                        $payObjectJson2['recharge_order_id'] = $payObjectJson['order_id'];
                        PayDetail::where('pay_id', $payDetailOrder->pay_id)->update([
                            'join_pay_object_json' => json_encode($payObjectJson2)
                        ]);
                    }
                }

                echo "支付记录【" . $payDetail['pay_id'] . "】已处理完成\n";
            }
            Db::commit();

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

            Db::rollBack();
            return self::SUCCESS;
        }

    }
}