package com.zcloud.primeport.plan.mjDevice;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.zcloud.primeport.persistence.dataobject.VehicleDockLogDO;
import com.zcloud.primeport.persistence.repository.VehicleDockLogRepository;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarBlackDto;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarRemoveDto;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarSaveDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 一级车辆门禁对接  前缀
 *
 */
@Component
public class OneLevelCarUtil {
    @Value("${kmmj.dockflag}")
    private Integer dockFlag;
    @Value("${oneLevelCar.prefix}")
    private String prefix;

    @Resource
    private VehicleDockLogRepository vehicleDockLogRepository;

    /**
     * 内部车辆保存
     *
     *
     */
    public void internalVehicleSave(OneLevelCarSaveDto oneLevelCarSaveDto) {
        if (dockFlag.equals(1)) {
            String body = HttpRequest.post(prefix + "HD_QHD_IGATE_1.0/webresources/login/CTruck/qaSaveInternal").body(new JSONObject(oneLevelCarSaveDto).toString()).execute().body();
            JSONObject resJson = new JSONObject(body);
            VehicleDockLogDO vehicleDockLogDO = new VehicleDockLogDO();
            vehicleDockLogDO.setLevel("1");
            vehicleDockLogDO.setAdmissionBlackout("admission");
            vehicleDockLogDO.setLicenceNo(oneLevelCarSaveDto.getTruckNo());
            vehicleDockLogDO.setPortArea(oneLevelCarSaveDto.getPortId());
            vehicleDockLogDO.setValidDte(oneLevelCarSaveDto.getValidDte());
            vehicleDockLogDO.setTruckAppTypCod(oneLevelCarSaveDto.getTruckAppTypCod());
            vehicleDockLogDO.setTruckCardColor(oneLevelCarSaveDto.getTruckCardColor());
            vehicleDockLogDO.setTruckTypCod(oneLevelCarSaveDto.getTruckTypCod());
            vehicleDockLogDO.setDockFlag("2");
            vehicleDockLogDO.setDockStatus(resJson.getStr("code"));
            vehicleDockLogDO.setReasonsRefusal(resJson.getStr("msg"));
            vehicleDockLogRepository.save(vehicleDockLogDO);
        }
    }

    /**
     * 内部车辆删除
     *
     */
    public void internalVehicleRemove(OneLevelCarRemoveDto carRemoveDto) {
        if (dockFlag.equals(1)) {
            String body = HttpRequest.post(prefix + "HD_QHD_IGATE_1.0/webresources/login/CTruck/qaDeleteInternal").body(new JSONObject(carRemoveDto).toString()).execute().body();
            JSONObject resJson = new JSONObject(body);
            VehicleDockLogDO vehicleDockLogDO = new VehicleDockLogDO();
            vehicleDockLogDO.setLevel("1");
            vehicleDockLogDO.setAdmissionBlackout("admission");
            vehicleDockLogDO.setLicenceNo(carRemoveDto.getTruckNo());
            vehicleDockLogDO.setTruckCardColor(carRemoveDto.getTruckCardColor());
            vehicleDockLogDO.setDockFlag("2");
            vehicleDockLogDO.setDockStatus(resJson.getStr("code"));
            vehicleDockLogDO.setReasonsRefusal(resJson.getStr("msg"));
            vehicleDockLogRepository.save(vehicleDockLogDO);
        }
    }

    /**
     * 临时车辆删除
     *
     *
     */
    public void tmpVehicleRemove(OneLevelCarRemoveDto carRemoveDto) {
        if (dockFlag.equals(1)) {
            String body = HttpRequest.post(prefix + "HD_QHD_IGATE_1.0/webresources/login/CTruck/qaDeleTemp").body(new JSONObject(carRemoveDto).toString()).execute().body();
            JSONObject resJson = new JSONObject(body);
        }

    }

