package com.ytpm.test;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ytpm.agent.param.AppParam;
import com.ytpm.middle.view.MiddleUserInfo;
import com.ytpm.risk.model.YtRiskConfig;
import com.ytpm.risk.model.YtRiskTemplate;
import com.ytpm.risk.model.YtRiskTemplateConfig;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lih
 * @date 2025-09-09 16:32
 */
public class GenerateTest {


    @Test
    public void test() {
        System.out.println("  ");
        MiddleUserInfo userInfo = new MiddleUserInfo();
        userInfo.setUserId("yt_agent_1972558716489269248");
        userInfo.setNickName("易推-苏洵");
        generateTest("咕噜咕噜", "7587e10fda4b4e82b698cc7515b9b425", userInfo);

    }

    public void generateTest(String appName, String appId, MiddleUserInfo loginUser) {
        AppParam param = new AppParam();
        param.setAppId(appId);
        param.setAppName(appName);
        param.setUserId(loginUser.getUserId());
        addTemp344(param, loginUser);
        addTemp345(param, loginUser);
        addTemp766(param, loginUser);
        addTemp767(param, loginUser);
        addTemp768(param, loginUser);

//        addTemp742(param, loginUser);
//        addTemp746(param, loginUser);
    }

    private void addTemp746(AppParam param, MiddleUserInfo loginUser) {
        String initRevenue = "0.15";
        String appId = param.getAppId();
        String agentId = param.getUserId();

        List<YtRiskConfig> configs = new ArrayList<>();

        YtRiskConfig config = createRiskConfig(param.getUserId(), "rewardCount", "当日前2条获得奖励的激励视频", "2",2);
        configs.add(config);
            config = createRiskConfig(param.getUserId(), "income", "总收益为<"+initRevenue, initRevenue,4);
        configs.add(config);

        String templateId = IdUtil.getSnowflakeNextIdStr();
        YtRiskTemplate template = new YtRiskTemplate(templateId,"最低收益限制",
                "当日前2条获得奖励的激励视频,总收益为<"+initRevenue,appId+"-746",agentId,
                2,appId,new Date(),loginUser.getUserId(),null,
                null,1,1,1);
        createTemplate(template);

        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }

    private void addTemp742(AppParam param, MiddleUserInfo loginUser) {
        String initEcpm = "20";
        String appId = param.getAppId();
        String agentId = param.getUserId();
        List<YtRiskConfig> configs = new ArrayList<>();

        YtRiskConfig config = createRiskConfig(param.getUserId(), "firstAdCount", "当日前4条激励视频", "4",2);
        configs.add(config);

        config = createRiskConfig(param.getUserId(), "haveCount", "有2条", "2",2);
        configs.add(config);

        config = createRiskConfig(param.getUserId(), "ecpm", "ecpm值为<"+initEcpm, initEcpm,4);
        configs.add(config);

        String templateId = IdUtil.getSnowflakeNextIdStr();
        YtRiskTemplate template = new YtRiskTemplate(templateId,"激励视频ecpm值控制",
                "当日前4条激励视频,有2条,ecpm值为<"+initEcpm,appId+"-742",agentId,
                2,appId,new Date(),loginUser.getUserId(),null,
                null,1,1,1);
        createTemplate(template);
        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }



    private void addTemp344(AppParam param, MiddleUserInfo loginUser) {
        String initVisitorRevenueLimit = "0.04";
        // 风控规则 开屏广告、横幅、原生（信息流）总预估收益<'{}'
        String appId = param.getAppId();
        String agentId = param.getUserId();
        List<YtRiskConfig> configs = new ArrayList<>();
        YtRiskConfig riskConfig = createRiskConfig(param.getUserId(), "revenueLimit",
                StrUtil.format("开屏广告、横幅、原生（信息流）总预估收益<{},", initVisitorRevenueLimit), initVisitorRevenueLimit, 4);
        configs.add(riskConfig);
        String templateId = IdUtil.getSnowflakeNextIdStr();
        YtRiskTemplate template = new YtRiskTemplate(templateId, "游客用户总预估收益限制",
                StrUtil.format("开屏广告、横幅、原生（信息流）总预估收益<{},", initVisitorRevenueLimit),
                appId + "-344", agentId, 2, appId,
                new Date(), loginUser.getUserId(), null, null,
                1, 1, 1);
        createTemplate(template);
        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }

