var System = require("../../system")
const uuidv4 = require('uuid/v4');
const crypto = require('crypto');
const cryptoJS = require("crypto-js");
const querystring = require('querystring');
const logCtl = System.getObject("web.oplogCtl");
const fs=require("fs");
const xlsx = require('node-xlsx')
var moment = require('moment')
class TestApi {
  constructor() {
    this.utilesignbaoSve = System.getObject("service.utilesignbaoSve");
    this.dkcontractSve = System.getObject("service.dkcontractSve");
  }

  async genPosterQrcodeImg(obj) {
    return await this.imghandleApi.genPosterQrcodeImg(obj.qrcode, obj.bakImg);
  }

  async testdk() {
    await this.dkcontractSve.downloadCompleteUrl(1000);
  }

  async createPerson(obj) {   //创建企业账号
    var params = {
      thirdId: await this.getUidStr(28),
      name: obj.userName || '王昆',
      idNo: obj.idNo || "222403198301071778",
      idType: obj.idType || 19,
      mobile: obj.mobile || "18600440811",
    };

    var tt = await this.utilesignbaoSve.createAccountId(params, "testApi");
    tt.aaa = "aaa";
    console.log(tt);
    return tt;
  }

  async creatCompanyAccountId(obj) {   //创建企业账号
    // var params = {
    //   thirdId: obj.thirdId || "owD7y5AqMWjDSru10gDacEeA9Q5g",    //企业用户在对接方的账号标识字段-该字段来唯一标识一个企业用户
    //   creatorId: obj.creatorId || "fc7d596a767b47a4874490966c7641c7",  //企业创建人在 e签宝的个人账号唯一标识(宋毅accountId)
    //   name: obj.name || "汉唐信通（北京）科技有限公司",   //企业名称
    //   organCode: obj.organCode || "911101057889696760",   //企业证照号(企业统一社会信用代码)
    //   organType: obj.organType || 11,    //企业账号证照类型--11为企业统一社会信用代码
    //   legalName: obj.legalName || "宋毅",  //企业法人姓名
    //   legalIdNo: obj.legalIdNo || "341221198504218256",    //企业法人身份证号
    //   legalIdType: 19,   //个人账号证件类型--19为中国大陆居民身份证
    //   email: obj.email || "lisa@gongsibao.com"   //企业邮箱
    // };
    var params = {
      thirdId: obj.thirdId || await this.getUidStr(28),    //企业用户在对接方的账号标识字段-该字段来唯一标识一个企业用户
      creatorId: obj.creatorId || "050da411512e4fc2bae921862ac938fc",  //企业创建人在 e签宝的个人账号唯一标识(宋毅accountId)
      name: obj.name || "汉唐信通（北京）科技有限公司",   //企业名称
      organCode: obj.organCode || "911101083066224948",   //企业证照号(企业统一社会信用代码)
      organType: obj.organType || 11,    //企业账号证照类型--11为企业统一社会信用代码
      legalName: obj.legalName || "李丽",  //企业法人姓名
      legalIdNo: obj.legalIdNo || "620105197605160029",    //企业法人身份证号
      legalIdType: 19,   //个人账号证件类型--19为中国大陆居民身份证
      email: obj.email || "lili@gongsibao.com"   //企业邮箱
    };
    console.log(params);
    var tt = await this.utilesignbaoSve.creatCompanyAccountId(params, "testApi");
    tt.aaa = "aaa";
    console.log(tt);
    return tt;
  }

  async importexcel() {
    var names = [];
    for(var i = 1; i <= 6; i++) {
      try {
        var filePath = "/tmp/banks"+ i +".xlsx";
        console.log("----------------------------- begin "+ i +" -----------------------------");
        var sheets = xlsx.parse(filePath);
        if (!sheets || sheets.length == 0) {
          return result;
        }
  
        var sheet = sheets[0];
        if (!sheet || !sheet.data || sheet.data.length == 0) {
          return result;
        }
  
        var rows = sheet.data;
        
        for (var idx in rows) {
          var cells = rows[idx];
          if (!cells || cells.length < 3) {
            continue;
          }
          var obj = {
            number: cells[0],
            bankName: cells[1],
            bankCode: cells[2]
          }
          await this.ebankSve.create(obj);
        }
        console.log("----------------------------- end "+ i +"-----------------------------");
      } catch (error) {
        console.log(error.stack);
      }
    }
  }

  async getUidStr(len, radix) {
    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
    var uuid = [],
      i;
    radix = radix || chars.length;
    if (len) {
      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
      var r;
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';
      for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | Math.random() * 16;
          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
      }
    }
    return uuid.join('');
  }
}
module.exports = TestApi;