package com.skr.mdm.publish;

import com.google.common.eventbus.EventBus;
import com.skr.mdm.event.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author nfq
 * @date 2022/06/08 14:33
 */
@Component
@Slf4j
public class MdmEventPublisher implements ApplicationEventPublisherAware {

    @Autowired
    private EventBus eventBus;

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void publishEvent(Integer equipmentId, Integer companyId, Integer departmentId) {
        log.info("发布用户设备激活事件 设备ID：{}", equipmentId);
        applicationEventPublisher.publishEvent(new UserActiveEvent(this, equipmentId, companyId, new Date(), departmentId));
    }

    public void publishEquipmentDeleteEvent(List<Integer> equipmentIdList) {
        applicationEventPublisher.publishEvent(new EquipmentDeleteEvent(this, equipmentIdList));
    }

    public void publishUserDeleteEvent(List<Integer> userIdList) {
        applicationEventPublisher.publishEvent(new UserDeleteEvent(this, userIdList));
    }

    public void publishCompanyDeleteEvent(Integer companyId) {
        applicationEventPublisher.publishEvent(new CompanyDeleteEvent(this, companyId));
    }

    public void publishDataBaseCleanEvent(Integer maxLimit) {
        applicationEventPublisher.publishEvent(new DataBaseCleanEvent(this, maxLimit));
    }

    public void publishMinIoFileCleanEvent(Integer fileRetentionDays) {
        applicationEventPublisher.publishEvent(new MinIoFileCleanEvent(this, fileRetentionDays));
    }

    public void publishPushUserEvent(List<Integer> equipmentIdList, Integer companyId, String cmdType, String data) {
        applicationEventPublisher.publishEvent(new PushUserEvent(this, equipmentIdList, companyId, cmdType, data));
    }

    public void publishFileUploadProgressEvent(String fileId, String progress) {
        eventBus.post(new FileUploadProgressEvent(this, fileId, progress));
    }

}
