package com.fshark.overseas.advert.processor;

import com.fshark.overseas.advert.modle.metrics.ActivationMetrics;
import com.fshark.overseas.advert.util.AppConfig;
import com.fshark.overseas.advert.util.Constants;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.http.util.TextUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class OuidStoreMetricProcess extends ProcessAllWindowFunction<ActivationMetrics, Object, TimeWindow> {

    private AppConfig appConfig;
    private Connection conn;

    public OuidStoreMetricProcess(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        if (appConfig.getSinkType().equals(Constants.TYPE_SINK)) {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } else {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        }
        conn = DriverManager.getConnection(appConfig.getDbHost(), appConfig.getDbUser(), appConfig.getDbPassword());

    }

    @Override
    public void process(Context context, Iterable<ActivationMetrics> elements, Collector<Object> out) throws Exception {
        //详细指标写入
        insertMetricsDetail(elements);

    }

    private void insertMetricsDetail(Iterable<ActivationMetrics> elements) {
        try {
            PreparedStatement insertStmt = conn.prepareStatement("insert into appsflyer_metrics_ouid_detail" +
                    " (`uid`,`data_date`,`data_hour`,`time_zone`,`region`, `platform`, `game_id`, `media_source`, `campaign_id` ,`ad_set_id`,`ad_id`,`channel`,`data_type`,`metric_name`,`metric_value`)" +
                    " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            for (ActivationMetrics element : elements) {
                dealMetrics(element, insertStmt);
            }
            insertStmt.executeBatch(); //批量执行
            conn.commit();
            insertStmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void createBatch(String name, PreparedStatement insertStmt, ActivationMetrics element) throws SQLException {
        if (!TextUtils.isEmpty(name)) {
            insertStmt.setString(1, element.getUid());
            insertStmt.setLong(2, element.getData_date());
            insertStmt.setLong(3, element.getData_hour());
            insertStmt.setString(4, element.getTime_zone());
            insertStmt.setString(5, element.getRegion());
            insertStmt.setString(6, element.getPlatform());
            insertStmt.setString(7, element.getGame_id());
            insertStmt.setString(8, element.getMedia_source());
            insertStmt.setString(9, element.getCampaign_id());
            insertStmt.setString(10, element.getAd_set_id());
            insertStmt.setString(11, element.getAd_id());
            insertStmt.setString(12, element.getChannel());
            insertStmt.setLong(13, element.getData_type());
            insertStmt.setString(14, name);
            insertStmt.setLong(15, 1l);
            insertStmt.addBatch();
        }
    }

    private String dealMetrics(ActivationMetrics element, PreparedStatement insertStmt) throws SQLException {

        if (element.getData_activation() > 0) {
            createBatch("data_activation", insertStmt, element);
        }

        if (element.getData_register() > 0) {
            createBatch("data_register", insertStmt, element);
        }

        if (element.getData_device_register() > 0) {
            createBatch("data_device_register", insertStmt, element);
        }

        if (element.getData_create_role() > 0) {
            createBatch("data_create_role", insertStmt, element);
        }

        if (element.getData_day_act_register() > 0) {
            createBatch("data_day_act_register", insertStmt, element);
        }

        if (element.getData_day_register_create() > 0) {
            createBatch("data_day_register_create", insertStmt, element);
        }

        if (element.getData_active_day_dau() > 0) {
            createBatch("data_active_day_dau", insertStmt, element);
        }

        if (element.getData_sdk_activation() > 0) {
            createBatch("data_sdk_activition", insertStmt, element);
        }

        if (element.getData_sdk_register() > 0) {
            createBatch("data_sdk_register", insertStmt, element);
        }

        if (element.getDateRemind() > 0) {
            createBatch("retention_day" + element.getDateRemind(), insertStmt, element);
        }

        if (element.getData_ouid_register() > 0) {
            createBatch("ouid_role", insertStmt, element);
        }
        return null;
    }

    @Override
    public void close() throws Exception {
        super.close();
        if (conn != null) {
            conn.close();
        }
    }
}
