package mobvista.dmp.datasource.dm

import mobvista.dmp.common.CommonSparkJob
import mobvista.dmp.format.TextMultipleOutputFormat
import org.apache.commons.cli.Options
import org.apache.commons.lang.StringUtils
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.io.Text
import org.apache.spark.sql.{Row, SparkSession}

import java.net.URI
import scala.collection.mutable.ArrayBuffer

/**
 * @author jiangfan
 * @date 2021/8/25 11:47
 */

class RtdmpTmpId1142110895 extends CommonSparkJob with Serializable {
  override protected def buildOptions(): Options = {
    val options = new Options
    options.addOption("coalesce", true, "[must] coalesce")
    options.addOption("output1", true, "[must] output1")
    options.addOption("output2", true, "[must] output2")
    options.addOption("output3", true, "[must] output3")
    options.addOption("output4", true, "[must] output4")
    options.addOption("output5", true, "[must] output5")
    options.addOption("output6", true, "[must] output6")
    options.addOption("output7", true, "[must] output7")
    options.addOption("output8", true, "[must] output8")
    options.addOption("output9", true, "[must] output9")
    options.addOption("output10", true, "[must] output10")
    options.addOption("output11", true, "[must] output11")
    options.addOption("output12", true, "[must] output12")
    options.addOption("output13", true, "[must] output13")
    options.addOption("output14", true, "[must] output14")
    options.addOption("output15", true, "[must] output15")
    options.addOption("dt_today", true, "[must] dt_today")
    options.addOption("dt_day", true, "[must] dt_day")
    options
  }

  override protected def run(args: Array[String]): Int = {
    val commandLine = commParser.parse(options, args)
    if (!checkMustOption(commandLine)) {
      printUsage(options)
      return -1
    } else printOptions(commandLine)

    val coalesce = commandLine.getOptionValue("coalesce")
    val output1 = commandLine.getOptionValue("output1")
    val output2 = commandLine.getOptionValue("output2")
    val output3 = commandLine.getOptionValue("output3")
    val output4 = commandLine.getOptionValue("output4")
    val output5 = commandLine.getOptionValue("output5")
    val output6 = commandLine.getOptionValue("output6")
    val output7 = commandLine.getOptionValue("output7")
    val output8 = commandLine.getOptionValue("output8")
    val output9 = commandLine.getOptionValue("output9")
    val output10 = commandLine.getOptionValue("output10")
    val output11 = commandLine.getOptionValue("output11")
    val output12 = commandLine.getOptionValue("output12")
    val output13 = commandLine.getOptionValue("output13")
    val output14 = commandLine.getOptionValue("output14")
    val output15 = commandLine.getOptionValue("output15")
    val dt_today = commandLine.getOptionValue("dt_today")
    val dt_day = commandLine.getOptionValue("dt_day")

    val spark = SparkSession.builder()
      .appName("RtdmpTmpId1142110895")
      .config("spark.rdd.compress", "true")
      .config("spark.io.compression.codec", "snappy")
      .config("spark.sql.orc.filterPushdown", "true")
      .config("spark.sql.warehouse.dir", "s3://mob-emr-test/spark-warehouse")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .enableHiveSupport()
      .getOrCreate()

    val sc = spark.sparkContext
    import spark.implicits._

    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output1), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output2), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output3), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output4), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output5), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output6), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output7), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output8), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output9), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output10), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output11), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output12), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output13), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output14), true)
    FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output15), true)

    try {
      val sql1=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('1142110895','id1142110895') and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql1).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output1}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql2=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('3876827262021090301','id3876827262021090301')   and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql2).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output2}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql3=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('3332062892021090301','id3332062892021090301')  and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql3).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output3}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql4=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('13403763232021090301','id13403763232021090301') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql4).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output4}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql5=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('10442830592021090301','id10442830592021090301') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql5).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output5}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql6=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_bes') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql6).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output6}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql7=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.ss.android.ugc.aweme_iqiyi') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql7).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output7}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql8=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('11421108952021090302','id11421108952021090302') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql8).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output8}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql9=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_oppoziyou') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql9).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output9}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql10=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_oppoziyou_notinstall') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql10).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output10}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql11=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_oppoziyou_hist_notinstall') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql11).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output11}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql12=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_oppolianmeng') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql12).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output12}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql13=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_oppolianmeng_hist1year_notinstall') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql13).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output13}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql14=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where dt='${dt_today}'  and package_name in ('com.xunmeng.pinduoduo_oppolianmeng_histhalfyear_notinstall') and business='dsp_req' and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql14).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output14}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

      val sql15=
        s"""
           |select device_id, device_type from dwh.dm_install_list_v2  where ( (dt='${dt_today}'  and business in ('btop'))  or  (dt='${dt_day}'  and business in ('TO')) )   and package_name in ('com.xunmeng.pinduoduo') and device_type not in ('androidid','android_id','ruid','upid')
        """.stripMargin

      spark.sql(sql15).rdd.flatMap(buildRes(_)).coalesce(coalesce.toInt)
        .saveAsNewAPIHadoopFile(s"${output15}", classOf[Text], classOf[Text], classOf[TextMultipleOutputFormat], spark.sparkContext.hadoopConfiguration)

    } finally {
      spark.stop()
    }
    0
  }

  def buildRes( row: Row): Array[Tuple2[Text, Text]] = {
    val buffer = new ArrayBuffer[Tuple2[Text, Text]]()
    val device_id = row.getAs[String]("device_id")
    val device_type = row.getAs[String]("device_type")
    if (StringUtils.isNotBlank(device_type)) {
      buffer += Tuple2(new Text(s"${device_type}, "), new Text(device_id))
    }
    buffer.toArray
  }
}

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