Explorar el Código

两种导出柱状图保留

gepangpang hace 1 año
padre
commit
7fbeba6979
Se han modificado 3 ficheros con 75 adiciones y 32 borrados
  1. 61 29
      src/export_columnar.rs
  2. 12 3
      src/export_template/template_columnar.rs
  3. 2 0
      src/utils/utils.rs

+ 61 - 29
src/export_columnar.rs

@@ -2,6 +2,7 @@ use std::str::FromStr;
 use chrono::NaiveDateTime;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
+use tracing::info;
 use crate::export_template::template_columnar;
 use crate::utils::utils;
 use crate::utils::utils::{ColumnarConfigInfo};
@@ -23,37 +24,68 @@ pub async fn export_columnar(config_info: ColumnarConfigInfo) {
     let mut over_list = vec![];
     let mut save_value: Vec<String> = vec![];
     let mut total = dec!(0);
-    for (index, item) in balance_list.iter().enumerate() {
-        if index == 0 {
-            total = Decimal::from_str(item[4].as_str().unwrap()).unwrap();
+    match config_info.export_mode {
+        1 => {
+            info!("正在导出余额柱状图!");
+            for (index, item) in balance_list.iter().enumerate() {
+                let time: i64 = item[1].as_str().unwrap().parse().unwrap();
+                let mut new_value: Vec<String> = vec![];
+                if time_list[0] < time {
+                    new_value.push(time_list[0].to_string());
+                    new_value.push(Decimal::from_str(item[4].as_str().unwrap()).unwrap().to_string());
+                    over_list.push(new_value.clone());
+                    time_list.remove(0);
+                }
+                save_value = item.as_array().unwrap().iter().map(|value| value.as_str().unwrap().to_string()).collect();
+                if index == balance_list.len() - 1 {
+                    if time <= time_list[0] {
+                        new_value.push(time_list[0].to_string());
+                        new_value.push(Decimal::from_str(item[4].as_str().unwrap()).unwrap().to_string());
+                        over_list.push(new_value.clone());
+                        time_list.remove(0);
+                    }
+                }
+            }
+            template_columnar::export_html(config_info, over_list);
         }
-        if index != 0 && item[5] == "transfer" {
-            total = total + (Decimal::from_str(item[4].as_str().unwrap()).unwrap() - Decimal::from_str(balance_list.clone()[index - 1][4].as_str().unwrap()).unwrap());
+        2 => {
+            info!("正在导出收益柱状图!");
+            for (index, item) in balance_list.iter().enumerate() {
+                if index == 0 {
+                    total = Decimal::from_str(item[4].as_str().unwrap()).unwrap();
+                }
+                if index != 0 && item[5] == "transfer" {
+                    total = total + (Decimal::from_str(item[4].as_str().unwrap()).unwrap() - Decimal::from_str(balance_list.clone()[index - 1][4].as_str().unwrap()).unwrap());
+                }
+                let time: i64 = item[1].as_str().unwrap().parse().unwrap();
+                let mut new_value: Vec<String> = vec![];
+                if time_list[0] < time {
+                    new_value.push(time_list[0].to_string());
+                    new_value.push((Decimal::from_str(item[4].as_str().unwrap()).unwrap() - total).to_string());
+                    over_list.push(new_value.clone());
+                    time_list.remove(0);
+                }
+                save_value = item.as_array().unwrap().iter().map(|value| value.as_str().unwrap().to_string()).collect();
+                if index == balance_list.len() - 1 && time <= time_list[0] {
+                    new_value.push(time_list[0].to_string());
+                    new_value.push((Decimal::from_str(item[4].as_str().unwrap()).unwrap() - total).to_string());
+                    save_value[1] = time_list[0].to_string();
+                    over_list.push(new_value.clone());
+                    time_list.remove(0);
+                }
+            }
+            let mut over_value_list: Vec<Vec<String>> = vec![];
+            for (index, item) in over_list.iter().enumerate() {
+                if index == 0 {
+                    over_value_list.push(vec![item[0].clone(), "0".to_string()]);
+                    continue;
+                }
+                over_value_list.push(vec![item[0].clone(), (Decimal::from_str(&item[1]).unwrap() - Decimal::from_str(&over_list[index - 1][1]).unwrap()).to_string()])
+            }
+            template_columnar::export_html(config_info, over_value_list);
         }
-        let time: i64 = item[1].as_str().unwrap().parse().unwrap();
-        let mut new_value: Vec<String> = vec![];
-        if time_list[0] < time {
-            new_value.push(time_list[0].to_string());
-            new_value.push((Decimal::from_str(item[4].as_str().unwrap()).unwrap() - total).to_string());
-            over_list.push(new_value.clone());
-            time_list.remove(0);
+        _ => {
+            info!("导出模式错误!");
         }
-        save_value = item.as_array().unwrap().iter().map(|value| value.as_str().unwrap().to_string()).collect();
-        if index == balance_list.len() - 1 && time <= time_list[0] {
-            new_value.push(time_list[0].to_string());
-            new_value.push((Decimal::from_str(item[4].as_str().unwrap()).unwrap() - total).to_string());
-            save_value[1] = time_list[0].to_string();
-            over_list.push(new_value.clone());
-            time_list.remove(0);
-        }
-    }
-    let mut over_value_list: Vec<Vec<String>> = vec![];
-    for (index, item) in over_list.iter().enumerate() {
-        if index == 0 {
-            over_value_list.push(vec![item[0].clone(), "0".to_string()]);
-            continue;
-        }
-        over_value_list.push(vec![item[0].clone(), (Decimal::from_str(&item[1]).unwrap() - Decimal::from_str(&over_list[index - 1][1]).unwrap()).to_string()])
     }
-    template_columnar::export_html(config_info, over_value_list);
 }

+ 12 - 3
src/export_template/template_columnar.rs

@@ -13,17 +13,26 @@ use crate::utils::utils::{ColumnarConfigInfo};
 
 pub fn export_html(config: ColumnarConfigInfo, data_list: Vec<Vec<String>>) {
     info!("正在生成网页,请稍后!");
+    let export_name = match config.export_mode {
+        1 => "export_balance_columnar".to_string(),
+        2 => "export_profit_columnar".to_string(),
+        _ => "导出模式错误".to_string()
+    };
     let export_path = if config.export_path == "" { "./" } else { config.export_path.as_str() };
-    let export_name = if config.export_name == "" { "export_columnar" } else { config.export_name.as_str() };
+    let export_name = if config.export_name == "" { export_name.as_str() } else { config.export_name.as_str() };
     let path = format!("{}/{}.html", export_path, export_name).replace("//", "/");
     // 创建 Handlebars 实例
     let mut handlebars = Handlebars::new();
 
     let x_values: Vec<String> = data_list.clone().iter().map(|item| item[0].clone()).collect();
     let y_values: Vec<String> = data_list.clone().iter().map(|item| item[1].clone()).collect();
-
+    let chart_title = match config.export_mode {
+        1 => "余额统计".to_string(),
+        2 => "盈利统计".to_string(),
+        _ => "导出模式错误".to_string()
+    };
     let data = serde_json::json!({
-        "chart_title": format!("盈利统计"),
+        "chart_title": chart_title,
         "x_values": x_values,
         "y_values": y_values,
     });

+ 2 - 0
src/utils/utils.rs

@@ -118,6 +118,7 @@ pub fn get_balance_config_info(file_path: &str) -> BalanceConfigInfo {
 pub struct ColumnarConfigInfo {
     pub is_export: bool,
     pub range_time: Vec<String>,
+    pub export_mode: i64,
     pub export_path: String,
     pub export_name: String,
 }
@@ -127,6 +128,7 @@ impl ColumnarConfigInfo {
         ColumnarConfigInfo {
             is_export: false,
             range_time: vec![],
+            export_mode: 0,
             export_path: "".to_string(),
             export_name: "".to_string(),
         }