package mobvista.dmp.datasource.id_mapping

import mobvista.dmp.datasource.id_mapping.Constant.{getDevId, parseUA, process}
import org.apache.commons.lang3.StringUtils
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.functions.lit
import org.apache.spark.sql.{Row, SparkSession}

/**
 * @package: mobvista.dmp.datasource.id_mapping
 * @author: wangjf
 * @date: 2021/11/30
 * @time: 7:51 下午
 * @email: jinfeng.wang@mobvista.com
 */
class ReYun extends EtlDeviceIdDaily {

  override def processData(date: String, i: Int, spark: SparkSession): RDD[(String, Row)] = {
    spark.udf.register("getDevId", getDevId _)
    spark.udf.register("parseUA", parseUA _)

    val year = date.substring(0, 4)
    val month = date.substring(4, 6)
    val day = date.substring(6, 8)

    val commonInput = "s3://mob-emr-test/reyun/dmp/onedata/dwd/dwd_device_ids_inc_daily"

    val tkioEventIosRDD = spark.read.orc(s"$commonInput/$year/$month/$day/tkio_event/ios")
    val tkioClickIosRDD = spark.read.orc(s"$commonInput/$year/$month/$day/tkio_click/ios")
    val abtestIosRDD = spark.read.orc(s"$commonInput/$year/$month/$day/abtest/ios")

    val tkioEventAdrRDD = spark.read.orc(s"$commonInput/$year/$month/$day/tkio_event/android")
    val tkioClickAdrRDD = spark.read.orc(s"$commonInput/$year/$month/$day/tkio_click/android")
    val abtestAdrRDD = spark.read.orc(s"$commonInput/$year/$month/$day/abtest/android")

    val iosDF = tkioEventIosRDD.union(tkioClickIosRDD).union(abtestIosRDD)
      .withColumn("cnt", lit(1L))
      .rdd.map(row => {
      ("ios", row)
    })
    val adrDF = tkioEventAdrRDD.union(tkioClickAdrRDD).union(abtestAdrRDD)
      .withColumn("cnt", lit(1L))
      .rdd.map(row => {
      ("android", row)
    })
    iosDF.union(adrDF)
  }
}

object ReYun {
  def main(args: Array[String]): Unit = {
    new ReYun().run(args)
  }
}