    private void addTemp345(AppParam param, MiddleUserInfo loginUser) {
        String initVisitorEcpmLimit = "20";
        // 风控规则 开屏广告、横幅、原生（信息流）总ecpm<'{}'
        String appId = param.getAppId();
        String agentId = param.getUserId();
        List<YtRiskConfig> configs = new ArrayList<>();
        YtRiskConfig riskConfig = createRiskConfig(param.getUserId(), "ecpmLimit",
                StrUtil.format("开屏广告、横幅、原生（信息流）总ecpm<{},", initVisitorEcpmLimit), initVisitorEcpmLimit, 4);
        configs.add(riskConfig);
        String templateId = IdUtil.getSnowflakeNextIdStr();
        YtRiskTemplate template = new YtRiskTemplate(templateId, "游客用户总ecpm限制",
                StrUtil.format("开屏广告、横幅、原生（信息流）总ecpm<{},", initVisitorEcpmLimit),
                appId + "-345", agentId, 2, appId,
                new Date(), loginUser.getUserId(), null, null,
                0, 1, 1);
        createTemplate(template);
        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }

    private void addTemp768(AppParam param, MiddleUserInfo loginUser) {
        // 每{}小时用户获得完成{}个获得奖励的激励视频
        String initHourCount = "3";
        String initHourTaskLimit = "10";
        String appId = param.getAppId();
        String agentId = param.getUserId();
        Date currentDate = new Date();
        List<YtRiskConfig> configs = new ArrayList<>();
        YtRiskConfig riskConfig;
        riskConfig = createRiskConfig(agentId, "hourCount",
                StrUtil.format("{}小时内，", initHourCount), initHourCount, 2);
        configs.add(riskConfig);
        riskConfig = createRiskConfig(agentId, "hourTaskLimit",
                StrUtil.format("获得奖励的激励视频数>{}", initHourTaskLimit), initHourTaskLimit, 3);
        configs.add(riskConfig);
        String templateId = IdUtil.getSnowflakeNextIdStr();
        YtRiskTemplate template = new YtRiskTemplate(templateId, "小时区间任务数完成限制",
                StrUtil.format("{}小时内，获得奖励的激励视频数>{}", initHourCount, initHourTaskLimit),
                appId + "-768", agentId, 2, appId,
                currentDate, loginUser.getUserId(), null, null,
                1, 1, 1);
        createTemplate(template);
        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }


    private void addTemp767(AppParam param, MiddleUserInfo loginUser) {
        // 风控规则 每{}月用户获得{}个获得奖励的激励视频
        String initMonthCount = "1";
        String initMonthTaskLimit = "80";

        String appId = param.getAppId();
        String agentId = param.getUserId();
        Date currentDate = new Date();
        List<YtRiskConfig> configs = new ArrayList<>();
        YtRiskConfig riskConfig;
        riskConfig = createRiskConfig(agentId, "monthCount",
                StrUtil.format("{}月内", initMonthCount), initMonthCount, 2);
        configs.add(riskConfig);
        riskConfig = createRiskConfig(agentId, "monthTaskLimit",
                StrUtil.format("获得奖励的激励视频数>{}", initMonthTaskLimit), initMonthTaskLimit, 3);
        configs.add(riskConfig);
        String templateId = IdUtil.getSnowflakeNextIdStr();
        YtRiskTemplate template = new YtRiskTemplate(templateId, "月区间任务完成数限制",
                StrUtil.format("{}月内，获得奖励的激励视频数>{}", initMonthCount, initMonthTaskLimit),
                appId + "-767", agentId, 2, appId,
                currentDate, loginUser.getUserId(), null, null,
                1, 1, 1);
        createTemplate(template);
        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }

