var system=require("../../system")
var settings=require("../../../config/settings");
const CtlBase = require("../ctl.base");
const uuidv4 = require('uuid/v4');
class FiledownloadCtl extends CtlBase{
   constructor(){
     super(CtlBase.getServiceName(FiledownloadCtl));
     this.excelClient = system.getObject("util.excelClient");
     this.utilstmSve=system.getObject("service.utilstmSve");
   }

   async download(queryobj,qobj,req){
     let id = qobj.code;
     var rows = qobj.rows || [];

     var code = uuidv4();
     var fileName = "data_" + code + ".xlsx";

     console.log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
     // console.log(rows);
     var biaoti = rows[0];
     var index = biaoti.length-1;
     if(biaoti.indexOf("商品/服务项")>0){
       for (var i = 1; i < rows.length; i++) {
         console.log(rows[i]);
         var tm= rows[i];
         var regist_num=tm[1];
         var ncl_one_codesstr=tm[2];
         var ncl_one_codes=ncl_one_codesstr.split("类 ")[0];
         var sbdata={
           reg_num:regist_num,
           nclone_code:ncl_one_codes
         };
         console.log(sbdata);
         var qunzutms=await this.utilstmSve.getGroupNclInfo(sbdata);
         var spfwxmlist=[];
         if(qunzutms.status==0){
           qunzutms.data.exist.forEach(function(c){
             if(spfwxmlist.findIndex(f => f == c.small_name) < 0){
               spfwxmlist.push(c.small_name)
             }
           });
         }
         var spfwxm=spfwxmlist.join(",");
         if(spfwxm!=""){
           rows[i][index]=spfwxm;
           console.log(rows[i][index]);
         }
         console.log(spfwxm);
       }
     }
     console.log("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
     console.log(rows);
     var param = {
       title: "商标查询结果列表",
       code : code,
       fileName : fileName,
       user : {},
       rows : rows
     }
     console.log("pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp");
     console.log(param);
     this.excelClient.download(param);
     return system.getResult2(code);
   }
   async bycode(queryobj,qobj,req) {
     var f = await this.service.findOne({code:(qobj.code || "")});
     return system.getResult2(f);
   }
}
module.exports=FiledownloadCtl;
