var System=require("../../system")
var settings=require("../../../config/settings");
const uuidv4 = require('uuid/v4');
class PatentycCtl{
   constructor(){
     this.GsbByChinaPatentSearchApi=System.getObject("api.chinapatentsearch");
     this.GsbByWorldPatentSearchApi=System.getObject("api.wordpatentsearch");
     this.GsbByChinaAffairSearchApi=System.getObject("api.chinaaffairsearch");
   }
   async advancedsearch(pobj,obj){
     console.log("--------------advancedsearch---------------");
     console.log(obj);
     var datalist=[];
     var patent1=[];
     var patent2=[];
     var total=0;
     var total1=0;
     var total2=0;
     var range1=obj.range.china.join();
     var database="";
     if(range1.indexOf("香港")>=0){
       if(database){
         database=database+"+"+"HKPATENT";
       }else{
         database=database+"HKPATENT";
       }
     }
     if(range1.indexOf("台湾")>=0){
       if(database){
         database=database+"+"+"TWZL";
       }else{
         database=database+"TWZL";
       }
     }
     if(range1.indexOf("澳门")>=0){
       if(database){
         database=database+"+"+"MACZL";
       }else{
         database=database+"MACZL";
       }
     }
     for(var i=0;i<obj.range.main.length;i++){
       if(obj.range.main[i]=="EPO"){
         if(database){
           database=database+"+"+"EPPATENT";
         }else{
           database=database+"EPPATENT";
         }
       }
       if(obj.range.main[i]=="WIPO"){
         if(database){
           database=database+"+"+"WOPATENT";
         }else{
           database=database+"WOPATENT";
         }
       }
       if(obj.range.main[i]=="美国"){
         if(database){
           database=database+"+"+"USPATENT";
         }else{
           database=database+"USPATENT";
         }
       }
       if(obj.range.main[i]=="日本"){
         if(database){
           database=database+"+"+"JPPATENT";
         }else{
           database=database+"JPPATENT";
         }
       }
       if(obj.range.main[i]=="韩国"){
         if(database){
           database=database+"+"+"KRPATENT";
         }else{
           database=database+"KRPATENT";
         }
       }
       if(obj.range.main[i]=="英国"){
         if(database){
           database=database+"+"+"GBPATENT";
         }else{
           database=database+"GBPATENT";
         }
       }
       if(obj.range.main[i]=="法国"){
         if(database){
           database=database+"+"+"FRPATENT";
         }else{
           database=database+"FRPATENT";
         }
       }
       if(obj.range.main[i]=="德国"){
         if(database){
           database=database+"+"+"DEPATENT";
         }else{
           database=database+"DEPATENT";
         }
       }
       if(obj.range.main[i]=="俄罗斯"){
         if(database){
           database=database+"+"+"RUPATENT";
         }else{
           database=database+"RUPATENT";
         }
       }
       if(obj.range.main[i]=="瑞士"){
         if(database){
           database=database+"+"+"CHPATENT";
         }else{
           database=database+"CHPATENT";
         }
       }
     }
     for(var i=0;i<obj.range.other.length;i++){
       if(obj.range.other[i]=="奥地利"){
         if(database){
           database=database+"+"+"ATPATENT";
         }else{
           database=database+"ATPATENT";
         }
       }
       if(obj.range.other[i]=="澳大利亚"){
         if(database){
           database=database+"+"+"AUPATENT";
         }else{
           database=database+"AUPATENT";
         }
       }
       if(obj.range.other[i]=="西班牙"){
         if(database){
           database=database+"+"+"ESPATENT";
         }else{
           database=database+"ESPATENT";
         }
       }
       if(obj.range.other[i]=="意大利"){
         if(database){
           database=database+"+"+"ITPATENT";
         }else{
           database=database+"ITPATENT";
         }
       }
       if(obj.range.other[i]=="加拿大"){
         if(database){
           database=database+"+"+"CAPATENT";
         }else{
           database=database+"CAPATENT";
         }
       }
       if(obj.range.other[i]=="瑞典"){
         if(database){
           database=database+"+"+"SEPATENT";
         }else{
           database=database+"SEPATENT";
         }
       }
     }
     if(range1.indexOf("中国大陆")>=0&&database.length!=0){
       if(obj.status==""&&obj.patenttype==""){
         var size=obj.pagesize/2;
       }else{
         var size=obj.pagesize;
       }
     }else{
       var size=obj.pagesize;
     }
     if(range1.indexOf("中国大陆")!=-1){
       console.log("-----------------中国大陆--------");
       var data1={
         appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
         pagesize:size,
         page:obj.currentpage,
         title:obj.patentname,
         abstr:obj.abstract,
         filingno:obj.applynum,
         pubno:obj.publishnum,
         priorno:obj.priornum,
         applname:obj.applier,
         invname:obj.inventor,
         ipcno:obj.ipc,
         filingdate1:obj.applydate1,
         filingdate2:obj.applydate2,
         pubdate1:obj.publishdate1,
         pubdate2:obj.publishdate2,
         grdate1:obj.grantdate1,
         grdate2:obj.grantdate2
       };
       var patentinfo1=await this.GsbByChinaPatentSearchApi.seniorSearch(data1);
     }
     if(range1.indexOf("香港")!=-1||range1.indexOf("澳门")!=-1||range1.indexOf("台湾")!=-1
     ||obj.range.main.length!=0||obj.range.other.length!=0){
       console.log("-----------------国外--------");
       var data2={
         appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
         pagesize:size,
         page:obj.currentpage,
         danames:database,
         title:obj.patentname,
         abstr:obj.abstract,
         filingno:obj.applynum,
         pubno:obj.publishnum,
         priorno:obj.priornum,
         applname:obj.applier,
         invname:obj.inventor,
         ipcno:obj.ipc,
         filingdate1:obj.applydate1,
         filingdate2:obj.applydate2,
         pubdate1:obj.publishdate1,
         pubdate2:obj.publishdate2,
         grdate1:obj.grantdate1,
         grdate2:obj.grantdate2
       };
       var patentinfo2=await this.GsbByWorldPatentSearchApi.seniorSearch(data2);
     }
     if(patentinfo1){
       if(patentinfo1.status==0){
         console.log("----------patentdata1-------------");
         console.log(patentinfo1);
         total1=patentinfo1.data.total;
         var patentdata1=patentinfo1.data.hits;
         for(var i=0;i<patentinfo1.data.hits.length;i++){
           var publish_num="";
           var publish_date="";
           if(patentinfo1.data.hits[i]._source.pub_type=="发明"){
             publish_num=patentinfo1.data.hits[i]._source.pub_no;
             publish_date=patentinfo1.data.hits[i]._source.pub_time;
           }else{
             publish_num=patentinfo1.data.hits[i]._source.gr_no;
             publish_date=patentinfo1.data.hits[i]._source.gr_time;
           }
           var applierinfo="";
           if(patentinfo1.data.hits[i]._source.applicant_name!=null){
             for(var j=0;j<patentinfo1.data.hits[i]._source.applicant_name.length;j++){
               if(applierinfo){
                 applierinfo=applierinfo+","+patentinfo1.data.hits[i]._source.applicant_name[j];
               }else{
                 applierinfo=applierinfo+patentinfo1.data.hits[i]._source.applicant_name[j];
               }
             }
           }
           var inventorinfo="";
           if(patentinfo1.data.hits[i]._source.inventor_name!=null){
             for(var j=0;j<patentinfo1.data.hits[i]._source.inventor_name.length;j++){
               if(inventorinfo){
                 inventorinfo=inventorinfo+","+patentinfo1.data.hits[i]._source.inventor_name[j];
               }else{
                 inventorinfo=inventorinfo+patentinfo1.data.hits[i]._source.inventor_name[j];
               }
             }
           }
           var item={
             applynum:patentinfo1.data.hits[i]._source.filing_no,
             publishnum:publish_num,
             patentname:patentinfo1.data.hits[i]._source.filing_name,
             applier:applierinfo,
             inventor:inventorinfo,
             applydate:patentinfo1.data.hits[i]._source.filing_time,
             publishdate:publish_date,
             articletype:patentinfo1.data.hits[i]._source.pub_type
           }
           patent1.push(item);
         }
       }
     }
     if(patentinfo2){
       if(patentinfo2.status==0){
         console.log("----------patentdata2-------------");
         console.log(patentinfo2);
         total2=patentinfo2.data.total;
         var patentdata2=patentinfo2.data.hits;
         for(var i=0;i<patentinfo2.data.hits.length;i++){
           var applierinfo="";
           if(patentinfo2.data.hits[i]._source.applicant_name!=null){
             for(var j=0;j<patentinfo2.data.hits[i]._source.applicant_name.length;j++){
               if(applierinfo){
                 applierinfo=applierinfo+","+patentinfo2.data.hits[i]._source.applicant_name[j];
               }else{
                 applierinfo=applierinfo+patentinfo2.data.hits[i]._source.applicant_name[j];
               }
             }
           }
           var inventorinfo="";
           if(patentinfo2.data.hits[i]._source.inventor_name!=null){
             for(var j=0;j<patentinfo2.data.hits[i]._source.inventor_name.length;j++){
               if(inventorinfo){
                 inventorinfo=inventorinfo+","+patentinfo2.data.hits[i]._source.inventor_name[j];
               }else{
                 inventorinfo=inventorinfo+patentinfo2.data.hits[i]._source.inventor_name[j];
               }
             }
           }
           var item={
             applynum:patentinfo2.data.hits[i]._source.app_number,
             publishnum:patentinfo2.data.hits[i]._source.pub_number,
             patentname:patentinfo2.data.hits[i]._source.title_name,
             applier:applierinfo,
             inventor:inventorinfo,
             applydate:patentinfo2.data.hits[i]._source.app_date,
             publishdate:patentinfo2.data.hits[i]._source.pub_date,
             articletype:patentinfo2.data.hits[i]._source.pub_type
           }
           patent2.push(item);
         }
       }
     }
     total=total1+total2;
     for(var i=0;i<patent1.length;i++){
       datalist.push(patent1[i]);
     }
     for(var i=0;i<patent2.length;i++){
       datalist.push(patent2[i]);
     }
     var result={
       total:total,
       dataList:datalist,
     }
     console.log("----------result--------------");
     console.log(result);
     return System.getResult2(result,null);
   };
   async simplesearch(pobj,obj){
     console.log("--------------obtainpatentinfo---------------");
     console.log(obj);
     var datalist1={patenttype:[],status:[],applydate:[],publishdate:[]};
     var datalist2=[];
     var juhe1={patenttype:[],status:[],applydate:[],publishdate:[]};
     var patent1=[];
     var juhe2={patenttype:[],status:[],applydate:[],publishdate:[]};
     var patent2=[];
     var range1=obj.range.china.join();
     var database="";
     var total=0;
     var total1=0;
     var total2=0;
     if(range1.indexOf("香港")>=0){
       if(database){
         database=database+"+"+"HKPATENT";
       }else{
         database=database+"HKPATENT";
       }
     }
     if(range1.indexOf("台湾")>=0){
       if(database){
         database=database+"+"+"TWZL";
       }else{
         database=database+"TWZL";
       }
     }
     if(range1.indexOf("澳门")>=0){
       if(database){
         database=database+"+"+"MACZL";
       }else{
         database=database+"MACZL";
       }
     }
     for(var i=0;i<obj.range.main.length;i++){
       if(obj.range.main[i]=="EPO"){
         if(database){
           database=database+"+"+"EPPATENT";
         }else{
           database=database+"EPPATENT";
         }
       }
       if(obj.range.main[i]=="WIPO"){
         if(database){
           database=database+"+"+"WOPATENT";
         }else{
           database=database+"WOPATENT";
         }
       }
       if(obj.range.main[i]=="美国"){
         if(database){
           database=database+"+"+"USPATENT";
         }else{
           database=database+"USPATENT";
         }
       }
       if(obj.range.main[i]=="日本"){
         if(database){
           database=database+"+"+"JPPATENT";
         }else{
           database=database+"JPPATENT";
         }
       }
       if(obj.range.main[i]=="韩国"){
         if(database){
           database=database+"+"+"KRPATENT";
         }else{
           database=database+"KRPATENT";
         }
       }
       if(obj.range.main[i]=="英国"){
         if(database){
           database=database+"+"+"GBPATENT";
         }else{
           database=database+"GBPATENT";
         }
       }
       if(obj.range.main[i]=="法国"){
         if(database){
           database=database+"+"+"FRPATENT";
         }else{
           database=database+"FRPATENT";
         }
       }
       if(obj.range.main[i]=="德国"){
         if(database){
           database=database+"+"+"DEPATENT";
         }else{
           database=database+"DEPATENT";
         }
       }
       if(obj.range.main[i]=="俄罗斯"){
         if(database){
           database=database+"+"+"RUPATENT";
         }else{
           database=database+"RUPATENT";
         }
       }
       if(obj.range.main[i]=="瑞士"){
         if(database){
           database=database+"+"+"CHPATENT";
         }else{
           database=database+"CHPATENT";
         }
       }
     }
     for(var i=0;i<obj.range.other.length;i++){
       if(obj.range.other[i]=="奥地利"){
         if(database){
           database=database+"+"+"ATPATENT";
         }else{
           database=database+"ATPATENT";
         }
       }
       if(obj.range.other[i]=="澳大利亚"){
         if(database){
           database=database+"+"+"AUPATENT";
         }else{
           database=database+"AUPATENT";
         }
       }
       if(obj.range.other[i]=="西班牙"){
         if(database){
           database=database+"+"+"ESPATENT";
         }else{
           database=database+"ESPATENT";
         }
       }
       if(obj.range.other[i]=="意大利"){
         if(database){
           database=database+"+"+"ITPATENT";
         }else{
           database=database+"ITPATENT";
         }
       }
       if(obj.range.other[i]=="加拿大"){
         if(database){
           database=database+"+"+"CAPATENT";
         }else{
           database=database+"CAPATENT";
         }
       }
       if(obj.range.other[i]=="瑞典"){
         if(database){
           database=database+"+"+"SEPATENT";
         }else{
           database=database+"SEPATENT";
         }
       }
     }
     if(range1.indexOf("中国大陆")>=0&&database.length!=0){
       if(obj.status==""&&obj.patenttype==""){
         var size=obj.pagesize/2;
       }else{
         var size=obj.pagesize;
       }
     }else{
       var size=obj.pagesize;
     }
     if(obj.lable=="patentname"){
       if(range1.indexOf("中国大陆")!=-1){
         console.log("---------patentname---------中国大陆--------");
         var data1={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           title:obj.content,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         console.log("----------data1------------");
         console.log(data1);
         var patentinfo1=await this.GsbByChinaPatentSearchApi.CommomSearchbyTitle(data1);
       }
       if(range1.indexOf("香港")!=-1||range1.indexOf("澳门")!=-1||range1.indexOf("台湾")!=-1
       ||obj.range.main.length!=0||obj.range.other.length!=0){
         console.log("---------patentname---------国外--------");
         var data2={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           title:obj.content,
           danames:database,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         console.log("------------data2--------------");
         console.log(data2);
         var patentinfo2=await this.GsbByWorldPatentSearchApi.CommomSearchbyTitle(data2);
       }

     }
     if(obj.lable=="applynum"){
       if(range1.indexOf("中国大陆")!=-1){
         console.log("---------applynum---------中国大陆--------");
         var data1={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           filingno:obj.content,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo1=await this.GsbByChinaPatentSearchApi.CommomSearchbyFilingno(data1);
       }
       if(range1.indexOf("香港")!=-1||range1.indexOf("澳门")!=-1||range1.indexOf("台湾")!=-1
       ||obj.range.main.length!=0||obj.range.other.length!=0){
         console.log("---------applynum---------国外--------");
         var data2={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           filingno:obj.content,
           danames:database,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo2=await this.GsbByWorldPatentSearchApi.CommomSearchbyFilingno(data2);
       }
     }
     if(obj.lable=="publishnum"){
       if(range1.indexOf("中国大陆")!=-1){
         console.log("---------publishnum---------中国大陆--------");
         var data1={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           pubno:obj.content,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo1=await this.GsbByChinaPatentSearchApi.CommomSearchbyPubno(data1);
       }
       if(range1.indexOf("香港")!=-1||range1.indexOf("澳门")!=-1||range1.indexOf("台湾")!=-1
       ||obj.range.main.length!=0||obj.range.other.length!=0){
         console.log("---------publishnum---------国外--------");
         var data2={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           pubno:obj.content,
           danames:database,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo2=await this.GsbByWorldPatentSearchApi.CommomSearchbyPubno(data2);
       }
     }
     if(obj.lable=="applier"){
       if(range1.indexOf("中国大陆")!=-1){
         console.log("---------applier---------中国大陆--------");
         var data1={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           applicant:obj.content,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo1=await this.GsbByChinaPatentSearchApi.CommomSearchbyApplicant(data1);
       }
       if(range1.indexOf("香港")!=-1||range1.indexOf("澳门")!=-1||range1.indexOf("台湾")!=-1
       ||obj.range.main.length!=0||obj.range.other.length!=0){
         console.log("---------applier---------国外--------");
         var data2={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           applicant:obj.content,
           danames:database,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo2=await this.GsbByWorldPatentSearchApi.CommomSearchbyApplicant(data2);
       }
     }
     if(obj.lable=="inventor"){
       if(range1.indexOf("中国大陆")!=-1){
         console.log("---------inventor---------中国大陆--------");
         var data1={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           inventor:obj.content,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo1=await this.GsbByChinaPatentSearchApi.CommomSearchbyInventor(data1);
       }
       if(range1.indexOf("香港")!=-1||range1.indexOf("澳门")!=-1||range1.indexOf("台湾")!=-1
       ||obj.range.main.length!=0||obj.range.other.length!=0){
         console.log("---------inventor---------国外--------");
         var data2={
           appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
           pagesize:size,
           page:obj.currentpage,
           inventor:obj.content,
           danames:database,
           pubtype:obj.patenttype,
           pubstatus:obj.status,
           filingyear:obj.applydate,
           pubyear:obj.publishdate
         };
         var patentinfo2=await this.GsbByWorldPatentSearchApi.CommomSearchbyInventor(data2);
       }
     }
     if(patentinfo1){
       if(patentinfo1.status==0){
         console.log("----------patentdata1-------------");
         console.log(patentinfo1);
         total1=patentinfo1.data.total;
         var juhedata1=patentinfo1.buckets;
         for(var i=0;i<patentinfo1.buckets.group_by_pub_type.buckets.length;i++){
           if(patentinfo1.buckets.group_by_pub_type.buckets[i].key=="发明"){
             var item={
               code:"发明",
               lable:"发明申请",
               num:patentinfo1.buckets.group_by_pub_type.buckets[i].doc_count
             };
             juhe1.patenttype.push(item);
           }
           if(patentinfo1.buckets.group_by_pub_type.buckets[i].key=="实用新型"){
             var item={
               code:"实用新型",
               lable:"实用新型",
               num:patentinfo1.buckets.group_by_pub_type.buckets[i].doc_count
             };
             juhe1.patenttype.push(item);
           }
           if(patentinfo1.buckets.group_by_pub_type.buckets[i].key=="外观设计"){
             var item={
               code:"外观设计",
               lable:"外观设计",
               num:patentinfo1.buckets.group_by_pub_type.buckets[i].doc_count
             };
             juhe1.patenttype.push(item);
           }
         }
         for(var i=0;i<patentinfo1.buckets.group_by_pub_status.buckets.length;i++){
           if(patentinfo1.buckets.group_by_pub_status.buckets[i].key=="有效"){
             var item={
               code:"有效",
               lable:"有效",
               num:patentinfo1.buckets.group_by_pub_status.buckets[i].doc_count
             };
             juhe1.status.push(item);
           }
           if(patentinfo1.buckets.group_by_pub_status.buckets[i].key=="在审"){
             var item={
               code:"在审",
               lable:"在审",
               num:patentinfo1.buckets.group_by_pub_status.buckets[i].doc_count
             };
             juhe1.status.push(item);
           }
           if(patentinfo1.buckets.group_by_pub_status.buckets[i].key=="无效"){
             var item={
               code:"无效",
               lable:"无效",
               num:patentinfo1.buckets.group_by_pub_status.buckets[i].doc_count
             };
             juhe1.status.push(item);
           }
         }
         for(var i=0;i<patentinfo1.buckets.group_by_filing_year.buckets.length;i++){
           var item={
             code:patentinfo1.buckets.group_by_filing_year.buckets[i].key,
             lable:patentinfo1.buckets.group_by_filing_year.buckets[i].key+"年",
             num:patentinfo1.buckets.group_by_filing_year.buckets[i].doc_count
           };
           juhe1.applydate.push(item);
         }
         for(var i=0;i<patentinfo1.buckets.group_by_pub_year.buckets.length;i++){
           var item={
             code:patentinfo1.buckets.group_by_pub_year.buckets[i].key,
             lable:patentinfo1.buckets.group_by_pub_year.buckets[i].key+"年",
             num:patentinfo1.buckets.group_by_pub_year.buckets[i].doc_count
           };
           juhe1.publishdate.push(item);
         }

         var patentdata1=patentinfo1.data.hits;
         for(var i=0;i<patentinfo1.data.hits.length;i++){
           var publish_num="";
           var publish_date="";
           if(patentinfo1.data.hits[i]._source.pub_type=="发明"){
             publish_num=patentinfo1.data.hits[i]._source.pub_no;
             publish_date=patentinfo1.data.hits[i]._source.pub_time;
           }else{
             publish_num=patentinfo1.data.hits[i]._source.gr_no;
             publish_date=patentinfo1.data.hits[i]._source.gr_time;
           }
           var applierinfo="";
           if(patentinfo1.data.hits[i]._source.applicant_name!=null){
             for(var j=0;j<patentinfo1.data.hits[i]._source.applicant_name.length;j++){
               if(applierinfo){
                 applierinfo=applierinfo+","+patentinfo1.data.hits[i]._source.applicant_name[j];
               }else{
                 applierinfo=applierinfo+patentinfo1.data.hits[i]._source.applicant_name[j];
               }
             }
           }
           var inventorinfo="";
           if(patentinfo1.data.hits[i]._source.inventor_name!=null){
             for(var j=0;j<patentinfo1.data.hits[i]._source.inventor_name.length;j++){
               if(inventorinfo){
                 inventorinfo=inventorinfo+","+patentinfo1.data.hits[i]._source.inventor_name[j];
               }else{
                 inventorinfo=inventorinfo+patentinfo1.data.hits[i]._source.inventor_name[j];
               }
             }
           }
           var item={
             applynum:patentinfo1.data.hits[i]._source.filing_no,
             publishnum:publish_num,
             patentname:patentinfo1.data.hits[i]._source.filing_name,
             applier:applierinfo,
             inventor:inventorinfo,
             applydate:patentinfo1.data.hits[i]._source.filing_time,
             publishdate:publish_date,
             articletype:patentinfo1.data.hits[i]._source.pub_type
           }
           patent1.push(item);
         }
       }
     };
     if(patentinfo2){
       if(patentinfo2.status==0){
       console.log("----------patentinfo2-------------");
       console.log(patentinfo2);
       total2=patentinfo2.data.total;
       var juhedata2=patentinfo2.buckets;
       for(var i=0;i<patentinfo2.buckets.group_by_filing_year.buckets.length;i++){
         var item={
           code:patentinfo2.buckets.group_by_filing_year.buckets[i].key,
           lable:patentinfo2.buckets.group_by_filing_year.buckets[i].key+"年",
           num:patentinfo2.buckets.group_by_filing_year.buckets[i].doc_count
         };
         juhe2.applydate.push(item);
       }
       for(var i=0;i<patentinfo2.buckets.group_by_pub_year.buckets.length;i++){
         var item={
           code:patentinfo2.buckets.group_by_pub_year.buckets[i].key,
           lable:patentinfo2.buckets.group_by_pub_year.buckets[i].key+"年",
           num:patentinfo2.buckets.group_by_pub_year.buckets[i].doc_count
         };
         juhe2.publishdate.push(item);
       }
       var patentdata2=patentinfo2.data.hits;
       for(var i=0;i<patentinfo2.data.hits.length;i++){
         var applierinfo="";
         if(patentinfo2.data.hits[i]._source.applicant_name!=null){
           for(var j=0;j<patentinfo2.data.hits[i]._source.applicant_name.length;j++){
             if(applierinfo){
               applierinfo=applierinfo+","+patentinfo2.data.hits[i]._source.applicant_name[j];
             }else{
               applierinfo=applierinfo+patentinfo2.data.hits[i]._source.applicant_name[j];
             }
           }
         }
         var inventorinfo="";
         if(patentinfo2.data.hits[i]._source.inventor_name!=null){
           for(var j=0;j<patentinfo2.data.hits[i]._source.inventor_name.length;j++){
             if(inventorinfo){
               inventorinfo=inventorinfo+","+patentinfo2.data.hits[i]._source.inventor_name[j];
             }else{
               inventorinfo=inventorinfo+patentinfo2.data.hits[i]._source.inventor_name[j];
             }
           }
         }
         var item={
           applynum:patentinfo2.data.hits[i]._source.app_number,
           publishnum:patentinfo2.data.hits[i]._source.pub_number,
           patentname:patentinfo2.data.hits[i]._source.title_name,
           applier:applierinfo,
           inventor:inventorinfo,
           applydate:patentinfo2.data.hits[i]._source.app_date,
           publishdate:patentinfo2.data.hits[i]._source.pub_date,
           articletype:patentinfo2.data.hits[i]._source.pub_type
         }
         patent2.push(item);
       }
     }
     }
     total=total1+total2;
     datalist1.patenttype=juhe1.patenttype;
     datalist1.status=juhe1.status;
     datalist1.applydate=juhe2.applydate;
     for (var i=0;i<juhe1.applydate.length;i++){
       var a=datalist1.applydate.findIndex(f => f.code==juhe1.applydate[i].code);
       if(a>-1){
         datalist1.applydate[a].num=datalist1.applydate[a].num+juhe1.applydate[i].num;
       }else{
         var item={
           code:juhe1.applydate[i].code,
           lable:juhe1.applydate[i].lable,
           num:juhe1.applydate[i].num
         };
         datalist1.applydate.push(item);
       }
     }
     datalist1.applydate.sort(this.compare("code"))
     console.log(datalist1.applydate);
     datalist1.publishdate=juhe2.publishdate;
     for (var i=0;i<juhe1.publishdate.length;i++){
       var a=datalist1.publishdate.findIndex(f => f.code==juhe1.publishdate[i].code);
       if(a>-1){
         datalist1.publishdate[a].num=datalist1.publishdate[a].num+juhe1.publishdate[i].num;
       }else{
         var item={
           code:juhe1.publishdate[i].code,
           lable:juhe1.publishdate[i].lable,
           num:juhe1.publishdate[i].num
         };
         datalist1.publishdate.push(item);
       }
     }
     datalist1.publishdate.sort(this.compare("code"))
     for(var i=0;i<patent1.length;i++){
       datalist2.push(patent1[i]);
     }
     for(var i=0;i<patent2.length;i++){
       datalist2.push(patent2[i]);
     }
     var result={
       total:total,
       dataList1:datalist1,
       dataList2:datalist2
     }
     console.log("----------result--------------");
     console.log(result);
     return System.getResult2(result,null);
   };
   compare(prop){
     return function (obj1, obj2) {
        var val1 = obj1[prop];
        var val2 = obj2[prop];
        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
            val1 = Number(val1);
            val2 = Number(val2);
        }
        if (val1 < val2) {
            return 1;
        } else if (val1 > val2) {
            return -1;
        } else {
            return 0;
        }
     }
   };
   async obtainpatentinfo(pobj,obj){
     console.log("--------------obtainpatentinfo---------------");
     console.log(obj);
     var result={
       patentname:"",
       patenttype:"",
       baseinfo:{},
       lawstatus:[],
       abstract:"",
       adstractpic:"",
       claim:"",
       instruction:"",
       figure:""
     };
     if(obj.publishnum.indexOf("CN")!=-1){
       console.log("中国专利");
       var data={
         appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
         applynum:obj.applynum,
         publishnum:obj.publishnum
       };
       var patentinfo=await this.GsbByChinaPatentSearchApi.ObtainChinaPatentInfo(data);
       console.log("---------ctl----patentinfo-------------");
       console.log(patentinfo);
       if(patentinfo.status==0){
         patentinfo=patentinfo.data[0]._source;
         console.log("---------ctl----patentinfo-------------");
         console.log(patentinfo);
         result.patentname=patentinfo.filing_name;
         result.patenttype=patentinfo.pub_type;
         if(patentinfo.other_ipc){
           var otheripc="";
           for (var i=0;i<patentinfo.other_ipc.length;i++){
             if(otheripc){
               otheripc=otheripc+","+patentinfo.other_ipc[i];
             }else{
               otheripc=otheripc+patentinfo.other_ipc[i];
             }
           }
         }
         var publishdateinfo="";
         if(obj.publishnum==patentinfo.pub_no){
           publishdateinfo=patentinfo.pub_time;
         }
         if(obj.publishnum==patentinfo.gr_no){
           publishdateinfo=patentinfo.gr_time;
         };
         if(patentinfo.applicant_name){
           var applierinfo="";
           for (var i=0;i<patentinfo.applicant_name.length;i++){
             if(applierinfo){
               applierinfo=applierinfo+","+patentinfo.applicant_name[i];
             }else{
               applierinfo=applierinfo+patentinfo.applicant_name[i];
             }
           }
         }
         if(patentinfo.inventor_name){
           var inventorinfo="";
           for (var i=0;i<patentinfo.inventor_name.length;i++){
             if(inventorinfo){
               inventorinfo=inventorinfo+","+patentinfo.inventor_name[i];
             }else{
               inventorinfo=inventorinfo+patentinfo.inventor_name[i];
             }
           }
         }
         var agentinfo=""
         if(patentinfo.agent_name1){
           if(patentinfo.agent_name2){
             agentinfo=patentinfo.agent_name1+patentinfo.agent_name2;
           }else{
             agentinfo=patentinfo.agent_name1;
           }
         }
         result.baseinfo={
           applynum:patentinfo.filing_no,
           mainclassnum:patentinfo.main_ipc,
           applydate:patentinfo.filing_time,
           classnum:otheripc,
           publishnum:obj.publishnum,
           address:patentinfo.appl_address,
           publishdate:publishdateinfo,
           countrycode:patentinfo.country_code,
           applier:applierinfo,
           agent:agentinfo,
           inventor:inventorinfo,
           agency:patentinfo.agency_name,
         };
         result.abstract=patentinfo.abstr_text;
         var patentinfostring=JSON.stringify(patentinfo);
         if(patentinfostring.indexOf("\"claim_text\":")!=-1){
           result.claim=patentinfo.claim_text;
         };
         var imgpathinfo=""
         if (patentinfo.img_path){
           imgpathinfo=patentinfo.img_path;
         }
         result.adstractpic = imgpathinfo;
         var affdata={
          appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
          filingno:obj.applynum
         };

         var affairinfo=await this.GsbByChinaAffairSearchApi.SearchbyFilingno(affdata);
         if(affairinfo.status==0){
            console.log("---------ctl----affairinfo-------------");
            affairinfo.data.hits.forEach(function(d){
              console.log(d);
              var affobj= {date:d._source.aff_date,status:d._source.aff_info}
              console.log(affobj);
              result.lawstatus.push(affobj);
            })
         }
       }
     }else{
       console.log("外国专利");
       var data={
         appKey:"9d9bcbb6a82f44dbbdb185afe28032bc",
         applynum:obj.applynum,
         publishnum:obj.publishnum
       };
       var patentinfo=await this.GsbByWorldPatentSearchApi.ObtainWorldPatentInfo(data);
       console.log("---------ctl----patentinfo-------------");
       console.log(patentinfo);
       if(patentinfo.status==0){
         patentinfo=patentinfo.data[0]._source;
         console.log("---------ctl----patentinfo-------------");
         console.log(patentinfo);
         result.patentname=patentinfo.title_name;
         result.patenttype=patentinfo.pub_type;
         if(patentinfo.other_ipc){
           var otheripc="";
           for (var i=0;i<patentinfo.other_ipc.length;i++){
             if(otheripc){
               otheripc=otheripc+","+patentinfo.other_ipc[i];
             }else{
               otheripc=otheripc+patentinfo.other_ipc[i];
             }
           }
         }
         if(patentinfo.applicant_name){
           var applierinfo="";
           for (var i=0;i<patentinfo.applicant_name.length;i++){
             if(applierinfo){
               applierinfo=applierinfo+","+patentinfo.applicant_name[i];
             }else{
               applierinfo=applierinfo+patentinfo.applicant_name[i];
             }
           }
         }
         if(patentinfo.inventor_name){
           var inventorinfo="";
           for (var i=0;i<patentinfo.inventor_name.length;i++){
             if(inventorinfo){
               inventorinfo=inventorinfo+","+patentinfo.inventor_name[i];
             }else{
               inventorinfo=inventorinfo+patentinfo.inventor_name[i];
             }
           }
         }
         result.baseinfo={
           applynum:patentinfo.app_number,
           mainclassnum:patentinfo.main_ipc,
           applydate:patentinfo.app_date,
           classnum:otheripc,
           publishnum:obj.pub_number,
           address:patentinfo.addr_name,
           publishdate:patentinfo.pub_date,
           countrycode:"",
           applier:applierinfo,
           agent:patentinfo.agent_name,
           inventor:inventorinfo,
           agency:patentinfo.agency_name,
         };
         result.abstract=patentinfo.abstr_text;
         result.claim=patentinfo.claim_text;
       }
     }
     return System.getResult2(result,null);
   }

}
module.exports=PatentycCtl;
