var system = require("../../../system");
const AppServiceBase = require("../../app.base");
var settings = require("../../../../config/settings");

//此类用来db中配置来进行调用-主要功能是推送蜂擎业务
class UtilsFqAliyunService extends AppServiceBase {
  constructor() {
    super();
    this.ucommuneOperatorPhone = settings.ucommuneOperatorPhone()
  }
  async pushOrderBusinessByProductCode2Fq(pobj) {//推送自定义产品码的订单商机到峰擎（订单已经支付成功才推送）
    var verifyResult = await this.verifyParam(pobj);
    var orderInfo = pobj.actionBody.order_info;
    var orderContact = pobj.actionBody.order_contact;
    var productInfo = pobj.actionBody.product_info;
    if (!orderInfo) {
      verifyResult = system.getResult(null, "orderInfo can not be empty，100420");
    }
    if (!orderInfo.orderStatus || orderInfo.orderStatus < 2) {
      verifyResult = system.getResult(null, "orderInfo status must be paid，100423");
    }
    if (!orderContact || !orderContact.mobile) {
      verifyResult = system.getResult(null, "orderInfo.orderContact.mobile can not be empty，100425");
    }
    if (!productInfo || !productInfo.service_business_code) {
      verifyResult = system.getResult(null, "orderInfo.productInfo.service_business_code can not be empty，100427");
    }
    if (!productInfo || !productInfo.price_item || !productInfo.price_item.service_code) {
      verifyResult = system.getResult(null, "orderInfo.productInfo.price_item.service_code can not be empty，100430");
    }
    if (verifyResult.status != 0) {
      return await this.disposePushResultFail(pobj, verifyResult, "fq->pushOrderBusinessByProductCode2Fq->verifyResult", this.pushlogFailType.FQ);
    }

    var interface_params_info = verifyResult.data;
    console.log("推送的业务员手机号为:",this.ucommuneOperatorPhone)
    var params = {
      idempotentId: orderInfo.orderNo,// 是 业务 ID
      idempotentSource: productInfo.service_business_code,// 是 业务来源（ic_jdyun、ic_ali）
      idempotentSourceName: productInfo.service_business_name || productInfo.service_business_code,// 是 京东云应用、阿里云应用
      city: "",// 否 所属城市
      phone: orderContact.mobile,// 是 手机号
      userId: orderInfo.channelUserId,// 否 用户 ID
      companyName: "",// 否 公司名称
      orderPrice: orderInfo.totalSum,// 是 订单金额
      productId: productInfo.price_item.service_code,// 是 产品 ID
      productQuantity: orderInfo.quantity,// 是 产品数量
      operator_phone: this.ucommuneOperatorPhone, // 2020 0704 推送fq 必填字段 这里以下为新增 7.7修改由写死改为从settings获取
      thirdPartyCustomerId: orderInfo.channelUserId,
      pay_flow_no: orderInfo.pay_flow_no,// 支付流水号
      pay_channel: orderInfo.pay_channel,// 支付渠道(微信、支付宝)(必填)
      pay_time: orderInfo.pay_time,//付款时间
      pay_payer_bank_no:orderInfo.pay_payer_bank_no//付款人账号
    };
    // var result = await this.opAliyunClientPost(pobj, "pushOrderBusiness", interface_params_info.opUrl, interface_params_info.key, interface_params_info.secret, params);
    var result = await this.opAliyunClientPost(pobj, interface_params_info.opUrl, interface_params_info.key, interface_params_info.secret, params);
    return await this.disposePushResult(pobj, result, "fq->pushOrderBusinessByProductCode2Fq->result", this.pushlogFailType.FQ);
  }

  /**
   * 推送阿里信息操作到峰擎
   * 阿里操作的信息不变外加增加组装的一些参数（推送工商需求商机、推送工商订单商机、推送关闭工商需求）
   * @param {*} pobj 
   */
  async pushAliBusiness2Fq(pobj) {//推送阿里信息操作到峰擎
    var verifyResult = await this.verifyParam(pobj);
    if (verifyResult.status != 0) {
      return await this.disposePushResultFail(pobj, verifyResult, "fq->pushAliBusiness2Fq->verifyResult", this.pushlogFailType.FQ);
    }
    var interface_params_info = verifyResult.data;
    var result = await this.opAliyunClientPost(pobj, interface_params_info.opUrl, interface_params_info.key, interface_params_info.secret, pobj.actionBody);
    return await this.disposePushResult(pobj, result, "fq->pushAliBusiness2Fq->result", this.pushlogFailType.FQ);
  }

  //--------------------------------------------------内部辅助方法-------------------start-----------------

  async opAliyunClientPost(pobj, url, key, secret, params) {
    try {
      var rc = system.getObject("util.aliyunClient");
      var rtn = await rc.post(url, key, secret, params);
      if (rtn.code != 200 && rtn.success != true) {
        return system.getResult(null, "推送失败，失败原因：" + rtn.errorMsg + "，selfrequestId=" + pobj.requestId + "，requestId=" + rtn.requestId);
      }
      return system.getResultSuccess(null, "推送成功");
    } catch (e) {
      //日志记录
      this.logCtl.error({
        appid: pobj.appInfo ? pobj.appInfo.uapp_id || "" : "",
        appkey: pobj.appInfo ? pobj.appInfo.uapp_key || "" : "",
        requestId: pobj.requestId || "",
        op: "service/impl/utilsSve/opAliyunClientPost.js/opAliyunClientPost",
        content: "error：" + e.stack,
        // clientIp: pobj.clientIp,
        optitle: pobj.opType + "推送操作异常->fq->opAliyunClientPost",
      });
      return system.getResult(null, "推送操作异常->fq->opAliyunClientPost->error：" + e.stack);
    }
  }
  async verifyParam(pobj) {//参数信息验证
    var verify = system.getResultSuccess();
    if (!pobj.interface_params) {
      verify = system.getResult(null, "interface_params can not be empty，100400");
    }
    var interface_params_info = JSON.parse(pobj.interface_params);
    if (!interface_params_info || !interface_params_info.opUrl || !interface_params_info.key || !interface_params_info.secret) {
      verify = system.getResult(null, "interface_params（opUrl、key、secret） can not be empty，100410");
    }
    verify.data = interface_params_info;
    return verify;
  }

  //--------------------------------------------------内部辅助方法-------------------end-----------------
}
module.exports = UtilsFqAliyunService;
