package nckd.jxccl.swc.hsas.formplugin.web.outdata.helper;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.orm.query.QFilter;
import kd.bos.orm.util.CollectionUtils;
import kd.bos.servicehelper.basedata.BaseDataServiceHelper;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import nckd.jxccl.swc.constants.SwcConstant;
import nckd.jxccl.swc.hsas.common.OutImpPresetItem;

import java.util.*;
import java.util.stream.Collectors;

public class OutImpTemplateHelper {

    //private static List<OutImpPresetItem> outImpPresetItemEnumList = new ArrayList(12);
    private static Map<Long, OutImpPresetItem> commonAndAddPresetItemEnumMap = new HashMap(4);


    public void OutImpTemplateHelper(){
        getPresetItemEnumList();
    }


    public static Map<String, Map<String, Object>> analyzeItemDatas(DynamicObjectCollection itemDatas, Map<Long, Map<String, Object>> existItemDataMap) {
        List<String> SIUnicodeList = new ArrayList();
        List<String> BIUnicodeList = new ArrayList();
        for (DynamicObject itemData : itemDatas) {
            String itemCategory = itemData.getString("nckd_itemcategory");
            String uniCode = itemData.getString("nckd_itemnumber");
            if (StringUtils.equals("SI", itemCategory)) {
                SIUnicodeList.add(uniCode);
            }
            else if (StringUtils.equals("BI", itemCategory)) {
                BIUnicodeList.add(uniCode);
            }
        }
        Map<String, Map<String, Object>> itemParamMap = new HashMap(itemDatas.size());
        // 添加薪酬项目
        itemParamMap.putAll(getItemParamMap("number", SIUnicodeList, "1", "hsbs_salaryitem", existItemDataMap, (Long) null));
        // 添加基本项目
        itemParamMap.putAll(getBaseItemParamMap("number", BIUnicodeList, "2", "nckd_outimportitem", existItemDataMap, (Long) null));
        return itemParamMap;
    }

    /**
     * 获取基本项目
     * @return
     */
    public static Map<String, Map<String, Object>> getBaseItemParamMap(String key, List<String> keyList, String itemType, String entityName, Map<Long, Map<String, Object>> existItemDataMap, Long orgId) {
        if (CollectionUtils.isEmpty(keyList)) {
            return new HashMap(0);
        } else {
            if (existItemDataMap == null) {
                existItemDataMap = new HashMap(0);
            }
            Map<String, Map<String, Object>> itemParamMap = new HashMap(keyList.size());
            HRBaseServiceHelper helper = new HRBaseServiceHelper(entityName);
            String fields = "id,number,name,nckd_datatype.id";

            QFilter filter = new QFilter(key, "in", keyList);
            filter.and(new QFilter("enable", "=", "1"));
            filter.and(new QFilter("nckd_ispreset", "=", "0"));
            if (orgId != null) {
                QFilter useOrgFilter = BaseDataServiceHelper.getBaseDataFilter(entityName, orgId);
                filter.and(useOrgFilter);
            }
            DynamicObjectCollection items = helper.queryOriginalCollection(fields, new QFilter[]{filter});

            for(DynamicObject item : items) {
                Long itemId = item.getLong("id");
                Long dataTypeId = item.getLong("nckd_datatype.id");
                Map<String, Object> itemParam = new HashMap(6);
                Map<String, Object> existItemData = (Map)((Map)existItemDataMap).get(itemId);
                itemParam.put("id", itemId);
                itemParam.put("dataTypeId", dataTypeId);
                itemParam.put("itemType", itemType);
                itemParam.put("itemname", item.getString("name"));
                itemParam.put("entityName", entityName);

                fillExistComment(entityName, item, existItemData, itemParam);
                itemParamMap.put(item.getString(key), itemParam);
            }

            return itemParamMap;
        }
    }


