package com.ann.demo.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * 转换拼音
 *
 * @Author: LeiJiaXin
 * @Date: 2019/8/7 11:15
 */
@Component
public class PinYinUtils {

    // 拼音路径
    public static String path;
    @Value("${file.pinyinPath}")
    private String pathTemp;


    @PostConstruct
    public void init() {
        path = pathTemp;
    }

    /**
     * 将字符串中的中文转化为拼音,英文字符不变
     *
     * @param inputString 患者姓名
     * @return
     */
    public static String getPingYin(String inputString) {
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String output = "";
        try {
            if (inputString != null && inputString.length() > 0
                    && !"null".equals(inputString)) {
                String substring = diffPro(inputString);
                char[] input = substring.trim().toCharArray();
                for (int i = 0; i < input.length; i++) {
                    if (java.lang.Character.toString(input[i]).matches(
                            "[\\u4E00-\\u9FA5]+")) {
                        char temp = PinyinHelper.toHanyuPinyinStringArray(input[i], defaultFormat)[0].charAt(0);
                        output += temp + "";
                    } else {
                        output += java.lang.Character.toString(input[i]);
                    }
                }

            } else {
                return null;
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return output;
    }

    /**
     * 与pinyin.properties中多音字的姓名对比
     *
     * @param inputString 中文姓名
     * @return 百家姓的拼音读法
     */
    private static String diffPro(String inputString) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(path)));
            properties.load(new InputStreamReader(inputStream, "utf-8"));

            Set keyValue = properties.keySet();
            for (Iterator it = keyValue.iterator(); it.hasNext(); ) {
                String key = (String) it.next();

                if (inputString.substring(0, key.length()).equals(key)) {
                    String value = inputString.substring(key.length(), inputString.length());
                    return properties.getProperty(key) + value;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputString;
    }

    public static void main(String[] args) {
    }
}
