package nckd.jxccl.sit.hcsi.formplugin.web.tp.helper;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
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.sit.hcsi.formplugin.web.tp.enums.SinsurPresetItemEnum;

import java.util.*;

public class SinsurTemplateHelper {
    private static List<SinsurPresetItemEnum> sinsurPresetItemEnumList = new ArrayList(12);
    private static Map<Long, SinsurPresetItemEnum> commonAndAddPresetItemEnumMap = new HashMap(4);
    private static Map<Long, SinsurPresetItemEnum> hisDataPresetItemEnumMap = new HashMap(12);
    private static Map<Long, SinsurPresetItemEnum> hisDataForMulTimeItemEnumMap = new HashMap(13);

    public static Map<String, Map<String, Object>> analyzeItemDatas(DynamicObjectCollection itemDatas, Map<Long, Map<String, Object>> existItemDataMap) {
        List<String> IIUnicodeList = new ArrayList(itemDatas.size());
        for (DynamicObject itemData : itemDatas) {
            String itemCategory = itemData.getString("nckd_itemcategory");
            String uniCode = itemData.getString("nckd_itemnumber");
            if (StringUtils.equals("II", itemCategory)) {
                IIUnicodeList.add(uniCode);
            }
        }
        Map<String, Map<String, Object>> itemParamMap = new HashMap(itemDatas.size());
        itemParamMap.putAll(getItemParamMap("number", IIUnicodeList, "1", "sitbs_insuranceitem", existItemDataMap, (Long) null));
        //单独处理一下基本项目：滞纳金跟利息
        itemParamMap.putAll(getBaseItemParamMap());
        return itemParamMap;
    }

    public static Map<String, Map<String, Object>> getBaseItemParamMap() {
        Map<String, Map<String, Object>> itemParamMap = new HashMap(2);
        for (int i = 0; i < 2; i++) {
            Map<String, Object> itemParam = new HashMap(6);
            if (i == 0) {
                itemParam.put("id", 999990L);
                itemParam.put("dataTypeId", 1020L);
                itemParam.put("itemType", "2");
                itemParam.put("itemname", "滞纳金");
                itemParamMap.put("999990", itemParam);
            }
            else {
                itemParam.put("id", 999991L);
                itemParam.put("dataTypeId", 1020L);
                itemParam.put("itemType", "2");
                itemParam.put("itemname", "利息");
                itemParamMap.put("999991", itemParam);
            }
        }
        return itemParamMap;
    }


    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;
        }
    }

    public static SinsurPresetItemEnum getPresetItemEnum(Long itemId) {
        SinsurPresetItemEnum presetItemEnum = (SinsurPresetItemEnum)commonAndAddPresetItemEnumMap.get(itemId);
        if (presetItemEnum == null) {
            presetItemEnum = (SinsurPresetItemEnum)hisDataPresetItemEnumMap.get(itemId);
        }

        if (presetItemEnum == null) {
            presetItemEnum = (SinsurPresetItemEnum)hisDataForMulTimeItemEnumMap.get(itemId);
        }

        return presetItemEnum;
    }

    public static List<SinsurPresetItemEnum> getPresetItemEnumList() {
        return sinsurPresetItemEnumList;
    }

    static {
        sinsurPresetItemEnumList.add(SinsurPresetItemEnum.EMP_IDCARD);
        sinsurPresetItemEnumList.add(SinsurPresetItemEnum.EMP_NAME);
    }



}


