var APIBase = require("../../api.base");
var system = require("../../../system");
var settings = require("../../../../config/settings");

class ActionAPI extends APIBase {
  constructor() {
    super();
    this.saasSve = system.getObject("service.saas.saasSve");
    this.saasbusinessSve = system.getObject("service.saas.saasbusinessSve");
    this.orgSve = system.getObject("service.org.orgSve");
    this.userSve = system.getObject("service.user.userSve");
    this.roleSve = system.getObject("service.role.roleSve");
    this.authSve = system.getObject("service.auth.authSve");

    this.saasplatformuserSve = system.getObject("service.user.saasplatformuserSve");
    this.saasmerchantuserSve = system.getObject("service.user.saasmerchantuserSve");
    this.saasmerchantappletuserSve = system.getObject("service.user.saasmerchantappletuserSve");
  }

  /**
   * 接口跳转
   * action_process  执行的流程
   * action_type     执行的类型
   * action_body     执行的参数
   */
  async springboard(pobj, qobj, req) {
    var result;
    if (!pobj.action_process) {
      return system.getResult(null, "action_process参数不能为空");
    }
    if (!pobj.action_type) {
      return system.getResult(null, "action_type参数不能为空");
    }
    console.log(pobj.action_process, pobj.action_type, pobj.action_body, "---------------- this is order micro sve --------------------");
    try {
      result = await this.handleRequest(pobj.action_process, pobj.action_type, pobj.action_body);
    } catch (error) {
      console.log(error);
    }
    return result;
  }

