var system=require("../../system")
const http=require("http")
const querystring = require('querystring');
var settings=require("../../../config/settings");
const ApiBase =require("../api.base");
const logCtl=system.getObject("web.oplogCtl");
class beehiveApi extends ApiBase{
   constructor(){
     super();
     this.connectionbh=system.getObject("db.connection").getConhb();
   }
   async getEntFileList(obj,obj2){
     console.log("obj111111111111111111111111111");
     console.log(obj);
     console.log(obj2);
     if(!obj){
       obj={
         companyName:"",creditCode:"",ps:"",cp:"",type:""
       }
     }
     console.log(obj);
     var companyName = obj.companyName || "";
     var creditCode = obj.creditCode || "";
     var ps = obj.ps || "10";
     var cp = obj.cp || "1";
     var type = obj.type || "";
     ps = Number(ps);
     cp = Number(cp);
     if(ps>50){
       ps=50;
     }
     if(cp<1){
       cp=1;
     }
     var start = (cp-1)*ps;
     var sql="SELECT tmc.credit_code,tmc.applier,att.file_url,att.`name` FROM	`ig_trade_mark_case` AS tmc JOIN ig_up_attachment AS att ON att.trade_mark_caseid = tmc.id ";
     sql = sql+" where 1=1 AND att.file_url IS NOT NULL and tmc.credit_code IS NOT NULL AND tmc.credit_code != \"\"";
     if(companyName){
       sql = sql + " and tmc.applier LIKE \"%"+companyName+"%\" ";
     }
     if(creditCode){
       sql = sql + " and tmc.credit_code LIKE  \"%"+creditCode+"%\"";
     }
     if(type=="yyzz"){
       sql = sql + " and att.name LIKE  \"%营业执照%\"";
     }
     sql =sql+" group by 	tmc.credit_code, tmc.applier, att.`name` "

     var sql2 = "select count(1) count from ("+sql+") as a";
     console.log(sql2);
     sql = sql + " limit "+start+","+ps;
     console.log(sql);
     try {
       var filelist = await this.connectionbh.query(sql);
       var countList = await this.connectionbh.query(sql2);
       if(filelist && filelist.length>0){
         var datalist = filelist[0];
         var count = 0;
         if(countList && countList.length>0){
           var counts = countList[0];
           if(counts){
             count = counts[0].count;
           }
         }
         return {code:1,count:count,data:datalist};
       }else{
         return {code:-1,msg:"未查到数据"};
       }
     } catch (e) {
       return {code:-2,msg:"操作失败"};;
     }
   }

}
module.exports=beehiveApi;
// var test = new beehiveApi();
// test.getEntFileList({cp:2,type:"yyzz"}).then(function(d){
//   console.log(d);
// })