    /**
     * 临时车辆新增
     *
     *
     */
    public void tmpVehicleSave(OneLevelCarSaveDto oneLevelCarSaveDto) {
        if (dockFlag.equals(1)) {
            oneLevelCarSaveDto.setIsNeedCheck("0");
            String body = HttpRequest.post(prefix + "HD_QHD_IGATE_1.0/webresources/login/CTruck/qaSaveTemp").body(new JSONObject(oneLevelCarSaveDto).toString()).execute().body();
            JSONObject resJson = new JSONObject(body);
            VehicleDockLogDO vehicleDockLogDO = new VehicleDockLogDO();
            vehicleDockLogDO.setLevel("1");
            vehicleDockLogDO.setAdmissionBlackout("admission");
            vehicleDockLogDO.setLicenceNo(oneLevelCarSaveDto.getTruckNo());
            vehicleDockLogDO.setPortArea(oneLevelCarSaveDto.getPortId());
            vehicleDockLogDO.setValidDte(oneLevelCarSaveDto.getValidDte());
            vehicleDockLogDO.setTruckAppTypCod(oneLevelCarSaveDto.getTruckAppTypCod());
            vehicleDockLogDO.setTruckCardColor(oneLevelCarSaveDto.getTruckCardColor());
            vehicleDockLogDO.setTruckTypCod(oneLevelCarSaveDto.getTruckTypCod());
            vehicleDockLogDO.setDockFlag("2");
            vehicleDockLogDO.setDockStatus(resJson.getStr("code"));
            vehicleDockLogDO.setReasonsRefusal(resJson.getStr("msg"));
            vehicleDockLogRepository.save(vehicleDockLogDO);
        }
    }

    /**
     * 黑名单车辆新增
     *
     *
     */
    public void blackVehicleSave(OneLevelCarBlackDto oneLevelCarBlackDto) {
        if (dockFlag.equals(1)) {
            String body = HttpRequest.post(prefix + "HD_QHD_IGATE_1.0/webresources/login/CTruck/qaSaveBlack").body(new JSONObject(oneLevelCarBlackDto).toString()).execute().body();
            JSONObject resJson = new JSONObject(body);
            VehicleDockLogDO vehicleDockLogDO = new VehicleDockLogDO();
            vehicleDockLogDO.setLevel("1");
            vehicleDockLogDO.setAdmissionBlackout("admission");
            vehicleDockLogDO.setLicenceNo(oneLevelCarBlackDto.getTruckNo());
            vehicleDockLogDO.setTruckCardColor(oneLevelCarBlackDto.getTruckCardColor());
            vehicleDockLogDO.setTruckTypCod(oneLevelCarBlackDto.getTruckTypCod());
            vehicleDockLogDO.setDockFlag("2");
            vehicleDockLogDO.setDockStatus(resJson.getStr("code"));
            vehicleDockLogDO.setReasonsRefusal(resJson.getStr("msg"));
            vehicleDockLogRepository.save(vehicleDockLogDO);
        }
    }

    /**
     * 黑名单车辆删除
     *
     *
     */
    public void blackVehicleRemove(OneLevelCarBlackDto oneLevelCarBlackDto) {
        if (dockFlag.equals(1)) {
            String body = HttpRequest.post(prefix + "HD_QHD_IGATE_1.0/webresources/login/CTruck/qaDeleteBlack").body(new JSONObject(oneLevelCarBlackDto).toString()).execute().body();
            JSONObject resJson = new JSONObject(body);
            VehicleDockLogDO vehicleDockLogDO = new VehicleDockLogDO();
            vehicleDockLogDO.setLevel("1");
            vehicleDockLogDO.setAdmissionBlackout("admission");
            vehicleDockLogDO.setLicenceNo(oneLevelCarBlackDto.getTruckNo());
            vehicleDockLogDO.setTruckCardColor(oneLevelCarBlackDto.getTruckCardColor());
            vehicleDockLogDO.setTruckTypCod(oneLevelCarBlackDto.getTruckTypCod());
            vehicleDockLogDO.setDockFlag("2");
            vehicleDockLogDO.setDockStatus(resJson.getStr("code"));
            vehicleDockLogDO.setReasonsRefusal(resJson.getStr("msg"));
            vehicleDockLogRepository.save(vehicleDockLogDO);
        }
    }
}