    /**
     * 获取薪酬项目
     * @param key
     * @param keyList
     * @param itemType
     * @param entityName
     * @param existItemDataMap
     * @param orgId
     * @return
     */
    public static Map<String, Map<String, Object>> getItemParamMap(String key, List<String> keyList, String itemType, String entityName, Map<Long, Map<String, Object>> existItemDataMap, Long orgId) {
        if (CollectionUtils.isEmpty(keyList)) {
            return new HashMap(0);
        } else {
            if (existItemDataMap == null) {
                existItemDataMap = new HashMap(0);
            }
            Map<String, Map<String, Object>> itemParamMap = new HashMap(keyList.size());
            HRBaseServiceHelper helper = new HRBaseServiceHelper(entityName);
            String fields = "id,number,name,datatype.id";

            QFilter filter = new QFilter(key, "in", keyList);
            filter.and(new QFilter("enable", "=", "1"));
            if (orgId != null) {
                QFilter useOrgFilter = BaseDataServiceHelper.getBaseDataFilter(entityName, orgId);
                filter.and(useOrgFilter);
            }
            DynamicObjectCollection items = helper.queryOriginalCollection(fields, new QFilter[]{filter});

            for(DynamicObject item : items) {
                Long itemId = item.getLong("id");
                Long dataTypeId = item.getLong("datatype.id");
                Map<String, Object> itemParam = new HashMap(6);
                Map<String, Object> existItemData = (Map)((Map)existItemDataMap).get(itemId);
                itemParam.put("id", itemId);
                itemParam.put("dataTypeId", dataTypeId);
                itemParam.put("itemType", itemType);
                itemParam.put("itemname", item.getString("name"));
                itemParam.put("entityName", entityName);

                fillExistComment(entityName, item, existItemData, itemParam);
                itemParamMap.put(item.getString(key), itemParam);
            }

            return itemParamMap;
        }
    }

    private static void fillExistComment(String entityName, DynamicObject item, Map<String, Object> existItemData, Map<String, Object> itemParam) {
        String itemCurrency;
        String existComment;
        if (existItemData != null && existItemData.size() > 0) {
            itemCurrency = (String)existItemData.get("comment");
            itemParam.put("matchcolumn", existItemData.get("matchcolumn"));
            if (StringUtils.isNotEmpty(itemCurrency)) {
                itemParam.put("comment", itemCurrency);
            }
        } else if (StringUtils.equals("hsbs_salaryitem", entityName)) {
//            itemCurrency = item.getString("outputcurrency.name");
//            if (StringUtils.isNotEmpty(itemCurrency)) {
//                existComment = ResManager.loadKDString("币种：{0}", "MigrationTemplateHelper_0", "swc-hsas-business", new Object[]{itemCurrency});
//                itemParam.put("comment", existComment);
//            }
        }
    }

    public static Map<String, Map<String, Object>> analyzeMatchItem(DynamicObjectCollection entryDatas) {
        if (CollectionUtils.isEmpty(entryDatas)) {
            return new HashMap(0);
        } else {
            List<Long> SLItemIdList = new ArrayList(entryDatas.size());
            List<Long> SPItemIdList = new ArrayList(entryDatas.size());
            List<Long> BSItemIdList = new ArrayList(entryDatas.size());
            List<Long> FTItemIdList = new ArrayList(entryDatas.size());
            Iterator var5 = entryDatas.iterator();

            while(var5.hasNext()) {
                DynamicObject entry = (DynamicObject)var5.next();
                String itemType = entry.getString("nckd_itemtype");
                Long itemId = entry.getLong("nckd_itemid");
                if (StringUtils.equals("1", itemType)) {
                    SLItemIdList.add(itemId);
                } else if (StringUtils.equals("2", itemType)) {
                    SPItemIdList.add(itemId);
                } else if (StringUtils.equals("3", itemType)) {
                    BSItemIdList.add(itemId);
                } else if (StringUtils.equals("4", itemType)) {
                    FTItemIdList.add(itemId);
                }
            }

            Map<String, Map<String, Object>> itemParamMap = new HashMap(entryDatas.size());
            //itemParamMap.putAll(getItemParamMap(SLItemIdList, "1", "hsbs_salaryitem", (Long)null, false));
            //itemParamMap.putAll(getItemParamMap(SPItemIdList, "2", "hsbs_supportitem", (Long)null, false));
            //itemParamMap.putAll(getItemParamMap(BSItemIdList, "3", "hsbs_bizitem", (Long)null, false));
            //itemParamMap.putAll(getItemParamMap(FTItemIdList, "4", "hsbs_fetchitem", (Long)null, false));
            return itemParamMap;
        }
    }

