package com.aliyun.odps.spark.examples.makedata

import com.alibaba.fastjson.{JSON, JSONObject}
import com.aliyun.odps.TableSchema
import com.aliyun.odps.data.Record
import com.aliyun.odps.spark.examples.myUtils.{MyDateUtils, MyHdfsUtils, ParamUtils, env}
import examples.extractor.RankExtractorFeature_20240530
import org.apache.hadoop.io.compress.GzipCodec
import org.apache.spark.sql.SparkSession
import org.xm.Similarity

import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
import scala.io.Source
import examples.extractor.ExtractorUtils
/*
   20240608 提取特征
 */

object makedata_17_bucketDataPrint_20240617 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName(this.getClass.getName)
      .getOrCreate()
    val sc = spark.sparkContext

    // 1 读取参数
    val param = ParamUtils.parseArgs(args)
    val tablePart = param.getOrElse("tablePart", "64").toInt
    val beginStr = param.getOrElse("beginStr", "2024061500")
    val endStr = param.getOrElse("endStr", "2024061523")
    val savePath = param.getOrElse("savePath", "/dw/recommend/model/17_for_check/")
    val project = param.getOrElse("project", "loghubods")
    val table = param.getOrElse("table", "alg_recsys_sample_all_new")
    val repartition = param.getOrElse("repartition", "32").toInt
    val readDate = param.getOrElse("readDate", "20240615")


    val loader = getClass.getClassLoader
    val resourceUrl = loader.getResource("20240608_feature_name.txt")
    val content =
      if (resourceUrl != null) {
        val content = Source.fromURL(resourceUrl).getLines().mkString("\n")
        Source.fromURL(resourceUrl).close()
        content
      } else {
        ""
      }
    println(content)
    val contentList = content.split("\n")
      .map(r => r.replace(" ", "").replaceAll("\n", ""))
      .filter(r => r.nonEmpty).toList
    val contentList_br = sc.broadcast(contentList)

    val resourceUrlBucket = loader.getResource("20240609_bucket_274.txt")
    val buckets =
      if (resourceUrlBucket != null) {
        val buckets = Source.fromURL(resourceUrlBucket).getLines().mkString("\n")
        Source.fromURL(resourceUrlBucket).close()
        buckets
      } else {
        ""
      }
    println(buckets)
    val bucketsMap = buckets.split("\n")
      .map(r => r.replace(" ", "").replaceAll("\n", ""))
      .filter(r => r.nonEmpty)
      .map(r => {
        val rList = r.split("\t")
        (rList(0), (rList(1).toDouble, rList(2).split(",").map(_.toDouble)))
      }).toMap
    val bucketsMap_br = sc.broadcast(bucketsMap)

    // 2 读取odps+表信息
    val odpsOps = env.getODPS(sc)

    // 3 循环执行数据生产
    val timeRange = MyDateUtils.getDateHourRange(beginStr, endStr)
    for (dt_hh <- timeRange) {
      val dt = dt_hh.substring(0, 8)
      val hh = dt_hh.substring(8, 10)
      val partition = s"dt=$dt,hh=$hh"
      println("开始执行partiton:" + partition)
      val odpsData = odpsOps.readTable(project = project,
        table = table,
        partition = partition,
        transfer = func,
        numPartition = tablePart)
        .map(record => {
          val featureMap = new JSONObject()

          // a 视频特征
          val b1: JSONObject = if (record.isNull("b1_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b1_feature"))
          val b2: JSONObject = if (record.isNull("b2_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b2_feature"))
          val b3: JSONObject = if (record.isNull("b3_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b3_feature"))
          val b6: JSONObject = if (record.isNull("b6_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b6_feature"))
          val b7: JSONObject = if (record.isNull("b7_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b7_feature"))

          val b8: JSONObject = if (record.isNull("b8_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b8_feature"))
          val b9: JSONObject = if (record.isNull("b9_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b9_feature"))
          val b10: JSONObject = if (record.isNull("b10_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b10_feature"))
          val b11: JSONObject = if (record.isNull("b11_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b11_feature"))
          val b12: JSONObject = if (record.isNull("b12_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b12_feature"))
          val b13: JSONObject = if (record.isNull("b13_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b13_feature"))
          val b17: JSONObject = if (record.isNull("b17_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b17_feature"))
          val b18: JSONObject = if (record.isNull("b18_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b18_feature"))
          val b19: JSONObject = if (record.isNull("b19_feature")) new JSONObject() else
            JSON.parseObject(record.getString("b19_feature"))


          val origin_data = List(
            (b1, b2, b3, "b123"), (b1, b6, b7, "b167"),
            (b8, b9, b10, "b8910"), (b11, b12, b13, "b111213"),
            (b17, b18, b19, "b171819")
          )
          for ((b_1, b_2, b_3, prefix1) <- origin_data) {
            for (prefix2 <- List(
              "1h", "2h", "3h", "4h", "12h", "1d", "3d", "7d"
            )) {
              val exp = if (b_1.isEmpty) 0D else b_1.getIntValue("exp_pv_" + prefix2).toDouble
              val share = if (b_2.isEmpty) 0D else b_2.getIntValue("share_pv_" + prefix2).toDouble
              val returns = if (b_3.isEmpty) 0D else b_3.getIntValue("return_uv_" + prefix2).toDouble
              val f1 = RankExtractorFeature_20240530.calDiv(share, exp)
              val f2 = RankExtractorFeature_20240530.calLog(share)
              val f3 = RankExtractorFeature_20240530.calDiv(returns, exp)
              val f4 = RankExtractorFeature_20240530.calLog(returns)
              val f5 = f3 * f4
              featureMap.put(prefix1 + "_" + prefix2 + "_" + "STR", f1)
              featureMap.put(prefix1 + "_" + prefix2 + "_" + "log(share)", f2)
              featureMap.put(prefix1 + "_" + prefix2 + "_" + "ROV", f3)
              featureMap.put(prefix1 + "_" + prefix2 + "_" + "log(return)", f4)
              featureMap.put(prefix1 + "_" + prefix2 + "_" + "ROV*log(return)", f5)
            }
          }

          val video_info: JSONObject = if (record.isNull("t_v_info_feature")) new JSONObject() else
            JSON.parseObject(record.getString("t_v_info_feature"))
          featureMap.put("total_time", if (video_info.containsKey("total_time")) video_info.getIntValue("total_time").toDouble else 0D)
          featureMap.put("bit_rate", if (video_info.containsKey("bit_rate")) video_info.getIntValue("bit_rate").toDouble else 0D)

          val c1: JSONObject = if (record.isNull("c1_feature")) new JSONObject() else
            JSON.parseObject(record.getString("c1_feature"))
          if (c1.nonEmpty) {
            featureMap.put("playcnt_6h", if (c1.containsKey("playcnt_6h")) c1.getIntValue("playcnt_6h").toDouble else 0D)
            featureMap.put("playcnt_1d", if (c1.containsKey("playcnt_1d")) c1.getIntValue("playcnt_1d").toDouble else 0D)
            featureMap.put("playcnt_3d", if (c1.containsKey("playcnt_3d")) c1.getIntValue("playcnt_3d").toDouble else 0D)
            featureMap.put("playcnt_7d", if (c1.containsKey("playcnt_7d")) c1.getIntValue("playcnt_7d").toDouble else 0D)
          }
          val c2: JSONObject = if (record.isNull("c2_feature")) new JSONObject() else
            JSON.parseObject(record.getString("c2_feature"))
          if (c2.nonEmpty) {
            featureMap.put("share_pv_12h", if (c2.containsKey("share_pv_12h")) c2.getIntValue("share_pv_12h").toDouble else 0D)
            featureMap.put("share_pv_1d", if (c2.containsKey("share_pv_1d")) c2.getIntValue("share_pv_1d").toDouble else 0D)
            featureMap.put("share_pv_3d", if (c2.containsKey("share_pv_3d")) c2.getIntValue("share_pv_3d").toDouble else 0D)
            featureMap.put("share_pv_7d", if (c2.containsKey("share_pv_7d")) c2.getIntValue("share_pv_7d").toDouble else 0D)
            featureMap.put("return_uv_12h", if (c2.containsKey("return_uv_12h")) c2.getIntValue("return_uv_12h").toDouble else 0D)
            featureMap.put("return_uv_1d", if (c2.containsKey("return_uv_1d")) c2.getIntValue("return_uv_1d").toDouble else 0D)
            featureMap.put("return_uv_3d", if (c2.containsKey("return_uv_3d")) c2.getIntValue("return_uv_3d").toDouble else 0D)
            featureMap.put("return_uv_7d", if (c2.containsKey("return_uv_7d")) c2.getIntValue("return_uv_7d").toDouble else 0D)
          }

          val title = if (video_info.containsKey("title")) video_info.getString("title") else ""
          if (!title.equals("")) {
            for (key_feature <- List("c3_feature", "c4_feature", "c5_feature", "c6_feature", "c7_feature")) {
              val c34567: JSONObject = if (record.isNull(key_feature)) new JSONObject() else
                JSON.parseObject(record.getString(key_feature))
              for (key_time <- List("tags_1d", "tags_3d", "tags_7d")) {
                val tags = if (c34567.containsKey(key_time)) c34567.getString(key_time) else ""
                if (!tags.equals("")) {
                  val (f1, f2, f3, f4) = funcC34567ForTags(tags, title)
                  featureMap.put(key_feature + "_" + key_time + "_matchnum", f1)
                  featureMap.put(key_feature + "_" + key_time + "_maxscore", f3)
                  featureMap.put(key_feature + "_" + key_time + "_avgscore", f4)
                }
              }
            }
          }

          val vid = if (record.isNull("vid")) "" else record.getString("vid")
          if (!vid.equals("")) {
            for (key_feature <- List("c8_feature", "c9_feature")) {
              val c89: JSONObject = if (record.isNull(key_feature)) new JSONObject() else
                JSON.parseObject(record.getString(key_feature))
              for (key_action <- List("share", "return")) {
                val cfListStr = if (c89.containsKey(key_action)) c89.getString(key_action) else ""
                if (!cfListStr.equals("")) {
                  val cfMap = cfListStr.split(",").map(r => {
                    val rList = r.split(":")
                    (rList(0), (rList(1), rList(2), rList(3)))
                  }).toMap
                  if (cfMap.contains(vid)) {
                    val (score, num, rank) = cfMap(vid)
                    featureMap.put(key_feature + "_" + key_action + "_score", score.toDouble)
                    featureMap.put(key_feature + "_" + key_action + "_num", num.toDouble)
                    featureMap.put(key_feature + "_" + key_action + "_rank", 1.0 / rank.toDouble)
                  }
                }
              }
            }
          }

          val d1: JSONObject = if (record.isNull("d1_feature")) new JSONObject() else
            JSON.parseObject(record.getString("d1_feature"))
          if (d1.nonEmpty) {
            featureMap.put("d1_exp", if (d1.containsKey("exp")) d1.getString("exp").toDouble else 0D)
            featureMap.put("d1_return_n", if (d1.containsKey("return_n")) d1.getString("return_n").toDouble else 0D)
            featureMap.put("d1_rovn", if (d1.containsKey("rovn")) d1.getString("rovn").toDouble else 0D)
          }


          val flag = record.isNull("allfeaturemap")

          val allfeaturemap = if (record.isNull("allfeaturemap")) new JSONObject() else
            JSON.parseObject(record.getString("allfeaturemap"))
//          val featuresStr = allfeaturemap.toMap.map(r=> r._1 + ":" + r._2.toString).toSeq
          val apptype = record.getString("apptype")
          val pagesource = record.getString("pagesource")
          val abcode = record.getString("abcode")
          val level = if (record.isNull("level")) "0" else record.getString("level")
          val label = record.getString("is_return")
          (apptype, pagesource, level, label, abcode, allfeaturemap, featureMap, flag)
        }).filter{
          case (apptype, pagesource, level, label, abcode, allfeaturemap, featureMap, flag) =>
            apptype.equals("3") && pagesource.endsWith("recommend") &&
//            Set("ab0", "ab1", "ab2", "ab3").contains(abcode) &&
              level.equals("0") && !flag
        }.mapPartitions(row => {
          val result = new ArrayBuffer[String]()
          val bucketsMap = bucketsMap_br.value
          row.foreach {
            case (apptype, pagesource, level, label, abcode, allfeaturemap, featureMap, flag) =>
              val offlineFeatrueMap = featureMap.filter(r => bucketsMap.containsKey(r._1)).map(r =>{
                val score = r._2.toString.toDouble
                if (score > 1E-8) {
                  val (bucketNum, buckets) = bucketsMap(r._1)
                  val scoreNew = 1.0 / bucketNum * (ExtractorUtils.findInsertPosition(buckets, score).toDouble + 1.0)
                  r._1 + ":" + scoreNew.toString
                } else {
                  ""
                }
              }).filter(_.nonEmpty)
              result.add((apptype, pagesource, level, label, abcode, allfeaturemap.toString, offlineFeatrueMap.iterator.mkString(","))
              .productIterator.mkString("\t"))
          }
          result.iterator
        })

      // 4 保存数据到hdfs
      val savePartition = dt + hh
      val hdfsPath = savePath + "/" + savePartition
      if (hdfsPath.nonEmpty && hdfsPath.startsWith("/dw/recommend/model/")){
        println("删除路径并开始数据写入:" + hdfsPath)
        MyHdfsUtils.delete_hdfs_path(hdfsPath)
        odpsData.coalesce(repartition).saveAsTextFile(hdfsPath, classOf[GzipCodec])
      }else{
        println("路径不合法，无法写入:" + hdfsPath)
      }
    }

    val data2 = sc.textFile(savePath + "/" + readDate + "*").mapPartitions(row=>{
      val result = new ArrayBuffer[(String,
        Map[String, String], Map[String, String], List[String], List[String], List[String], List[String])]()
      val contentList = contentList_br.value
      row.foreach(r=>{
        val rList = r.split("\t")
        val label = rList(3)
        val allfeaturemap = JSON.parseObject(rList(5)).toMap.map(r => (r._1, r._2.toString))
        val offlineFeatrueMap = rList(6).split(",").map(r => (r.split(":")(0), r.split(":")(1))).toMap

        val v3 = contentList.map(name =>{
          val useOfflineNames = Set("c3_feature_tags_1d_matchnum","c3_feature_tags_1d_maxscore","c3_feature_tags_1d_avgscore","c3_feature_tags_3d_matchnum","c3_feature_tags_3d_maxscore","c3_feature_tags_3d_avgscore","c3_feature_tags_7d_matchnum","c3_feature_tags_7d_maxscore","c3_feature_tags_7d_avgscore","c4_feature_tags_1d_matchnum","c4_feature_tags_1d_maxscore","c4_feature_tags_1d_avgscore","c4_feature_tags_3d_matchnum","c4_feature_tags_3d_maxscore","c4_feature_tags_3d_avgscore","c4_feature_tags_7d_matchnum","c4_feature_tags_7d_maxscore","c4_feature_tags_7d_avgscore","c5_feature_tags_1d_matchnum","c5_feature_tags_1d_maxscore","c5_feature_tags_1d_avgscore","c5_feature_tags_3d_matchnum","c5_feature_tags_3d_maxscore","c5_feature_tags_3d_avgscore","c5_feature_tags_7d_matchnum","c5_feature_tags_7d_maxscore","c5_feature_tags_7d_avgscore","c6_feature_tags_1d_matchnum","c6_feature_tags_1d_maxscore","c6_feature_tags_1d_avgscore","c6_feature_tags_3d_matchnum","c6_feature_tags_3d_maxscore","c6_feature_tags_3d_avgscore","c6_feature_tags_7d_matchnum","c6_feature_tags_7d_maxscore","c6_feature_tags_7d_avgscore","c7_feature_tags_1d_matchnum","c7_feature_tags_1d_maxscore","c7_feature_tags_1d_avgscore","c7_feature_tags_3d_matchnum","c7_feature_tags_3d_maxscore","c7_feature_tags_3d_avgscore","c7_feature_tags_7d_matchnum","c7_feature_tags_7d_maxscore","c7_feature_tags_7d_avgscore","c8_feature_share_score","c8_feature_share_num","c8_feature_share_rank","c8_feature_return_score","c8_feature_return_num","c8_feature_return_rank","c9_feature_share_score","c9_feature_share_num","c9_feature_share_rank","c9_feature_return_score","c9_feature_return_num","c9_feature_return_rank")
          if (useOfflineNames.contains(name)){
            if (offlineFeatrueMap.contains(name)){
              name + ":" + offlineFeatrueMap(name)
            }else{
              ""
            }
          }else{
            if (allfeaturemap.contains(name)) {
              name + ":" + allfeaturemap(name)
            } else {
              ""
            }
          }
        }).filter(_.nonEmpty)

        val v4 = contentList.map(name => {
          val useOfflineNames = Set("b123_1h_STR","b123_1h_log(share)","b123_1h_ROV","b123_1h_log(return)","b123_1h_ROV*log(return)","b123_2h_STR","b123_2h_log(share)","b123_2h_ROV","b123_2h_log(return)","b123_2h_ROV*log(return)","b123_3h_STR","b123_3h_log(share)","b123_3h_ROV","b123_3h_log(return)","b123_3h_ROV*log(return)","b123_4h_STR","b123_4h_log(share)","b123_4h_ROV","b123_4h_log(return)","b123_4h_ROV*log(return)","b123_12h_STR","b123_12h_log(share)","b123_12h_ROV","b123_12h_log(return)","b123_12h_ROV*log(return)","b123_1d_STR","b123_1d_log(share)","b123_1d_ROV","b123_1d_log(return)","b123_1d_ROV*log(return)","b123_3d_STR","b123_3d_log(share)","b123_3d_ROV","b123_3d_log(return)","b123_3d_ROV*log(return)","b123_7d_STR","b123_7d_log(share)","b123_7d_ROV","b123_7d_log(return)","b123_7d_ROV*log(return)","b167_1h_STR","b167_1h_log(share)","b167_1h_ROV","b167_1h_log(return)","b167_1h_ROV*log(return)","b167_2h_STR","b167_2h_log(share)","b167_2h_ROV","b167_2h_log(return)","b167_2h_ROV*log(return)","b167_3h_STR","b167_3h_log(share)","b167_3h_ROV","b167_3h_log(return)","b167_3h_ROV*log(return)","b167_4h_STR","b167_4h_log(share)","b167_4h_ROV","b167_4h_log(return)","b167_4h_ROV*log(return)","b167_12h_STR","b167_12h_log(share)","b167_12h_ROV","b167_12h_log(return)","b167_12h_ROV*log(return)","b167_1d_STR","b167_1d_log(share)","b167_1d_ROV","b167_1d_log(return)","b167_1d_ROV*log(return)","b167_3d_STR","b167_3d_log(share)","b167_3d_ROV","b167_3d_log(return)","b167_3d_ROV*log(return)","b167_7d_STR","b167_7d_log(share)","b167_7d_ROV","b167_7d_log(return)","b167_7d_ROV*log(return)","b8910_1h_STR","b8910_1h_log(share)","b8910_1h_ROV","b8910_1h_log(return)","b8910_1h_ROV*log(return)","b8910_2h_STR","b8910_2h_log(share)","b8910_2h_ROV","b8910_2h_log(return)","b8910_2h_ROV*log(return)","b8910_3h_STR","b8910_3h_log(share)","b8910_3h_ROV","b8910_3h_log(return)","b8910_3h_ROV*log(return)","b8910_4h_STR","b8910_4h_log(share)","b8910_4h_ROV","b8910_4h_log(return)","b8910_4h_ROV*log(return)","b8910_12h_STR","b8910_12h_log(share)","b8910_12h_ROV","b8910_12h_log(return)","b8910_12h_ROV*log(return)","b8910_1d_STR","b8910_1d_log(share)","b8910_1d_ROV","b8910_1d_log(return)","b8910_1d_ROV*log(return)","b8910_3d_STR","b8910_3d_log(share)","b8910_3d_ROV","b8910_3d_log(return)","b8910_3d_ROV*log(return)","b8910_7d_STR","b8910_7d_log(share)","b8910_7d_ROV","b8910_7d_log(return)","b8910_7d_ROV*log(return)","b111213_1h_STR","b111213_1h_log(share)","b111213_1h_ROV","b111213_1h_log(return)","b111213_1h_ROV*log(return)","b111213_2h_STR","b111213_2h_log(share)","b111213_2h_ROV","b111213_2h_log(return)","b111213_2h_ROV*log(return)","b111213_3h_STR","b111213_3h_log(share)","b111213_3h_ROV","b111213_3h_log(return)","b111213_3h_ROV*log(return)","b111213_4h_STR","b111213_4h_log(share)","b111213_4h_ROV","b111213_4h_log(return)","b111213_4h_ROV*log(return)","b111213_12h_STR","b111213_12h_log(share)","b111213_12h_ROV","b111213_12h_log(return)","b111213_12h_ROV*log(return)","b111213_1d_STR","b111213_1d_log(share)","b111213_1d_ROV","b111213_1d_log(return)","b111213_1d_ROV*log(return)","b111213_3d_STR","b111213_3d_log(share)","b111213_3d_ROV","b111213_3d_log(return)","b111213_3d_ROV*log(return)","b111213_7d_STR","b111213_7d_log(share)","b111213_7d_ROV","b111213_7d_log(return)","b111213_7d_ROV*log(return)","b171819_1h_STR","b171819_1h_log(share)","b171819_1h_ROV","b171819_1h_log(return)","b171819_1h_ROV*log(return)","b171819_2h_STR","b171819_2h_log(share)","b171819_2h_ROV","b171819_2h_log(return)","b171819_2h_ROV*log(return)","b171819_3h_STR","b171819_3h_log(share)","b171819_3h_ROV","b171819_3h_log(return)","b171819_3h_ROV*log(return)","b171819_4h_STR","b171819_4h_log(share)","b171819_4h_ROV","b171819_4h_log(return)","b171819_4h_ROV*log(return)","b171819_12h_STR","b171819_12h_log(share)","b171819_12h_ROV","b171819_12h_log(return)","b171819_12h_ROV*log(return)","b171819_1d_STR","b171819_1d_log(share)","b171819_1d_ROV","b171819_1d_log(return)","b171819_1d_ROV*log(return)","b171819_3d_STR","b171819_3d_log(share)","b171819_3d_ROV","b171819_3d_log(return)","b171819_3d_ROV*log(return)","b171819_7d_STR","b171819_7d_log(share)","b171819_7d_ROV","b171819_7d_log(return)","b171819_7d_ROV*log(return)")
          if (useOfflineNames.contains(name)) {
            if (offlineFeatrueMap.contains(name)) {
              name + ":" + offlineFeatrueMap(name)
            } else {
              ""
            }
          } else {
            if (allfeaturemap.contains(name)) {
              name + ":" + allfeaturemap(name)
            } else {
              ""
            }
          }
        }).filter(_.nonEmpty)

        val v5 = contentList.map(name => {
          val useOfflineNames = Set("d1_exp", "d1_return_n", "d1_rovn")
          if (useOfflineNames.contains(name)) {
            if (offlineFeatrueMap.contains(name)) {
              name + ":" + offlineFeatrueMap(name)
            } else {
              ""
            }
          } else {
            if (allfeaturemap.contains(name)) {
              name + ":" + allfeaturemap(name)
            } else {
              ""
            }
          }
        }).filter(_.nonEmpty)

        val v6 = contentList.map(name => {
          val useOfflineNames = Set("playcnt_6h", "playcnt_1d", "playcnt_3d", "playcnt_7d",
            "share_pv_12h", "share_pv_1d", "share_pv_3d", "share_pv_7d", "return_uv_12h", "return_uv_1d", "return_uv_3d", "return_uv_7d")
          if (useOfflineNames.contains(name)) {
            if (offlineFeatrueMap.contains(name)) {
              name + ":" + offlineFeatrueMap(name)
            } else {
              ""
            }
          } else {
            if (allfeaturemap.contains(name)) {
              name + ":" + allfeaturemap(name)
            } else {
              ""
            }
          }
        }).filter(_.nonEmpty)


        result.add((label, offlineFeatrueMap, allfeaturemap, v3, v4, v5, v6))

      })
      result.iterator
    })

    val saveV1 = "/dw/recommend/model/17_for_check_v1/" + readDate
    if (saveV1.nonEmpty && saveV1.startsWith("/dw/recommend/model/")) {
      println("删除路径并开始数据写入:" + saveV1)
      MyHdfsUtils.delete_hdfs_path(saveV1)
      data2.map(r => r._1 + "\t" + r._2.map(r=> r._1 + ":" + r._2).mkString("\t")).saveAsTextFile(saveV1, classOf[GzipCodec])
    } else {
      println("路径不合法，无法写入:" + saveV1)
    }

    val saveV2 = "/dw/recommend/model/17_for_check_v2/" + readDate
    if (saveV2.nonEmpty && saveV2.startsWith("/dw/recommend/model/")) {
      println("删除路径并开始数据写入:" + saveV2)
      MyHdfsUtils.delete_hdfs_path(saveV2)
      data2.map(r => r._1 + "\t" + r._3.map(r=> r._1 + ":" + r._2).mkString("\t")).saveAsTextFile(saveV2, classOf[GzipCodec])
    } else {
      println("路径不合法，无法写入:" + saveV2)
    }

    val saveV3 = "/dw/recommend/model/17_for_check_v3/" + readDate
    if (saveV3.nonEmpty && saveV3.startsWith("/dw/recommend/model/")) {
      println("删除路径并开始数据写入:" + saveV3)
      MyHdfsUtils.delete_hdfs_path(saveV3)
      data2.map(r => r._1 + "\t" + r._4.mkString("\t")).saveAsTextFile(saveV3, classOf[GzipCodec])
    } else {
      println("路径不合法，无法写入:" + saveV3)
    }

    val saveV4 = "/dw/recommend/model/17_for_check_v4/" + readDate
    if (saveV4.nonEmpty && saveV4.startsWith("/dw/recommend/model/")) {
      println("删除路径并开始数据写入:" + saveV4)
      MyHdfsUtils.delete_hdfs_path(saveV4)
      data2.map(r => r._1 + "\t" + r._5.mkString("\t")).saveAsTextFile(saveV4, classOf[GzipCodec])
    } else {
      println("路径不合法，无法写入:" + saveV4)
    }

    val saveV5 = "/dw/recommend/model/17_for_check_v5/" + readDate
    if (saveV5.nonEmpty && saveV5.startsWith("/dw/recommend/model/")) {
      println("删除路径并开始数据写入:" + saveV5)
      MyHdfsUtils.delete_hdfs_path(saveV5)
      data2.map(r => r._1 + "\t" + r._6.mkString("\t")).saveAsTextFile(saveV5, classOf[GzipCodec])
    } else {
      println("路径不合法，无法写入:" + saveV5)
    }

    val saveV6 = "/dw/recommend/model/17_for_check_v6/" + readDate
    if (saveV6.nonEmpty && saveV6.startsWith("/dw/recommend/model/")) {
      println("删除路径并开始数据写入:" + saveV6)
      MyHdfsUtils.delete_hdfs_path(saveV6)
      data2.map(r => r._1 + "\t" + r._7.mkString("\t")).saveAsTextFile(saveV6, classOf[GzipCodec])
    } else {
      println("路径不合法，无法写入:" + saveV6)
    }



  }



  def func(record: Record, schema: TableSchema): Record = {
    record
  }
  def funcC34567ForTags(tags: String, title: String): Tuple4[Double, String, Double, Double] = {
    // 匹配数量 匹配词 语义最高相似度分 语义平均相似度分
    val tagsList = tags.split(",")
    var d1 = 0.0
    val d2 = new ArrayBuffer[String]()
    var d3 = 0.0
    var d4 = 0.0
    for (tag <- tagsList){
      if (title.contains(tag)){
        d1 = d1 + 1.0
        d2.add(tag)
      }
      val score = Similarity.conceptSimilarity(tag, title)
      d3 = if (score > d3) score else d3
      d4 = d4 + score
    }
    d4 = if (tagsList.nonEmpty) d4 / tagsList.size else d4
    (d1, d2.mkString(","), d3, d4)
  }
}
