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.gateway.ClosedAreaCarApplyGateway;
import com.zcloud.primeport.domain.model.ClosedAreaCarApplyE;
import com.zcloud.primeport.dto.ClosedAreaCarApplyAuditCmd;
import com.zcloud.primeport.dto.ClosedAreaCarApplyUpdateCmd;
import com.zcloud.primeport.plan.mjDevice.TwoCarUtil;
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 dearLin
 * @Date 2026-03-20 10:07:15
 */
@Component
@AllArgsConstructor
public class ClosedAreaCarApplyUpdateExe {
    private final ClosedAreaCarApplyGateway closedAreaCarApplyGateway;
    private final MessageNoticeExe messageNoticeExe;
    private final MessageTemplateConfig messageTemplateConfig;
    private final TwoCarUtil twoCarUtil;

    @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 = closedAreaCarApplyGateway.queryById(auditCmd.getId());
        if (closedAreaCarApplyE == null) {
            throw new BizException("车辆申请记录不存在");
        }
        closedAreaCarApplyE.setId(auditCmd.getId());
        closedAreaCarApplyE.setAuditFlag(auditCmd.getAuditFlag());
        closedAreaCarApplyE.setRefusalReason(auditCmd.getRefusalReason());
        closedAreaCarApplyE.setAuditTime(DateUtil.date2Str(new Date()));
        closedAreaCarApplyGateway.update(closedAreaCarApplyE);

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

        // 发送通知
        if (closedAreaCarApplyE.getApplyPersonUserId() != null) {
            Map <String, Object> sendParams = new HashMap <>();
            String sourceCode = "您提交的一级口门车辆审批已通过";
            if (auditCmd.getAuditFlag().equals(2)) {
                // 通过
                sourceCode = messageTemplateConfig.getClosedAreaApplyReceiveCarAudit();
            }else {
                // 驳回
                sourceCode = messageTemplateConfig.getClosedAreaApplyReceiveCarAuditReject();
            }

            messageNoticeExe.sendMessageNotice(closedAreaCarApplyE.getApplyPersonUserId(), sourceCode, sendParams);
        }

        // 审批通过 → 同步门禁权限（二级海康 + 一级车行）
        // 根据carBelongType判断车辆类型，内部车不走临时接口，不覆盖一级原有权限
        if (auditCmd.getAuditFlag().equals(2)) {
            twoCarUtil.levelTwoCarCompanyByApplyId(auditCmd.getId());
        }
    }
}

