package com.hz.pm.api.user.task;

import com.hz.pm.api.user.manage.SyncMhUserOrgManage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 * SyncMhUserTask
 * </p>
 *
 * @author WendyYang
 * @since 11:02 2023/12/25
 */
@Slf4j
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = SyncMhUserProperties.PREFIX, value = "open", matchIfMissing = true)
public class SyncMhUserTask {

    private final SyncMhUserProperties syncMhUserProperties;
    private final SyncMhUserOrgManage syncMhUserOrgManage;

    @Scheduled(fixedRateString = "#{syncMhUserProperties.getFixedRate()}", timeUnit = TimeUnit.MINUTES)
    public void execute() {
        Integer fixedRate = syncMhUserProperties.getFixedRate();
        LocalDateTime syncDateTime = LocalDateTime.now().minusMinutes(fixedRate + 1L);
        syncMhUserOrgManage.syncUsers(syncDateTime);
    }

}
