package com.fshark.overseas.advert.sink;

import com.fshark.overseas.advert.entity.dao.MetricsDao;
import com.fshark.overseas.advert.modle.metrics.RemainMetrics;
import com.fshark.overseas.advert.util.AppConfig;
import com.fshark.overseas.advert.util.Constants;
import com.fshark.overseas.advert.util.DatabaseUtils;
import com.fshark.overseas.advert.util.JdbiContext;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

public class RemainSink extends RichSinkFunction<RemainMetrics> {
    private AppConfig appConfig;
    private MetricsDao metricsDao;

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

    private JdbiContext jdbiContext;

    @Override
    public void invoke(RemainMetrics value, Context context) throws Exception {
        super.invoke(value, context);
        Long days = value.getDays();
        //长线留存
        if ((days >= 1 && days <= 30) || days == 35 || days == 40 || days == 45 || days == 50 || days == 55
                || days == 60 || days == 65 || days == 70 || days == 75 || days == 80 || days == 85 || days == 90 || days == 120 || days == 150 || days == 180) {
            String column = "retention_day" + days;

            jdbiContext.getJdbi().withHandle(handle -> {
                String sql = String.format("insert into appsflyer_retention" +
                                " (`data_date`,`region`, `platform`, `game_id`, `media_source`, `campaign_id` ,`ad_set_id`,`ad_id`,`currency`,`data_type`,%s)" +
                                " values ('%s','%s','%s','%s','%s','%s','%s','%s','KRW',%s,%s) ",
                        column, value.getData_date(), value.getRegion(), value.getPlatform(), value.getGame_id(), value.getMedia_source(), value.getCampaign_id()
                        , value.getAd_set_id(), value.getAd_id(), value.getData_type(), value.getUser_num()
                );
                handle.createUpdate(sql).execute();
                return null;
            });
        }

        //短线留存
        days = days + 1;
        if (days == 2 || days == 3 || days == 7 || days == 15 || days == 30) {
            String column = "data_active_" + days + "day";
            jdbiContext.getJdbi().withHandle(handle -> {
                String sql = String.format("insert into appsflyer_metrics" +
                                " (`data_date`,`data_hour`,`time_zone`,`region`, `platform`, `game_id`, `media_source`, `campaign_id` ,`ad_set_id`,`ad_id`,`channel`,`data_type`,`%s`)" +
                                " values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%s,'%s') ",
                        column, value.getData_date(), value.getData_hour(), value.getTime_zone(), value.getRegion(), value.getPlatform(), value.getGame_id(), value.getMedia_source(), value.getCampaign_id()
                        , value.getAd_set_id(), value.getAd_id(), value.getChannel(), value.getData_type(), value.getUser_num()
                );
                handle.createUpdate(sql).execute();
                return null;
            });
        }
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        if (appConfig.getSinkType().equals(Constants.TYPE_SINK)) {
            jdbiContext = DatabaseUtils.getGenJdbiClient(appConfig.getDbHost(), appConfig.getDbUser(), appConfig.getDbPassword());
        } else {
            jdbiContext = DatabaseUtils.getCHGenJdbiClient(appConfig.getDbHost(), appConfig.getDbUser(), appConfig.getDbPassword());
        }
        metricsDao = jdbiContext.getJdbi().onDemand(MetricsDao.class);

    }

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

}
