package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.primeport.domain.gateway.VehicleApplyGateway;
import com.zcloud.primeport.domain.gateway.VehicleBlackGateway;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.domain.model.VehicleBlackE;
import com.zcloud.primeport.dto.VehicleBlackAddCmd;
import com.zcloud.primeport.plan.mjDevice.CarDockUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:26
 */
@Component
@AllArgsConstructor
public class VehicleBlackAddExe {
    private final VehicleBlackGateway vehicleBlackGateway;
    private final VehicleApplyGateway vehicleApplyGateway;
    private final CarDockUtil carDockUtil;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(VehicleBlackAddCmd cmd) {
        VehicleBlackE examTypeE = new VehicleBlackE();
        BeanUtils.copyProperties(cmd, examTypeE);
        boolean res = false;
        try {
            res = vehicleBlackGateway.add(examTypeE);
            VehicleApplyE vehicleApplyE = new VehicleApplyE();
            vehicleApplyE.setBlockedFlag(1);
            vehicleApplyE.setLicenceNo(cmd.getLicenceNo());
            vehicleApplyGateway.updateBlackFlag(vehicleApplyE);
            carDockUtil.executeBlackSave(examTypeE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}

