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.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");

    }

    /**
     * 接口跳转
     * 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 "addOrg":
                opResult = await this.orgSve.apiAddOrg(action_body);
                break;
            case "updOrg":
                opResult = await this.orgSve.apiUpdOrg(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 "byPid":
                opResult = await this.orgSve.apiByPid(action_body);
                break;
            case "orgTree":
                opResult = await this.orgSve.apiTree();
                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 "byPid":
                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 "addUser":
                action_body.org = await this.orgSve.findById(Number(action_body.org_id));
                if (!action_body.org) {
                    opResult = system.getResult(null, `组织机构不存在`);
                } else {
                    opResult = this.userSve.add(action_body);
                }
                break;
            case "updUser":
                action_body.org = await this.orgSve.findById(Number(action_body.org_id));
                if (!action_body.org) {
                    opResult = system.getResult(null, `当前SAAS不能删除`);
                } else {
                    opResult = this.userSve.upd(action_body);
                }
                break;
            case "userInfo":
                opResult = this.userSve.info(action_body);
                break;
            case "delUser":
                break;
            case "listUser":
                opResult = this.userSve.pageByCondition(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;