package com.diagbot.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 分割工具类
 * @author: SGTY
 * @create: 2024/8/14 17:40
 */

public class SplitUtil {

    /**
     * @description: 拆分年龄、（年龄不足一周岁的）年龄
     * @name: splitAge
     * @return: java.util.List<java.lang.String>
     * @author: SGTY
     * @create: 2024/8/15 11:02
     */
    public static List<String> splitAge(String data) {
        List<String> ageList = new ArrayList<>();
        StringBuilder currentAge = new StringBuilder();
        StringBuilder currentAgeUnit = new StringBuilder();
        for (int i = 0; i < data.length(); i++) {
            char ch = data.charAt(i);
            if (ch == '岁' || ch == '月' || ch == '天') {
                currentAgeUnit.append(ch);
                ageList.add(currentAge + "," + currentAgeUnit);
                currentAge.setLength(0);
                currentAgeUnit.setLength(0);
            } else if (ch >= '0' && ch <= '9') {
                currentAge.append(ch);
            }
        }
        if (currentAge.length() != 0) {
            ageList.add(currentAge + "," + currentAgeUnit);
        }
        return ageList;
    }

    /**
     * @description: 拆分新生儿出生体重、新生儿入院体重
     * @name: splitWeight
     * @return: java.util.List<java.lang.String>
     * @author: SGTY
     * @create: 2024/8/15 11:08
     */
    public static List<String> splitWeight(String data) {
        List<String> weightList = new ArrayList<>();
        StringBuilder currentWeight = new StringBuilder();
        StringBuilder currentWeightUnit = new StringBuilder();
        // 遍历字符串
        for (int i = 0; i < data.length(); i++) {
            char ch = data.charAt(i);
            if (ch == '克') {
                currentWeightUnit.append(ch);
                weightList.add(currentWeight + "," + currentWeightUnit);
                currentWeight.setLength(0);
                currentWeightUnit.setLength(0);
            } else if (ch >= '0' && ch <= '9') {
                currentWeight.append(ch);
            }
        }
        if (currentWeight.length() != 0) {
            weightList.add(currentWeight + "," + currentWeightUnit);
        }
        return weightList;
    }

    /**
     * @description: 拆分综合医疗服务类、诊断类、治疗类、康复类、中医类、西药类、中药类、血液和血液制品类、耗材类、其他类等费用
     * @name: splitFees
     * @return: java.util.List<java.lang.String>
     * @author: SGTY
     * @create: 2024/8/14 17:50
     */
    public static List<String> splitFees(String data) {
        List<String> feesList = new ArrayList<>();
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+：\\d*(\\.?\\d{0,2})");
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            String split2 = matcher.group().replace("：", ",");
            feesList.add(split2);
        }
        return feesList;
    }

    /**
     * @description: 拆分总费用、自付金额
     * @name: splitTotalFees
     * @return: java.util.List<java.lang.String>
     * @author: SGTY
     * @create: 2024/8/15 11:44
     */
    public static List<String> splitTotalFees(String data) {
        List<String> TotalFeesList = new ArrayList<>();
        String[] parts = data.split("（|）"); // 使用“（”和“）”作为分隔符分割字符串
        StringBuilder currentFeeValue = new StringBuilder();

        for (int i = 3; i < parts[0].length(); i++) {
            currentFeeValue.append(parts[0].charAt(i));
        }
        TotalFeesList.add("总费用" + "," + currentFeeValue);
        currentFeeValue.setLength(0);
        for (int i = 4; i < parts[1].length(); i++) {
            currentFeeValue.append(parts[1].charAt(i));
        }
        TotalFeesList.add("自付金额" + "," + currentFeeValue);

        return TotalFeesList;
    }

    /**
     * @description: 拆分抢救次数、成功次数
     * @name: splitRescueTimes
     * @return: java.util.List<java.lang.String>
     * @author: SGTY
     * @create: 2024/8/15 11:51
     */
    public static List<String> splitRescueTimes(String data) {
        List<String> numbers = new ArrayList<>();
        // 正则表达式匹配一个或多个数字
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            numbers.add(matcher.group());
        }
        return numbers;
    }

    /**
     * @description: 拆分符合与不符合的诊断
     * @name: checkDiagnosis
     * @return: java.util.List<java.lang.String>
     * @author: SGTY
     * @create: 2024/8/16 09:11
     */
    public static List<String> checkDiagnosis(String data) {
        List<String> complianceStatusList = new ArrayList<>();
        String[] split = data.split("\\d+\\.");
        for (String s : split) {
            if (!s.equals("")) {
                String[] split1 = s.split("：");
                complianceStatusList.add(split1[0] + "," + split1[1]);
            }
        }
        return complianceStatusList;
    }

    public static void main(String[] args) {
        String data1 = "（1）一般医疗服务费：850.00（2）一般治疗服务费：（3）护理费：572.60（4）其他费用：120.00";
        String data2 = "（5）病理诊断费：0.00（6）实验室诊断费：0.00（7）影像学诊断费：0.00（8）临床诊断项目费：0.00";
        String data3 = "（9）非手术治疗项目费：0.00（临床物理治疗费：0.00）（10）手术治疗费：10939.50（麻醉费：1063.00手术费：1063.00）";
        String data4 = "（11）康复费：";
        String data5 = "（12）中医治疗费：0.00";
        String data6 = "（13）西药费：3364.59（抗菌药物费用：0.00）";
        String data7 = "（14）中成药费：0.00（15）中草药费：0.00";
        String data8 = "（16）血费：0.00（17）白蛋白类制品费：0.00（18）球蛋白类制品费：0.00（19）凝血因子类制品费：0.00（20）细胞因子类制品费：0.00";
        String data9 = "（21）检查用一次性医用耗材费：（22）治疗用一次性医用耗材费：（23）手术用一次性医用耗材费：";
        String data10 = "（24）其他费：0.00";
        List<String> fees = splitFees(data1);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data2);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data3);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data4);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data5);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data6);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data7);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data8);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data9);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        fees = splitFees(data10);
        for (String fee : fees) {
            System.out.println(fee);
        }
        System.out.println("=====================================");
        String age = "- 月- 天";
        List<String> ages = splitAge(age);
        System.out.println(ages);
//        for (String a : ages) {
//            System.out.println(a);
//        }
        System.out.println("=====================================");
        String weight = "-克";
        List<String> weights = splitWeight(weight);
        for (String w : weights) {
            System.out.println(w);
        }
        System.out.println("=====================================");
        String total = "总费用28142.34（自付金额9992.46）";
        List<String> totals = splitTotalFees(total);
        for (String t : totals) {
            System.out.println(t);
        }
        System.out.println("=====================================");
        String rescueTimes = "抢救0次，成功0次";
        List<String> times = splitRescueTimes(rescueTimes);
        for (String t : times) {
            System.out.println(t);
        }
        System.out.println("=====================================");
        String diagnosis = "1.门诊与出院：符合2.入院与出院：符合3.术前与术后：符合4.临床与病理：符合5.放射与病理：符合";
        List<String> complianceList = checkDiagnosis(diagnosis);
        for (String c : complianceList) {
            System.out.println(c);
        }
    }
}
