package org.jeecg.yw.spark

import org.apache.spark.ml.PipelineModel
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.{StringType, StructField, StructType}

import java.util
import java.util.{HashSet, Map, Set}
import scala.collection.convert.ImplicitConversions.{`collection AsScalaIterable`, `map AsScala`}

object analysis {

  import org.apache.spark.sql.Row

  // 机器学习预测
  def getML(map: Map[String, AnyRef]): Double = {
    val spark = SparkSession.builder()
      .appName("Weather Temperature Prediction")
      .master("local[*]")
      .getOrCreate()

    // 加载模型
    val model = PipelineModel.load("hdfs://192.168.192.100:8020/Model")

    // 从map中获取键值对，并创建一个新的DataFrame
    val row = Row.fromSeq(map.values.toSeq)
    val df = spark.createDataFrame(
      spark.sparkContext.parallelize(Seq(row)),
      StructType(map.keys.toSeq.map(fieldName => StructField(fieldName, StringType)))
    )

    // 使用模型进行预测
    val predictions = model.transform(df)
    predictions.select("prediction").show()
    spark.stop()
    val prediction = predictions.select("prediction").collect()(0)(0).toString.toDouble
    prediction
  }

  def getScore(map: Map[String, AnyRef]): Double = {
    val age: Int = map.get("age").toString.toInt
    val weight: Int = map.get("weight").toString.toInt
    val sleepTime: Int = map.get("sleepTime").toString.toInt
    val tableDataObject: AnyRef = map.get("tableData")
    var Score: Double = 100

    import scala.jdk.CollectionConverters._

    val tableDataList: List[Map[String, AnyRef]] = tableDataObject.asInstanceOf[java.util.ArrayList[java.util.Map[String, AnyRef]]].asScala.toList
    // 遍历列表
    for (item <- tableDataList) {
      val `type`: String = item.get("type").toString
      val num: Int = item.get("num").toString.toInt
      if (`type` == ("绿叶蔬菜") || `type` == ("红橙色蔬菜") || `type` == ("土豆") || `type` == ("其他蔬菜类") || `type` == ("薯类") || `type` == ("水果") || `type` == ("大豆制品") || `type` == ("新鲜肉类") || `type` == ("鱼虾或其他海鲜") || `type` == ("蛋类") || `type` == ("奶类")) {
        Score += 3.25 * (10 + num) / 10
      }
      else {
        Score -= 10.25 * (10 + num) / 10
      }
    }

    var temp: Double = 0
    // 计算年龄
    Score = (Score * (100 + age - 10) / 100).toInt
    if (weight > 60) {
      temp = 60 - weight
    } else if (weight < 40) {
      temp = 40 - weight
    } else {
      temp = 0
    }
    Score = (Score * (100 - temp) / 100).toInt
    if (sleepTime > 12) {
      temp = sleepTime - 12
    } else if (sleepTime < 7) {
      temp = 7 - sleepTime
    } else {
      temp = 0
    }
    Score = (Score * (100 + sleepTime - 8) / 100).toInt
    if (Score > 97) {
      Score = 97
    }
    return Score
  }

  def getResult(map: util.Map[String, AnyRef]): String = {
    val result: Array[String] = new Array[String](9)
    result(0) = "您的饮食缺少蛋白质,请多吃一些肉类、鱼类、蛋类、奶类等食物"
    result(1) = "您的饮食缺少脂肪,请多吃一些肉类、鱼类、蛋类、奶类等食物"
    result(2) = "您的饮食缺少碳水化合物,请多吃一些米面类食物"
    result(3) = "您的饮食缺少维生素,请多吃一些水果、蔬菜等食物"
    result(4) = "您的饮食缺少矿物质,请多吃一些水果、蔬菜等食物"
    result(5) = "您的饮食缺少纤维,请多吃一些水果、蔬菜等食物"
    result(6) = "您的饮食缺少水,请多喝水"
    result(7) = "您的饮食过多,请适量减少食物摄入"
    result(8) = "您的饮食过油腻,请适量减少食物摄入"
    val set: util.Set[Integer] = new util.HashSet[Integer]
    for (i <- 0 until 9) {
      val random: Int = (Math.random * 10).toInt
      if (random < 3) set.add(i)
    }
    //拼接结果
    var resultStr: String = ""
    var j: Int = 0
    for (i <- 0 until set.size) {
      j += 1
      resultStr += j + "." + result(i) + "\n"
    }
    resultStr
  }

  def getRecord(): Double = {
    // 一个70-90的随机数
    val random: Double = (Math.random * 10).toInt
    random
  }
}
