package com.fshark.overseas.advert.sink;

import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.util.AppConfig;
import com.fshark.overseas.advert.util.Constants;
import com.fshark.overseas.advert.util.DateUtils;
import lombok.val;
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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class EventInfoSinkProcess extends ProcessAllWindowFunction<AppsFlyerEvent, Object, TimeWindow> {

    private AppConfig appConfig;

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

    // 定义连接、预编译语句
    private Connection conn;

    @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 close() throws Exception {
        super.close();
        if (conn != null) {
            conn.close();
        }
    }

    @Override
    public void process(Context context, Iterable<AppsFlyerEvent> elements, Collector<Object> out) throws Exception {
        try {
            PreparedStatement insertStmt = conn.prepareStatement(" insert into appsflyer_event_info (`region`, `uid`, `did`, `platform`, `game_id`, `media_source`, `campaign_id`,`campaign_name`, `ad_set_id`,`ad_set_name`, `ad_id`,`ad_name`, `event_name`, `event_time`, `event_value`,`channel`,`time_zone`) values" +
                    "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

            for (AppsFlyerEvent element : elements) {
                insertStmt.setString(1, element.getRegion());
                insertStmt.setString(2, element.getUid());
                insertStmt.setString(3, element.getDid());
                insertStmt.setString(4, element.getPlatform());
                insertStmt.setString(5, element.getGame_id());
                insertStmt.setString(6, element.getMedia_source());
                insertStmt.setString(7, element.getCampaign_id());
                insertStmt.setString(8, element.getCampaign());
                insertStmt.setString(9, element.getAd_set_id());
                insertStmt.setString(10, element.getAd_set());
                insertStmt.setString(11, element.getAd_id());
                insertStmt.setString(12, element.getAd());
                insertStmt.setString(13, element.getEvent());
                insertStmt.setString(14, DateUtils.formatAsYMDHMS(element.getEvent_time()));
                insertStmt.setString(15, element.getEvent_value());
                insertStmt.setString(16, element.getChannel());
                insertStmt.setString(17, element.getTime_zone());
                insertStmt.addBatch();
            }
            insertStmt.executeBatch(); //批量执行
            conn.commit();
            insertStmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
