package com.fshark.overseas.advert.sink;

import com.fshark.overseas.advert.entity.dao.GameMappingDao;
import com.fshark.overseas.advert.entity.dao.OrderDao;
import com.fshark.overseas.advert.modle.OrderData;
import com.fshark.overseas.advert.modle.PlatformEnum;
import com.fshark.overseas.advert.modle.SinkMetricEvent;
import com.fshark.overseas.advert.modle.mapping.Game;
import com.fshark.overseas.advert.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

import java.util.Optional;

public class PurchaseSink extends RichSinkFunction<SinkMetricEvent> {

    private AppConfig appConfig;

    private JdbiContext jdbiContext;
    private OrderDao orderDao;
    private GameMappingDao gameMappingDao;

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

    @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());
        }
        orderDao = jdbiContext.getJdbi().onDemand(OrderDao.class);
        gameMappingDao = jdbiContext.getJdbi().onDemand(GameMappingDao.class);
    }

    @Override
    public void invoke(SinkMetricEvent value, Context context) throws Exception {
        super.invoke(value, context);

        if (Constants.TYPE_SINK_ORDER.equals(value.getEventType())) {
            OrderData eventData = value.getOrderData();
            //获取汇率
            int platform_id = 1;
            if (PlatformEnum.IOS.getName().equals(eventData.getPlatform())) {
                platform_id = 2;
            } else if (PlatformEnum.PC.getName().equals(eventData.getPlatform())){
                platform_id = 3;
            }
            if (!appConfig.getSinkType().equals(Constants.TYPE_SINK)) {
                Optional<Game> gameByPrimary = gameMappingDao.findGameByPrimary(eventData.getGame_id(), platform_id);
                gameByPrimary.ifPresent(game -> eventData.setRate(game.getRate()));
            }
            orderDao.insertOrder(eventData);
        } else if (Constants.TYPE_SINK_OUID_ORDER.equals(value.getEventType())){
            OrderData eventData = value.getOrderData();
            //获取汇率
            //获取汇率
            int platform_id = 1;
            if (PlatformEnum.IOS.getName().equals(eventData.getPlatform())) {
                platform_id = 2;
            } else if (PlatformEnum.PC.getName().equals(eventData.getPlatform())){
                platform_id = 3;
            }
            if (!appConfig.getSinkType().equals(Constants.TYPE_SINK)) {
                Optional<Game> gameByPrimary = gameMappingDao.findGameByPrimary(eventData.getGame_id(), platform_id);
                gameByPrimary.ifPresent(game -> eventData.setRate(game.getRate()));
            }
            orderDao.insertOuidOrder(eventData);
        }
    }


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

    }
}
