package com.hys.app.util;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.framework.util.StringUtil;
import lombok.experimental.UtilityClass;
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.HanyuPinyinVCharType;

/**
 * 校验工具类
 *
 * @author 张崧
 * @since 2023-12-11
 */
@UtilityClass
public class PinYingUtil {

    /**
     * 将中文字符转换为拼音全拼
     * @param chinese 中文字符
     * @return 拼音全拼
     */
    public static String toPinyin(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuilder pinyin = new StringBuilder();
        try {
            for (char c : chinese.toCharArray()) {
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null) {
                        pinyin.append(pinyinArray[0]);
                    }
                } else {
                    pinyin.append(c);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pinyin.toString();
    }

    /**
     * 将中文字符转换为拼音首字母
     * @param chinese 中文字符
     * @return 拼音首字母
     */
    public static String toPinyinFirstLetter(String chinese) {
        StringBuilder pinyin = new StringBuilder();
        for (char c : chinese.toCharArray()) {
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null) {
                pinyin.append(pinyinArray[0].charAt(0));
            } else {
                pinyin.append(c);
            }
        }
        return pinyin.toString().toUpperCase();
    }
}
