package com.tzld.piaoquan.recommend.server.service.score4recall.strategy;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * 动态高斯函数
 *
 * @author sunxy
 */
public class DynamicGaussianFunction {
    private static final double SIGMA = 24.0; // 标准差，可以根据需要调整这个值

    public static double calculateValue(LocalDateTime testTime, LocalDateTime startTime, LocalDateTime endTime,
                                        LocalDateTime peakTime) {
        long testTimestamp = testTime.toEpochSecond(ZoneOffset.UTC) * 1000;
        // 检查时间是否在指定范围内
        if (testTimestamp < startTime.toEpochSecond(ZoneOffset.UTC) * 1000 ||
                testTimestamp > endTime.toEpochSecond(ZoneOffset.UTC) * 1000) {
            return 0;
        }

        // 计算 t_0，即从开始时间到峰值时间的小时数
        double t_0 = (peakTime.toEpochSecond(ZoneOffset.UTC) - startTime.toEpochSecond(ZoneOffset.UTC)) / 3600.0;

        // 计算时间差（以小时为单位）
        double t = (testTimestamp / 1000.0 - startTime.toEpochSecond(ZoneOffset.UTC)) / 3600.0;

        // 计算高斯函数值
        return 100 * Math.exp(-Math.pow(t - t_0, 2) / (2 * Math.pow(SIGMA, 2)));
    }

    public static void main(String[] args) {
        // 示例：计算2024-02-03 09:00的值
        LocalDateTime startTime = LocalDateTime.of(2024, 1, 29, 0, 0);
        LocalDateTime endTime = LocalDateTime.of(2024, 2, 3, 23, 59);
        LocalDateTime peakTime = LocalDateTime.of(2024, 2, 3, 9, 0);
        LocalDateTime testTime = LocalDateTime.of(2024, 1, 31, 22, 0);

        double value = calculateValue(testTime, startTime, endTime, peakTime);
        System.out.println("Function value at " + testTime + ": " + value);
    }
}
