package com.tzy.common.utils.group;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GroupHotnessCalculator {


    public static void main(String[] args) {
        // 总库存100,
        // 销售数量60
        // 累计销售总金额 250000元
        // 距离开始销售的天数 4
        // 近1小时销售总金额 120000元
        // 商家加成    1.2
        HotParameters params = new HotParameters().setTotalStock(100).setSoldQuantity(60).setTotalSalesAmount(250000.0).setRecentSalesAmount(120000.0)
                .setDaysSinceLaunch(4).setSellerBonusMultiplier(1.2).setBaseSortValue(10);
        int hotnessScore = calculateHotnessScore(params);
        System.out.println("Hotness Score: " + hotnessScore);
    }

    /**
     * 计算商品热度评分
     *
     * @param params 商品参数
     * @return 商品热度评分
     */
    public static int calculateHotnessScore(HotParameters params) {
        double soldPercentageScore = calculateSoldPercentageScore(params.getSoldQuantity(), params.getTotalStock(),params.getTotalSalesAmount());
        double totalSalesScore = calculateTotalSalesScore(params.getTotalSalesAmount(), params.getDaysSinceLaunch());
        double recentSalesScore = calculateRecentSalesScore(params.getRecentSalesAmount());
        double newProductBonus = calculateNewProductBonus(params.getDaysSinceLaunch(),params.getTotalPrize());
        log.info("进度分:{},总销售额分:{},近期销售额分:{},新品加成分:{}", soldPercentageScore, totalSalesScore, recentSalesScore, newProductBonus);
        double baseScore = params.getWeightSoldPercentage() * soldPercentageScore +
                params.getWeightTotalSales() * totalSalesScore +
                params.getWeightRecentSales() * recentSalesScore +
                params.getWeightNewProduct() * newProductBonus +
                params.getBaseSortValue();
        return (int) (baseScore * params.getSellerBonusMultiplier());
    }

    /**
     * 计算售出百分比的分数
     *
     * @param soldQuantity 已售数量
     * @param totalStock   总库存
     * @return 售出百分比分数
     */
    private static double calculateSoldPercentageScore(int soldQuantity, int totalStock, double totalPrize) {
        if (totalStock == 0 || soldQuantity == 0) {
            return 0;
        }
        double soldPercentage = (double) soldQuantity / totalStock;
        int rate = 10;
        if (totalPrize > 10000) {
            rate = 200;
        } else if (totalPrize > 5000) {
            rate = 100;
        } else if (totalPrize > 1000) {
            rate = 50;
        }
        // 直接使用百分比作为分值
        return soldPercentage * rate;
    }

    /**
     * 计算累计销售总金额的分数
     *
     * @param totalSalesAmount 累计销售总金额
     * @param daysSinceLaunch  距离开始销售的天数
     * @return 累计销售总金额的分数
     */
    private static double calculateTotalSalesScore(double totalSalesAmount, int daysSinceLaunch) {
        double timeWeight;
        if (daysSinceLaunch <= 3) {
            timeWeight = 1.0;
        } else if (daysSinceLaunch <= 5) {
            timeWeight = 0.8;
        } else if (daysSinceLaunch <= 7) {
            timeWeight = 0.6;
        } else {
            timeWeight = 0.4;
        }
        // 每100元1分，不满100元也算1分，总金额上限1500分
        double totalSalesPoints = Math.min((totalSalesAmount / 200) + 1, 1000);
        return totalSalesPoints * timeWeight;
    }

    /**
     * 计算近1小时销售总金额的分数
     *
     * @param recentSalesAmount 近1小时销售总金额
     * @return 近1小时销售总金额的分数
     */
    private static double calculateRecentSalesScore(double recentSalesAmount) {
        // 每100元1分，不满100元也算1分，近2小时销售额上限500分
        return Math.min((recentSalesAmount / 100) + 1, 1000);
    }

    /**
     * 计算新品基础加成值
     *
     * @param daysSinceLaunch  距离开始销售的天数
     * @param totalPrize 总价
     * @return 新品基础加成值
     */
    private static double calculateNewProductBonus(int daysSinceLaunch, double totalPrize) {
        return daysSinceLaunch == 0 && totalPrize > 500000 ? 60 : 0;
    }
}
