package com.fshark.overseas.advert.modle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fshark.overseas.advert.util.DateUtils;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Locale;

/**
 * @author yangjun
 * @date 19/3/6 13:22
 * Description:
 */
@Data
public class OrderData implements Serializable {
    /**
     * 事件 af_purchase
     */
    private String event;

    /**
     * 日志时间(ms)
     */
    private LocalDateTime event_time;

    /**
     * json event_value
     */
    private String event_value;

    /**
     * 日志时间(ms)
     */
    private String time_zone;

    /**
     * 用户唯一标识 appsflyer_device_id
     */
    private String uid;

    /**
     * 设备唯一标识
     * ios --> idfa
     * android --> advertising
     */
    private String did;

    /**
     * 平台
     * android | ios
     */
    private String platform;

    /**
     * 媒体平台
     * googleadwords_int | Facebook Ad
     */
    private String media_source;

    /**
     * 游戏ID bundle_id
     */
    private String game_id;

    /**
     * 游戏对应名称
     */
    private String app_name;

    /**
     * 广告计划ID
     */
    private String campaign_id;

    /**
     * 广告计划
     */
    private String campaign;

    /**
     * 广告组(计划)ID
     */
    private String ad_set_id;

    /**
     * 广告组(计划)
     */
    private String ad_set;

    /**
     * 广告ID
     */
    private String ad_id;

    /**
     * 广告
     */
    private String ad;
    /**
     * 地区
     */
    private String region;

    /**
     * 渠道
     */
    private String channel;


    /**
     * 订单编号,根据数据生成，md5(uid|gameId|time|money)
     */
    private String orderno;

    /**
     * 货币单位 currency
     */
    private String currency;

    /**
     * 收益 revenue_in_selected_currency
     */
    private Double revenue;

    private String pay_time;

    /**
     * 注册时间
     */
    private String reg_time;

    /**
     * 激活时间
     */
    private String activation_time;

    /**
     * 归因时间
     */
    private String retargeting_time;


    private String reg_server_id;

    private String reg_server_time;

    private String server_reg_date;

    private String server_reg_hour;

    private int data_type;


    public OrderData(AppsFlyerEvent appsFlyerEvent) {
        this.uid = appsFlyerEvent.getUid();
        this.game_id = appsFlyerEvent.getGame_id();
        this.event = appsFlyerEvent.getEvent();
        this.event_time = appsFlyerEvent.getEvent_time();
        this.event_value = appsFlyerEvent.getEvent_value();
        this.time_zone = appsFlyerEvent.getTime_zone();
        this.did = appsFlyerEvent.getDid();
        this.platform = appsFlyerEvent.getPlatform();
        this.media_source = appsFlyerEvent.getMedia_source();
        this.app_name = appsFlyerEvent.getApp_name();
        this.campaign = appsFlyerEvent.getCampaign();
        this.campaign_id = appsFlyerEvent.getCampaign_id();
        this.ad_set = appsFlyerEvent.getAd_set();
        this.ad_set_id = appsFlyerEvent.getAd_set_id();
        this.ad = appsFlyerEvent.getAd();
        this.ad_id = appsFlyerEvent.getAd_id();
        this.channel = appsFlyerEvent.getChannel();
        this.pay_time = DateUtils.formatAsYMDHMS(appsFlyerEvent.getEvent_time());
        JSONObject json = JSON.parseObject(event_value);
        this.currency = json.getString("af_currency");
        this.revenue = Double.valueOf(json.getString("af_revenue"));
//        this.orderno = getOrderUniqueId();
        this.orderno = json.getString("orderid");
        this.region = appsFlyerEvent.getRegion();
        this.setData_type(1);
        if (appsFlyerEvent.getIs_retargeting()) { //再营销数据
            this.setData_type(2);
        }
        if (appsFlyerEvent.getCampaign().toLowerCase(Locale.ROOT).startsWith("acp")) { //预注册数据
            this.setData_type(3);
        }
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }
}
