var System=require("../../system");
var settings=require("../../../config/settings");
var pinyin = require("pinyin");
var moment=require('moment');
class magicsearchCtl{
  constructor(){
    this.GsbMagicSearchApi=System.getObject("api.magicsearchapi");
    this.testApi=System.getObject("api.testApi");
    this.utilstmSve=System.getObject("service.utilstmSve");
    this.utilscontent=System.getObject("service.utilscontentfilterSve");
  }
  convertDate(time){
     if(time==null){
       return "";
     }
     var date =  new Date(Number(time*1000));
     var y = 1900+date.getYear();
     var m = "0"+(date.getMonth()+1);
     var d = "0"+date.getDate();
     return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
  }

  async magicsearch(query,obj){
    var result={rows:[],date_counts:[],count:""};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++magic+++++++++++++");
    console.log(obj);
    var sources=[];
    var date_count=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    var pageSize=obj.pageSize;
    var currentPage=obj.currentPage;
    var data={
      datestart:datestart,
      dateend:dateend,
      checkedStatu:checkedStatu,
      companyType:companyType,
      industry:industry,
      province:province,
      capitalstart:capitalstart,
      capitalend:capitalend,
      businessScope:businessScope,
      pagesize:pageSize,
      page:currentPage
    };
    var magic = await this.GsbMagicSearchApi.magicEssearch(data);
    var magicjhe = await this.GsbMagicSearchApi.magicEssearchJuhe(data);
    console.log("========================================");
    // console.log(magic);
    console.log(magicjhe);
    if(magic.status==0){
      result.count=magic.total;
      for (var i = 0; i < magic.data.length; i++) {
        var tm =magic.data[i];
        var source={
          company_name:tm.company_name,//公司名称
          english_company_name:tm.english_company_name,//公司英文名称
          old_company_name:tm.old_company_name,//公司曾用名
          legal_persion_id:tm.legal_persion_id,//法人ID
          legal_persion_name:tm.legal_persion_name,//法人姓名
          legal_persion_type:tm.legal_persion_type,//法人类型
          usc_code:tm.usc_code,//统一社会信用代码
          reg_number:tm.reg_number,//注册号
          company_org_type:tm.company_org_type,//公司类型
          reg_location:tm.reg_location,//注册地址
          establish_time:tm.establish_time,//公司注册时间
          business_scope:tm.business_scope,//经营范围
          from_time:tm.from_time,//营业期限开始日期
          to_time:tm.to_time,//营业期限终止日期
          reg_institute:tm.reg_institute,//登记机关
          reg_status:tm.reg_status,//企业状态
          reg_capital:tm.reg_capital,//注册资金
          reg_capital_unit:tm.reg_capital_unit,//注册资金货币单位
          actual_capital:tm.actual_capital,//实收注册资金
          org_number:tm.org_number,//组织机构代码
          list_code:tm.list_code,//上市代码
          cancel_time:tm.cancel_time,//注销时间
          approved_time:tm.approved_time,//核准日期
          legal_persion_type:tm.legal_persion_type,//法人类型，1 人 2 公司
          new_company_name_id:tm.new_company_name_id,// 变更后新公司的名字
          company_province:tm.company_province,//公司省份
          company_city:tm.company_city,//公司二级市
          company_cate_1:tm.company_cate_1,//行业类别
        }
        sources.push(source);
      }
      result.rows=sources;
    }
    if(magicjhe.status==0){
      magicjhe.buckets.forEach(function(buck){
        var count={
          "key":buck.key,
          "keycount":buck.doc_count,
        };
        date_count.push(count);
      });
      result.date_counts=date_count;
    }
    console.log(result);
    return System.getResult2(result,null);
  }
  async magicsearchmonth(query,obj){//按照时间查询数据
    var result={rows:[]};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++magic+++++++++++++");
    console.log(obj);
    var sources=[];
    var monthtime=obj.monthtime;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    var pageSize=obj.pageSize;
    var currentPage=obj.currentPage;
    var data={
      monthtime:monthtime,
      checkedStatu:checkedStatu,
      companyType:companyType,
      industry:industry,
      province:province,
      capitalstart:capitalstart,
      capitalend:capitalend,
      businessScope:businessScope,
      pagesize:pageSize,
      page:currentPage
    };
    var magic = await this.GsbMagicSearchApi.magicEssearchmonth(data);
    console.log("========================================");
    // console.log(magic);
    if(magic.status==0){
      for (var i = 0; i < magic.data.length; i++) {
        var tm =magic.data[i];
        var source={
          company_name:tm.company_name,//公司名称
          english_company_name:tm.english_company_name,//公司英文名称
          old_company_name:tm.old_company_name,//公司曾用名
          legal_persion_id:tm.legal_persion_id,//法人ID
          legal_persion_name:tm.legal_persion_name,//法人姓名
          legal_persion_type:tm.legal_persion_type,//法人类型
          usc_code:tm.usc_code,//统一社会信用代码
          reg_number:tm.reg_number,//注册号
          company_org_type:tm.company_org_type,//公司类型
          reg_location:tm.reg_location,//注册地址
          establish_time:tm.establish_time,//公司注册时间
          business_scope:tm.business_scope,//经营范围
          from_time:tm.from_time,//营业期限开始日期
          to_time:tm.to_time,//营业期限终止日期
          reg_institute:tm.reg_institute,//登记机关
          reg_status:tm.reg_status,//企业状态
          reg_capital:tm.reg_capital,//注册资金
          reg_capital_unit:tm.reg_capital_unit,//注册资金货币单位
          actual_capital:tm.actual_capital,//实收注册资金
          org_number:tm.org_number,//组织机构代码
          list_code:tm.list_code,//上市代码
          cancel_time:tm.cancel_time,//注销时间
          approved_time:tm.approved_time,//核准日期
          legal_persion_type:tm.legal_persion_type,//法人类型，1 人 2 公司
          new_company_name_id:tm.new_company_name_id,// 变更后新公司的名字
          company_province:tm.company_province,//公司省份
          company_city:tm.company_city,//公司二级市
          company_cate_1:tm.company_cate_1,//行业类别
        }
        sources.push(source);
      }
      result.rows=sources;
    }
    console.log(result);
    return System.getResult2(result,null);
  }
  async magicDetail(query,obj){//工商数据详情信息查询
    console.log(obj);
    var result={detaildata:[]};
    var usc_code=obj.usc_code;
    var company_name=obj.company_name;
    var sources=[];
    var data={
      usc_code:usc_code,
      company_name:company_name
    };
    var magicdetail= await this.GsbMagicSearchApi.magicdetailapi(data);
    console.log("00000000000000000000000000000000000000000000000000000000");
    console.log(magicdetail);
    if(magicdetail.status==0&&magicdetail.data.length>0){
      var tm =magicdetail.data[0];
      var source={
        company_name:tm.company_name,//公司名称
        english_company_name:tm.english_company_name,//公司英文名称
        old_company_name:tm.old_company_name,//公司曾用名
        legal_persion_id:tm.legal_persion_id,//法人ID
        legal_persion_name:tm.legal_persion_name,//法人姓名
        legal_persion_type:tm.legal_persion_type,//法人类型
        usc_code:tm.usc_code,//统一社会信用代码
        reg_number:tm.reg_number,//注册号
        company_org_type:tm.company_org_type,//公司类型
        reg_location:tm.reg_location,//注册地址
        establish_time:tm.establish_time,//公司注册时间
        business_scope:tm.business_scope,//经营范围
        from_time:tm.from_time,//营业期限开始日期
        to_time:tm.to_time,//营业期限终止日期
        reg_institute:tm.reg_institute,//登记机关
        reg_status:tm.reg_status,//企业状态
        reg_capital:tm.reg_capital,//注册资金
        reg_capital_unit:tm.reg_capital_unit,//注册资金货币单位
        actual_capital:tm.actual_capital,//实收注册资金
        org_number:tm.org_number,//组织机构代码
        list_code:tm.list_code,//上市代码
        cancel_time:tm.cancel_time,//注销时间
        approved_time:tm.approved_time,//核准日期
        legal_persion_type:tm.legal_persion_type,//法人类型，1 人 2 公司
        new_company_name_id:tm.new_company_name_id,// 变更后新公司的名字
        company_province:tm.company_province,//公司省份
        company_city:tm.company_city,//公司二级市
        company_cate_1:tm.company_cate_1,//行业类别
      }
      sources.push(source);
    }
    result.detaildata=sources;
    console.log(result);
    return System.getResult2(result,null);

  }
  async comparetime(query,obj){//工商数据时间对比
    var result={rows:[]};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++compareprovince+++++++++++++");
    console.log(obj);
    var sources=[];
    var date_count=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    var data={
      datestart:datestart,
      dateend:dateend,
      checkedStatu:checkedStatu,
      companyType:companyType,
      industry:industry,
      province:province,
      capitalstart:capitalstart,
      capitalend:capitalend,
      businessScope:businessScope
    };
    var magic = await this.GsbMagicSearchApi.comparetimeEssearch(data);
    console.log("========================================");
    if(magic.status==0){
      var source={
        key:"结果集2",
        keycount:magic.total
      }
      sources.push(source);
    }
    result.rows=sources;
    console.log(result);
    return System.getResult2(result,null);

  }
  async compareindustry(query,obj){//工商数据行业对比
    var result={rows:[],count:""};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++compareprovince+++++++++++++");
    console.log(obj);
    var sources=[];
    var date_count=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industrys=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    if(industrys.length>0){
      for(var i=0;i<industrys.length;i++){
        var industry = industrys[i];
        var data={
          datestart:datestart,
          dateend:dateend,
          checkedStatu:checkedStatu,
          companyType:companyType,
          industry:industry,
          province:province,
          capitalstart:capitalstart,
          capitalend:capitalend,
          businessScope:businessScope
        };
        var magic = await this.GsbMagicSearchApi.compareindustryEssearch(data);
        console.log("========================================");
        if(magic.status==0){
          var source={
            key:industry,
            keycount:magic.total
          }
          sources.push(source);
        }
      }
    }
    result.rows=sources;
    console.log(result);
    return System.getResult2(result,null);
  }
  async compareprovince(query,obj){//工商数据单个地区对比
    var result={rows:[],count:""};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++compareprovince+++++++++++++");
    console.log(obj);
    var sources=[];
    var date_count=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    if(province.length>0){
      for(var i=0;i<province.length;i++){
        var prov = province[i];
        var data={
          datestart:datestart,
          dateend:dateend,
          checkedStatu:checkedStatu,
          companyType:companyType,
          industry:industry,
          province:prov,
          capitalstart:capitalstart,
          capitalend:capitalend,
          businessScope:businessScope
        };
        var magic = await this.GsbMagicSearchApi.compareprovinceEssearch(data);
        console.log("========================================");
        if(magic.status==0){
          var source={
            key:prov,
            keycount:magic.total
          }
          sources.push(source);
        }
      }
    }
    result.rows=sources;
    console.log(result);
    return System.getResult2(result,null);
  }
  async comparepresult(query,obj){//工商数据对比
    var result={rows:[]};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++magic+++++++++++++");
    console.log(obj);
    var date_count=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    var data={
      datestart:datestart,
      dateend:dateend,
      checkedStatu:checkedStatu,
      companyType:companyType,
      industry:industry,
      province:province,
      capitalstart:capitalstart,
      capitalend:capitalend,
      businessScope:businessScope
    };
    var magicjhe = await this.GsbMagicSearchApi.magicEssearchJuhe(data);
    console.log("========================================");
    // console.log(magic);
    console.log(magicjhe);
    if(magicjhe.status==0){
      magicjhe.buckets.forEach(function(buck){
        var count={
          "key":buck.key,
          "keycount":buck.doc_count,
        };
        date_count.push(count);
      });
      result.rows=date_count;
    }
    console.log(result);
    return System.getResult2(result,null);
  }
  async comparesearch(query,obj){//对比数据查询
    var result={rows:[]};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++magic+++++++++++++");
    console.log(obj);
    var sources=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    var pageSize=obj.pageSize;
    var currentPage=obj.currentPage;
    var data={
      datestart:datestart,
      dateend:dateend,
      checkedStatu:checkedStatu,
      companyType:companyType,
      industry:industry,
      province:province,
      capitalstart:capitalstart,
      capitalend:capitalend,
      businessScope:businessScope,
      pagesize:pageSize,
      page:currentPage
    };
    var magic = await this.GsbMagicSearchApi.compareEssearch(data);
    console.log("========================================");
    // console.log(magic);
    if(magic.status==0){
      for (var i = 0; i < magic.data.length; i++) {
        var tm =magic.data[i];
        var source={
          company_name:tm.company_name,//公司名称
          english_company_name:tm.english_company_name,//公司英文名称
          old_company_name:tm.old_company_name,//公司曾用名
          legal_persion_id:tm.legal_persion_id,//法人ID
          legal_persion_name:tm.legal_persion_name,//法人姓名
          legal_persion_type:tm.legal_persion_type,//法人类型
          usc_code:tm.usc_code,//统一社会信用代码
          reg_number:tm.reg_number,//注册号
          company_org_type:tm.company_org_type,//公司类型
          reg_location:tm.reg_location,//注册地址
          establish_time:tm.establish_time,//公司注册时间
          business_scope:tm.business_scope,//经营范围
          from_time:tm.from_time,//营业期限开始日期
          to_time:tm.to_time,//营业期限终止日期
          reg_institute:tm.reg_institute,//登记机关
          reg_status:tm.reg_status,//企业状态
          reg_capital:tm.reg_capital,//注册资金
          reg_capital_unit:tm.reg_capital_unit,//注册资金货币单位
          actual_capital:tm.actual_capital,//实收注册资金
          org_number:tm.org_number,//组织机构代码
          list_code:tm.list_code,//上市代码
          cancel_time:tm.cancel_time,//注销时间
          approved_time:tm.approved_time,//核准日期
          legal_persion_type:tm.legal_persion_type,//法人类型，1 人 2 公司
          new_company_name_id:tm.new_company_name_id,// 变更后新公司的名字
          company_province:tm.company_province,//公司省份
          company_city:tm.company_city,//公司二级市
          company_cate_1:tm.company_cate_1,//行业类别
        }
        sources.push(source);
      }
      result.rows=sources;
    }
    console.log(result);
    return System.getResult2(result,null);
  }
  async magicsecondsearch(query,obj){//第二次数据查询
    var result={rows:[],count:""};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++magic+++++++++++++");
    console.log(obj);
    var sources=[];
    var datestart=obj.datestart;
    var dateend=obj.dateend;
    var checkedStatu=obj.checkedStatu;
    var companyType=obj.companyType;
    var industry=obj.industry;
    var province=obj.province;
    var capitalstart=obj.capitalstart;
    var capitalend=obj.capitalend;
    var businessScope=obj.businessScope;
    var pageSize=obj.pageSize;
    var currentPage=obj.currentPage;
    var data={
      datestart:datestart,
      dateend:dateend,
      checkedStatu:checkedStatu,
      companyType:companyType,
      industry:industry,
      province:province,
      capitalstart:capitalstart,
      capitalend:capitalend,
      businessScope:businessScope,
      pagesize:pageSize,
      page:currentPage
    };
    var magic = await this.GsbMagicSearchApi.compareEssearch(data);
    console.log("========================================");
    // console.log(magic);
    if(magic.status==0){
      result.count=magic.total;
      for (var i = 0; i < magic.data.length; i++) {
        var tm =magic.data[i];
        var source={
          company_name:tm.company_name,//公司名称
          english_company_name:tm.english_company_name,//公司英文名称
          old_company_name:tm.old_company_name,//公司曾用名
          legal_persion_id:tm.legal_persion_id,//法人ID
          legal_persion_name:tm.legal_persion_name,//法人姓名
          legal_persion_type:tm.legal_persion_type,//法人类型
          usc_code:tm.usc_code,//统一社会信用代码
          reg_number:tm.reg_number,//注册号
          company_org_type:tm.company_org_type,//公司类型
          reg_location:tm.reg_location,//注册地址
          establish_time:tm.establish_time,//公司注册时间
          business_scope:tm.business_scope,//经营范围
          from_time:tm.from_time,//营业期限开始日期
          to_time:tm.to_time,//营业期限终止日期
          reg_institute:tm.reg_institute,//登记机关
          reg_status:tm.reg_status,//企业状态
          reg_capital:tm.reg_capital,//注册资金
          reg_capital_unit:tm.reg_capital_unit,//注册资金货币单位
          actual_capital:tm.actual_capital,//实收注册资金
          org_number:tm.org_number,//组织机构代码
          list_code:tm.list_code,//上市代码
          cancel_time:tm.cancel_time,//注销时间
          approved_time:tm.approved_time,//核准日期
          legal_persion_type:tm.legal_persion_type,//法人类型，1 人 2 公司
          new_company_name_id:tm.new_company_name_id,// 变更后新公司的名字
          company_province:tm.company_province,//公司省份
          company_city:tm.company_city,//公司二级市
          company_cate_1:tm.company_cate_1,//行业类别
        }
        sources.push(source);
      }
      result.rows=sources;
    }
    console.log(result);
    return System.getResult2(result,null);
  }
  async magicbasesearch(query,obj){
    var result={rows:[],count:""};
    console.log("++++++++++++++++++++++++++++++++++++++++++++++magicbase+++++++++++++");
    console.log(obj);
    var sources=[];
    var selectvalue=obj.selectvalue;
    var mark=obj.mark;
    var pageSize=obj.pageSize;
    var currentPage=obj.currentPage;
    var data={
      selectvalue:selectvalue,
      mark:mark,
      pagesize:pageSize,
      page:currentPage
    };
    var magic = await this.GsbMagicSearchApi.magicbaseEssearch(data);
    console.log("========================================");
    // console.log(magic);
    if(magic.status==0){
      result.count=magic.total;
      for (var i = 0; i < magic.data.length; i++) {
        var tm =magic.data[i];
        var source={
          company_name:tm.company_name,//公司名称
          english_company_name:tm.english_company_name,//公司英文名称
          old_company_name:tm.old_company_name,//公司曾用名
          legal_persion_id:tm.legal_persion_id,//法人ID
          legal_persion_name:tm.legal_persion_name,//法人姓名
          legal_persion_type:tm.legal_persion_type,//法人类型
          usc_code:tm.usc_code,//统一社会信用代码
          reg_number:tm.reg_number,//注册号
          company_org_type:tm.company_org_type,//公司类型
          reg_location:tm.reg_location,//注册地址
          establish_time:tm.establish_time,//公司注册时间
          business_scope:tm.business_scope,//经营范围
          from_time:tm.from_time,//营业期限开始日期
          to_time:tm.to_time,//营业期限终止日期
          reg_institute:tm.reg_institute,//登记机关
          reg_status:tm.reg_status,//企业状态
          reg_capital:tm.reg_capital,//注册资金
          reg_capital_unit:tm.reg_capital_unit,//注册资金货币单位
          actual_capital:tm.actual_capital,//实收注册资金
          org_number:tm.org_number,//组织机构代码
          list_code:tm.list_code,//上市代码
          cancel_time:tm.cancel_time,//注销时间
          approved_time:tm.approved_time,//核准日期
          legal_persion_type:tm.legal_persion_type,//法人类型，1 人 2 公司
          new_company_name_id:tm.new_company_name_id,// 变更后新公司的名字
          company_province:tm.company_province,//公司省份
          company_city:tm.company_city,//公司二级市
          company_cate_1:tm.company_cate_1,//行业类别
        }
        sources.push(source);
      }
      result.rows=sources;
    }
    console.log(result);
    return System.getResult2(result,null);
  }
}
module.exports=magicsearchCtl;
