package com.ruoyi.common.filepathsplicing;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class FilePathSplicingUtil {

    @Value("${spring.profiles.active}")
    private String active;

    @Value("${oss.filePathOssDomain}")
    private String filePathOssDomain;

    @Value("${oss.filePathAtlDomain}")
    private String filePathAtlDomain;

    public String getPrefix() {
        if ("prod".equals(active)) {
            return filePathAtlDomain;
        } else {
            //线下
            return filePathOssDomain;
        }
    }

    public String handleFieldValue(String value) {
        if (ObjectUtil.isEmpty(value) || value.startsWith("http")) {
            return value;
        }
        String prefix = this.getPrefix();

        if (value.contains(",")) {
            String[] split = value.split(",");
            List<String> list = new ArrayList<>();
            for (String v : split) {
                list.add(prefix + v);
            }
            return String.join(",", list);
        } else {
            return prefix + value;
        }
    }
}
