// 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
    if (!["findSystemMsgCount", "findUnreadCount"].includes(req.params && req.params.method || '')) {
      //第三个字段应该存公司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 { userName, errorInfo, messageBody, resultInfo, created_at, opTitle, requestId } = search;
    let query = {
      indexName: `logs-sytxpublic-msgq-${settings.logindex}`,
      pageSize: pageInfo.pageSize,
      currentPage: pageInfo.pageNo
    };
    if (company_id && company_id != 1) {
      query.identifyCode = `_${company_id}_`;
    }
    if (opTitle) {
      query.opTitle = opTitle;
    }
    if (requestId) {
      query.requestId = requestId;
    }
    if (userName) {
      query.messageBody = userName;
    }
    if (errorInfo) {
      query.errorInfo = errorInfo;
    }
    if (messageBody) {
      query.messageBody = messageBody;
    }
    if (resultInfo) {
      query.resultInfo = resultInfo;
    }
    if (created_at && created_at[0] && created_at[1]) {
      query.startTime = new Date(created_at[0]).getTime();
      query.entTime = new Date(created_at[1]).getTime();
    }
    console.log("请求--------日志");
    console.log(settings.logUrl());
    console.log(query);
    // return {
    //   "totalCount": 1,
    //   "pageSize": 20,
    //   "currentPage": 0,
    //   "list": [
    //     {
    //       "opTitle": "center_manage_首页",
    //       "identifyCode": "10_",
    //       "messageBody": "{\"gname\":\"msg\",\"qname\":\"msgCtl\",\"method\":\"findUnreadCount\",\"param\":{\"clientIp\":\"9.223.9.138\",\"agent\":\"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36\",\"classname\":\"msg.msgCtl\",\"app_id\":1,\"company_id\":\"10\",\"userid\":\"15\",\"username\":\"j2\",\"bizpath\":\"/home\",\"is_read\":false},\"actionType\":\"\",\"pushUrl\":\"\"}",
    //       "resultInfo": "{\"status\":0}",
    //       "errorInfo": "null",
    //       "requestId": "19a258474e384a0db939270580f01407",
    //       "created_at": "2020-07-16T05:18:48.454Z"
    //     }, {
    //       "opTitle": "center_manage_消息中心",
    //       "identifyCode": "10_",
    //       "messageBody": "{\"gname\":\"msg\",\"qname\":\"msgCtl\",\"method\":\"findUnreadCount\",\"param\":{\"clientIp\":\"9.223.9.138\",\"agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36\",\"classname\":\"msg.msgCtl\",\"app_id\":1,\"company_id\":\"10\",\"userid\":\"15\",\"username\":\"j2\",\"bizpath\":\"/message/message_page\",\"is_read\":false},\"actionType\":\"\",\"pushUrl\":\"\"}",
    //       "resultInfo": "{\"status\":-1}",
    //       "errorInfo": "null",
    //       "requestId": "06d94fbcd05b44a1ae292a21f5931f41",
    //       "created_at": "2020-07-16T05:51:54.888Z"
    //     }]
    // }
    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;
