package com.zcloud.primeport.plan.mjDevice;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.domain.model.VehicleBlackE;
import com.zcloud.primeport.persistence.repository.VehicleApplyRepository;
import com.zcloud.primeport.plan.mjDevice.dto.CarEnum;
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.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class CarDockUtil {
    @Resource
    private VehicleApplyRepository vehicleApplyRepository;
    @Resource
    private OneLevelCarUtil oneLevelCarUtil;

    public void execute(String licenceNo) {
        // 一级口门车辆临时、相关方车
        List<OneLevelCarSaveDto> dataList = vehicleApplyRepository.listTodayHasPort(licenceNo);
        dataList.forEach(item -> {
            item.setTruckCardColor(CarEnum.getMenuKeyByPath(item.getTruckCardColor()));
            item.setTruckTypCod(CarEnum.getMenuKeyByPath(item.getTruckTypCod()));
            item.setTruckTeamNam(" ");
            // 1-东港区 2-西港区
            item.setPortId("0");
            if ("QHD_GQ_DGQ".equals(item.getAuthAreasCode())) {
                item.setPortId("1");
            }
            if ("QHD_GQ_XGQ".equals(item.getAuthAreasCode())) {
                item.setPortId("2");
            }
            oneLevelCarUtil.tmpVehicleSave(item);
        });
    }

    // 一级口门内部车 拉黑原因  block_reason
    public void executeInternalVehicle(VehicleApplyE applyE) {
        OneLevelCarSaveDto o = new OneLevelCarSaveDto();
        o.setTruckNo(applyE.getLicenceNo());
        o.setTruckCardColor(CarEnum.getMenuKeyByPath(applyE.getLicenceType()));
        o.setTruckTypCod(CarEnum.getMenuKeyByPath(applyE.getVehicleType()));
        o.setYqlCheckFlag("0");
        o.setValidDte(applyE.getVisitEndTime());
        o.setDriverNam(applyE.getEmployeeVehicleUserName());
        o.setVin("无");
        o.setTruckAppTypCod(CarEnum.getMenuKeyByPath(applyE.getVehicleBelongType()));
        o.setRecNam(applyE.getCreateName());
        o.setRecTim(DateUtil.date2Str(new Date()));
//        o.setPortId();

        JSONObject entries = new JSONObject(applyE.getGateLevelAuthArea());
        JSONArray area = entries.getJSONArray("area");
        if (area.size() > 1) {
            o.setPortId("0");
        }
        JSONObject o1 = area.getJSONObject(0);
        if ("QHD_GQ_DGQ".equals(o1.get("bianma",String.class))) {
            o.setPortId("1");
        }
        if ("QHD_GQ_XGQ".equals(o1.get("bianma",String.class))) {
            o.setPortId("2");
        }
        OneLevelCarRemoveDto r = new OneLevelCarRemoveDto();
        r.setTruckNo(applyE.getLicenceNo());
        r.setTruckCardColor(CarEnum.getMenuKeyByPath(applyE.getLicenceType()));
        oneLevelCarUtil.internalVehicleRemove(r);
        oneLevelCarUtil.internalVehicleSave(o);

    }
    public void executeBlackSave(VehicleBlackE examTypeE) {
        OneLevelCarBlackDto oneLevelCarBlackDto = new OneLevelCarBlackDto();
        oneLevelCarBlackDto.setTruckNo(examTypeE.getLicenceNo());
        oneLevelCarBlackDto.setTruckTypCod(CarEnum.getMenuKeyByPath(examTypeE.getVehicleType()));
        oneLevelCarBlackDto.setTruckCardColor(CarEnum.getMenuKeyByPath(examTypeE.getLicenceNo()));
        oneLevelCarBlackDto.setRecTim(DateUtil.date2Str(new Date()));
        oneLevelCarBlackDto.setRecNam(AuthContext.getCurrentUser().getName());
        oneLevelCarUtil.blackVehicleSave(oneLevelCarBlackDto);
    }
    public void executeBlackRemove(VehicleBlackE examTypeE) {
        OneLevelCarBlackDto oneLevelCarBlackDto = new OneLevelCarBlackDto();
        oneLevelCarBlackDto.setTruckNo(examTypeE.getLicenceNo());
        oneLevelCarBlackDto.setTruckTypCod(CarEnum.getMenuKeyByPath(examTypeE.getVehicleType()));
        oneLevelCarBlackDto.setTruckCardColor(CarEnum.getMenuKeyByPath(examTypeE.getLicenceNo()));
        oneLevelCarBlackDto.setRecTim(DateUtil.date2Str(new Date()));
        oneLevelCarBlackDto.setRecNam(AuthContext.getCurrentUser().getName());
        oneLevelCarUtil.blackVehicleRemove(oneLevelCarBlackDto);
    }

    public static void main(String[] args) {
        String x = "{\"area\": [{\"value\": \"秦皇岛港区-全部\", \"bianma\": \"QHD_GQ_DXGQ\"}]}";
        JSONObject entries = new JSONObject(x);
        JSONArray area = entries.getJSONArray("area");
        if (area.size() > 1) {

        }
    }
}
