<?php

namespace app\controller\Bus;

use app\controller\utils;
use app\model\Vinlicense;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class VinBindLicense extends utils
{
    public function ShowVinLicense($parkNo = ""): \think\response\Json
    {
        if (!empty($parkNo)) {
            $vin = Vinlicense::where("ParkNo", $parkNo)->value("EvcInfoNo");
            $VinLicense = Vinlicense::where("EvcInfoNo", $vin)->select();
            return $this->ResponseJson($VinLicense, 200, "Request Success");
        }
        $VinLicense = Vinlicense::select();
        return $this->ResponseJson($VinLicense, 200, "Request Success");
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function AddVinLicense($vin, $parkNo, $selfCode): \think\response\Json
    {
        $checkVin = Vinlicense::where("EvcInfoNo", $vin)->find();
        $checkLicense = Vinlicense::where("ParkNo", $parkNo)->find();
        $checkSelfCode = Vinlicense::where("SelfCode", $selfCode)->find();
        if (empty($checkVin) && empty($checkLicense) && empty($checkSelfCode)) {
            $AddVinLicense = (new \app\model\Vinlicense)->save([
                "EvcInfoNo" => $vin,
                'ParkNo' => $parkNo,
                "SelfCode" => $selfCode
            ]);
            if ($AddVinLicense == 1) {
                $NewVinLicense = Vinlicense::where("EvcInfoNo", $vin)->select();
                return $this->ResponseJson($NewVinLicense, 200, "新增成功");
            } else {
                return $this->ResponseJson([], 415, "新增失败");
            }
        } else {
            return $this->ResponseJson([], 416, "Vin或车牌号已存在");
        }
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function UpdateVinLicense($id, $vin, $parkNo, $selfCode): \think\response\Json
    {
        $oldVin = Vinlicense::where("id", $id)->value('EvcInfoNo');
        $oldParkNo = Vinlicense::where("id", $id)->value('ParkNo');
        $oldSelfCode = Vinlicense::where("id", $id)->value('ParkNo');
        if ($oldVin != $vin) {
            $checkVin = Vinlicense::where("EvcInfoNo", $vin)->find();
            if (!empty($checkVin)) {
                return $this->ResponseJson([], 421, "所输入的vin已存在");
            }
        }
        if ($oldParkNo != $parkNo) {
            $checkLicense = Vinlicense::where("ParkNo", $parkNo)->find();
            if (!empty($checkLicense)) {
                return $this->ResponseJson([], 422, "所输入的车牌号已存在");
            }
        }

        if ($oldSelfCode != $selfCode) {
            $checkLicense = Vinlicense::where("SelfCode", $selfCode)->find();
            if (!empty($checkLicense)) {
                return $this->ResponseJson([], 423, "所输入的自编码已存在");
            }
        }

        $checkId = Vinlicense::where("id", $id)->find();
        if (empty($checkId)) {
            return $this->ResponseJson([], 420, "没有找到该ID");
        }

        $UpdateVinLicense = Vinlicense::where("id", $id)->save([
            'EvcInfoNo' => $vin,
            'ParkNo' => $parkNo,
            'SelfCode' => $selfCode
        ]);
        if ($UpdateVinLicense == 1) {
            $NewVinLicense = Vinlicense::where("id", $id)->select();
            return $this->ResponseJson($NewVinLicense, 200, "修改成功");
        } else {
            return $this->ResponseJson([], 417, "修改失败");
        }

    }

    public function DeleteVinLicense($id): \think\response\Json
    {
        $DeleteVinLicense = Vinlicense::where("id", $id)->delete();
        if ($DeleteVinLicense != 0) {
            return $this->ResponseJson([], 200, "删除成功");
        } else {
            return $this->ResponseJson([], 418, "删除失败");
        }
    }

}