package org.jeecg.cyl

import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._


object analysis {

  def main(args: Array[String]): Unit = {
    // 创建Spark Session
    val spark = SparkSession.builder()
      .appName("Weather Temperature Prediction")
      .master("local[*]")
      .getOrCreate()

    // 加载数据
    val mumBabyDF = spark.read
      .option("header", "true") // 使用首行作为列名
      .option("inferSchema", "true") // 推断数据类型
      .csv("C:\\Users\\23972\\Desktop\\Completion-template-cmd\\system\\start\\src\\main\\java\\org\\jeecg\\cyl\\tianchi_mum_baby.csv")

    val tradeHistoryDF = spark.read
      .option("header", "true")
      .option("inferSchema", "true")
      .csv("C:\\Users\\23972\\Desktop\\Completion-template-cmd\\system\\start\\src\\main\\java\\org\\jeecg\\cyl\\tianchi_mum_baby_trade_history.csv")

    // 统计各产品类别的购买次数，并按次数降序排列
    val popularCategories = tradeHistoryDF
      .groupBy("category")
      .count()
      .orderBy(desc("count"))

    popularCategories.show()

    // 假设tradeHistoryDF包含日期字段`date`，格式为'yyyy-MM-dd'
    val monthlySalesTrend = tradeHistoryDF
      .withColumn("month", month(col("date")))
      .groupBy("month")
      .agg(sum("price").alias("total_sales"))
      .orderBy("month")

    monthlySalesTrend.show()
    // 对每个产品类别，计算平均购买价格和平均购买数量
    val avgPriceAndQuantity = tradeHistoryDF
      .groupBy("category")
      .agg(
        avg("price").alias("average_price"),
        avg("quantity").alias("average_quantity")
      )
      .orderBy(desc("average_price"))

    avgPriceAndQuantity.show()
    // 统计每位顾客的总消费额，并找出消费最高的顶级顾客
    val topCustomers = tradeHistoryDF
      .groupBy("customer_id")
      .agg(sum("price").alias("total_spent"))
      .orderBy(desc("total_spent"))

    topCustomers.show()

    popularCategories.write.option("header", "true").csv("path_to_popular_categories.csv")
    topCustomers.write.option("header", "true").csv("path_to_top_customers.csv")
    monthlySalesTrend.write.option("header", "true").csv("path_to_monthly_sales_trend.csv")
    avgPriceAndQuantity.write.option("header", "true").csv("path_to_avg_price_and_quantity.csv")

  }
}