package com.weather.modules.weather.filescan;

import cn.hutool.core.util.StrUtil;
import com.weather.common.constant.Constant;
import com.weather.modules.job.task.ITask;
import com.weather.modules.sys.service.SysParamsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component("fileScanTask")
@AllArgsConstructor
public class FileScanTask implements ITask {

    private final SysParamsService sysParamsService;
    private final FileWatchServiceManager fileWatchServiceManager;

    @Override
    public void run(String params) {
        String rootPath = sysParamsService.getValue(Constant.FILE_SCAN_ROOT_PATH);
        if (StrUtil.isBlank(rootPath) || "0".equals(rootPath)) {
            log.warn("FILE_SCAN_ROOT_PATH 未配置，跳过文件扫描定时任务");
            return;
        }

        log.info("文件扫描定时任务开始执行, 根路径: {}", rootPath);

        // 确保目录结构存在（路径可能在启动后才配置）
        fileWatchServiceManager.ensureDirectoriesExist();

        // 如果 WatchService 尚未注册部门目录则注册
        if (fileWatchServiceManager.deptDirMap.isEmpty()) {
            fileWatchServiceManager.registerDeptDirectories();
        }

        fileWatchServiceManager.scanAllDirectories();
        log.info("文件扫描定时任务执行完毕");
    }
}
