| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- use std::str::FromStr;
- use actix_web::HttpResponse;
- use rust_decimal::{Decimal};
- use serde_json::Value;
- use crate::server::Response;
- pub fn parse_str_to_decimal(value: Value, param_name: &str) -> Result<Decimal, HttpResponse> {
- match get_str(value.clone(), param_name) {
- Ok(decimal_str) => {
- match Decimal::from_str(decimal_str.as_str()) {
- Ok(rst) => {
- Ok(rst)
- }
- Err(_) => {
- // 返回数据
- let response = Response {
- query: value.clone(),
- msg: Some(format!("{}你传的怕不是个数字啊 你传的是: {}", param_name, value[param_name])),
- code: 500,
- data: Value::Null,
- };
- let json_string = serde_json::to_string(&response).unwrap();
- Err(HttpResponse::Ok().content_type("application/json").body(json_string))
- }
- }
- }
- Err(response) => {
- Err(response)
- }
- }
- }
- pub fn get_str(value: Value, param_name: &str) -> Result<String, HttpResponse> {
- match value[param_name].as_str() {
- None => {
- // 返回数据
- let response = Response {
- query: value.clone(),
- msg: Some(format!("{}是必填项, 你的参数: {}", param_name, value.to_string())),
- code: 500,
- data: Value::Null,
- };
- let json_string = serde_json::to_string(&response).unwrap();
- Err(HttpResponse::Ok().content_type("application/json").body(json_string))
- }
- Some(str) => {
- if str.is_empty() {
- // 返回数据
- let response = Response {
- query: value.clone(),
- msg: Some(format!("{}传了个空字符串", param_name)),
- code: 500,
- data: Value::Null,
- };
- let json_string = serde_json::to_string(&response).unwrap();
- Err(HttpResponse::Ok().content_type("application/json").body(json_string))
- } else {
- Ok(str.to_string())
- }
- }
- }
- }
- #[tokio::test]
- async fn test_decimal_to_i64() {
- use global::log_utils::init_log_with_info;
- use tracing::info;
- use rust_decimal_macros::dec;
- use rust_decimal::prelude::ToPrimitive;
- init_log_with_info();
- info!("{}", dec!(50).to_i64().unwrap());
- info!("{}", dec!(50.5).to_i64().unwrap());
- }
|