package com.ruoyi.credit.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.credit.event.LoanChangeEvent;

/**
 * 贷款验证事件发布工具类
 * 提供便捷的事件发布方法
 * 
 * @author ruoyi
 * @date 2025-12-31
 */
@Component
public class LoanValidationEventPublisher {

    private static final Logger log = LoggerFactory.getLogger(LoanValidationEventPublisher.class);

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    /**
     * 发布贷款新增事件
     * 
     * @param loanId 贷款ID
     */
    public void publishLoanCreatedEvent(Long loanId) {
        try {
            String currentUser = getCurrentUser();
            LoanChangeEvent event = LoanChangeEvent.createLoanCreatedEvent(this, loanId, currentUser);
            eventPublisher.publishEvent(event);
            
            log.info("发布贷款新增事件成功，贷款ID: {}", loanId);
            
        } catch (Exception e) {
            log.error("发布贷款新增事件失败，贷款ID: {}", loanId, e);
        }
    }

    /**
     * 发布贷款修改事件
     * 
     * @param loanId 贷款ID
     * @param oldData 修改前数据
     * @param newData 修改后数据
     */
    public void publishLoanUpdatedEvent(Long loanId, Object oldData, Object newData) {
        try {
            String currentUser = getCurrentUser();
            LoanChangeEvent event = LoanChangeEvent.createLoanUpdatedEvent(this, loanId, currentUser, oldData, newData);
            eventPublisher.publishEvent(event);
            
            log.info("发布贷款修改事件成功，贷款ID: {}", loanId);
            
        } catch (Exception e) {
            log.error("发布贷款修改事件失败，贷款ID: {}", loanId, e);
        }
    }

    /**
     * 发布债权凭证变更事件
     * 
     * @param loanId 贷款ID
     * @param creditId 债权凭证ID
     */
    public void publishCreditChangedEvent(Long loanId, Long creditId) {
        try {
            String currentUser = getCurrentUser();
            LoanChangeEvent event = LoanChangeEvent.createCreditChangedEvent(this, loanId, creditId, currentUser);
            eventPublisher.publishEvent(event);
            
            log.info("发布债权凭证变更事件成功，贷款ID: {}, 债权凭证ID: {}", loanId, creditId);
            
        } catch (Exception e) {
            log.error("发布债权凭证变更事件失败，贷款ID: {}, 债权凭证ID: {}", loanId, creditId, e);
        }
    }

    /**
     * 发布服务周期变更事件
     * 
     * @param loanId 贷款ID
     * @param servicePeriodId 服务周期ID
     */
    public void publishServicePeriodChangedEvent(Long loanId, Long servicePeriodId) {
        try {
            String currentUser = getCurrentUser();
            LoanChangeEvent event = LoanChangeEvent.createServicePeriodChangedEvent(this, loanId, servicePeriodId, currentUser);
            eventPublisher.publishEvent(event);
            
            log.info("发布服务周期变更事件成功，贷款ID: {}, 服务周期ID: {}", loanId, servicePeriodId);
            
        } catch (Exception e) {
            log.error("发布服务周期变更事件失败，贷款ID: {}, 服务周期ID: {}", loanId, servicePeriodId, e);
        }
    }

    /**
     * 发布批量验证请求事件
     */
    public void publishBatchValidationRequestEvent() {
        try {
            String currentUser = getCurrentUser();
            LoanChangeEvent event = LoanChangeEvent.createBatchValidationRequestEvent(this, currentUser);
            eventPublisher.publishEvent(event);
            
            log.info("发布批量验证请求事件成功");
            
        } catch (Exception e) {
            log.error("发布批量验证请求事件失败", e);
        }
    }

    /**
     * 获取当前用户
     * 
     * @return 当前用户名
     */
    private String getCurrentUser() {
        try {
            return SecurityUtils.getUsername();
        } catch (Exception e) {
            return "system";
        }
    }
}