use std::path::{PathBuf};
use br_addon::action::Action;
use br_addon::{ApiResponse, Plugin};
use br_addon::request::Request;
use json::{JsonValue, object};
use br_excel::Head;
use br_fields::Field;
use crate::addon::{{plugin}}::{{model_a}}::{{model}};
use crate::addon::Plugins;



pub struct {{action}} {
    pub module: {{model}},
}

impl Action for {{action}} {
    fn title(&self) -> &'static str { "{{title}}" }
    fn params(&mut self) -> JsonValue {
        let mut params = object! {};
        params["file"] = br_fields::files::Files::new(true, "file", "文件").option(vec!["xlsx"]).field();
        return params;
    }
    fn index(&mut self,request: Request) -> ApiResponse {
        let file_url = Plugins::api_run("file.file.get_path", request.clone()).unwrap();

               if !PathBuf::from(file_url.as_str().unwrap()).is_file() {
                   return ApiResponse::fail(800002, "文件不存在");
               }
               let heads = vec![
                   Head::new("sn", "入库单号", "", 0),
                   Head::new("order_sn", "业务单号", "", 0),
                   Head::new("name", "名称", "", 0),
                   Head::new("barcode", "条码", "", 0),
                   Head::new("code", "货号", "", 0),
                   Head::new("number", "数量", "", 0),
               ];
               let data = br_excel::read::Read::export(file_url.as_str().unwrap().clone(), 0, 1, heads);
               let mut data = data.members().map(|x| x.clone()).collect::<Vec<JsonValue>>();
               data.retain(|item| {
                   if item["order_sn"].is_empty() {
                       false
                   } else {
                       true
                   }
               });
               /*业务处理*/
               ApiResponse::success(JsonValue::from(data), "导入订单成功")
    }
}