package nckd.jimin.jyyy.fi.plugin.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.BasedataItem;
import kd.bos.entity.datamodel.events.QueryImportBasedataEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import nckd.jimin.jyyy.fi.common.constant.BillTypeConstants;

import java.util.List;
import java.util.Map;

public class LimitRelasetEdit extends AbstractBillPlugIn {

    @Override
    public void queryImportBasedata(QueryImportBasedataEventArgs e) {
        super.queryImportBasedata(e);
        Map<BasedataItem, List<Object>> searchResult = e.getSearchResult();

        for(Map.Entry<BasedataItem, List<Object>> entryRow : searchResult.entrySet()){
            BasedataItem basedataItem = entryRow.getKey();
            String entityNumber = basedataItem.getEntityNumber();
            List<Object> dataValues = entryRow.getValue();
            // 岗位 或者 职级 且 查询数量大于1
            if(dataValues.size() > 1 && basedataItem.getFieldKey().equals("nckd_classfield_jobpos")){
                // 查询职级方案
                DynamicObjectCollection dataCol = null;
                if("hbjm_joblevelhr".equals(entityNumber)){
                    dataCol = QueryServiceHelper.query(entityNumber, "id", new QFilter[]{
                            new QFilter("id", QCP.in, dataValues),
                            new QFilter("enable", QCP.equals, "1"),
                            new QFilter("joblevelscm.iscurrentversion", QCP.equals, "1")
                    });
                }else{
                    dataCol = QueryServiceHelper.query(entityNumber, "id", new QFilter[]{
                            new QFilter("id", QCP.in, dataValues),
                            new QFilter("iscurrentversion", QCP.equals, "1"),
                            new QFilter("datastatus", QCP.equals, "1")
                    });
                }

                if(dataCol != null && dataCol.size() == 1){
                    dataValues.clear();
                    dataValues.add(dataCol.get(0).get("id"));
                }
            }
        }
    }
}
