package yem.base.common.utils;

import kd.bos.algo.*;
import kd.bos.algo.input.CollectionInput;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 报表工具类
 *
 * @author longh
 * @description:
 * @date 2021年12月4日 上午9:35:41
 */
public class RptUtil {

    public static Object[] getBaseDataPkIds(DynamicObjectCollection col) {
        List<Object> list = new ArrayList<>();
        if (col != null) {
            for (DynamicObject baseData : col) {
                list.add(baseData.getPkValue());
            }
        }

        return list.toArray();
    }

    public static List<String> getBaseDataPkIdStrList(DynamicObjectCollection col) {
        List<String> list = new ArrayList<>();
        if (col != null) {
            for (DynamicObject baseData : col) {
                list.add(baseData.getString("id"));
            }
        }

        return list;
    }

    public static List<String> getDataSetAliasList(DataSet dataSet) {
        return getDataSetAliasList(dataSet, true, null);
    }

    public static List<String> getDataSetAliasList(DataSet dataSet, String joinKey) {
        return getDataSetAliasList(dataSet, false, joinKey);
    }

    public static String[] getDataSetFiledAlias(DataSet dataSet) {
        return getDataSetAliasList(dataSet, true, null).toArray(new String[0]);
    }

    public static String[] getDataSetFiledAlias(DataSet dataSet, String joinKey) {
        return getDataSetAliasList(dataSet, false, joinKey).toArray(new String[0]);
    }

    private static List<String> getDataSetAliasList(DataSet dataSet, boolean isLeft, String joinKey) {
        Stream<String> flatMap = Stream.of(dataSet.getRowMeta().getFields()).flatMap(v -> Stream.of(v.getAlias()));
        return (isLeft ? flatMap : flatMap.filter(v -> !v.equals(joinKey))).collect(Collectors.toList());
    }

    public static JoinHint getJoinHint(boolean nullAsZero) {
        JoinHint joinHint = new JoinHint();
        joinHint.setNullAsZero(nullAsZero);

        return joinHint;
    }

    public static DataSet zeroNullNums(DataSet dataSet) {
        return dataSet.select(Stream.of(dataSet.getRowMeta().getFields()).flatMap(v -> {
            DataType dataType = v.getDataType();
            String alias = v.getAlias();
            String value;
            switch (dataType.ordinal) {
                case 2://IntegerType
                    value = "0";
                    break;
                case 3://LongType
                    value = "0l";
                    break;
                case 4://DoubleType
                case 5://BigDecimalType
                    value = "0.0";
                    break;
                default:
                    return Stream.of(alias);
            }
            return Stream.of(String.format("(case when %s is null then %s else %s end) %s", alias, value, alias, alias));
        }).toArray(String[]::new));
    }

    public static DataSet plainDataSet(String algoKey, DataSet dataSet, DynamicObjectCollection dataObjs) {
        RowMeta rowMeta = dataSet.getRowMeta();
        int fieldCount = rowMeta.getFieldCount();
        Collection<Object[]> collection = new ArrayList<>();
        for (DynamicObject dataObj : dataObjs) {
            Object[] obj = new Object[fieldCount];
            for (int i = 0; i < fieldCount; i++) {
                obj[i] = dataObj.get(i);
            }
            collection.add(obj);
        }
        return Algo.create(algoKey).createDataSet(new CollectionInput(rowMeta, collection));
    }

}
