package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.jjb.saas.message.client.message.facede.MessageFacade;
import com.jjb.saas.message.client.message.request.MessageSendCmd;
import com.jjb.saas.message.client.message.request.MessageTargetCmd;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.gbscommon.utils.UuidUtil;
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 lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
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 TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private MessageFacade messageFacade;
    @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("修改失败");
        }
    }

    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);
        TodoListCompleteEvent todoListCompleteEvent = new TodoListCompleteEvent();
        todoListCompleteEvent.setForeignSubsidiaryKey(vehicleAuditE.getId());
        todoListEventPusherUtil.sendMessageCompleteEvent(todoListCompleteEvent);
        //口门车辆审批
        MessageSendCmd messageSendCmd = new MessageSendCmd();
        messageSendCmd.setBusinessId(UuidUtil.get32UUID());
        MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
        messageTargetCmd.setUserId(vehicleAuditE.getId());
        messageSendCmd.setTargetCmd(messageTargetCmd);
        messageSendCmd.setNeedTokenEnum(false);
        Map<String, Object> sendParams = new HashMap<String, Object>();
        messageSendCmd.setSourceCode("MS000119");
        sendParams.put("HazardDeferralApplicationNumber", 1);
        sendParams.put("ReformHazardNumber", 1);
        messageSendCmd.setParams(sendParams);
        messageFacade.send(messageSendCmd);
    }
}

