package com.moka.gdtauto.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.moka.gdtauto.common.AutoPlanRule;
import com.tencent.ads.model.v3.WriteTargetingSetting;

public class SampleTest {
    public static void main(String[] args) {
        String pwd = "123456" + "#" + "11203";
        System.out.println(pwd);
        String password = DigestUtils.md5DigestAsHex(pwd.getBytes());
        System.out.println(password);
    }

    @Test
    public void test2() {
        BigDecimal bigDecimal = new BigDecimal("100.00");
        System.out.println(bigDecimal);
        double value = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
        System.out.println(value);
    }

    @Test
    public void Test() {
        WriteTargetingSetting targetingSetting = TargetConfUtil.getTargetingSetting(1);
        System.out.println(JSON.toJSONString(targetingSetting, true));
    }

    @Test
    public void testRuleSort() {
        String str = """
            [{"type":1,"newAd":1,"minCost":100,"minIor":0.15,"scanMaxCost":10},
            {"type":1,"newAd":1,"minCost":30,"minIor":0.1,"scanMaxCost":10},
            {"type":2,"newAd":1,"minCost":0,"minIor":0.1,"scanMaxCost":10},
            {"type":2,"newAd":2,"minCost":0,"minIor":0,"scanMaxCost":100}]
        """;
        List<AutoPlanRule> rules = JSON.parseArray(str, AutoPlanRule.class);
        List<AutoPlanRule> newRules = new ArrayList<>();
        List<AutoPlanRule> oldRules = new ArrayList<>();
        rules.forEach(r -> {
            if (r.getNewAd() == 1) {
                newRules.add(r);
            } else {
                oldRules.add(r);
            }
        });
        //ROI在前 规则类型：1 roi 2 粉价
        newRules.sort((a, b) -> Integer.compare(a.getType(), b.getType()));
        oldRules.sort((a, b) -> Integer.compare(a.getType(), b.getType()));
        newRules.sort((a, b) -> Double.compare(b.getMinCost(), a.getMinCost()));
        oldRules.sort((a, b) -> Double.compare(b.getMinCost(), a.getMinCost()));
        newRules.sort((a, b) -> Double.compare(b.getScanMaxCost(), a.getScanMaxCost()));
        oldRules.sort((a, b) -> Double.compare(b.getScanMaxCost(), a.getScanMaxCost()));

        List<AutoPlanRule> allRules = new ArrayList<>();
        allRules.addAll(newRules);
        allRules.addAll(oldRules);

        System.out.println(JSON.toJSONString(allRules, true));
    }

    @Test
    public void testRuleSort2() {
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(1702382479, 0, java.time.ZoneOffset.UTC);
        System.out.println(dateTime);
    }
    
    @Test
    public void testCompare() {
        BigDecimal planRoi = new BigDecimal("0.15");
        BigDecimal ruleRoi = new BigDecimal("0.2");
        System.out.println(planRoi.compareTo(ruleRoi)); // > 1 表示 planRoi 大于 ruleRoi
    }

    @Test
    public void testAddDailyBudget() {
        BigDecimal dailyBudgetRateTop = new BigDecimal("0.9");
        Long dailyBudget = 10000L;
        Long dailyBudgetLimit = dailyBudgetRateTop.multiply(BigDecimal.valueOf(dailyBudget)).longValue();
        System.out.println(dailyBudgetLimit);
    }
}
