package com.lantone.qc.trans.comsis;

import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.util.ModuleMappingUtil;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.pub.util.StringUtil;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/4/2 14:04
 */
public class OrdinaryAssistant {

    public static Map<String, String> mapKeyContrast(Map<String, String> sourceMap, List<String> keyContrasts) {
        Map<String, String> retMap = Maps.newHashMap();
        String modeId = ModuleMappingUtil.getXmlDataTypeModuleId(sourceMap);
        if (StringUtil.isNotBlank(modeId)) {
            retMap.put("mode_id", modeId);
            sourceMap.put("mode_id", modeId);
        }
        Map<String, String> sourceMap_ = MapUtil.copyMap((Map) sourceMap);
        String[] arry = null;
        String sourceKey = null, targetKey;
        Set<String> removeKey = new HashSet<>();
        for (String keyContrast : keyContrasts) {
            arry = keyContrast.split("=");
            sourceKey = arry[0];
            if (arry.length == 1) {
                targetKey = arry[0];
            } else {
                targetKey = arry[1];
            }
            if (StringUtil.isNotBlank(sourceMap_.get(sourceKey))
                    && (!retMap.containsKey(targetKey) || StringUtil.isBlank(retMap.get(targetKey)))) {
                retMap.put(targetKey, sourceMap_.get(sourceKey));
            }
            removeKey.add(sourceKey);
            //            sourceMap_.remove(sourceKey); // 如果直接remove，多个标准key对应xml中同一个key就会出问题
        }
        //        retMap.putAll(sourceMap_); //如果key相同会覆盖之前的值，存在bug
        Set<String> keySet = retMap.keySet();
        for (String key : sourceMap_.keySet()) {
            if (!keySet.contains(key) && !removeKey.contains(key)) { // 如果之前已放过key就不用放了
                retMap.put(key, sourceMap_.get(key));
            }
        }
        return retMap;
    }

    /**
     * 返回给前端的页面数据多条时排序
     *
     * @param map1
     * @param map2
     * @param key
     * @param format
     * @return
     */
    public static int pageDataTimeSort(Map<String, Object> map1, Map<String, Object> map2, String key, String format) {
        Date date1 = null, date2 = null;
        if (map1.get(key) != null) {
            date1 = DateUtil.parseDate(map1.get(key).toString(), format);
        }
        if (map2.get(key) != null) {
            date2 = DateUtil.parseDate(map2.get(key).toString(), format);
        }
        long time1 = date1 == null ? 0 : date1.getTime();
        long time2 = date2 == null ? 0 : date2.getTime();

        if (time1 == time2) {
            return 0;
        }
        if (time1 > time2) {
            return -1;
        } else {
            return 1;
        }
    }

    /**
     * 去除value中空格（HTML格式空格）
     *
     * @param structureMap
     */
    public static void removeBlank(Map<String, String> structureMap) {
        for (Map.Entry<String, String> map : structureMap.entrySet()) {
            String value = map.getValue();
            if (StringUtil.isBlank(value)) {
                continue;
            }
            if (value.contains("&amp;") || value.contains("amp;") || value.contains("nbsp;") || value.contains("gt;")) {
                structureMap.put(map.getKey(), value.replaceAll("[&amp;|amp;|nbsp;|gt;]", ""));
            }
        }
    }

}