package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.domain.config.MessageTemplateConfig;
import com.zcloud.primeport.domain.enums.VehicleBelongTypeEnum;
import com.zcloud.primeport.domain.gateway.VehicleApplyGateway;
import com.zcloud.primeport.domain.gateway.VehicleAuditGateway;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.domain.model.VehicleAuditE;
import com.zcloud.primeport.dto.VehicleAuditUpdateCmd;
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;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:26
 */
@Component
@AllArgsConstructor
public class VehicleAuditUpdateExe {
    private final VehicleAuditGateway vehicleAuditGateway;
    private final VehicleApplyGateway vehicleApplyGateway;
    private final CarDockUtil carDockUtil;
    private final MessageNoticeExe messageNoticeExe;
    private final MessageTemplateConfig messageTemplateConfig;

    @Transactional(rollbackFor = Exception.class)
    public void execute(VehicleAuditUpdateCmd vehicleAuditUpdateCmd) {
        VehicleAuditE vehicleAuditE = new VehicleAuditE();
        BeanUtils.copyProperties(vehicleAuditUpdateCmd, vehicleAuditE);
        boolean res = vehicleAuditGateway.update(vehicleAuditE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void audit(VehicleAuditUpdateCmd cmd) {
        if (!(cmd.getAuditStatus().equals(2) || cmd.getAuditStatus().equals(3))) {
            throw new BizException("审批状态必须是通过或不通过");
        }
        VehicleAuditE vehicleAuditE = new VehicleAuditE();
        BeanUtils.copyProperties(cmd, vehicleAuditE);
        vehicleAuditE.setAuditTime(DateUtil.date2Str(new Date()));
        VehicleApplyE vehicleApplyE = new VehicleApplyE();
        vehicleApplyE.setId(cmd.getVehicleApplyId());
        vehicleApplyE.setAuditFlag(cmd.getAuditStatus());
        vehicleApplyE.setMkmjPermission(1);
        if (cmd.getAuditStatus().equals(3)) {
            vehicleApplyE.setMkmjPermission(2);
        }
        vehicleApplyE.setStatusFlag(2);
        vehicleApplyGateway.update(vehicleApplyE);
        vehicleAuditGateway.update(vehicleAuditE);

        VehicleApplyE vehicleApplyE1 = vehicleApplyGateway.getById(cmd.getVehicleApplyId());

        // 审批通过 进行数据对接。
        if (cmd.getAuditStatus().equals(2)) {
            if (vehicleApplyE1 != null && (vehicleApplyE1.getVehicleBelongType().equals(VehicleBelongTypeEnum.TEMP_VEHICLES.getCode()) ||
                    vehicleApplyE1.getVehicleBelongType().equals(VehicleBelongTypeEnum.XGF_VEHICLES.getCode()))) {
                carDockUtil.execute(vehicleApplyE1.getLicenceNo());
            } else {
                carDockUtil.executeInternalVehicle(vehicleApplyE1);
            }
        }

        // 完成待办
        messageNoticeExe.sendMessageCompleteEvent(vehicleAuditE.getId());

        // 发送通知
        if (vehicleApplyE1 != null && vehicleApplyE1.getEmployeeVehicleUserId() != null) {
            Map<String, Object> sendParams = new HashMap <>();
            String sourceCode = "";
            if (cmd.getAuditStatus().equals(2)) {
                // 通过
                sourceCode = messageTemplateConfig.getFirstLevelApplyReceiveCarAudit();
            } else {
                // 驳回
                sourceCode = messageTemplateConfig.getFirstLevelApplyReceiveCarAuditReject();
            }

            messageNoticeExe.sendMessageNotice(vehicleApplyE1.getEmployeeVehicleUserId(), sourceCode, sendParams);
        }

    }
}

