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.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.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * web-app
 *
 * @Author dearLin
 * @Date 2026-03-20 10:07:15
 */
@Component
@AllArgsConstructor
public class ClosedAreaCarApplyUpdateExe {
    private final ClosedAreaCarApplyGateway closedAreaCarApplyGateway;
    private final TodoListEventPusherUtil todoListEventPusherUtil;

    @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);

        TodoListCompleteEvent todoListCompleteEvent = new TodoListCompleteEvent();
        todoListCompleteEvent.setForeignSubsidiaryKey(auditCmd.getId());
        todoListEventPusherUtil.sendMessageCompleteEvent(todoListCompleteEvent);
    }
}

