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.ClosedAreaCarApplyGateway;
import com.zcloud.primeport.domain.model.ClosedAreaCarApplyE;
import com.zcloud.primeport.dto.ClosedAreaCarApplyAuditCmd;
import com.zcloud.primeport.dto.ClosedAreaCarApplyUpdateCmd;
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 dearLin
 * @Date 2026-03-20 10:07:15
 */
@Component
@AllArgsConstructor
public class ClosedAreaCarApplyUpdateExe {
    private final ClosedAreaCarApplyGateway closedAreaCarApplyGateway;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private MessageFacade messageFacade;
    @Transactional(rollbackFor = Exception.class)
    public void execute(ClosedAreaCarApplyUpdateCmd closedAreaCarApplyUpdateCmd) {
        ClosedAreaCarApplyE closedAreaCarApplyE = new ClosedAreaCarApplyE();
        BeanUtils.copyProperties(closedAreaCarApplyUpdateCmd, closedAreaCarApplyE);
        boolean res = closedAreaCarApplyGateway.update(closedAreaCarApplyE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void auditCarApply(ClosedAreaCarApplyAuditCmd auditCmd) {
        ClosedAreaCarApplyE closedAreaCarApplyE = new ClosedAreaCarApplyE();
        closedAreaCarApplyE.setId(auditCmd.getId());
        closedAreaCarApplyE.setAuditFlag(auditCmd.getAuditFlag());
        closedAreaCarApplyE.setRefusalReason(auditCmd.getRefusalReason());
        closedAreaCarApplyE.setAuditTime(DateUtil.date2Str(new Date()));
        closedAreaCarApplyGateway.update(closedAreaCarApplyE);
        //封闭区域车辆审批
        MessageSendCmd messageSendCmd = new MessageSendCmd();
        messageSendCmd.setBusinessId(UuidUtil.get32UUID());
        MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
        messageTargetCmd.setUserId(auditCmd.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);
        TodoListCompleteEvent todoListCompleteEvent = new TodoListCompleteEvent();
        todoListCompleteEvent.setForeignSubsidiaryKey(auditCmd.getId());
        todoListEventPusherUtil.sendMessageCompleteEvent(todoListCompleteEvent);
    }
}

