添加了weatherlistresponse并进行了一些修改
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use sqlx::FromRow;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
// 定义JWT载荷结构体
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
@@ -36,6 +38,61 @@ pub struct ErrorResponse {
|
||||
pub errmsg: Option<String>,
|
||||
}
|
||||
|
||||
// 定义新类型,包装 Vec<f64>(当前 crate 内的类型)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FloatVec(pub Vec<f64>);
|
||||
|
||||
// 为 Vec<f64> 实现从 JsonValue 的转换
|
||||
impl TryFrom<JsonValue> for FloatVec {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: JsonValue) -> Result<Self, Self::Error> {
|
||||
// 检查 JSON 值是否为数组
|
||||
let arr = value
|
||||
.as_array()
|
||||
.ok_or_else(|| "JSON 类型错误:期望数组,但得到其他类型".to_string())?;
|
||||
|
||||
// 遍历数组元素,转换为 f64
|
||||
let mut result = Vec::with_capacity(arr.len());
|
||||
for item in arr {
|
||||
let num = item
|
||||
.as_f64()
|
||||
.ok_or_else(|| format!("JSON 元素类型错误:期望数字,但得到 {:?}", item))?;
|
||||
result.push(num);
|
||||
}
|
||||
|
||||
Ok(FloatVec(result))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, FromRow)] // 添加 FromRow 派生
|
||||
pub struct WeatherDataBrief {
|
||||
#[sqlx(rename = "id")] // 数据库表的 id 列(自增主键),查询映射时使用
|
||||
pub id: i32,
|
||||
|
||||
pub title: String,
|
||||
|
||||
pub date: NaiveDate,
|
||||
|
||||
#[serde(rename = "hours")]
|
||||
#[sqlx(rename = "hour")] // 数据库中是 hour 字段
|
||||
pub hours: i32,
|
||||
|
||||
#[serde(rename = "min")]
|
||||
#[sqlx(rename = "min")]
|
||||
pub min: i32,
|
||||
|
||||
pub longitude: f64,
|
||||
pub latidute: f64,
|
||||
}
|
||||
|
||||
// 在models.rs中添加(可放在WeatherDataBrief下方)
|
||||
#[derive(Debug, Serialize, FromRow)]
|
||||
pub struct WeatherListResponse {
|
||||
pub list: Vec<WeatherDataBrief>, // 分页数据列表
|
||||
pub total: i64, // 总数据条数
|
||||
}
|
||||
|
||||
// 天气数据结构体(修改后)
|
||||
#[derive(Debug, Deserialize, Serialize, FromRow)] // 添加 FromRow 派生
|
||||
pub struct WeatherData {
|
||||
@@ -50,7 +107,7 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "assignmentnumber")]
|
||||
#[sqlx(rename = "assignmentnumber")]
|
||||
pub assignment_number: String,
|
||||
pub assignment_number: Option<String>,
|
||||
|
||||
#[serde(rename = "atmosphericStability")]
|
||||
#[sqlx(rename = "atmosphericstability")]
|
||||
@@ -66,7 +123,7 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "calculatedwindspeed")]
|
||||
#[sqlx(rename = "calculatedwindspeed")]
|
||||
pub calculated_wind_speed: f64,
|
||||
pub calculated_wind_speed: Option<f64>,
|
||||
|
||||
#[serde(rename = "cloudIndex")]
|
||||
#[sqlx(rename = "cloudindex")]
|
||||
@@ -74,7 +131,7 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "convertedWindSpeed")]
|
||||
#[sqlx(rename = "convertedwindspeed")]
|
||||
pub converted_wind_speed: f64,
|
||||
pub converted_wind_speed: i32,
|
||||
|
||||
pub date: NaiveDate,
|
||||
|
||||
@@ -92,7 +149,7 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "inspectiontype")]
|
||||
#[sqlx(rename = "inspectiontype")]
|
||||
pub inspection_type: String,
|
||||
pub inspection_type: Option<String>,
|
||||
|
||||
pub latitude: f64,
|
||||
pub longitude: f64,
|
||||
@@ -133,7 +190,7 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "solarRadiationLevel")]
|
||||
#[sqlx(rename = "solarradiationlevel")]
|
||||
pub solar_radiation_level: f64,
|
||||
pub solar_radiation_level: i32,
|
||||
|
||||
#[serde(rename = "suitabilityDegree")]
|
||||
#[sqlx(rename = "suitabilitydegree")]
|
||||
@@ -148,7 +205,8 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "windDirection")]
|
||||
#[sqlx(rename = "winddirection")]
|
||||
pub wind_direction: Vec<f64>,
|
||||
#[sqlx(try_from = "serde_json::Value")]
|
||||
pub wind_direction: FloatVec,
|
||||
|
||||
#[serde(rename = "windDirectionStandardDeviation")]
|
||||
#[sqlx(rename = "winddirectionstandarddeviation")]
|
||||
@@ -160,7 +218,8 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "windSpeed")]
|
||||
#[sqlx(rename = "windspeed")]
|
||||
pub wind_speed: Vec<f64>,
|
||||
#[sqlx(try_from = "serde_json::Value")]
|
||||
pub wind_speed: FloatVec,
|
||||
|
||||
#[serde(rename = "windSpeedSuitability")]
|
||||
#[sqlx(rename = "windspeedsuitability")]
|
||||
|
||||
Reference in New Issue
Block a user