package yem.base.common.module.meta.report;

import kd.bos.algo.*;
import kd.bos.algo.input.CollectionInput;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

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

/**
 * 异常字段表 数据包插件
 */
public class FileKeyDiffRptListDataPlugin extends AbstractReportListDataPlugin {

    private static final String algoKey = "FileKeyDiffListDataPlugin";
    public static final DataType[] DATATYPESXS = {
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType,
            DataType.StringType };
    public static final String[] FIELDSXS = {
            "yem_bizcloudname",
            "yem_appname",
            "yem_entitykey",
            "yem_entityname",
            "yem_modeltype",
            "yem_parentname",
            "yem_entitysname",
            "yem_fieldnumber",
            "yem_fieldnumbers",
            "yem_aliasname",
            "yem_fieldname",
            "yem_fieldnames",
            "yem_datatype",
            "yem_fieldtype",
            "yem_basedatatype",
            "yem_textfield1"};

    @Override
    public DataSet query(ReportQueryParam queryParam, Object o) throws Throwable {

        // 创建显示行字段
        Collection<Object[]> coll = new ArrayList<>();
        RowMeta createRowMeta = RowMetaFactory.createRowMeta(FIELDSXS, DATATYPESXS);//字段集合
        CollectionInput collectionInput = new CollectionInput(createRowMeta, coll);
        DataSet createDataSet = Algo.create(algoKey).createDataSet(collectionInput);//数据集合

//        // 获取过滤
//        List<FilterItemInfo> filters = queryParam.getFilter().getFilterItems();
//        String tpv_ordernumm = null;
//        for (FilterItemInfo filterItem : filters) {
//            switch (filterItem.getPropName()) {
//                case "tpv_ordernumm":
//                    tpv_ordernumm = filterItem.getString();
//                    break;
//                default:
//                    break;
//            }
//        }
        List<QFilter> filterList1 = new ArrayList<>();
        filterList1.add(new QFilter("yem_fieldnumber", QCP.not_equals2, "billno"));
        filterList1.add(new QFilter("yem_fieldnumber", QCP.not_equals2, "id"));
        filterList1.add(new QFilter("yem_fieldnumber", QCP.not_equals2, "org"));
        filterList1.add(new QFilter("yem_fieldnumber", QCP.not_equals2, "seq"));

        DataSet dataSet1 = QueryServiceHelper.queryDataSet(algoKey, "yem_base_fieldkeystore",
                "id,yem_bizcloudname ,yem_appname, yem_entitykey , yem_entityname, yem_modeltype, yem_parentname," +
                        " yem_entitysname, yem_fieldnumber, yem_fieldnumbers, yem_aliasname, yem_fieldname, yem_fieldnames ," +
                        " yem_datatype, yem_fieldtype, yem_basedatatype"
                ,filterList1.toArray(new QFilter[0]), "yem_fieldname desc");
        GroupbyDataSet groupby1 = dataSet1.groupBy(new String[]{"yem_fieldnumber","yem_aliasname","yem_fieldname","yem_fieldnames"});
        groupby1.minP("id","yem_bizcloudname");
        groupby1.minP("id","yem_appname");
        groupby1.minP("id","yem_entitykey");
        groupby1.minP("id","yem_entityname");
        groupby1.minP("id","yem_modeltype");
        groupby1.minP("id","yem_parentname");
        groupby1.minP("id","yem_entitysname");
        groupby1.minP("id","yem_datatype");
        groupby1.minP("id","yem_fieldtype");
        groupby1.minP("id","yem_basedatatype");
        groupby1.minP("id","yem_fieldnumbers");
        groupby1.count("yem_textfield1");
        DataSet dataSet2 = groupby1.finish(); //构造完毕，返回DataSet
        dataSet2 = dataSet2.orderBy(new String[]{"yem_fieldnames"});

        DataSet dataSet3 = dataSet2.copy();
        dataSet3 = dataSet3.select("yem_fieldnames as yem_fieldnames2");
        GroupbyDataSet groupby2 = dataSet3.groupBy(new String[]{"yem_fieldnames2"});
        groupby2.count("yem_count");
        DataSet dataSet4 = groupby2.finish(); //构造完毕，返回DataSet
        dataSet4 = dataSet4.where("yem_count>1");

        DataSet dataSet5 =  dataSet4.join(dataSet2,JoinType.INNER).on("yem_fieldnames2", "yem_fieldnames")
                .select("yem_bizcloudname", "yem_appname", "yem_entitykey","yem_entityname", "yem_modeltype",
                        "yem_parentname", "yem_entitysname", "yem_fieldnumber","yem_fieldnumbers", "yem_aliasname",
                        "yem_fieldname", "yem_fieldnames", "yem_datatype","yem_fieldtype", "yem_basedatatype","yem_textfield1")
                .finish();

        DataSet dataSetfinal = dataSet5.copy();
        for (Row row : dataSetfinal) {
            String yem_bizcloudname = row.getString("yem_bizcloudname");
            String yem_appname = row.getString("yem_appname");
            String yem_entitykey = row.getString("yem_entitykey");
            String yem_entityname = row.getString("yem_entityname");
            String yem_modeltype = row.getString("yem_modeltype");
            String yem_parentname = row.getString("yem_parentname");
            String yem_entitysname = row.getString("yem_entitysname");
            String yem_fieldnumber = row.getString("yem_fieldnumber");
            String yem_fieldnumbers = row.getString("yem_fieldnumbers");
            String yem_aliasname = row.getString("yem_aliasname");
            String yem_fieldname = row.getString("yem_fieldname");
            String yem_fieldnames = row.getString("yem_fieldnames");
            String yem_datatype = row.getString("yem_datatype");
            String yem_fieldtype = row.getString("yem_fieldtype");
            String yem_basedatatype = row.getString("yem_basedatatype");
            String yem_textfield1 = row.getString("yem_textfield1");

            Object[] tempData = new Object[FIELDSXS.length];
            tempData[0] =  yem_bizcloudname;
            tempData[1] =  yem_appname;
            tempData[2] =  yem_entitykey;
            tempData[3] =  yem_entityname;
            tempData[4] =  yem_modeltype;
            tempData[5] =  yem_parentname;
            tempData[6] =  yem_entitysname;
            tempData[7] =  yem_fieldnumber;
            tempData[8] =  yem_fieldnumbers;
            tempData[9] =  yem_aliasname;
            tempData[10] =  yem_fieldname;
            tempData[11] =  yem_fieldnames;
            tempData[12] =  yem_datatype;
            tempData[13] =  yem_fieldtype;
            tempData[14] =  yem_basedatatype;
            tempData[15] =  yem_textfield1;

            coll.add(tempData);
        }

        return createDataSet;
    }
}
