package com.yem.rf.utils;

import kd.bos.algo.DataSet;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QFilter;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: TODO
 * @Date: 2024/7/8 11:17
 * @Created: by ZZSLL
 */

public class RFUtils {

    public static QFilter getBaseQFilter() {
        return new QFilter("billstatus", "in", new String[]{"C"});
    }

    public static QFilter getBaseQFilter(String[] args) {
        return new QFilter("billstatus", "in", args);
    }

    public static String formatDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    public static String formatDate(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    public static String printBigDecimal(Object number) {
        BigDecimal num = new BigDecimal(String.valueOf(number));
        return num.stripTrailingZeros().toPlainString();
    }

    public static List<Map<String, Object>> debugDataSet(DataSet dataSet) {
        dataSet = dataSet.copy();
        List<Map<String, Object>> list = new ArrayList<>();
        DynamicObjectCollection collection = ORM.create().toPlainDynamicObjectCollection(dataSet);
        for (DynamicObject dynamicObject : collection) {
            Map<String, Object> map = new LinkedHashMap<>();
            DataEntityPropertyCollection properties = dynamicObject.getDataEntityType().getProperties();
            for (IDataEntityProperty property : properties) {
                String name = property.getName();
                if (dynamicObject.containsProperty(name)) {
                    Object value = dynamicObject.get(name);
                    map.put(name, value);
                }
            }
            list.add(map);
        }
        return list;
    }

    public static Object debugGetIfExist(DataSet dataSet, String propName, int idx) {
        dataSet = dataSet.copy();
        List<Map<String, Object>> collection = debugDataSet(dataSet);
        if (idx > collection.size()) {
            return "idx > Collection.size() |" + idx + " > " + collection.size();
        }
        Map<String, Object> dynamicObject = collection.get(idx);
        if (dynamicObject.containsKey(propName)) {
            return dynamicObject.get(propName);
        } else {
            return "不存在属性： " + propName;
        }
    }

    public static List<Object> debugGetIfExist(DataSet dataSet, String propName) {
        dataSet = dataSet.copy();
        List<Object> list = new ArrayList<>();
        List<Map<String, Object>> collection = debugDataSet(dataSet);
        for (Map<String, Object> dynamicObject : collection) {
            if (dynamicObject.containsKey(propName)) {
                list.add(dynamicObject.get(propName));
            }
        }
        return list;
    }
}
