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


import com.fshark.overseas.advert.entity.mapper.MediaMapper;
import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.mapping.Media;
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 count(1) from game where platform_id = :platform_id and af_app_id = :game_id")
    boolean findGameByPrimary(@BindBean AppsFlyerEvent event, @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)")
    void saveMediaSource(@BindBean AppsFlyerEvent value);

    @SqlQuery("select count(1) from adgroup_media_mapping where af_media_id = :media_source and af_group_id = :campaign_id ")
    boolean existsMediaCampaign(@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)")
    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);
}
