package ink.wgink.module.file.manager;

import ink.wgink.interfaces.manager.IFilesShowCodeService;
import ink.wgink.pojo.bos.files.FilesShowCode;
import ink.wgink.util.string.WStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: FilesManager
 * @Description: 文件管理
 * @Author: wanggeng
 * @Date: 2022/8/1 10:47
 * @Version: 1.0
 */
public class FilesManager {
    private static final Logger LOG = LoggerFactory.getLogger(FilesManager.class);
    // 查看码超时清理时间，1分钟
    private static FilesManager filesManager = FilesManagerBuilder.filesManager;
    private IFilesShowCodeService filesShowCodeService;

    private FilesManager() {

    }

    public static FilesManager getInstance() {
        return filesManager;
    }

    /**
     * 生成显示码
     *
     * @param fileId
     * @return
     */
    public String generateCode(String fileId, String fileName) {
        FilesShowCode showCode = filesShowCodeService.getShowCode(fileId);
        String code;
        if (showCode == null) {
            code = WStringUtil.randomSubStr(fileId.replaceAll("-", ""), 4);
            filesShowCodeService.addShowCode(code, fileId, fileName);
        } else {
            code = showCode.getCode();
        }
        return code;
    }

    /**
     * 清除超时显示码
     *
     * @return
     */
    public void clearTimeoutShowCode() {
        filesShowCodeService.clearTimeoutShowCode();
    }

    /**
     * 获取显示码
     *
     * @param fileId
     * @param code
     */
    public FilesShowCode getShowCode(String fileId, String code) {
        return filesShowCodeService.getShowCode(fileId, code);
    }

    public void setFilesShowCodeService(IFilesShowCodeService filesShowCodeService) {
        this.filesShowCodeService = filesShowCodeService;
    }

    private static class FilesManagerBuilder {
        public static FilesManager filesManager = new FilesManager();
    }

}
