// var log4js = require('log4js');
var settings = require("../../config/settings");
const uuidv4 = require('uuid/v4');
const system = require("../system");
class LogClient {
  constructor() {
    // log4js.configure(settings.basepath+"/app/config/log4js.json");
    // this.logerApp=log4js.getLogger("app");
    // this.logerHttp=log4js.getLogger("http");
  }
  getUUID() {
    var uuid = uuidv4();
    var u = uuid.replace(/\-/g, "");
    return u;
  }
  async log(pobj, req, rtninfo, errinfo) {
    rtninfo.requestId = this.getUUID()
    req.params.param = pobj
    //第三个字段应该存公司id
    system.execLogs(settings.appname + "_" + req.xctx.codetitle, req.params, pobj.company_id + "_", rtninfo, errinfo).then(res => {
      if (res && res.status == 1) {
        console.log("log.....success")
      } else {
        console.log("log.....fail")
      }
    }).catch(e => {
      console.log("log.....fail")
    })
  }

  async logList(pobj) {
    let { search, pageInfo, company_id } = pobj;
    let query = {
      indexName: settings.logindex,
      pageSize: pageInfo.pageSize,
      currentPage: pageInfo.pageNo
    };
    if (company_id && company_id != 1) {
      query.identifyCode = company_id
    }
    if (search.opTitle) {
      query.opTitle = search.opTitle
    }
    // {
    //   "opTitle": "",// N 操作的业务标题
    //   "indexName": "sytxpublic-msgq-request",// Y es索引值，同一个项目用一个值                    
    //   "identifyCode": "needSubmit",// Y 操作的业务标识
    //   "messageBody": "",//N 日志的描述信息
    //   "resultInfo": "",//N 返回信息
    //   "errorInfo": "",//N 错误信息
    //   "requestId": "",//请求返回的requestId
    // }
    console.log("请求--------日志");
    console.log(settings.logUrl());
    console.log(query);
    let rtn = await system.postJsonTypeReq(settings.logUrl(), {
      "actionType": "queryLogsData",// Y 功能名称
      "actionBody": query
    });

    if (rtn.statusCode === 200) {
      rtn = rtn.data;
      if (rtn && rtn.status === 1) {
        return rtn.data;
      } else {
        throw new Error(rtn && rtn.message || '请联系管理员');
      }
    } else {
      throw new Error(rtn)
    }
  }
}
module.exports = LogClient;
