package com.wmyun.module.infra.service.file;

import com.wmyun.farmwork.word.core.TemplateTagsGenerate;
import com.wmyun.farmwork.word.core.model.gen.TagsGenDataModel;
import com.wmyun.framework.common.util.io.IoUtils;
import com.wmyun.module.infra.dal.dataobject.file.FileDO;
import com.wmyun.module.infra.dal.mysql.file.FileMapper;
import com.wmyun.module.infra.framework.file.core.client.FileClient;
import jakarta.annotation.Resource;
import jodd.io.IOUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @Description: TODO
 * @Date: 2025/3/1 10:33
 * @Created: by ZZSLL
 */

@Service
public class WordTemplateTagServiceImpl implements WordTemplateTagService {

    @Autowired
    private FilePreviewService previewService;

    @Autowired
    private FileConfigService fileConfigService;

    @Override
    public List<TagsGenDataModel> queryAllTag(String fileId) throws Exception {
        FileDO file = previewService.queryFileInfoByFileId(fileId);
        FileClient client = fileConfigService.getFileClient(file.getConfigId());
        byte[] content = client.getContent(file.getPath());

        String name = file.getName();
        Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
        Path targetPath = tempDir.resolve(name);
        Files.write(targetPath, content);
        return TemplateTagsGenerate.execAllTags(targetPath.toAbsolutePath().toString());
    }
}
