package org.ta.pddserver.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class PddNationalPriceTool {

    //首重重量
    private static int initialWeight = 1000;
    //上限重量
    private static int maxWeight = 200000;

    // 首重价格
    private static int initalPrice = 450;


    //超重计费标准
    private static int excessWeight = 1000;

    //超重价格
    private static int excessPrice = 100;

    //标椎三边长度
    private static int standard = 100;

    //标准三边之和
    private static int maxStandard = 160;


    /*
     * 根据类型、重量（g） 计算运费
     * */
    public static PddJYPriceTool.FeeResult FreightCostCalculation(int length, int width, int height, int weight, String enterTime) {
        PddJYPriceTool.FeeResult result = new PddJYPriceTool.FeeResult();
        int price = initalPrice;
        if (weight > initialWeight) {
            int pw = weight - initialWeight;
            int pc = pw / excessWeight;
            if (pw % excessWeight != 0) {
                pc += 1;
            }
            price = price + pc * excessPrice;
            log.info("续重数量：{}", pc);
        }
        int rent = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse(enterTime);
            Date date2 = new Date();
            // 将Date转换为LocalDate
            LocalDate localDate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate localDate2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            // 计算两个日期之间的天数差异
            long daysBetween = Math.abs(java.time.temporal.ChronoUnit.DAYS.between(localDate2, localDate1));
            if (daysBetween > 180) {
                rent = (int)daysBetween - 180;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        result.setRentFee(rent * 100);
        result.setFreightFee(price);
        result.setWeight(weight);
        return result;
    }
}
