gepangpang hace 1 año
padre
commit
597daa3da3
Se han modificado 2 ficheros con 114 adiciones y 84 borrados
  1. 112 84
      src/export_balance.rs
  2. 2 0
      src/utils/utils.rs

+ 112 - 84
src/export_balance.rs

@@ -11,10 +11,11 @@ pub async fn export_balance(config_info: BalanceConfigInfo) {
     let config_info_clone = config_info.clone();
     let start_time = NaiveDateTime::parse_from_str(&config_info.range_time[0].clone(), "%Y-%m-%d %H:%M:%S").unwrap().timestamp() - 8 * 3600;
     let end_time = NaiveDateTime::parse_from_str(&config_info.range_time[1].clone(), "%Y-%m-%d %H:%M:%S").unwrap().timestamp() - 8 * 3600;
+    let count_start_time = NaiveDateTime::parse_from_str(&config_info.count_range_time[0].clone(), "%Y-%m-%d %H:%M:%S").unwrap().timestamp() - 8 * 3600;
+    let count_end_time = NaiveDateTime::parse_from_str(&config_info.count_range_time[1].clone(), "%Y-%m-%d %H:%M:%S").unwrap().timestamp() - 8 * 3600;
 
     //获取不同账号的数据
     let mut acc_all_data: BTreeMap<String, Vec<Vec<String>>> = BTreeMap::new();
-    let mut data_array_all: Vec<Vec<String>> = vec![];
     let mut account_name_list: Vec<String> = vec![];
     loop {
         if config_info_clone.account_list.len() == 0 {
@@ -50,9 +51,7 @@ pub async fn export_balance(config_info: BalanceConfigInfo) {
 
                         let balance = item["balance"].as_str().unwrap();
                         let type_str = match item["type"].as_str().unwrap() {
-                            "dnw" => {
-                                "转入转出"
-                            }
+                            "dnw" => { "转入转出" }
                             "pnl" => { "减仓盈亏" }
                             "fee" => {
                                 "交易手续费";
@@ -110,6 +109,25 @@ pub async fn export_balance(config_info: BalanceConfigInfo) {
         break;//这里是为了 代码收纳,用了loop来放置代码
     }
 
+    // 根据账户变动填充
+    let mut count_time_list = vec![];
+    for (_account_name, account_info) in acc_all_data.clone() {
+        for value in account_info {
+            let time = value[0].clone().parse::<i64>().unwrap();
+            if !count_time_list.contains(&time) && time > count_start_time && time < count_end_time {
+                count_time_list.push(time)
+            }
+        }
+    };
+    count_time_list.sort_by(|a, b| a.cmp(&b));
+    let mut bbbbbb = supply_balance(acc_all_data.clone(), count_time_list.clone());
+
+    let aaaaa: Vec<Vec<String>> = bbbbbb.clone().into_iter().filter(|value| {
+        let t = value[1].clone().parse::<i64>().unwrap();
+        t >= start_time && t <= end_time
+    }).collect();
+    bbbbbb = aaaaa;
+
     // 按小时取时间片
     let mut last_timestamp: i64 = -1;
     let mut export_time_list: Vec<i64> = vec![];
@@ -121,81 +139,98 @@ pub async fn export_balance(config_info: BalanceConfigInfo) {
     }
     export_time_list.push(end_time);
 
+    let mut data_array_all: Vec<Vec<String>> = supply_balance(acc_all_data.clone(), export_time_list.clone());
     //2. 数据根据时间片 补全数据
-    for time in &export_time_list {
+    let data_array_all_array: Vec<Vec<String>> = data_array_all.clone().into_iter().filter(|value| {
+        let t = value[1].clone().parse::<i64>().unwrap();
+        t >= start_time && t <= end_time
+    }).collect();
+    data_array_all = data_array_all_array;
+
+    //生成 html
+    account_name_list.push("sum_acc".to_string());
+    export_template::template_balance::export_html(config_info_clone.clone(), &account_name_list, &count_time_list, &bbbbbb);
+}
+
+pub fn supply_balance(source_balance_info: BTreeMap<String, Vec<Vec<String>>>, time_slicer: Vec<i64>) -> Vec<Vec<String>> {
+    let mut balance_info_list: Vec<Vec<String>> = vec![];
+    for time in time_slicer.clone() {
         // 根据时间片去拿数据 ,如果有添加没有,手动添加
-        let mut sum_pr = Decimal::ZERO;
-        for (key, vale) in acc_all_data.clone() {
-            let mut value = vale.clone();
-            value.sort_by(|a, b| a[0].cmp(&b[0]));
-            let mut da: Option<Vec<String>> = None;
-            for v in value.clone() {
-                let acc_time = v[0].parse::<i64>().unwrap();
-                if acc_time == time.clone() {
-                    //时间相同,如果记录的 时间片不存在,则直接保存,如果存在则比较成交id,
-                    if da.is_none() {
-                        da = Option::from(v);//记录当前实际
-                    } else {
-                        let da_id = da.clone().unwrap()[1].clone();
-                        let v_id = v[1].clone();
-                        if da_id < v_id {
-                            da = Option::from(v);//记录当前实际
-                        }
-                    }
+        let mut total_price = Decimal::ZERO;
+        for (key, value) in source_balance_info.clone() {
+            let mut balance_info = value.clone();
+            balance_info.sort_by(|a, b| a[0].cmp(&b[0]));
+
+            let mut new_info: Option<Vec<String>> = None;
+            for mut info in balance_info.clone() {
+                let info_time = info[0].parse::<i64>().unwrap();
+                if info_time == time {
+                    info.insert(0, key.clone());
+                    new_info = Option::from(info.clone());
+                    break;
                 }
             }
 
-            //如果匹配到时间片,直接使用,没有则创建
-            let new_d: Vec<String> = match da {
+            match new_info {
                 None => {
-                    let mut row: Vec<String> = vec![];
-                    let filter_arrya: Vec<Vec<String>> = value.clone().into_iter().filter(|s| {
-                        // info!("读取time:{:?}",s);
-                        let time_v = s[0].clone().parse::<i64>().unwrap();
-                        time_v < time.clone()
-                    }).collect();
-                    // info!("计算filter_arrya:---{:?}",filter_arrya.clone());
-                    let acc_name = key.clone().to_string();
-                    let acc_time = time.to_string();
-                    let acc_o_id = "填补数据类型".to_string();
-                    let mut acc_change = "0".to_string();
-                    let mut acc_balance = "0".to_string();
-                    let acc_type = "填补数据类型".to_string();
-                    let mut acc_contract = "填补数据类型".to_string();
-
-                    if filter_arrya.len() > 0 {
-                        // info!("{:?}--{}--{:?}",acc_name, time, filter_arrya[filter_arrya.len() - 1].clone());
-                        let zj_v = filter_arrya[filter_arrya.len() - 1].clone();
-
-                        acc_change = zj_v[2].clone();
-                        acc_balance = zj_v[3].clone();
-                        acc_contract = zj_v[5].clone();
-                    }
-
-                    let acc_balance_round = Decimal::from_str(&acc_balance).unwrap().round_dp(2);
-
-                    row.push(acc_name);
-                    row.push(acc_time);
-                    row.push(acc_o_id);
-                    row.push(acc_change);
-                    row.push(acc_balance_round.to_string());
-                    row.push(acc_type);
-                    row.push(acc_contract);
-                    row
+                    let balance_info_filter: Vec<Vec<String>> = balance_info.iter().filter(|item| {
+                        item[0].parse::<i64>().unwrap() < time
+                    }).cloned().collect();
+                    let mut last_balance_info = balance_info_filter[balance_info_filter.len() - 1].clone();
+                    last_balance_info.insert(0, key.clone());
+                    total_price += Decimal::from_str(&last_balance_info[4]).unwrap();
+                    balance_info_list.push(last_balance_info);
                 }
-                Some(d) => {
-                    let mut row: Vec<String> = vec![];
-                    row.push(key.clone());
-                    row.extend(d.clone());
-                    row
+                Some(info) => {
                 }
-            };
-            // let time_str = NaiveDateTime::from_timestamp_millis((new_d[1].clone().parse::<i64>().unwrap() + 8 * 3600) * 1000).unwrap().format("%Y-%m-%d %H:%M:%S%.3f").to_string();
-            // info!("{:?}----时间片:{:?}",new_d[0].clone(),time_str);
-            // info!("计算3333:---{:?}",time_str);
-            // info!("{:?}=----pr:{:?}",key.clone().to_string(),new_d[4].clone());
-            sum_pr = sum_pr + Decimal::from_str(&new_d[4]).unwrap();
-            data_array_all.push(new_d);
+            }
+            // //如果匹配到时间片,直接使用,没有则创建
+            // let new_d: Vec<String> = match da {
+            //     None => {
+            //         let mut row: Vec<String> = vec![];
+            //         let filter_arrya: Vec<Vec<String>> = value.clone().into_iter().filter(|s| {
+            //             // info!("读取time:{:?}",s);
+            //             let time_v = s[0].clone().parse::<i64>().unwrap();
+            //             time_v < time.clone()
+            //         }).collect();
+            //         // info!("计算filter_arrya:---{:?}",filter_arrya.clone());
+            //         let acc_name = key.clone().to_string();
+            //         let acc_time = time.to_string();
+            //         let acc_o_id = "填补数据类型".to_string();
+            //         let mut acc_change = "0".to_string();
+            //         let mut acc_balance = "0".to_string();
+            //         let acc_type = "填补数据类型".to_string();
+            //         let mut acc_contract = "填补数据类型".to_string();
+            //
+            //         if filter_arrya.len() > 0 {
+            //             // info!("{:?}--{}--{:?}",acc_name, time, filter_arrya[filter_arrya.len() - 1].clone());
+            //             let zj_v = filter_arrya[filter_arrya.len() - 1].clone();
+            //
+            //             acc_change = zj_v[2].clone();
+            //             acc_balance = zj_v[3].clone();
+            //             acc_contract = zj_v[5].clone();
+            //         }
+            //
+            //         let acc_balance_round = Decimal::from_str(&acc_balance).unwrap().round_dp(2);
+            //
+            //         row.push(acc_name);
+            //         row.push(acc_time);
+            //         row.push(acc_o_id);
+            //         row.push(acc_change);
+            //         row.push(acc_balance_round.to_string());
+            //         row.push(acc_type);
+            //         row.push(acc_contract);
+            //         row
+            //     }
+            //     Some(d) => {
+            //         let mut row: Vec<String> = vec![];
+            //         row.push(key.clone());
+            //         row.extend(d.clone());
+            //         row
+            //     }
+            // };
+            // total_price = total_price + Decimal::from_str(&new_d[4]).unwrap();
+            // data_array_all.push(new_d);
         }
 
         let mut sum_row: Vec<String> = vec![];
@@ -203,19 +238,12 @@ pub async fn export_balance(config_info: BalanceConfigInfo) {
         sum_row.push(time.clone().to_string());
         sum_row.push("".to_string());
         sum_row.push("0".to_string());
-        sum_row.push(sum_pr.to_string());
+        sum_row.push(total_price.to_string());
         sum_row.push("".to_string());
         sum_row.push("".to_string());
-        data_array_all.push(sum_row.clone());
-    }
-
-    let data_array_all_array: Vec<Vec<String>> = data_array_all.clone().into_iter().filter(|value| {
-        let t = value[1].clone().parse::<i64>().unwrap();
-        t >= start_time && t <= end_time
-    }).collect();
-    data_array_all = data_array_all_array;
-
-    //生成 html
-    account_name_list.push("sum_acc".to_string());
-    export_template::template_balance::export_html(config_info_clone.clone(), &account_name_list, &export_time_list, &data_array_all);
+        sum_row.push("".to_string());
+        balance_info_list.push(sum_row.clone());
+    };
+    println!("------------------{:?}", balance_info_list);
+    balance_info_list
 }

+ 2 - 0
src/utils/utils.rs

@@ -71,6 +71,7 @@ pub struct BalanceConfigInfo {
     pub account_list: Vec<Vec<String>>,
     pub exchanges: Vec<String>,
     pub range_time: Vec<String>,
+    pub count_range_time: Vec<String>,
     pub export_path: String,
     pub export_name: String,
 }
@@ -83,6 +84,7 @@ impl BalanceConfigInfo {
             account_list: vec![],
             exchanges: vec![],
             range_time: vec![],
+            count_range_time: vec![],
             export_path: "".to_string(),
             export_name: "".to_string(),
         }