package nckd.jxccl.base.common.algo;

import kd.bos.algo.CustomAggFunction;
import kd.bos.algo.DataType;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* 去重并按逗号拼接
* @author W.Y.C
* @date 2025/12/30 12:41
* @version 1.0
*/
public class DistinctConcatFunction extends CustomAggFunction<Set<String>> {
    
    public DistinctConcatFunction() {
        // “distinct_concat”是函数名，DataType.StringType 指定返回类型为字符串
        super("distinct_concat", DataType.StringType);
    }

    public Set<String> newAggValue() {
        return new HashSet<>();
    }

    public Set<String> addValue(Set<String> aggValue, Object value) {

        aggValue = aggValue == null ?  new HashSet<>() : aggValue;
        aggValue.add(String.valueOf(value));
        return aggValue;
    }

    public Set<String> combineAggValue(Set<String> aggValue, Set<String> aggValue2) {
        aggValue.addAll(aggValue2);
        return aggValue;
    }

    public Object getResult(Set<String> aggValue) {
        return aggValue == null ? "" : String.join(",", aggValue);
    }
}
