Browse Source

修改导出余额柱状图统计

gepangpang 1 year ago
parent
commit
058f164e82
2 changed files with 32 additions and 12 deletions
  1. 29 9
      src/export_columnar.rs
  2. 3 3
      src/export_template/template_columnar.rs

+ 29 - 9
src/export_columnar.rs

@@ -1,4 +1,7 @@
+use std::str::FromStr;
 use chrono::NaiveDateTime;
+use rust_decimal::Decimal;
+use rust_decimal_macros::dec;
 use crate::export_template::template_columnar;
 use crate::utils::utils;
 use crate::utils::utils::{ColumnarConfigInfo};
@@ -19,21 +22,38 @@ pub async fn export_columnar(config_info: ColumnarConfigInfo) {
     let mut time_list = time_slicer.clone();
     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();
+        }
+        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 {
-            save_value[1] = time_list[0].to_string();
-            over_list.push(save_value.clone());
+            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 {
-            if time <= time_list[0] {
-                save_value[1] = time_list[0].to_string();
-                over_list.push(save_value.clone());
-                time_list.remove(0);
-            }
+        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_list);
+    template_columnar::export_html(config_info, over_value_list);
 }

+ 3 - 3
src/export_template/template_columnar.rs

@@ -19,11 +19,11 @@ pub fn export_html(config: ColumnarConfigInfo, data_list: Vec<Vec<String>>) {
     // 创建 Handlebars 实例
     let mut handlebars = Handlebars::new();
 
-    let x_values: Vec<String> = data_list.clone().iter().map(|item| item[1].clone()).collect();
-    let y_values: Vec<String> = data_list.clone().iter().map(|item| item[4].clone()).collect();
+    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 data = serde_json::json!({
-        "chart_title": format!("余额统计"),
+        "chart_title": format!("盈利统计"),
         "x_values": x_values,
         "y_values": y_values,
     });