package lingtao.net.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lingtao.net.bean.Product;
import lingtao.net.util.PriceUtils;
import lingtao.net.vo.PricingListVo;
import lingtao.net.vo.ProductVo;
import lingtao.net.vo.ProvidePrice;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class PriceService {

    public PricingListVo getAddressPrice(Product product, List<ProductVo> proList) {

        Province province = parseAddress(product.getAddress());
        if (province == null) {
            return new PricingListVo(100, "无效地址。");
        }
        String plantName = product.getPlantName();
        if ("领鸿".equals(plantName) || "欣海信".equals(plantName)) {
            List<ProvincePriceVo> lyst = JSONObject.parseArray(ly_st, ProvincePriceVo.class);
            List<ProvincePriceVo> lyyt = JSONObject.parseArray(ly_yt, ProvincePriceVo.class);
            List<BigDecimal> prices = getProvincePrice(lyst, province.province);
            if (prices != null && prices.size() > 0) {
                double firstPrice = StringtoDouble(prices.get(4).toString());
                double secPrice = StringtoDouble(prices.get(5).toString());
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());
                    double finalePrice = 0;
                    if (weight <= 0.5) {
                        finalePrice = StringtoDouble(prices.get(0).toString());
                    } else if (weight <= 1) {
                        finalePrice = StringtoDouble(prices.get(1).toString());
                    } else if (weight <= 2) {
                        finalePrice = StringtoDouble(prices.get(2).toString());
                    } else if (weight <= 3) {
                        finalePrice = StringtoDouble(prices.get(3).toString());
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                    productVo.getProvidePrices().add(new ProvidePrice("申通快递", finalePrice));
                }
            }
            prices = getProvincePrice(lyyt, province.province);
            if (prices != null && prices.size() > 0) {
                double firstPrice = StringtoDouble(prices.get(5).toString());
                double secPrice = StringtoDouble(prices.get(6).toString());
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());
                    double finalePrice = 0;
                    if (weight <= 0.3 && StringtoDouble(prices.get(0).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(0).toString());
                    } else if (weight <= 0.5 && StringtoDouble(prices.get(1).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(1).toString());
                    } else if (weight <= 1 && StringtoDouble(prices.get(2).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(2).toString());
                    } else if (weight <= 2 && StringtoDouble(prices.get(3).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(3).toString());
                    } else if (weight <= 3 && StringtoDouble(prices.get(4).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(4).toString());
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                    productVo.getProvidePrices().add(new ProvidePrice("圆通快递", finalePrice));
                }
            }

        }
        if ("艾印".equals(plantName) || "即客".equals(plantName)) {
            List<String> list17 = Arrays.asList("新疆", "西藏", "甘肃", "宁夏", "青海");
            List<String> list8 = Arrays.asList("内蒙古");
            double price = 0;
            if (isInAddress(list17, province.getProvince())) {
                price = 17;
            } else if (isInAddress(list8, province.getProvince())) {
                price = 8;
            } else {
                price = 3.5;
                if ("即客".equals(plantName)) {
                    price = 4;
                }
            }
            for (ProductVo productVo : proList) {
                productVo.getProvidePrices().add(new ProvidePrice("EMS", price));
            }
        }

        if ("彩印通".equals(plantName)) {
            List<ProvincePriceVo> cytzt = JSONObject.parseArray(cyt_zt, ProvincePriceVo.class);
            List<ProvincePriceVo> cytyt = JSONObject.parseArray(cyt_yt, ProvincePriceVo.class);
            List<ProvincePriceVo2> sfsome = JSONObject.parseArray(cyt_sf_some, ProvincePriceVo2.class);
            List<ProvincePriceVo2> sfmorning = JSONObject.parseArray(cyt_sf_morning, ProvincePriceVo2.class);
            List<ProvincePriceVo2> sfnextDay = JSONObject.parseArray(cyt_sf_nextDay, ProvincePriceVo2.class);
            List<ProvincePriceVo2> sftowDay = JSONObject.parseArray(cyt_sf_towDay, ProvincePriceVo2.class);

            calculateAndAddPrices(cytzt, "中通速递", province, proList, false);
            calculateAndAddPrices(cytyt, "圆通速递", province, proList, false);
            calculateAndAddPrices2(sfsome, "顺丰今日达", province, proList, false);
            calculateAndAddPrices2(sfmorning, "顺丰次晨达", province, proList, false);
            calculateAndAddPrices2(sfnextDay, "顺丰次日达", province, proList, false);
            calculateAndAddPrices2(sftowDay, "顺丰隔日达", province, proList, true);

        }
        if ("鸿晟".equals(plantName)) {
            for (ProductVo productVo : proList) {
                productVo.getProvidePrices().add(new ProvidePrice("Other", 3.5));
            }
            double price = 0;
            List<String> list12 = Arrays.asList("浙江", "江苏", "上海", "安徽");
            List<String> list16 = Arrays.asList("福建");
            if (isInAddress(list12, province.getProvince())) {
                price = 12;
            } else if (isInAddress(list16, province.getProvince())) {
                price = 16;
            } else {
                price = 23;
            }
            for (ProductVo productVo : proList) {
                productVo.getProvidePrices().add(new ProvidePrice("SF", price));
            }
        }
        if ("驰远".equals(plantName)) {
            Map<String, Map<String, List<String>>> sf_bk = (Map<String, Map<String, List<String>>>) JSON.parse(cy_sf_bk);
            Map<String, Map<String, List<String>>> sf_tk = (Map<String, Map<String, List<String>>>) JSON.parse(cy_sf_tk);
            List<ProvincePriceVo> yt_xiao = JSONObject.parseArray(cy_yt_xiao, ProvincePriceVo.class);
            List<ProvincePriceVo> yt_sd = JSONObject.parseArray(cy_yt_sd, ProvincePriceVo.class);
            List<ProvincePriceVo> ems = JSONObject.parseArray(cy_ems, ProvincePriceVo.class);

            String priceString = priceKey(sf_bk, province);
            if (!StringUtils.isEmpty(priceString)) {
                double firstPrice = StringtoDouble(priceString.split("_")[1]);
                double secPrice = StringtoDouble(priceString.split("_")[2]);
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());
                    double secWeightPrice = 0.0;
                    if (weight > 1) {
                        secWeightPrice = Math.ceil(weight - 1.0) * secPrice;
                    }
                    productVo.getProvidePrices().add(new ProvidePrice("顺丰标快", firstPrice + secWeightPrice));
                }
            }
            priceString = priceKey(sf_tk, province);
            if (!StringUtils.isEmpty(priceString)) {
                double firstPrice = StringtoDouble(priceString.split("_")[1]);
                double secPrice = StringtoDouble(priceString.split("_")[2]);
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());
                    double secWeightPrice = 0.0;
                    if (weight > 1) {
                        secWeightPrice = Math.ceil(weight - 1.0) * secPrice;
                    }
                    productVo.getProvidePrices().add(new ProvidePrice("顺丰特快", firstPrice + secWeightPrice));
                }
            }
            List<BigDecimal> prices = getProvincePrice(yt_xiao, province.province);
            if (prices != null && prices.size() > 0) {
                double firstPrice = StringtoDouble(prices.get(4).toString());
                double secPrice = StringtoDouble(prices.get(5).toString());
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());
                    double finalePrice = 0;
                    if (weight <= 0.5) {
                        finalePrice = StringtoDouble(prices.get(0).toString());
                    } else if (weight <= 1) {
                        finalePrice = StringtoDouble(prices.get(1).toString());
                    } else if (weight <= 2) {
                        finalePrice = StringtoDouble(prices.get(2).toString());
                    } else if (weight <= 3) {
                        finalePrice = StringtoDouble(prices.get(3).toString());
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                    productVo.getProvidePrices().add(new ProvidePrice("圆通快递", finalePrice));
                }
            }

            prices = getProvincePrice(yt_sd, province.province);
            if (prices != null && prices.size() > 0) {
                double firstPrice = StringtoDouble(prices.get(0).toString());
                double secPrice = StringtoDouble(prices.get(1).toString());
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());
                    double finalePrice = 0;

                    finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    productVo.getProvidePrices().add(new ProvidePrice("圆通速递", finalePrice));
                }
            }

            prices = getProvincePrice(ems, province.province);
            if (prices != null && prices.size() > 0) {
                double firstPrice = StringtoDouble(prices.get(4).toString());
                double secPrice = StringtoDouble(prices.get(5).toString());
                for (ProductVo productVo : proList) {
                    double weight = StringtoDouble(productVo.getWeight());

                    double finalePrice = 0;
                    if (weight <= 0.5 && StringtoDouble(prices.get(0).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(0).toString());
                    } else if (weight <= 1 && StringtoDouble(prices.get(1).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(1).toString());
                    } else if (weight <= 2 && StringtoDouble(prices.get(2).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(2).toString());
                    } else if (weight <= 3 && StringtoDouble(prices.get(3).toString()) != 0) {
                        finalePrice = StringtoDouble(prices.get(3).toString());
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                    productVo.getProvidePrices().add(new ProvidePrice("邮政快递", finalePrice));
                }
            }

        }
        return new PricingListVo(proList);
    }

    private static Double StringtoDouble(String str) {
        try {
            return new BigDecimal(str).doubleValue();
        } catch (Exception e) {
            return 0.0;
        }
    }

    // 定义一个通用方法来处理价格计算逻辑
    private void calculateAndAddPrices(List<ProvincePriceVo> priceData, String courierName, Province province, List<ProductVo> proList, boolean hasThirdPrice) {
        List<BigDecimal> prices = getProvincePrice(priceData, province.province);
        List<String> nofirst = Arrays.asList("内蒙古", "西藏", "新疆", "香港", "澳门", "台湾");
        if (prices == null || prices.size() < (hasThirdPrice ? 3 : 2)) {
            return; // 如果价格数据不足，则直接返回
        }
        try {
            double firstPrice = StringtoDouble(prices.get(0).toString());
            double secPrice = StringtoDouble(prices.get(1).toString());
            double thrPrice = hasThirdPrice && prices.size() > 2 ? StringtoDouble(prices.get(2).toString()) : 0;

            for (ProductVo productVo : proList) {
                double weight = StringtoDouble(productVo.getWeight());
                double finalePrice = 0;

                if (courierName.equals("中通速递") || courierName.equals("圆通速递")) {
                    if (weight <= 3 && !isInAddress(nofirst, province.getProvince())) {
                        finalePrice = firstPrice;
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                } else {
                    if (hasThirdPrice && weight > 30) {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * thrPrice;
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                }

                productVo.getProvidePrices().add(new ProvidePrice(courierName, finalePrice));
            }
        } catch (Exception e) {
            // 记录日志或采取其他措施处理异常
            System.err.println("Error parsing price data: " + e.getMessage());
        }
    }

    private void calculateAndAddPrices2(List<ProvincePriceVo2> priceData, String courierName, Province province, List<ProductVo> proList, boolean hasThirdPrice) {
        List<BigDecimal> prices = getProvincePrice2(priceData, province);
        List<String> nofirst = Arrays.asList("内蒙古", "西藏", "新疆", "香港", "澳门", "台湾");
        if (prices == null || prices.size() < (hasThirdPrice ? 3 : 2)) {
            return; // 如果价格数据不足，则直接返回
        }
        try {
            double firstPrice = StringtoDouble(prices.get(0).toString());
            double secPrice = StringtoDouble(prices.get(1).toString());
            double thrPrice = hasThirdPrice && prices.size() > 2 ? StringtoDouble(prices.get(2).toString()) : 0;

            for (ProductVo productVo : proList) {
                double weight = StringtoDouble(productVo.getWeight());
                double finalePrice = 0;

                if (courierName.equals("中通速递") || courierName.equals("圆通速递")) {
                    if (weight <= 3 && !isInAddress(nofirst, province.getProvince())) {
                        finalePrice = firstPrice;
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                } else {
                    if (hasThirdPrice && weight > 30) {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * thrPrice;
                    } else {
                        finalePrice = firstPrice + Math.ceil(weight - 1.0) * secPrice;
                    }
                }

                productVo.getProvidePrices().add(new ProvidePrice(courierName, finalePrice));
            }
        } catch (Exception e) {
            // 记录日志或采取其他措施处理异常
            System.err.println("Error parsing price data: " + e.getMessage());
        }
    }

    private static List<BigDecimal> getProvincePrice2(List<ProvincePriceVo2> list, Province province) {
        List<BigDecimal> prices = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (isInAddress(Arrays.asList(list.get(i).getProvince()), province.getProvince())) {
                List<String> cities = list.get(i).getCities();
                if (cities.size() == 0 || isInAddress(cities, province.getCity())) {
                    prices = list.get(i).getPrices();
                } else if (list.get(i).getOtherPrices().size() > 0) {
                    prices = list.get(i).getOtherPrices();
                }
            }
        }
        return prices;
    }

    private static List<BigDecimal> getProvincePrice(List<ProvincePriceVo> list, String provinceName) {
        List<BigDecimal> prices = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (isInAddress(list.get(i).getRegions(), provinceName)) {
                prices = list.get(i).getPrice();
            }
        }
        return prices;
    }

    /**
     * 根据省份信息查找价格键
     * 该方法通过遍历一个复杂的映射结构来查找与给定省份信息相对应的价格键
     * 它首先遍历外层映射，然后遍历内层映射中的每个省份列表，检查是否有匹配的省份和城市
     *
     * @param map      一个映射，其键是价格键，值是另一个映射；内层映射的键是省份名称，值是城市列表
     * @param province 省份对象，包含需要查找的省份和城市信息
     * @return 如果找到匹配的省份和城市，则返回对应的价格键；否则返回空字符串
     */
    private static String priceKey(Map<String, Map<String, List<String>>> map, Province province) {
        // 初始化结果字符串
        String result = "";
        // 获取外层映射的条目迭代器
        Iterator<Map.Entry<String, Map<String, List<String>>>> iterator = map.entrySet().iterator();
        // 遍历外层映射
        while (iterator.hasNext()) {
            // 获取当前外层映射条目
            Map.Entry<String, Map<String, List<String>>> entry = iterator.next();
            // 获取内层映射
            Map<String, List<String>> value = entry.getValue();
            // 获取内层映射的条目迭代器
            Iterator<Map.Entry<String, List<String>>> iteratorValue = value.entrySet().iterator();
            // 遍历内层映射
            while (iteratorValue.hasNext()) {
                // 获取当前内层映射条目
                Map.Entry<String, List<String>> provinces = iteratorValue.next();
                // 获取省份名称
                String provincesName = provinces.getKey();
                // 检查省份名称是否与给定的省份信息匹配
                if (isInAddress(Arrays.asList(provincesName), province.getProvince())) {
                    // 获取城市列表
                    List<String> citys = provinces.getValue();
                    // 如果城市列表为空，则直接返回当前的价格键
                    if (citys.size() == 0) {
                        result = entry.getKey();
                        return result;
                    } else {
                        // 如果城市列表不为空，进一步检查城市是否匹配
                        if (isInAddress(citys, province.getCity())) {
                            result = entry.getKey();
                            return result;
                        }
                    }
                }
            }
        }
        // 如果没有找到匹配的省份和城市，返回空字符串
        return result;
    }

    //驰远顺丰标快
    private static String cy_sf_bk = "{\"price_12_1\":{\"福建省\":[\"龙岩\"]},\"price_13_2\":{\"福建省\":[\"南平\",\"莆田\",\"福州\",\"泉州\",\"漳州\",\"厦门\",\"三明\",\"宁德\"]},\"price_16_6\":{\"广东省\":[\"河源\",\"韶关\",\"汕头\",\"清远\",\"深圳\",\"广州\",\"中山\",\"惠州\",\"揭阳\",\"东莞\",\"佛山\",\"汕尾\",\"潮州\",\"梅州\"],\"浙江省\":[\"台州\",\"温州\"]},\"price_18_5\":{\"北京市\":[],\"上海市\":[],\"天津市\":[],\"河北省\":[],\"河南省\":[],\"山东省\":[],\"浙江省\":[\"湖州\",\"宁波\",\"舟山\",\"丽水\",\"金华\",\"嘉兴\",\"衢州\",\"绍兴\",\"杭州\"],\"江西省\":[]},\"price_18_6\":{\"广西壮族自治区\":[\"河池\",\"防城港\",\"百色\",\"梧州/贺州\",\"钦州\",\"北海\",\"柳州\",\"来宾\",\"桂林\",\"玉林\",\"崇左\",\"贵港\",\"南宁\"],\"海南省\":[],\"重庆市\":[],\"辽宁省\":[],\"江苏省\":[],\"湖北省\":[],\"四川省\":[],\"陕西省\":[],\"贵州省\":[],\"云南省\":[],\"山西省\":[],\"内蒙古自治区\":[\"包头\",\"通辽\",\"呼和浩特\",\"乌海\",\"锡林郭勒盟\",\"赤峰\",\"鄂尔多斯\",\"乌兰察布\",\"阿拉善盟\",\"巴彦淖尔\"],\"安徽省\":[],\"广东省\":[\"江门\",\"茂名\",\"云浮\",\"湛江\",\"阳江\",\"珠海\",\"肇庆\"],\"湖南省\":[],\"甘肃省\":[],\"宁夏回族自治区\":[],\"青海省\":[\"海南藏族自治州\",\"果洛藏族自治州\",\"海东\",\"黄南藏族自治州\",\"海西蒙古族藏族自治州\",\"格尔木\",\"海北藏族自治州\",\"西宁\"]},\"price_18_9\":{\"吉林省\":[],\"黑龙江省\":[],\"内蒙古自治区\":[\"呼伦贝尔\",\"兴安盟\"]},\"price_20_10\":{\"新疆维吾尔自治区\":[]},\"price_21_12\":{\"青海省\":[\"玉树藏族自治州\"]},\"price_25_19\":{\"西藏自治区\":[\"拉萨\",\"日喀则\",\"那曲\",\"山南\",\"林芝\"]},\"price_26_21\":{\"西藏自治区\":[\"昌都\"]}}";
    //驰远顺丰特快
    private static String cy_sf_tk = "{\"price_18_8\":{\"广东省\":[\"汕头\",\"揭阳\",\"汕尾\",\"潮州\"]},\"price_20_8\":{\"广东省\":[\"河源\",\"茂名\",\"韶关\",\"清远\",\"深圳\",\"珠海\",\"广州\",\"肇庆\",\"中山\",\"江门\",\"云浮\",\"惠州\",\"湛江\",\"东莞\",\"阳江\",\"佛山\"],\"浙江省\":[\"宁波\",\"舟山\"]},\"price_22_10\":{\"上海市\":[],\"江苏省\":[],\"浙江省\":[\"湖州\",\"丽水\",\"金华\",\"嘉兴\",\"衢州\",\"绍兴\",\"杭州\"]},\"price_22_8\":{\"江西省\":[]},\"price_23_10\":{\"北京市\":[],\"天津市\":[],\"湖北省\":[],\"河北省\":[],\"河南省\":[],\"山东省\":[],\"安徽省\":[],\"湖南省\":[],\"广西壮族自治区\":[],\"海南省\":[]},\"price_23_13\":{\"重庆市\":[],\"辽宁省\":[],\"四川省\":[],\"陕西省\":[],\"贵州省\":[],\"云南省\":[],\"山西省\":[],\"甘肃省\":[],\"宁夏回族自治区\":[]},\"price_23_14\":{\"内蒙古自治区\":[\"包头\",\"通辽\",\"呼和浩特\",\"乌海\",\"锡林郭勒盟\",\"赤峰\",\"鄂尔多斯\",\"乌兰察布\",\"阿拉善盟\",\"巴彦淖尔\"],\"青海省\":[\"海南藏族自治州\",\"果洛藏族自治州\",\"海东\",\"黄南藏族自治州\",\"海西蒙古族藏族自治州\",\"格尔木\",\"玉树藏族自治州\",\"海北藏族自治州\",\"西宁\"]},\"price_23_18\":{\"吉林省\":[],\"黑龙江省\":[],\"内蒙古自治区\":[\"呼伦贝尔\",\"兴安盟\"]},\"price_26_21\":{\"西藏自治区\":[\"拉萨\",\"日喀则\",\"那曲\",\"山南\",\"林芝\",\"昌都\"],\"新疆维吾尔自治区\":[\"阿勒泰地区\",\"克孜勒苏柯尔克孜自治州\",\"哈密地区\",\"铁门关\",\"双河\",\"巴音郭楞蒙古自治州\",\"博尔塔拉蒙古自治州\",\"阿克苏地区\",\"昌吉回族自治州\",\"克拉玛依\",\"乌鲁木齐\",\"石河子\",\"喀什地区\",\"奎屯\",\"吐鲁番地区\",\"伊犁哈萨克自治州\",\"塔城地区\",\"和田地区\"]},\"price_20_10\":{\"新疆维吾尔自治区\":[\"乌鲁木齐\",\"石河子\",\"喀什地区\",\"奎屯\",\"吐鲁番地区\",\"伊犁哈萨克自治州\",\"塔城地区\",\"和田地区\"]},\"price_21_12\":{\"青海省\":[\"玉树藏族自治州\"]},\"price_25_19\":{\"西藏自治区\":[\"拉萨\",\"日喀则\",\"那曲\",\"山南\",\"林芝\"]}}";
    //驰远圆通快递
    private static String cy_yt_xiao = "[{\"price\":[2.7,3.2,3.7,4.7,4.5,0.8],\"regions\":[\"省内\"]},{\"price\":[2.7,3.2,3.7,4.7,5.0,1.2],\"regions\":[\"江苏\",\"浙江\",\"安徽\",\"广东\",\"江西\",\"湖南\"]},{\"price\":[3.7,4.2,4.7,5.7,7.0,1.4],\"regions\":[\"上海\"]},{\"price\":[2.7,3.2,3.7,4.7,5.5,1.8],\"regions\":[\"广西\",\"湖北\",\"河南省\",\"河北省\",\"天津市\",\"山西\"]},{\"price\":[2.7,3.2,3.7,4.7,6.0,2.3],\"regions\":[\"四川省\",\"重庆市\",\"云南\",\"贵州\"]},{\"price\":[2.7,3.2,3.7,4.7,6.0,1.8],\"regions\":[\"山东\"]},{\"price\":[2.7,3.2,3.7,4.7,6.0,2.1],\"regions\":[\"陕西省\"]},{\"price\":[4.2,4.7,5.2,6.2,8.0,2.0],\"regions\":[\"北京\"]},{\"price\":[8.3,8.3,14.3,20.3,8.0,6.0],\"regions\":[\"甘肃省\",\"宁夏\"]},{\"price\":[8.3,8.3,16.3,24.3,8.0,8.0],\"regions\":[\"青海省\"]},{\"price\":[10.3,10.3,18.3,26.3,10.0,8.0],\"regions\":[\"内蒙古\"]},{\"price\":[18.3,18.3,33.3,48.3,18.0,15.0],\"regions\":[\"新疆\"]},{\"price\":[22.3,22.3,40.3,58.3,22.0,18.0],\"regions\":[\"西藏\"]},{\"price\":[2.7,3.2,3.7,4.7,7.0,3.2],\"regions\":[\"黑龙江\",\"吉林\",\"辽宁\"]}]";
    //驰远邮政快递
    private static String cy_ems = "[{\"price\":[2.8,3.4,4.0,6.0,6.0,2.0],\"regions\":[\"福建\"]},{\"price\":[2.8,3.4,4.0,6.0,8.0,2.0],\"regions\":[\"江西省\",\"浙江省\",\"湖北省\",\"湖南省\",\"上海市\",\"广东省\",\"江苏省\",\"安徽省\"]},{\"price\":[2.8,3.4,6.0,8.0,10.0,3.0],\"regions\":[\"河南省\",\"贵州省\",\"山东省\",\"广西\",\"河北省\",\"陕西省\",\"海南省\",\"山西省\",\"天津市\",\"重庆市\",\"四川省\"]},{\"price\":[3.5,5.0,7.0,20.0,14.0,4.0],\"regions\":[\"云南省\",\"甘肃省\",\"内蒙\",\"辽宁省\",\"吉林省\",\"宁夏\",\"黑龙江省\"]},{\"price\":[8.0,0,0,0,10.0,3.0],\"regions\":[\"北京\"]},{\"price\":[12.0,0,0,0,16.0,12.0],\"regions\":[\"青海省\",\"新疆\",\"西藏\"]}]";
    //驰远圆通速递(重货)
    private static String cy_yt_sd = "[{\"price\":[4.0,0.8],\"regions\":[\"福建\"]},{\"price\":[4.0,1.0],\"regions\":[\"广东\"]},{\"price\":[5.0,1.3],\"regions\":[\"上海\"]},{\"price\":[4.5,1.1],\"regions\":[\"浙江\",\"江西\",\"江苏\",\"安徽\",\"湖南\"]},{\"price\":[4.5,1.4],\"regions\":[\"湖北\",\"山东\",\"河南\",\"河北\",\"天津\"]},{\"price\":[5.5,1.7],\"regions\":[\"北京\"]},{\"price\":[5.5,1.6],\"regions\":[\"陕西\"]},{\"price\":[5.5,1.4],\"regions\":[\"广西\",\"山西\"]},{\"price\":[5.5,1.9],\"regions\":[\"贵州\",\"重庆\",\"云南\",\"四川\",\"海南\"]},{\"price\":[5.5,2.5],\"regions\":[\"辽宁\",\"黑龙江\",\"吉林\"]},{\"price\":[12.0,6.6],\"regions\":[\"甘肃\",\"宁夏\",\"青海\"]},{\"price\":[13.0,7.6],\"regions\":[\"内蒙古\"]},{\"price\":[20.0,15.6],\"regions\":[\"西藏\",\"新疆\"]}]";

    //彩印通中通
    private static String cyt_zt = "[{\"price\":[3.5,1],\"regions\":[\"广东\"]},{\"price\":[3.5,2],\"regions\":[\"浙江\",\"湖南\",\"福建\",\"江苏\",\"湖北\",\"广西\",\"江西\",\"安徽\"]},{\"price\":[3.5,3],\"regions\":[\"天津\",\"河南\",\"河北\",\"山东\",\"四川\",\"重庆\",\"贵州\",\"海南\",\"云南\"]},{\"price\":[3.5,4],\"regions\":[\"山西\",\"陕西\",\"黑龙江\",\"吉林\",\"辽宁\"]},{\"price\":[6,2],\"regions\":[\"上海\"]},{\"price\":[4.5,3],\"regions\":[\"北京\"]},{\"price\":[6,6],\"regions\":[\"甘肃\",\"宁夏\",\"青海\",\"内蒙古\"]},{\"price\":[20,20],\"regions\":[\"澳门\",\"香港\",\"台湾\"]},{\"price\":[10,8],\"regions\":[\"新疆\",\"西藏\"]}]";
    //彩印通圆通
    private static String cyt_yt = "[{\"price\":[2.6,1],\"regions\":[\"广东\"]},{\"price\":[2.6,2],\"regions\":[\"江苏省\",\"浙江省\",\"安徽省\",\"上海\",\"广西壮族自治区\",\"福建省\",\"江西省\",\"湖南省\",\"湖北省\"]},{\"price\":[2.6,3],\"regions\":[\"贵州省\",\"云南省\",\"海南省\",\"河南省\",\"山东省\",\"四川省\",\"重庆市\",\"北京市\",\"河北省\",\"天津市\"]},{\"price\":[2.6,4],\"regions\":[\"黑龙江省\",\"辽宁省\",\"吉林省\",\"山西省\",\"陕西省\"]},{\"price\":[2.6,5],\"regions\":[\"宁夏回族自治区\",\"青海省\",\"甘肃省\"]},{\"price\":[6,6],\"regions\":[\"内蒙古自治区\"]},{\"price\":[10,8],\"regions\":[\"新疆维吾尔自治区\",\"西藏自治区\"]},{\"price\":[30,15],\"regions\":[\"台湾\",\"香港\",\"澳门\"]}]\n";
    //彩印通顺丰今日达
    private static String cyt_sf_some = "[{\"province\":\"广东\",\"cities\":[\"佛山\"],\"price\":[11,1]},{\"province\":\"广东\",\"cities\":[\"广州\",\"深圳\"],\"price\":[16,2]},{\"province\":\"广东\",\"cities\":[\"江门\",\"中山\",\"东莞\"],\"price\":[15,2]}]";
    //彩印通顺丰次晨达
    private static String cyt_sf_morning = "[{\"province\":\"广东\",\"cities\":[\"佛山\",\"肇庆\",\"云浮\"],\"price\":[11,1]},{\"province\":\"广东\",\"cities\":[\"广州\",\"深圳\"],\"price\":[13,2]},{\"province\":\"广东\",\"cities\":[\"江门\",\"中山\",\"东莞\",\"惠州\",\"珠海\",\"肇庆\",\"清远\",\"云浮\",\"阳江\",\"揭阳\",\"茂名\",\"韶关\",\"梅州\",\"汕头\",\"湛江\",\"河源\",\"潮州\",\"汕尾\"],\"price\":[12,2]},{\"province\":\"广西\",\"cities\":[\"贺州\",\"梧州\"],\"price\":[16,6]},{\"province\":\"福建\",\"cities\":[\"泉州\"],\"price\":[20,8]},{\"province\":\"江苏\",\"cities\":[\"南京\",\"无锡\",\"镇江\",\"苏州\",\"常州\"],\"price\":[25,13]},{\"province\":\"上海\",\"cities\":[\"上海\"],\"price\":[25,13]},{\"province\":\"浙江\",\"cities\":[\"杭州\",\"湖州\",\"嘉兴\",\"宁波\",\"绍兴\",\"台州\",\"温州\"],\"price\":[25,13]},{\"province\":\"北京\",\"cities\":[\"北京\"],\"price\":[25,14]}]";
    //彩印通次日达
    private static String cyt_sf_nextDay = "[{\"province\":\"广东\",\"cities\":[\"阳江\",\"揭阳\",\"茂名\",\"韶关\",\"梅州\",\"汕头\",\"湛江\",\"河源\",\"潮州\",\"汕尾\"],\"prices\":[22,8],\"otherPrices\":[0,0]},{\"province\":\"福建\",\"cities\":[],\"prices\":[20,8],\"otherPrices\":[0,0]},{\"province\":\"广西\",\"cities\":[\"贺州\",\"梧州\"],\"prices\":[16,6],\"otherPrices\":[22,8]},{\"province\":\"江西\",\"cities\":[],\"prices\":[22,8],\"otherPrices\":[0,0]},{\"province\":\"湖南\",\"cities\":[],\"prices\":[22,8],\"otherPrices\":[0,0]},{\"province\":\"海南\",\"cities\":[],\"prices\":[22,8],\"otherPrices\":[0,0]},{\"province\":\"贵州\",\"cities\":[],\"prices\":[23,12],\"otherPrices\":[0,0]},{\"province\":\"安徽\",\"cities\":[],\"prices\":[23,12],\"otherPrices\":[0,0]},{\"province\":\"湖北\",\"cities\":[],\"prices\":[23,12],\"otherPrices\":[0,0]},{\"province\":\"江苏\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"上海\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"浙江\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"重庆\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"四川\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"云南\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"河南\",\"cities\":[],\"prices\":[23,13],\"otherPrices\":[0,0]},{\"province\":\"北京\",\"cities\":[],\"prices\":[23,14],\"otherPrices\":[0,0]},{\"province\":\"天津\",\"cities\":[],\"prices\":[23,14],\"otherPrices\":[0,0]},{\"province\":\"辽宁\",\"cities\":[],\"prices\":[23,14],\"otherPrices\":[0,0]},{\"province\":\"陕西\",\"cities\":[],\"prices\":[23,14],\"otherPrices\":[0,0]},{\"province\":\"河北\",\"cities\":[],\"prices\":[23,14],\"otherPrices\":[0,0]},{\"province\":\"山东\",\"cities\":[],\"prices\":[23,14],\"otherPrices\":[0,0]},{\"province\":\"甘肃\",\"cities\":[],\"prices\":[23,18],\"otherPrices\":[0,0]},{\"province\":\"宁夏\",\"cities\":[],\"prices\":[23,18],\"otherPrices\":[0,0]},{\"province\":\"青海\",\"cities\":[],\"prices\":[23,18],\"otherPrices\":[0,0]},{\"province\":\"内蒙古\",\"cities\":[\"呼伦贝尔\",\"兴安盟\"],\"prices\":[25,20],\"otherPrices\":[23,18]},{\"province\":\"吉林\",\"cities\":[],\"prices\":[25,20],\"otherPrices\":[0,0]},{\"province\":\"黑龙江\",\"cities\":[],\"prices\":[25,20],\"otherPrices\":[0,0]},{\"province\":\"新疆\",\"cities\":[],\"prices\":[28,25],\"otherPrices\":[0,0]},{\"province\":\"西藏\",\"cities\":[],\"prices\":[28,25],\"otherPrices\":[0,0]}]";
    //彩印通隔日达
    private static String cyt_sf_towDay = "[{\"province\":\"江西\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"湖南\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"海南\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"贵州\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"安徽\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"湖北\",\"cities\":[],\"prices\":[18,8,8],\"otherPrices\":[0,0,0]},{\"province\":\"江苏\",\"cities\":[],\"prices\":[18,9,9],\"otherPrices\":[0,0,0]},{\"province\":\"上海\",\"cities\":[],\"prices\":[18,9,9],\"otherPrices\":[0,0,0]},{\"province\":\"浙江\",\"cities\":[],\"prices\":[18,9,9],\"otherPrices\":[0,0,0]},{\"province\":\"重庆\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"四川\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"云南\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"河南\",\"cities\":[],\"prices\":[18,6,6],\"otherPrices\":[0,0,0]},{\"province\":\"北京\",\"cities\":[],\"prices\":[18,7,7],\"otherPrices\":[0,0,0]},{\"province\":\"天津\",\"cities\":[],\"prices\":[18,7,7],\"otherPrices\":[0,0,0]},{\"province\":\"辽宁\",\"cities\":[],\"prices\":[18,7,7],\"otherPrices\":[0,0,0]},{\"province\":\"陕西\",\"cities\":[],\"prices\":[18,7,7],\"otherPrices\":[0,0,0]},{\"province\":\"河北\",\"cities\":[],\"prices\":[18,7,7],\"otherPrices\":[0,0,0]},{\"province\":\"山东\",\"cities\":[],\"prices\":[18,7,7],\"otherPrices\":[0,0,0]},{\"province\":\"甘肃\",\"cities\":[],\"prices\":[18,9,9],\"otherPrices\":[0,0,0]},{\"province\":\"宁夏\",\"cities\":[],\"prices\":[18,9,9],\"otherPrices\":[0,0,0]},{\"province\":\"青海\",\"cities\":[],\"prices\":[18,9,9],\"otherPrices\":[0,0,0]},{\"province\":\"内蒙古\",\"cities\":[\"呼伦贝尔\",\"兴安盟\"],\"prices\":[20,10,10],\"otherPrices\":[18,9,9]},{\"province\":\"吉林\",\"cities\":[],\"prices\":[20,10,10],\"otherPrices\":[0,0,0]},{\"province\":\"黑龙江\",\"cities\":[],\"prices\":[20,10,10],\"otherPrices\":[0,0,0]},{\"province\":\"新疆\",\"cities\":[],\"prices\":[22,12,12],\"otherPrices\":[0,0,0]},{\"province\":\"西藏\",\"cities\":[],\"prices\":[0,0,0],\"otherPrices\":[0,0,0]}]";
    //龙岩圆通
    private static String ly_yt = "[{\"price\":[1.9,2.2,2.9,3.5,4.5,4.9,0.6],\"regions\":[\"福建\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,4.9,1.2],\"regions\":[\"广东\",\"安徽\",\"浙江\",\"江苏\",\"江西\",\"湖北\",\"湖南\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,4.9,1.4],\"regions\":[\"上海\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,4.9,1.9],\"regions\":[\"北京\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,4.9,1.6],\"regions\":[\"河南\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,5.9,1.6],\"regions\":[\"山东\",\"天津\",\"河北\",\"陕西\",\"广西\",\"山西\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,5.9,2.1],\"regions\":[\"海南\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,5.9,2.0],\"regions\":[\"四川\",\"贵州\",\"云南\",\"重庆\"]},{\"price\":[1.9,2.2,2.9,3.5,4.5,6.9,2.5],\"regions\":[\"黑龙江\",\"吉林\",\"辽宁\"]},{\"price\":[0,0,0,0,0,8.4,6.0],\"regions\":[\"甘肃\",\"宁夏\",\"青海\"]},{\"price\":[0,0,0,0,0,9.4,7.0],\"regions\":[\"内蒙古\"]},{\"price\":[0,0,0,0,0,15.4,12.0],\"regions\":[\"新疆\"]},{\"price\":[0,0,0,0,0,19.4,17.0],\"regions\":[\"西藏\"]}]";
    //龙岩申通
    private static String ly_st = "[{\"price\":[2.4,3.2,3.5,4.5,4.5,0.8],\"regions\":[\"福建\"]},{\"price\":[2.4,3.2,3.5,4.5,4.5,1.2],\"regions\":[\"广东\",\"安徽\",\"浙江\",\"江苏\",\"上海\",\"江西\",\"湖北\",\"湖南\"]},{\"price\":[2.4,3.2,3.5,4.5,5.0,2.0],\"regions\":[\"北京\"]},{\"price\":[2.4,3.2,3.5,4.5,5.0,2.5],\"regions\":[\"河南\",\"山东\",\"陕西\",\"四川\",\"广西\",\"山西\",\"贵州\",\"云南\",\"重庆\"]},{\"price\":[2.4,3.2,3.5,4.5,5.0,2.0],\"regions\":[\"天津\",\"河北\",\"海南\"]},{\"price\":[2.4,3.2,3.5,4.5,5.5,4.0],\"regions\":[\"黑龙江\",\"吉林\",\"辽宁\"]},{\"price\":[10.0,10.0,20.0,30.0,10.0,10.0],\"regions\":[\"甘肃\",\"宁夏\",\"青海\",\"内蒙古\"]},{\"price\":[15.0,16.0,30.0,45.0,18.0,16.0],\"regions\":[\"新疆\",\"西藏\"]}]";

    private static Boolean isInAddress(List<String> list, String address) {
        Boolean result = false;
        for (int i = 0; i < list.size(); i++) {
            if (address.contains(list.get(i))) {
                result = true;
                break;
            }
        }
        return result;
    }

    private static class ProvincePriceVo2 {
        private List<BigDecimal> prices = new ArrayList<>();
        private List<String> cities = new ArrayList<>();
        private List<BigDecimal> otherPrices = new ArrayList<>();
        private String province = "";

        public List<BigDecimal> getPrices() {
            return prices;
        }

        public List<String> getCities() {
            return cities;
        }

        public List<BigDecimal> getOtherPrices() {
            return otherPrices;
        }

        public String getProvince() {
            return province;
        }

        public void setPrices(List<BigDecimal> prices) {
            this.prices = prices;
        }

        public void setCities(List<String> cities) {
            this.cities = cities;
        }

        public void setOtherPrices(List<BigDecimal> otherPrices) {
            this.otherPrices = otherPrices;
        }

        public void setProvince(String province) {
            this.province = province;
        }


    }

    private static class ProvincePriceVo {
        private List<BigDecimal> price = new ArrayList<>();
        private List<String> regions = new ArrayList<>();
        private List<BigDecimal> otherPrices = new ArrayList<>();

        public List<BigDecimal> getPrice() {
            return price;
        }

        public List<String> getRegions() {
            return regions;
        }

        public List<BigDecimal> getOtherPrices() {
            return otherPrices;
        }

        public void setPrice(List<BigDecimal> price) {
            this.price = price;
        }

        public void setRegions(List<String> regions) {
            this.regions = regions;
        }

        public void setOtherPrices(List<BigDecimal> otherPrices) {
            this.otherPrices = otherPrices;
        }

    }

    private static class Province {
        private String province;
        private String city;
        private String district;
        private String detailAddress;

        public Province(String province, String city, String district, String detailAddress) {
            this.province = province;
            this.city = city;
            this.district = district;
            this.detailAddress = detailAddress;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public String getDetailAddress() {
            return detailAddress;
        }

        public void setDetailAddress(String detailAddress) {
            this.detailAddress = detailAddress;
        }
    }

    private static Province parseAddress(String address) {
        if (StringUtils.isEmpty(address)) {
            return null;
        }
        String province = "";
        String city = "";
        String district = "";
        String detailAddress = "";
        // 匹配省级（省、自治区、直辖市）
        String provinceRegex = "(?<province>[^省]+省|.+自治区|北京市|上海市|天津市|重庆市)";
        Matcher provinceMatcher = Pattern.compile(provinceRegex).matcher(address);
        if (provinceMatcher.find()) {
            province = provinceMatcher.group("province");
            String remaining = address.substring(province.length());

            // 匹配市级（市、自治州、地区、盟）
            String cityRegex = "(?<city>[^市]+市|.+自治州|.+地区|.+盟|市辖区)";
            Matcher cityMatcher = Pattern.compile(cityRegex).matcher(remaining);
            if (cityMatcher.find()) {
                city = cityMatcher.group("city");
                String districtPart = remaining.substring(city.length());

                // 匹配区级（区、县、市、旗）
                String districtRegex = "(?<district>[^区]+区|.+县|.+市|.+旗)";
                Matcher districtMatcher = Pattern.compile(districtRegex).matcher(districtPart);
                if (districtMatcher.find()) {
                    district = districtMatcher.group("district");
                    detailAddress = districtPart.substring(district.length());
                }
            }
        }
        return new Province(province, city, district, detailAddress);
    }

    public PricingListVo getCostPrice(Product product) {
        if (product.getProTypeValue() == null) {
            return new PricingListVo(100, "暂无设置价格。");
        }
        if (!"彩印通".equals(product.getPlantName())) {
            return new PricingListVo(100, "暂无设置价格。");
        }
        if ("彩印通".equals(product.getPlantName()) && "卡片".equals(product.getProTypeValue())) {
            return getCYTCardPrice(product);
        }
        if ("彩印通".equals(product.getPlantName()) && "不干胶".equals(product.getProTypeValue())) {
            return getCYTStickerPrice(product);
        }
        return null;
    }

    private PricingListVo getCYTStickerPrice(Product product) {
        List<ProductVo> resultList = new ArrayList<>();
        double width = product.getWidth();
        double length = product.getLength();
        int count = product.getCount();
        int number = product.getNumber();
        // 定义纸张尺寸（单位：毫米）
        double paperLength = 287; // 纸张长度
        double paperWidth = 406;  // 纸张宽度

        width = convertToMillimetersWithMargin(width, 2);
        length = convertToMillimetersWithMargin(length, 2);

        List<String> smallPaper = Arrays.asList("加厚铜版纸不干胶", "艾丽铜板不干胶", "特光不干胶", "铜板可移不干胶", "牛皮纸", "书写纸", "易碎纸", "大红洒金");
        if (smallPaper.contains(product.getKind())) {
            paperLength = 300; // 纸张长度
            paperWidth = 420;  // 纸张宽度
        }

        if (!isWithinPaperSize(width, length, paperLength, paperWidth)) {
            return new PricingListVo(100, "超出纸张尺寸限制"); // 返回空列表表示不适用
        }

        double basePrice = 3;

        if ("加厚铜版纸不干胶".equals(product.getKind())) {
            basePrice = 2;
        }
        if ("铜板可移不干胶".equals(product.getKind())) {
            basePrice = 2;
        }
        if ("牛皮纸".equals(product.getKind())) {
            basePrice = 1.8;
        }
        if ("书写纸".equals(product.getKind())) {
            basePrice = 1.8;
        }

        List<String> base_4 = Arrays.asList("黑胶铜版(遮盖/遮光)", "易碎纸", "洒金宣纸(白色)", "亮银", "亮金");
        if (base_4.contains(product.getKind())) {
            basePrice = 4;
        }
        List<String> base_3 = Arrays.asList("超白浮雕纹", "净白彩岩纹", "pp合成纸不干胶", "素面银镭射", "拉丝银", "拉丝金", "哑金");
        if (base_3.contains(product.getKind())) {
            basePrice = 3.5;
        }

        List<String> base_2 = Arrays.asList("艾丽铜板不干胶", "特光不干胶", "大红洒金", "白布纹纸");
        if (base_2.contains(product.getKind())) {
            basePrice = 2.5;
        }
        // 计算一张纸能容纳的最大金属标数量
        double maxItemsPerSheet = calculateMaxItemsPerSheet(length, width, paperLength, paperWidth);

        // 计算所需纸张数量
        int requiredSheets = calculateRequiredSheets(count, number, maxItemsPerSheet);


        BigDecimal price = new BigDecimal(basePrice).multiply(new BigDecimal(requiredSheets)).setScale(2, RoundingMode.HALF_UP);

        ProductVo pro = new ProductVo();
        pro.setPrice(price.doubleValue());
        pro.setCount(count);
        pro.setNumber(number);
        resultList.add(pro);
        return new PricingListVo(resultList);
    }

    private PricingListVo getCYTCardPrice(Product product) {
        List<ProductVo> resultList = new ArrayList<>();
        double width = product.getWidth();
        double length = product.getLength();
        int count = product.getCount();
        int number = product.getNumber();
        // 定义纸张尺寸（单位：毫米）
        double paperLength = 300; // 纸张长度
        double paperWidth = 420;  // 纸张宽度

        width = convertToMillimetersWithMargin(width, 2);
        length = convertToMillimetersWithMargin(length, 2);

        if (!isWithinPaperSize(width, length, paperLength, paperWidth)) {
            return new PricingListVo(100, "超出纸张尺寸限制"); // 返回空列表表示不适用
        }
        List<String> list = Arrays.asList("300克双铜纸", "300克单铜白卡", "300克冰白卡纸", "300克荷兰白卡", "300克水晶磨砂", "300克铂金磨砂", "300克米黄荷兰白", "300克雅典米白灵感纸", "300克米黄丝绵纸", "325克素面镭射卡白底", "330克超白草香纸", "350克雅柔纸", "350克冰白卡纸", "350克荷兰白卡", "350克布纹", "350克蛋壳纹", "350克安格纹", "350克刚古纹", "350克米黄刚古纹", "350克米黄冰白", "350克超白超感纸", "雅柔毯纹超白");
        int[] counts = new int[]{1, 5, 10, 20, 25, 50};
        double[] basePrices = new double[]{1.8, 9, 17, 34, 41.25, 82.5};
        if ("300克单铜白卡".equals(product.getKind())) {
            basePrices = new double[]{2, 10, 19, 38, 45, 90};
        }
        if ("300克冰白卡纸".equals(product.getKind())) {
            basePrices = new double[]{2.5, 12.5, 24, 46, 55, 100};
        }
        if ("300克荷兰白卡".equals(product.getKind())) {
            basePrices = new double[]{2.1, 10.5, 21, 40, 50, 90};
        }
        if ("300克水晶磨砂".equals(product.getKind())) {
            basePrices = new double[]{5, 40, 47, 100, 107.5, 200};
        }
        if ("300克铂金磨砂".equals(product.getKind())) {
            basePrices = new double[]{4.5, 22.5, 42, 80, 95, 175};
        }
        if ("300克米黄荷兰白".equals(product.getKind())) {
            basePrices = new double[]{2.5, 12.5, 24, 46, 55, 100};
        }
        if ("300克雅典米白灵感纸".equals(product.getKind())) {
            basePrices = new double[]{3, 15, 28, 52, 62.5, 115};
        }
        if ("300克米黄丝绵纸".equals(product.getKind())) {
            basePrices = new double[]{3.5, 17.5, 34, 66, 80, 150};
        }
        if ("325克素面镭射卡白底".equals(product.getKind())) {
            basePrices = new double[]{5, 25, 45, 86, 100, 190};
        }
        if ("330克超白草香纸".equals(product.getKind())) {
            basePrices = new double[]{5, 25, 48, 90, 107.5, 200};
        }
        if ("350克雅柔纸".equals(product.getKind())) {
            basePrices = new double[]{3, 15, 28, 54, 65, 125};
        }
        if ("350克米黄冰白".equals(product.getKind())) {
            basePrices = new double[]{3, 15, 28, 54, 65, 125};
        }

        if ("雅柔毯纹超白".equals(product.getKind())) {
            basePrices = new double[]{20, 38, 70, 82.5, 150};
        }
        List<String> list_100 = Arrays.asList("350克冰白卡纸", "350克荷兰白卡", "350克布纹", "350克蛋壳纹", "350克安格纹", "350克刚古纹", "350克米黄刚古纹", "350克超白超感纸");
        if (list_100.contains(product.getKind())) {
            basePrices = new double[]{2.5, 12.5, 24, 46, 55, 100};
        }
        // 计算一张纸能容纳的最大金属标数量
        double maxItemsPerSheet = calculateMaxItemsPerSheet(length, width, paperLength, paperWidth);

        // 计算所需纸张数量
        int requiredSheets = calculateRequiredSheets(count, number, maxItemsPerSheet);

        double basePrice = PriceUtils.TableCountDoublePrice(requiredSheets, basePrices, counts);

        BigDecimal price = new BigDecimal(basePrice / requiredSheets).multiply(new BigDecimal(requiredSheets)).setScale(2, RoundingMode.HALF_UP);

        ProductVo pro = new ProductVo();
        pro.setPrice(price.doubleValue());
        pro.setCount(count);
        pro.setNumber(number);
        resultList.add(pro);
        return new PricingListVo(resultList);
    }

    /**
     * 将给定的尺寸转换为毫米，并在每边增加3毫米的边距
     *
     * @param size 需要转换的原始尺寸
     * @return 转换后的尺寸，单位为毫米，包括边距
     */
    private double convertToMillimetersWithMargin(double size, int num) {
        return size * 10 + 2 * num; // 换成毫米，每边+2
    }

    /**
     * 检查给定的长度和宽度是否在纸张尺寸范围内
     *
     * @param length      需要检查的长度
     * @param width       需要检查的宽度
     * @param paperLength 纸张的长度
     * @param paperWidth  纸张的宽度
     * @return 如果给定尺寸在纸张尺寸范围内，则返回true；否则返回false
     */
    private boolean isWithinPaperSize(double length, double width, double paperLength, double paperWidth) {
        return (length <= paperLength && width <= paperWidth) || (length <= paperWidth && width <= paperLength);
    }

    /**
     * 计算每张纸最多可以容纳的物品数量
     *
     * @param length      物品的长度
     * @param width       物品的宽度
     * @param paperLength 纸张的长度
     * @param paperWidth  纸张的宽度
     * @return 每张纸最多可以容纳的物品数量
     */
    private double calculateMaxItemsPerSheet(double length, double width, double paperLength, double paperWidth) {
        return Math.max(
                Math.floor(paperLength / length) * Math.floor(paperWidth / width),
                Math.floor(paperLength / width) * Math.floor(paperWidth / length)
        );
    }

    /**
     * 计算完成指定数量的物品所需的纸张数量
     *
     * @param count            物品的总数量
     * @param number           每个物品需要的数量
     * @param maxItemsPerSheet 每张纸最多可以容纳的物品数量
     * @return 完成指定数量的物品所需的纸张数量
     */
    private int calculateRequiredSheets(int count, int number, double maxItemsPerSheet) {
        return (int) Math.ceil((count * number) / maxItemsPerSheet);
    }
}
