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')
const uuidv1 = require('uuid/v1');
class TestApi {
  constructor() {
    this.utilesignbaoSve = System.getObject("service.utilesignbaoSve");
    this.redisClient = System.getObject("util.redisClient");
    this.downcontractClient = System.getObject("util.downcontractClient");
    // this.publishClient = System.getObject("util.publishClient");
    this.redisLock = System.getObject("util.redisLock");
  }

  // async test34() {
  //   let b4 = {
  //     name: "范戎凯",
  //     idno: "130481199007010130",
  //     cardno: "6214680041375229",
  //     mobile: "15010033768",
  //   }
  //
  //   let b3 = {
  //     name: b4.name,
  //     idno: b4.idno,
  //     cardno: b4.cardno
  //   };
  //
  //   let b2 = {
  //     name: b4.name,
  //     idno: b4.idno,
  //   };
  //
  //   var b2rs = await this.utilesignbaoSve.personTwo(b2, "");
  //   let b3rs = await this.utilesignbaoSve.bankthree(b3, "") || {};
  //   let b4rs = await this.utilesignbaoSve.bankfour(b4, "") || {};
  // }

  async testAddDL(obj) {
    let contracts = obj.contracts || [];
    for(let p of contracts) {
      await this.redisClient.rpushBCD(p);
    }
    return {msg: "success"}
  }

  async donloadContract(obj) {
    this.downcontractClient.donloadContract();
    return {msg: "start success"};
  }

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


  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 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);
      }
    }
  }

  sleep(time) {
    return new Promise((resolve) => {
      setTimeout(function () {
        resolve();
      }, time || 1000);
    });
  }

  async test1(key) {
    try {
      const id = uuidv1();
      await this.redisLock.lock(key, id, 20);
      console.log(11111111)
      const unLock = await this.redisLock.unLock(key, id);
      console.log('unLock: ', key, id, unLock);
    } catch (err) {
      console.log('上锁失败', err);
    }

  }

  async sss(key){
    this.test1(key.key)
    this.test1(key.key)
    this.test1(key.key)
  }



}
module.exports = TestApi;