  async handleRequest(action_process, action_type, action_body) {
    var opResult = null;
    switch (action_type) {
      // saas
      case "addSaas":
        opResult = await this.saasSve.apiAddSaas(action_body);
        break;
      case "updSaas":
        opResult = await this.saasSve.apiUpdSaas(action_body);
        break;
      case "delSaas":
        opResult = await this.userSve.apiFindUserBySaasId(action_body);
        if (opResult.status == 0 && opResult.data.length == 0) {
          opResult = await this.saasSve.apiDelSaas(action_body);
        } else {
          opResult = system.getResult(-1, `当前SAAS不能删除`);
        }
        break;
      case "listSaas":
        opResult = await this.saasSve.apiListSaas(action_body);
        break;
      case "saasQueryById":
        opResult = await this.saasSve.apiQueryById(action_body);
        break;

      // 组织机构
      case "saveOrg":
        opResult = await this.orgSve.saveOrg(action_body);
        break;
      case "delOrg":
        opResult = await this.orgSve.apiDelOrg(action_body);
        break;
      case "listOrg":
        opResult = await this.orgSve.apiListOrg(action_body);
        break;
      case "findById":
        opResult = await this.orgSve.apiFindById(action_body);
        break;
      case "orgByPid":
        opResult = await this.orgSve.apiByPid(action_body);
        break;
      case "orgTree":
        opResult = await this.orgSve.apiTree(action_body);
        break;

      // 菜单权限
      case "addAuth":
        opResult = this.authSve.add(action_body);
        break;
      case "updAuth":
        opResult = this.authSve.upd(action_body);
        break;
      case "authTree":
        opResult = this.authSve.tree(action_body);
        break;
      case "authInfo":
        opResult = this.authSve.info(action_body);
        break;
      case "authByPid":
        opResult = this.authSve.byPid(action_body);
        break;
      case "delAuth":
        // TODO 验证是否有角色关联
        opResult = this.authSve.del(action_body);
        break;

      // 角色
      case "addRole":
        opResult = await this.roleSve.apiAddRole(action_body);
        break;
      case "updRole":
        opResult = await this.roleSve.apiUpdRole(action_body);
        break;
      case "delRole":
        opResult = await this.roleSve.apiDelRole(action_body);
        break;
      case "listRole":
        opResult = await this.roleSve.apiListRole(action_body);
        break;
      case "roleQueryById":
        opResult = await this.roleSve.apiQueryById(action_body);
        break;
      case "setAuth":
        opResult = await this.roleSve.setAuth(action_body);
        break;

      case "saveUser":
        opResult = await this.userSve.saveUser(action_body);
        break;
      case "userInfo":
        opResult = await this.userSve.info(action_body);
        break;
      case "enabled":
        opResult = await this.userSve.enabled(action_body);
        break;
      case "delUser":
        opResult = await this.userSve.delUser(action_body);
        break;
      case "userPage":
        opResult = await this.userSve.pageByCondition(action_body);
        break;
      case "updPassword":
        opResult = await this.userSve.updPassword(action_body);
        break;
      case "login":
        opResult = await this.userSve.login(action_body);
        break;
      case "loginByUcid":
        opResult = await this.userSve.loginByUcid(action_body);
        break;
      case "mapUserByIds":
        opResult = await this.userSve.mapByIds(action_body);
        break;
      case "findUsers":
        opResult = await this.userSve.findUsers(action_body);
        break;

      // -------------------------------- saas 用户接口 --------------------------------------
      case "openSaasBusiness":
        opResult = await this.saasbusinessSve.openSaasBusiness(action_body);
        break;

      case "platformLogin":
        opResult = await this.saasplatformuserSve.login(action_body);
        break;
      case "platformRegisterInner":
        opResult = await this.saasplatformuserSve.registerInner(action_body);
        break;
      case "platformResetPasswordInner":
        opResult = await this.saasplatformuserSve.resetPasswordInner(action_body);
        break;
      case "platformInfo":
        opResult = await this.saasplatformuserSve.info(action_body);
        break;
      case "platformEnabled":
        opResult = await this.saasplatformuserSve.enabled(action_body);
        break;
      case "platforMapByIds":
        opResult = await this.saasplatformuserSve.mapByIds(action_body);
        break;

      // saas商户
      case "merchantLogin":
        opResult = await this.saasmerchantuserSve.login(action_body);
        break;
      case "merchantSave":
        opResult = await this.saasmerchantuserSve.save(action_body);
        break;
      case "merchantInfo":
        opResult = await this.saasmerchantuserSve.info(action_body);
        break;
      case "merchantEnabled":
        opResult = await this.saasmerchantuserSve.enabled(action_body);
        break;
      case "merchantPage":
        opResult = await this.saasmerchantuserSve.pageByCondition(action_body);
        break;
      case "merchantPassword":
        opResult = await this.saasmerchantuserSve.updPassword(action_body);
        break;
      case "merchantMapByIds":
        opResult = await this.saasmerchantuserSve.mapByIds(action_body);
        break;
      // 商户小程序
      case "appletLogin":
        opResult = await this.saasmerchantappletuserSve.appletLogin(action_body);
        break;
      case "appletUserInfo":
        opResult = await this.saasmerchantappletuserSve.info(action_body);
        break;
    }
    return opResult;
  }

  exam() {
    return `<pre><pre/>`;
  }

  classDesc() {
    return {
      groupName: "op",
      groupDesc: "元数据服务包",
      name: "ActionAPI",
      desc: "此类是对外提供接口服务",
      exam: "",
    };
  }

  methodDescs() {
    return [{
      methodDesc: `<pre><pre/>`,
      methodName: "springboard",
      paramdescs: [{
        paramDesc: "请求的行为，传递如：sjb",
        paramName: "action_process",
        paramType: "string",
        defaultValue: null,
      },
        {
          paramDesc: "业务操作类型，详情见方法中的描述",
          paramName: "action_type",
          paramType: "string",
          defaultValue: null,
        },
        {
          paramDesc: "业务操作类型的参数，action_body必须传递的参数有，times_tamp（时间戳，类型int）、sign(签名,类型string)，其余的为业务需要的参数",
          paramName: "action_body",
          paramType: "json",
          defaultValue: null,
        }
      ],
      rtnTypeDesc: `<pre><pre/>`,
      rtnType: `<pre><pre/>`
    }];
  }
}

module.exports = ActionAPI;