package com.fshark.overseas.advert.entity.dao;


import com.fshark.overseas.advert.entity.mapper.AdMappingMapper;
import com.fshark.overseas.advert.entity.mapper.GameMapper;
import com.fshark.overseas.advert.entity.mapper.MediaCampaignMapper;
import com.fshark.overseas.advert.entity.mapper.MediaMapper;
import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.mapping.AdMapping;
import com.fshark.overseas.advert.modle.mapping.Game;
import com.fshark.overseas.advert.modle.mapping.Media;
import com.fshark.overseas.advert.modle.mapping.MediaCampaign;
import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.util.Optional;

/**
 * 游戏广告相关的映射
 */
public interface GameMappingDao {
    /**
     * 拉取游戏
     */
    @SqlQuery("select * from game where af_app_id = :game_id and platform_id = :platform_id ")
    @RegisterRowMapper(GameMapper.class)
    Optional<Game> findGameByPrimary(@Bind("game_id") String game_id,@Bind("platform_id")int platform_id);


    @SqlUpdate("insert into game (`platform_id`, `af_app_id`, `af_app_name`, `app_id`,`select_time_zone`) values (:platform_id,:game_id,:app_name,:app_id,:select_time_zone)")
    void saveGame(@BindBean AppsFlyerEvent value, @Bind("platform_id") int platform_id);

    @SqlQuery("select * from media where af_media_id = :media_source")
    @RegisterRowMapper(MediaMapper.class)
    Optional<Media> findMediaByPrimary(@BindBean AppsFlyerEvent value);

    @SqlUpdate("insert into media (`af_media_id`) values (:media_source)")
    @RegisterRowMapper(MediaMapper.class)
    void saveMediaSource(@BindBean AppsFlyerEvent value);

    @SqlQuery("select * from adgroup_media_mapping where af_media_id = :media_source and af_group_id = :campaign_id ")
    @RegisterRowMapper(MediaCampaignMapper.class)
    Optional<MediaCampaign> findMediaCampaign(@BindBean AppsFlyerEvent appsFlyerEvent);

    @SqlUpdate("insert into adgroup_media_mapping (`media_id`, `af_media_id`, `af_group_id`, `af_group_name`) values (:id, :media_source, :campaign_id, :campaign) on duplicate key update af_group_name = values(af_group_name)")
    void saveMediaCampaign(@BindBean AppsFlyerEvent appsFlyerEvent, @BindBean Media media);


    @SqlUpdate("insert into ad_mapping (`media_id`, `af_media_id`, `af_ad_group_id`, `af_ad_group_name`, `af_ad_plan_id`, `af_ad_plan_name`, `af_ad_id`, `af_ad_name`) " +
            " values (:id, :media_source , :campaign_id, :campaign, :ad_set_id, :ad_set, :ad_id, :ad) on duplicate key update af_ad_group_name = values(af_ad_group_name), af_ad_plan_name = values(af_ad_plan_name), af_ad_name = values(af_ad_name)")
    void saveAdMapping(@BindBean AppsFlyerEvent appsFlyerEvent, @BindBean Media media);


    @SqlQuery("select * from ad_mapping where af_media_id = :media_source and af_ad_group_id = :campaign_id and af_ad_plan_id = :ad_set_id and af_ad_id = :ad_id")
    @RegisterRowMapper(AdMappingMapper.class)
    Optional<AdMapping> findAdMapping(@BindBean AppsFlyerEvent value);
}
