1. 程式人生 > >前端 後端的基本 互動(後端 scala)(二)

前端 後端的基本 互動(後端 scala)(二)

最上層開始,一個 Boot.scala

~pathPrefix (“lttt”/”comm”){
服務的名稱 ! _
}

定義服務的介面路徑字首,之後在服務裡再定義一些一些介面,只寫字首後面的路徑,但是傳給前端的url 是 服務字首的路徑+服務裡介面的路徑

scala 後端返回處理的資料型別

List 和 Map 形式

List 形式 返回相當於陣列 []

Map形式 返回相當於列表 {}

例如:前端需要 [
{“info”:[[20,30],[20,30],[20,30] ]},
{“info1”:[[20,30],[20,30],[20,30] ]},
{“info2”:[[20,30],[20,30],[20,30] ]}
]
後端返回處理資料

 val sql = new ProjDataInfoSql(cellKey).generateSql.query.value
    if (sql.length != 0) {
      val sqlResult = sql(0)
      log.info("eResult length==" + sqlResult)
      val info= List("age", "name", "address")
      val info1= List("age1", "name1", "address1")
      val info2= List("age2", "name2"
, "address2") val info3= List("age3", "name3", "address3") val infoend: List[List[String]] = (0 to info.length - 1).map(i => List(info(i), sqlResult(i))).toList val info1end: List[List[String]] = (0 to info1.length - 1).map(i => List(info2(i), sqlResult(i + 10))).toList val info2end: List
[List[String]] = (0 to info2.length - 1).map(i => List(info2(i), sqlResult(i + 22))).toList val info3end: List[List[String]] = (0 to info3.length - 1).map(i => List(info3(i), sqlResult(i + 24))).toList List(Map("info" -> infoend), Map("info1" -> info1end), Map("info2" -> info2end), Map("info3" -> info3end)) } else { List() }

根據語言匹配返回不同的資料(中文或者英文)

class ProjDataInfo extends LogSupport {

  def query(cellKey: String, language:String) = {
    import com.zte.bigdata.vmax.common.database.ConnectionFactoryPrefs.GBase       //宣告查詢 gbase  的資料




根據lanhuage 語言  返回對應的  資料
val finalResult = language match {
        case "en" =>
          val sqlResult = getZipedSqlResult(sqlResult, enAntennaInfoLists, enDeviceInfoLists, enRadioInfoLists, enBasicInfoLists, enPerformanceInfoLists )
     //呼叫函式,結果會返回5個數據,用  ._就可得到函式的每個資料
          List(Map("Antenna Info" -> sqlResult._1), Map("Device Info" ->sqlResult._2), Map("Radio Power Info" -> sqlResult._3),
            Map("Basic Info" ->sqlResult._4), Map("Radio Info" -> sqlResult._5))
        case "zh" =>
          val sqlResult = getZipedSqlResult(sqlResult, zhAntennaInfoLists, zhDeviceInfoLists, zhRadioInfoLists, zhBasicInfoLists, zhPperformanceInfoLists)
          List(Map("天線資訊" -> sqlResult._1), Map("裝置資訊" ->sqlResult._2), Map("無線功率配置" -> sqlResult._3),
            Map("基礎資訊" ->sqlResult._4), Map("無線引數" -> sqlResult._5))

      }

      finalResult       //查詢返回給前端的結果   
       } else {
      List()
    }
  }

 def getZipedSqlResult(sqlResult:List[String], antennaInfoLists: List[String], 
 radioInfoLists: List[String],
 basicInfoLists: List[String],
 performanceInfoLists: List[String])     = {

 val antennaResult: List[List[String]] = (0 to antennaInfoLists.length - 1).map(i => List(antennaInfoLists(i), sqlResult(i))).toList

val deviceResult: List[List[String]] = (0 to deviceInfoLists.length - 1).map(i => List(deviceInfoLists(i), sqlResult(i + 10))).toList

val radioResult: List[List[String]] = (0 to radioInfoLists.length - 1).map(i => List(radioInfoLists(i), sqlResult(i + 22))).toList

val basicResult: List[List[String]] = (0 to basicInfoLists.length - 1).map(i => List(basicInfoLists(i), sqlResult(i + 24))).toList

val performanceResult: List[List[String]] = (0 to performanceInfoLists.length - 1).map(i => List(performanceInfoLists(i), sqlResult(i + 44))).toList

    (antennaResult, deviceResult, radioResult, basicResult, performanceResult)     //相當於return  返回的資料

  }
}

後端的基本流程:

* 1.定義介面 *

~
path("commom" / "pinjun") {
//頁面進入趨勢圖-指標平均值,麥芒!
get {
//定義傳參
parameters('provinceCode, 'cityCode, 'districtCode, 'timeUnit, 'time, 'algorithmName, 'modelName, 'frequency, 'language, 'inOrOut.?(""), 'qci.?("")) {
(provinceCode: String, cityCode: String, districtCode: String, timeUnit: String, time: String, algorithmName: String, modelName: String, frequency: String, language: String, inOrOut: String, qci: String) => detach() {
complete {
try {
val geo = GeoSegment(provinceCode, cityCode, districtCode)
//新的 服務
new AvgIndexHistoryTrendService().query(geo, timeUnit, time, algorithmName, modelName, frequency, language, inOrOut, qci)
} catch {
case e: Exception =>
val msg = "create avgindexhistory trendchart is not available!" + e.getMessage
log.error(msg, e)
HttpResponse(500, msg)
}
}
}
}
}
}

2.定義 服務

class AvgIndexHistoryTrendService extends XmlConfigUtils {
  def query(geo: GeoSegment, timeUnit: String, time: String, algorithmName: String, modelName: String, frequency: String) ={


//服務裡面定義呼叫sql 的方法

//以及查詢資料庫 對 返回的結果進行處理

//按前端所需要的格式進行返回


}

3.處理資料 查詢資料庫,編寫sql語句

classSql(cellKey: String) extends SqlGenerator {
  override def genSql: String = s"""select antenna,round(height,0) as height,round(azimuth,0) as azimuth,powerdivisiontype,powerdivisionid,bsid,
                                  case when boardtype=1 then 'CC16' when boardtype=2 then 'CCE' when boardtype=3 then 'Other' else 'null' end as boardtype from lte_cm_projdata where reportcellkey='${cellKey}'"""

}