<?php

namespace app\command;

use app\controller\Bus\PlugCharge;
use app\controller\HardMessage;
use DateTime;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;

// 引入数据库操作

class ReservationTask extends Command
{

    protected function configure()
    {
        $this->setName('reservation:task')->setDescription('Execute the reservation charging task');
    }

    protected function execute(Input $input, Output $output)
    {
        // 在这里可以使用 $this->param1 和 $this->param2 作为传递的参数
//        $EquipAuthSeq = $this->param1;
//        $ConnectorID = $this->param2;
        // ... 其他代码

//        $hard = new HardMessage();
        $currentDate = new DateTime();
        $todayDate = $currentDate->format('Y-m-d H:i:s');
        $nowDay = $currentDate->format('Y-m-d');
        $PlugOrder = Db::table('appointment')->whereDay('plug_time', $nowDay)->where('plug_time', '<=', $todayDate)->where('IsExec', 1)->select();
        if (!$PlugOrder->isEmpty()) {
            foreach ($PlugOrder as $order) {
                $plug = new PlugCharge();
                $orderZu = Db::table('charge_order_gongjiao')->whereDay('charge_date', $nowDay)->where('ConnectorID', $order['ConnectorID'])->where('EvcInfoNo', $order['Vin'])->where('Status', 'in', [2, 3])->find();
                $status = Db::table('charge_pile')->where('ConnectorID', $order['ConnectorID'])->value('status');
                if (!empty($orderZu)) {
                    Db::table('appointment')->where('id', $order['id'])->save(['IsExec' => 2]);
                    Log::write('Reservation tasks executed successfully. But The ConnectorID status has charged', 'info');
                } elseif ($status == 3) {
                    Db::table('appointment')->where('id', $order['id'])->save(['IsExec' => 2]);
                    Log::write('Reservation tasks executed successfully. But The ConnectorID status has charged', 'info');
                } else {
                    $message = $plug->PlugGennerateOrder($order['ConnectorID'], $order['Vin']);
                    if ($message['code'] == 200) {
                        Db::table('appointment')->where('id', $order['id'])->save(['IsExec' => 2]);
                        Log::write('Reservation tasks executed successfully.', 'info');
                        $output->writeln('Reservation tasks executed successfully.');
                    } elseif ($message['code'] == 2) {
                        Db::table('appointment')->where('id', $order['id'])->save(['IsExec' => 2]);
                        Log::write('Reservation tasks executed successfully. But The ConnectorID has charged', 'info');
                    } else {
                        $output->writeln('Reservation tasks executed successfully But StartChargeFailed. The Reason:' . $message['message']);
                        Log::write('Reservation tasks executed successfully But StartChargeFailed. The Reason:' . $message['message']);
                    }
                }
            }
        } else {
            $output->writeln('Reservation tasks executed successfully But PlugOrderIsEmpty.');
            Log::write('Reservation tasks executed successfully But PlugOrderIsEmpty.');
        }
    }
}
