
import com.dxhy.api.model.config.Business;
import com.dxhy.common.util.JsonUtils;
import com.dxhy.common.util.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author Allen
 * @version 1.0.0
 * @ClassName test1.java
 * @Description TODO
 * @createTime 2024年05月15日 09:48:00
 */
@Slf4j
public class test1 {

    public static String processTaxRate(String taxRateStr) {
        try {
            if (StringUtils.isNotBlank(taxRateStr)) {
                // 移除字符串中的所有非数字以及“.”的字符
                String normalizedRate = taxRateStr.replaceAll("[^\\d.]", "");
                if (StringUtils.isNotBlank(normalizedRate)) {
                    // 如果原字符串包含“%”，或者normalizedRate只包含整数或大于1.00，则除以100，保留三位小数四舍五入
                    if (taxRateStr.contains("%") || normalizedRate.matches("\\d+") || Double.valueOf(normalizedRate) > 1.00) {
                        BigDecimal bigDecimal = new BigDecimal(normalizedRate);
                        normalizedRate = String.valueOf(bigDecimal.divide(new BigDecimal(100)).setScale(3, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    //保留两位小数，小数位不足补0
                    DecimalFormat decimalFormat = new DecimalFormat("0.00#");
                    return decimalFormat.format(Double.valueOf(normalizedRate));
                }
            }
        } catch (Exception e) {
            log.info("税率格式化异常,传入的税率：{}，异常信息：{}", taxRateStr,e);
        }
        return taxRateStr;
    }

    public static void main(String[] args) {
//        System.out.println(Arrays.asList("1", "2").size());
        // 测试样例
        /*String[] testCases = {"0.00013","1.5","0.015","1.5%","0.5%","0.0","6","10", "13", "13%", "6%","0.03","0.13",null,"fjasdiosfjo"};
        for (String taxRate : testCases) {
            System.out.println( processTaxRate(taxRate));
        }*/


//        Map<String, Object> map = new HashMap<>();
//        List<Business> list = new ArrayList<>();
//        Business b = new Business();
//        b.setBusinessCode("AQB");
//        b.setBusinessName("安全部");
//
//        Business b1 = new Business();
//        b1.setBusinessCode("AQB1");
//        b1.setBusinessName("安全部1");
//
//        list.add(b);
//        list.add(b1);
//
//        map.put("qszt", "99");
//        map.put("business", list);

//        System.out.println(JsonUtils.getInstance().toJsonString(map));
//        System.out.println(map.toString());
        String str = "{\"business\":[{\"businessCode\":\"AQB\",\"businessName\":\"安全部\"},{\"businessCode\":\"AQB1\",\"businessName\":\"安全部1\"}],\"qszt\":\"99\"}";
        Map<String,Object> map = JsonUtils.getInstance().fromJson(str, Map.class);
        String business = JsonUtils.getInstance().toJsonString(map.get("business"));
        System.out.println(business);
        List<Business> businesses = JsonUtils.getInstance().jsonToList(business, Business.class);
        String a = getBusinessName("a", businesses);
        System.out.println(a);
    }


    public static String getBusinessName(String businessCode, List<Business> businessList) {
        String businessName = "";
        if (!StringHelper.isBlank(businessCode) && businessList != null && businessList.size() > 0) {
            for (Business business : businessList) {
                if (!StringHelper.isBlank(business.getBusinessCode())
                        && business.getBusinessCode().equals(businessCode)) {
                    businessName = StringHelper.trim(business.getBusinessName(), "");
                    break;
                }
            }
        }
        return businessName;
    }
}
