package yem.base.common.module.version.helper;

import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * F7过滤帮助类
 *
 * @description:
 * @date: 2024/5/27
 * @auther: longh
 */
public class F7Helper {
    public static final String STAUTS = "status";
    public static final String ENABLE = "enable";

    public F7Helper() {
    }

    public static QFilter getEnableFilter() {
        return new QFilter(ENABLE, QCP.equals, "1");
    }

    public static QFilter getCommonStatusFilter() {
        QFilter fs = new QFilter(STAUTS, QCP.equals, "C");
        fs.and(ENABLE, QCP.equals, "1");
        return fs;
    }

    public static String getSelectCols(List<String> cols) {
        if (cols == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            for (int len = cols.size(); i < len; ++i) {
                if (i > 0) {
                    sb.append(',');
                }
                sb.append(cols.get(i));
            }
            return sb.toString();
        }
    }

    public static String getSelectCols(String... cols) {
        if (cols == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cols.length; ++i) {
                if (i > 0) {
                    sb.append(',');
                }
                sb.append(cols[i]);
            }
            return sb.toString();
        }
    }

    public static String appendSelectCols(String exsistCols, String... cols) {
        String selects = getSelectCols(cols);
        if (StringUtils.isEmpty(exsistCols)) {
            return selects;
        } else {
            return StringUtils.isEmpty(selects) ? exsistCols : exsistCols + "," + selects;
        }
    }

    public static void addF7Filter(BeforeF7SelectEvent evt, QFilter qFilter) {
        getFilter(evt).setFilter(qFilter);
    }

    public static void addF7Filter(BeforeF7SelectEvent evt, List<QFilter> qFilters) {
        getFilter(evt).setQFilters(qFilters);
    }

    public static ListFilterParameter getFilter(BeforeF7SelectEvent evt) {
        return ((ListShowParameter) evt.getFormShowParameter()).getListFilterParameter();
    }

    public static QFilter getMetaTypeFs(String... types) {
        if (types != null) {
            List<String> type = Arrays.asList(types);
            return new QFilter("modeltype", QCP.in, type);
        } else {
            return new QFilter("1", QCP.equals, 2);
        }
    }

}