    /**
     * 获取预设项目 转成对象
     * @param itemId
     * @return
     */
    public static OutImpPresetItem getPresetItemEnum(Long itemId) {
        OutImpPresetItem presetItemEnum = (OutImpPresetItem)commonAndAddPresetItemEnumMap.get(itemId);
        return presetItemEnum;
    }

    /**
     * 获取外单位导入项目中 是否预置项目 = 是 的数据，并转成OutImpPresetItem
     * @return
     */
    public static List<OutImpPresetItem> getPresetItemEnumList() {
        String fields = "id,number,name,nckd_datatype.id,nckd_ispreset,nckd_presetcomment";
        QFilter filter = new QFilter("enable", "=", "1");
        filter.and(new QFilter("nckd_ispreset", "=", true));

        DynamicObjectCollection dyns = SwcConstant.OUTIMPORTITEM_ENTITY.queryOriginalCollection(fields, new QFilter[]{filter});
        List<OutImpPresetItem> outImpPresetItemEnumList = new ArrayList(12);
        dynToOutImpPresetItem(dyns, outImpPresetItemEnumList);
        return outImpPresetItemEnumList;
    }

    /**
     * 数据包转成OutImpPresetItem
     * @param dyns
     */
    public static void dynToOutImpPresetItem (DynamicObjectCollection dyns, List<OutImpPresetItem> outImpPresetItemEnumList) {
        for (DynamicObject dyn : dyns) {
            OutImpPresetItem item = new OutImpPresetItem();
            item.setItemId(dyn.getLong("id"));
            item.setItemName(dyn.getString("name"));
            item.setComment(dyn.getString("nckd_presetcomment"));
            item.setDataTypeId(dyn.getLong("nckd_datatype.id"));
            item.setItemNumber(dyn.getString("number"));
            //item.setMatchColumn(dyn.);
            outImpPresetItemEnumList.add(item);
        }
    }

    public static Map<Long, OutImpPresetItem> getPresetItemEnumMap() {
        List<OutImpPresetItem> itemEnumList = OutImpTemplateHelper.getPresetItemEnumList();
        return itemEnumList.stream()
                .collect(Collectors.toMap(
                        item -> item.getItemId(),
                        item -> item,
                        (existing, replacement) -> existing  // 如果键重复，保留原有的值
                ));
    }

    public static Map<Long, DynamicObject> getAllBaseItemMap() {
        String fields = "id,number,name,nckd_datatype.id,nckd_ispreset,nckd_presetcomment";
        QFilter filter = new QFilter("enable", "=", "1");
        DynamicObjectCollection dyns = SwcConstant.OUTIMPORTITEM_ENTITY.queryOriginalCollection(fields, new QFilter[]{filter});
        return dyns.stream().collect(Collectors.toMap(item -> item.getLong("id"), item -> item));
    }


    static {
//        String fields = "id,number,name,nckd_datatype.id";
//        QFilter filter = new QFilter("enable", "=", "1");
//        filter.and(new QFilter("nckd_ispreset", "=", "0"));
//        DynamicObjectCollection dyns = SwcConstant.OUTIMPORTITEM_ENTITY.queryOriginalCollection(fields, new QFilter[]{filter});
//        for (DynamicObject dyn : dyns) {
//            OutImpPresetItem item = new OutImpPresetItem();
//            item.setItemId(dyn.getLong("id"));
//            item.setItemName(dyn.getString("name"));
//            item.setComment(dyn.getString("nckd_presetcomment"));
//            item.setDataTypeId(dyn.getLong("nckd_datatype.id"));
//            //item.setMatchColumn(dyn.);
//            outImpPresetItemEnumList.add(item);
//        }
    }

}
