<?php

namespace app\controller\Bus;

use app\controller\ChargeOrder;
use app\model\Vinlicense;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class SearchMessage
{
    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function BusOrder($message = "", $start_time = "", $end_time = ""): \think\response\Json
    {
//        $order = new ChargeOrder();
//        $order->OrderList("o-wLg5DGWU_rYwfmp1-1Wck1lKGw");
        if (!empty($end_time)) {
            $end_time = date("Y-m-d", strtotime("+1day", strtotime($end_time)));
        }
        $busOrder = ["code" => 9001, "Msg" => "请输入正确的参数"];
        if (empty($message) && empty($start_time) && empty($end_time)) {
            $busOrder = Db::table("charge_order_gongjiao")->select();
        } elseif (!empty($message) && empty($start_time) && empty($end_time)) {
            $busOrder = Db::table("charge_order_gongjiao")->where('order_number', 'like', '%' . $message . '%')->select();
        } elseif (empty($message) && !empty($start_time) && !empty($end_time)) {
            $busOrder = Db::table("charge_order_gongjiao")->whereBetweenTime('start_time', $start_time, $end_time)->select();
        } elseif (!empty($message) && !empty($start_time) && !empty($end_time)) {
            $busOrder = Db::table("charge_order_gongjiao")->where('order_number', 'like', '%' . $message . '%')->whereBetweenTime('start_time', $start_time, $end_time)->select();
        }

        $busOrder = $busOrder->toArray();
        for ($i = 0; $i < count($busOrder); $i++) {
            if (!isset($busOrder['code'])) {
                $busOrder[$i]["order_id"] = $i + 1;
                $busOrder[$i]["charge_station_name"] = Db::table("charge_station")->where("charge_station_id", $busOrder[$i]["charge_station_id"])->value("charge_station_name");
                $busOrder[$i]["charge_station_number"] = Db::table("charge_station")->where("charge_station_id", $busOrder[$i]["charge_station_id"])->value("charge_station_number");
                $busOrder[$i]["no"] = Db::table("charge_pile")->where("charge_pile_number", $busOrder[$i]["Equipment_number"])->value("no");
                $busOrder[$i]["parkNo"] = Vinlicense::where("EvcInfoNo", $busOrder[$i]["EvcInfoNo"])->value("ParkNo");
                $busOrder[$i]["SelfCode"] = Vinlicense::where("EvcInfoNo", $busOrder[$i]["EvcInfoNo"])->value("SelfCode");

                if ($busOrder[$i]["IdentCode"] == 7) {
                    $busOrder[$i]["IdentCode"] = '设备异常';
                } elseif ($busOrder[$i]["IdentCode"] == 15) {
                    $busOrder[$i]["IdentCode"] = '系统异常';
                }

                if (strlen((string)$busOrder[$i]['stop_type']) != 0 && $busOrder[$i]['stop_type'] == 0) {
                    $busOrder[$i]['stop_type'] = '正常停止';
                } elseif ($busOrder[$i]['stop_type'] == 4) {
                    $busOrder[$i]['stop_type'] = '连接器断开';
                } elseif ($busOrder[$i]['stop_type'] == 5) {
                    $busOrder[$i]['stop_type'] = '电池已充满';
                } elseif ($busOrder[$i]['stop_type'] == 7) {
                    $busOrder[$i]['stop_type'] = '设备异常';
                } elseif ($busOrder[$i]['stop_type'] == 8) {
                    $busOrder[$i]['stop_type'] = '枪头被拔下';
                } elseif ($busOrder[$i]['stop_type'] == 10) {
                    $busOrder[$i]['stop_type'] = '设备故障';
                } elseif ($busOrder[$i]['stop_type'] == 11) {
                    $busOrder[$i]['stop_type'] = '车辆故障';
                } elseif ($busOrder[$i]['stop_type'] == 14) {
                    $busOrder[$i]['stop_type'] = '未知';
                } elseif ($busOrder[$i]['stop_type'] == 15) {
                    $busOrder[$i]['stop_type'] = '系统异常';
                } elseif ($busOrder[$i]['stop_type'] == 22) {
                    $busOrder[$i]['stop_type'] = '车辆识别失败';
                } elseif ($busOrder[$i]['stop_type'] == 24) {
                    $busOrder[$i]['stop_type'] = '设备终止';
                } elseif ($busOrder[$i]['stop_type'] == 25) {
                    $busOrder[$i]['stop_type'] = '互联互通终止';
                } elseif ($busOrder[$i]['stop_type'] == 26) {
                    $busOrder[$i]['stop_type'] = '平台终止';
                } elseif ($busOrder[$i]['stop_type'] == 27) {
                    $busOrder[$i]['stop_type'] = 'SOC限制终止';
                } elseif ($busOrder[$i]['stop_type'] == 13) {
                    $busOrder[$i]['stop_type'] = '急停按钮被按下';
                } elseif ($busOrder[$i]['stop_type'] == 16) {
                    $busOrder[$i]['stop_type'] = '主动防护';
                } elseif ($busOrder[$i]['stop_type'] == 6) {
                    $busOrder[$i]['stop_type'] = 'BMS异常';
                } elseif ($busOrder[$i]['stop_type'] == 12) {
                    $busOrder[$i]['stop_type'] = '账户余额不足';
                } elseif ($busOrder[$i]['stop_type'] == 31) {
                    $busOrder[$i]['stop_type'] = '操作频繁，请稍后再试';
                } elseif ($busOrder[$i]['stop_type'] == 32) {
                    $busOrder[$i]['stop_type'] = '请重新插枪启动充电';
                } elseif ($busOrder[$i]['stop_type'] == 33) {
                    $busOrder[$i]['stop_type'] = '账户超出限额';
                } elseif (strlen((string)$busOrder[$i]['stop_type']) == 0) {
                    $busOrder[$i]['stop_type'] = null;
                } else {
                    $busOrder[$i]['stop_type'] = '未知错误';
                }
            }

        }
        return json($busOrder);
    }

    public function BusOrderPark($start_time = "", $end_time = "", $parkNo = ""): \think\response\Json
    {
//        $order = new ChargeOrder();
//        $order->OrderList("o-wLg5DGWU_rYwfmp1-1Wck1lKGw");
        if (!empty($end_time)) {
            $end_time = date("Y-m-d", strtotime("+1day", strtotime($end_time)));
        }
        $busOrder = ["code" => 9001, "Msg" => "请输入正确的参数"];
        if (empty($parkNo) && empty($start_time) && empty($end_time)) {
            $busOrder = Db::table("charge_order_gongjiao")->select();
        } elseif (!empty($parkNo) && empty($start_time) && empty($end_time)) {
            $vin = Vinlicense::where("ParkNo", $parkNo)->value("EvcInfoNo");
            if (empty($vin)) {
                $vin = "NotFound";
            }
            $busOrder = Db::table("charge_order_gongjiao")->where('EvcInfoNo', $vin)->select();
        } elseif (empty($parkNo) && !empty($start_time) && !empty($end_time)) {
            $busOrder = Db::table("charge_order_gongjiao")->whereBetweenTime('start_time', $start_time, $end_time)->select();
        } elseif (!empty($parkNo) && !empty($start_time) && !empty($end_time)) {
            $vin = Vinlicense::where("ParkNo", $parkNo)->value("EvcInfoNo");
            if (empty($vin)) {
                $vin = "NotFound";
            }
            $busOrder = Db::table("charge_order_gongjiao")->where('EvcInfoNo', $vin)->whereBetweenTime('start_time', $start_time, $end_time)->select();
        }
        $busOrder = $busOrder->toArray();
        for ($i = 0; $i < count($busOrder); $i++) {
            if (!isset($busOrder['code'])) {
                $busOrder[$i]["order_id"] = $i + 1;
                $busOrder[$i]["charge_station_name"] = Db::table("charge_station")->where("charge_station_id", $busOrder[$i]["charge_station_id"])->value("charge_station_name");
                $busOrder[$i]["charge_station_number"] = Db::table("charge_station")->where("charge_station_id", $busOrder[$i]["charge_station_id"])->value("charge_station_number");
                $busOrder[$i]["no"] = Db::table("charge_pile")->where("charge_pile_number", $busOrder[$i]["Equipment_number"])->value("no");
                $busOrder[$i]["parkNo"] = Vinlicense::where("EvcInfoNo", $busOrder[$i]["EvcInfoNo"])->value("ParkNo");
                $busOrder[$i]["SelfCode"] = Vinlicense::where("EvcInfoNo", $busOrder[$i]["EvcInfoNo"])->value("SelfCode");
                if ($busOrder[$i]["IdentCode"] == 7) {
                    $busOrder[$i]["IdentCode"] = '设备异常';
                } elseif ($busOrder[$i]["IdentCode"] == 15) {
                    $busOrder[$i]["IdentCode"] = '系统异常';
                }

                if (strlen((string)$busOrder[$i]['stop_type']) != 0 && $busOrder[$i]['stop_type'] == 0) {
                    $busOrder[$i]['stop_type'] = '正常停止';
                } elseif ($busOrder[$i]['stop_type'] == 4) {
                    $busOrder[$i]['stop_type'] = '连接器断开';
                } elseif ($busOrder[$i]['stop_type'] == 5) {
                    $busOrder[$i]['stop_type'] = '电池已充满';
                } elseif ($busOrder[$i]['stop_type'] == 7) {
                    $busOrder[$i]['stop_type'] = '设备异常';
                } elseif ($busOrder[$i]['stop_type'] == 8) {
                    $busOrder[$i]['stop_type'] = '枪头被拔下';
                } elseif ($busOrder[$i]['stop_type'] == 10) {
                    $busOrder[$i]['stop_type'] = '设备故障';
                } elseif ($busOrder[$i]['stop_type'] == 11) {
                    $busOrder[$i]['stop_type'] = '车辆故障';
                } elseif ($busOrder[$i]['stop_type'] == 14) {
                    $busOrder[$i]['stop_type'] = '未知';
                } elseif ($busOrder[$i]['stop_type'] == 15) {
                    $busOrder[$i]['stop_type'] = '系统异常';
                } elseif ($busOrder[$i]['stop_type'] == 22) {
                    $busOrder[$i]['stop_type'] = '车辆识别失败';
                } elseif ($busOrder[$i]['stop_type'] == 24) {
                    $busOrder[$i]['stop_type'] = '设备终止';
                } elseif ($busOrder[$i]['stop_type'] == 25) {
                    $busOrder[$i]['stop_type'] = '互联互通终止';
                } elseif ($busOrder[$i]['stop_type'] == 26) {
                    $busOrder[$i]['stop_type'] = '平台终止';
                } elseif ($busOrder[$i]['stop_type'] == 27) {
                    $busOrder[$i]['stop_type'] = 'SOC限制终止';
                } elseif ($busOrder[$i]['stop_type'] == 13) {
                    $busOrder[$i]['stop_type'] = '急停按钮被按下';
                } elseif ($busOrder[$i]['stop_type'] == 16) {
                    $busOrder[$i]['stop_type'] = '主动防护';
                } elseif ($busOrder[$i]['stop_type'] == 6) {
                    $busOrder[$i]['stop_type'] = 'BMS异常';
                } elseif ($busOrder[$i]['stop_type'] == 12) {
                    $busOrder[$i]['stop_type'] = '账户余额不足';
                } elseif ($busOrder[$i]['stop_type'] == 31) {
                    $busOrder[$i]['stop_type'] = '操作频繁，请稍后再试';
                } elseif ($busOrder[$i]['stop_type'] == 32) {
                    $busOrder[$i]['stop_type'] = '请重新插枪启动充电';
                } elseif ($busOrder[$i]['stop_type'] == 33) {
                    $busOrder[$i]['stop_type'] = '账户超出限额';
                } elseif (strlen((string)$busOrder[$i]['stop_type']) == 0) {
                    $busOrder[$i]['stop_type'] = null;
                } else {
                    $busOrder[$i]['stop_type'] = '未知错误';
                }
            }
        }
        return json($busOrder);
    }

    public function ParkNo(): \think\response\Json
    {
        $Park = (new \app\model\Vinlicense)->column("ParkNo");
        $data = [];
        foreach ($Park as $p) {
            $data["children"][]["LicenseNumber"] = $p;
        }
        return json($data);
    }
}