package com.lantone.common.util;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName : CatalogueUtil
 * @Description :
 * @Author : 楼辉荣
 * @Date: 2020-03-09 10:59
 */
public class CatalogueUtil {

    /**
     * 拼接结构化数据
     **/
    public static String structureMapJoin(Map<String, String> structureMap, List<String> keys) {
        String textJoin = "";
        for (String key : keys) {
            if (StringUtil.isNotBlank(structureMap.get(key))) {
                textJoin += key + "：" + structureMap.get(key) + "\n";
            }
        }
        return textJoin;
    }

    public static String removeSpecialChar(String content) {
        if (StringUtil.isEmpty(content)) {
            return "";
        }
        return content.replaceAll("[\r\n|\n|\t]", " ").trim();
    }

    /**
     * 获取查房记录标题中医师职称
     *
     * @return
     */
    public static String subTitle(String srcText) {
        String title = "";
        if (StringUtil.isNotBlank(srcText)) {
            //标题不包含代字，直接返回原标题
            if (!srcText.contains("代")) {
                return srcText;
            }
            //有携、兼字时，取携、兼字之前部分
            if (srcText.contains("携")) {
                title += srcText.substring(0, srcText.indexOf("携"));
            } else if (srcText.contains("兼")) {
                title += srcText.substring(0, srcText.indexOf("兼"));
            }
            //有代字时，取代字之后部分
            if (srcText.contains("代") && srcText.indexOf("代") != srcText.length() - 1) {
                title += srcText.substring(srcText.lastIndexOf("代") + 1);
            } else {
                return srcText;
            }
        }
        return title;
    }

    /**
     * 比较时间,以分钟为单位
     * endDate比startDate多diff分钟则返回true
     *
     * @param startDate
     * @param endDate
     * @param diff
     * @return
     */
    public static boolean compareTime(Date startDate, Date endDate, Long diff) {
        boolean overtime = false;
        if (startDate == null || endDate == null) {
            return false;
        }
        Calendar calendar_s = Calendar.getInstance();
        Calendar calendar_e = Calendar.getInstance();
        Long time_s, time_e, time_diff;
        try {
            calendar_s.setTime(startDate);
            calendar_e.setTime(endDate);

            time_s = calendar_s.getTimeInMillis();
            time_e = calendar_e.getTimeInMillis();

            time_diff = (time_e - time_s) / (1000 * 60);
            if (time_diff > diff) {
                overtime = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return overtime;
    }

    public static String[] extractDigit(String value) {
        String[] res = new String[2];
        try {
            String regTime = "([\\d]+)([\\u4e00-\\u9fa5]+)";
            Pattern pattern = Pattern.compile(regTime);
            Matcher matcher = pattern.matcher(value);
            if (matcher.find(0)) {
                res[0] = matcher.group(1);
                res[1] = matcher.group(2);
            } else {
                res[0] = value;
                res[1] = "";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }

}