    private void addTemp766(AppParam param, MiddleUserInfo loginUser) {
        // 风控规则：当用户完成广告类型 激励视频  {}个 数时，获取近期{} * {} 条收益（平均取整）平均值 ，低于预设值 {} 时，
        String initAdCompletedCount = "10";
        String initFirstCount = "4";
        String initAverageRevenue = "0.12";

        String appId = param.getAppId();
        String agentId = param.getUserId();
        Date currentDate = new Date();
        List<YtRiskConfig> configs = new ArrayList<>();
        YtRiskConfig riskConfig;
        riskConfig = createRiskConfig(agentId, "adCompletedCount",
                StrUtil.format("当日用户获得{}个获得奖励的激励视频，"), initAdCompletedCount, 2);
        configs.add(riskConfig);
        riskConfig = createRiskConfig(agentId, "firstCount",
                StrUtil.format("最后{}个获得奖励的激励视频", initFirstCount), initFirstCount, 2);
        configs.add(riskConfig);
        riskConfig = createRiskConfig(agentId, "averageRevenue",
                StrUtil.format("平均收益<{}", initAverageRevenue), initAverageRevenue, 4);
        configs.add(riskConfig);
        String templateId = IdUtil.getSnowflakeNextIdStr();
        String templateContent = StrUtil.format("当日用户获得{}个获得奖励的激励视频，最后{}个获得奖励的激励视频平均收益<{}",
                initAdCompletedCount, initFirstCount, initAverageRevenue);
        YtRiskTemplate template = new YtRiskTemplate(templateId,"成本收益限制", templateContent,
                appId + "-766", agentId, 2, appId,
                currentDate, loginUser.getUserId(),null, null,
                1,1,1);
        createTemplate(template);
        // 风控模板关联配置项
        bindTempConfig(loginUser, template, configs);
        //风控模版关联应用
        createRelation(template, loginUser, param.getAppName());
    }

    private void createTemplate(YtRiskTemplate template) {
        //        riskMapper.insertTemplate(template);
        System.out.println(StrUtil.format("insert into yt_risk_template (template_id,template_name, template_content, template_code, channel_id, effect_node, app_id,all_satisfy, create_time,create_user_id, enabled, can_modify ) " +
                        " values ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}' );\n", template.getTemplateId(), template.getTemplateName(), template.getTemplateContent(), template.getTemplateCode(),
                template.getChannelId(), template.getEffectNode(), template.getAppId(), template.getAllSatisfy(), DateUtil.format(template.getCreateTime(), "yyyy-MM-dd HH:mm:ss"), template.getCreateUserId(), template.getEnabled(), template.getCanModify()));
    }

    private void createRelation(YtRiskTemplate template, MiddleUserInfo loginUser, String appName) {
        //        riskMapper.relativeApp(appId, param.getAppName(), template.getTemplateId(), loginUser.getNickName(), loginUser.getUserId());
        System.out.println(StrUtil.format("insert into yt_cofig_app (template_id, app_id, app_name, operator, operator_name, operator_time) values ('{}','{}','{}','{}','{}',now());\n",
                template.getTemplateId(), template.getAppId(), appName, loginUser.getUserId(), loginUser.getNickName()));
    }

    /**
     * 创建输出框类型配置项
     */
    private YtRiskConfig createRiskConfig(String agentId, String fieldName, String filedDesc, String initVal, Integer multi) {
        String configId = IdUtil.getSnowflakeNextIdStr();
        YtRiskConfig config = new YtRiskConfig(configId, agentId, fieldName, filedDesc, 1, initVal, multi);
//        riskMapper.insertConfigItem(config);
        System.out.println(StrUtil.format("insert into yt_risk_config (config_id,field_name,field_desc,config_type,config_val,multy,channel_id ) values ('{}','{}','{}',{},'{}',{},'{}');\n",
                config.getConfigId(), config.getFieldName(), config.getFieldDesc(), config.getConfigType(), config.getConfigVal(), config.getMulty(), config.getChannelId()));
        return config;
    }

    /**
     * 绑定模板配置项
     */
    private void bindTempConfig(MiddleUserInfo loginUser, YtRiskTemplate template, List<YtRiskConfig> configs) {
        YtRiskTemplateConfig templateConfig;
        Date currentDate = new Date();
        for (YtRiskConfig config : configs) {
            templateConfig = new YtRiskTemplateConfig(template.getTemplateId(), config.getConfigId(),
                    loginUser.getUserId(), loginUser.getNickName(), currentDate);
//            riskMapper.insertTempConfig(templateConfig);
            System.out.println(StrUtil.format("insert into yt_risk_template_config (template_id,config_id,operator,operator_time,operator_name) values('{}','{}','{}','{}','{}');\n",
                    templateConfig.getTemplateId(), templateConfig.getConfigId(), templateConfig.getOperator(), DateUtil.format(templateConfig.getOperatorTime(), "yyyy-MM-dd HH:mm:ss")));
        }
    }
}
