7.4 定义 Controller 来接收用户请求
package com.atguigu.dw.gmallpublisher.controller
import java.text.SimpleDateFormat
import java.util.Date
import com.atguigu.dw.gmallpublisher.service.PublisherService
import org.apache.commons.lang.time.DateUtils
import org.json4s.jackson.JsonMethods
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.{GetMapping, RequestParam, RestController}
import scala.collection.mutable
@RestController
class PublisherController {
@Autowired
var publisherService: PublisherService = _
@GetMapping(Array("realtime-total"))
def getRealTimeTotal(@RequestParam("date") date: String): String = {
val total: Long = publisherService.getDauTotal(date)
val result =
s"""
|[
| {"id":"dau","name":"新增日活","value":$total},
| {"id":"new_mid","name":"新增用户","value":233}
|]
""".stripMargin
result
}
@GetMapping(Array("realtime-hour"))
def getRealTimeHour(@RequestParam("id") id: String, @RequestParam("date") date: String) = {
if (id == "dau") {
val todayMap: Map[String, Long] = publisherService.getDauHour2countMap(date)
val yesterdayMap: Map[String, Long] = publisherService.getDauHour2countMap(date2Yesterday(date))
val resultMap: mutable.Map[String, Map[String, Long]] = mutable.Map[String, Map[String, Long]]()
resultMap += "today" -> todayMap
resultMap += "yesterday" -> yesterdayMap
println(resultMap)
import org.json4s.JsonDSL._
JsonMethods.compact(JsonMethods.render(resultMap))
} else {
null
}
}
private val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
private def date2Yesterday(date: String): String = {
val today: Date = dateFormat.parse(date)
val yesterday: Date = DateUtils.addDays(today, -1)
dateFormat.format(yesterday)
}
}