const system = require("../../../system");
const ServiceBase = require("../../svems.base")

class AuthService extends ServiceBase {
  constructor() {
    super();
    this.feeSve = system.getObject("service.fee.feeSve");
    this.orderSve = system.getObject("service.order.orderSve");
    this.orderauthlogSve = system.getObject("service.order.orderauthlogSve");
    this.enginesignSve = system.getObject("service.engine.enginesignSve");
    this.tradelogDao = system.getObject("db.common.tradelogDao");
  }
  async createAccount(params) {
    try {
      params.apiName = "sign@";
      params.isFee = false;

      // 验证参数
      let idName = this.trim(params.idName);
      let idNo = this.trim(params.idNo).toUpperCase();
      let mobile = this.trim(params.mobile);
      if (!idName) {
        return system.getResultFail(1000000, "姓名错误");
      }
      if (!idNo) {
        return system.getResultFail(1000000, "身份证错误");
      }
      return this.operatorSign(params, async (p) => {
        // 调用创建账户API
        let validRes = await this.enginesignSve.createAccount({
          id_name: idName,
          id_no: idNo,
          mobile: mobile,
          channel: p.orderProduct.channel,
        });
        return validRes;
      });
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }
  async createTemplate(params) {
    try {
      params.apiName = "sign@";
      params.isFee = false;

    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async handSign(params) {
    try {
      params.apiName = "sign@hand";
      params.isFee = true;


    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }
  async autosign(params) {
    params.apiName = "sign@auto";
    params.isFee = true;

    try {
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async operatorSign(params, operatorMethod) {
    let app = params.app;
    let merchantId = app.merchant_id;
    let isFee = params.isFee;

    // 获取订单产品
    let orderProduct = await this.orderSve.getMerchantOrderProduct(merchantId, params.apiName);
    if (orderProduct.status !== 0) {
      return orderProduct;
    }
    orderProduct = orderProduct.data;
    params.orderProduct = orderProduct;
    // 扣费
    let tradeLog;
    let trade;
    if (isFee) {
      let tres = await this.trade(params);
      if (tres.status !== 0) {
        return tres;
      }
      trade = tres.data.trade;
    }
    // 2. 调用签约引擎接口
    let res = await operatorMethod(params);

    if (isFee) {
      // 生成订单
      let logres = await this.orderauthlogSve.saveEorderAuthLog({
        order_id: orderProduct.id,
        product_id: orderProduct.product_id,
        platform_name: app.name,
        platform_id: app.id,
        spended_num: trade.trade_amt,
        user_name: params.idName,         // 使用方
        actual_spend_name: app.merchant_name,// 实际使用方
        engine_trade_id: trade.id,
        pass: pass ? 1 : 0
      });
      console.log(logres);
    }
    // 4. 返回认证结果
    return system.getResultSuccess("TODO 返回具体参数", res.msg);
  }

  // 交易
  async trade(params) {
    let orderProduct = params.orderProduct;

    let trade_amt = 1;
    if (orderProduct == 2) {
      trade_amt = Number(orderProduct.price);
    }

    let tradeLog = await this.tradelogDao.create({
      requestid: params.requestid,
      merchant_id: orderProduct.merchant_id,
      product_id: orderProduct.product_id,
      trade_amt: trade_amt,
      result: 0,
    });


    let tradeRes = await this.feeSve.trade({
      account_id: orderProduct.engine_account_id,
      trade_amt: trade_amt,
      trade_no: tradeLog.id,
      trade_desc: "二要素验证",
    });
    if (tradeRes.status !== 0) {
      tradeLog.result = 2;
      tradeLog.fee = 0;
      tradeLog.fee_remark = "扣费失败，" + tradeRes.msg;
      await tradeLog.save();
      return system.getResultFail(1002001, "扣费失败，" + tradeRes.msg);
    } else {
      tradeLog.result = 1;
      tradeLog.fee = 1;
      tradeLog.fee_remark = "扣费成功";
      await tradeLog.save();
    }
    return system.getResultSuccess({
      tradeLog: tradeLog,
      trade: tradeRes.data
    })
  }
}

module.exports = AuthService;