/**
 * Copyright (c) 2021,Zhejiang @hua
 * All rights reserved.
 *
 * file name:SDKApi.cs
 *
 * version:1.0
 * author:27401
 * date:2021-6-18
 * history:create
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace DPSDK_CSharpDemo
{
    class SDKApi
    {

        // Get MQ config
        public static int GetMQConfig(ref GetMQConfigResponseData respData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyBody = false;
            baseReq.strUri = "/admin/API/BRM/Config/GetMqConfig?token=" + SDKBase.GetToken();

            BaseProV7Request<BaseProV7RequestData> req = new BaseProV7Request<BaseProV7RequestData>();
            req.method = "BRM.Config.GetMqConfig";
            req.data = new BaseProV7RequestData();
            req.data.optional = baseReq.strUri;

            BaseResponse<GetMQConfigResponseData> resp = new BaseResponse<GetMQConfigResponseData>();
            resp.data = new GetMQConfigResponseData();

            int iRet = SDKBase.RequestV7<BaseProV7Request<BaseProV7RequestData>, BaseResponse<GetMQConfigResponseData>>(ref baseReq, ref req, ref resp);
            respData = resp.data;
            return iRet;
        }

        // Get page users
        public static int GetPageUsers(ref GetPageUsersUrlParam urlParam, ref GetPageUsersResponseData respData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = false;
            baseReq.strMethod = "GET";
            baseReq.strUri = "/brms/api/v1.1/user/page?";

            BaseProV8Request<GetPageUsersUrlParam, String> req = new BaseProV8Request<GetPageUsersUrlParam, String>();
            req.urlParam = urlParam;

            BaseResponse<GetPageUsersResponseData> resp = new BaseResponse<GetPageUsersResponseData>();
            resp.data = new GetPageUsersResponseData();

            int iRet = SDKBase.RequestV8<GetPageUsersUrlParam, String, BaseResponse<GetPageUsersResponseData>>(ref baseReq, ref req, ref resp);
            respData = resp.data;
            return iRet;
        }
        // example
        public static int Test()
        {
            GetMQConfigResponseData respDataMQ = new GetMQConfigResponseData();
            SDKApi.GetMQConfig(ref respDataMQ);

            GetPageUsersUrlParam urlParamPageUsers = new GetPageUsersUrlParam();
            urlParamPageUsers.page = "1";
            urlParamPageUsers.pageSize = "20";
            GetPageUsersResponseData respDataPageUsers = new GetPageUsersResponseData();
            SDKApi.GetPageUsers(ref urlParamPageUsers, ref respDataPageUsers);

            return 0;
        }
        //Add role
        public static int AddRole(ref AddRoleRequestData reqData, ref AddRoleResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = false;
            baseReq.strMethod = "POST";
            baseReq.strUri = "/brms/api/v1.1/role";

            BaseProV8Request<String, AddRoleRequestData> req = new BaseProV8Request<String, AddRoleRequestData>();
            req.data = reqData;

            BaseResponse<AddRoleResponseData> rsp = new BaseResponse<AddRoleResponseData>();
            rsp.data = new AddRoleResponseData();

            int iRet = SDKBase.RequestV8<String, AddRoleRequestData, BaseResponse<AddRoleResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }          
            return iRet;
        }
        // Delete role
        // ids: Role ID, separated by commas
        public static int DeleteRole(ref DeleteRolesRequestUrlParam reqUrlParam, ref DeleteRolesResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = false;
            baseReq.bEmptyBody = true;
            baseReq.strMethod = "DELETE";
            baseReq.strUri = "/brms/api/v1.1/role?";

            BaseProV8Request<DeleteRolesRequestUrlParam, String> req = new BaseProV8Request<DeleteRolesRequestUrlParam, String>();
            req.urlParam = reqUrlParam;

            BaseResponse<DeleteRolesResponseData> rsp = new BaseResponse<DeleteRolesResponseData>();
            rsp.data = new DeleteRolesResponseData();

            int iRet = SDKBase.RequestV8<DeleteRolesRequestUrlParam, String, BaseResponse<DeleteRolesResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }            
            return iRet;
        }
        // Get role
        // id: Role ID
        public static int GetRole(ref string id, ref GetRoleResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = true;
            baseReq.strMethod = "GET";
            baseReq.strUri = string.Format("/brms/api/v1.1/role/{0}", id);

            BaseProV8Request<String, String> req = new BaseProV8Request<String, String>();

            BaseResponse<GetRoleResponseData> rsp = new BaseResponse<GetRoleResponseData>();
            rsp.data = new GetRoleResponseData();

            int iRet = SDKBase.RequestV8<String, String, BaseResponse<GetRoleResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }
            return iRet;
        }
        // Get role list
        public static int GetRoleList(ref GetRolesResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = true;
            baseReq.strMethod = "GET";
            baseReq.strUri = "/brms/api/v1.1/role/list";

            BaseProV8Request<String, String> req = new BaseProV8Request<String, String>();

            BaseResponse<GetRolesResponseData> rsp = new BaseResponse<GetRolesResponseData>();
            rsp.data = new GetRolesResponseData();

            int iRet = SDKBase.RequestV8<String, String, BaseResponse<GetRolesResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }         
            return iRet;
        }
        // Update role
        // id: Role ID
        public static int UpdateRole(ref UpdateRoleRequestData reqData, ref string id)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = false;
            baseReq.strMethod = "PUT";
            baseReq.strUri = string.Format("/brms/api/v1.1/role/{0}", id);

            BaseProV8Request<String, UpdateRoleRequestData> req = new BaseProV8Request<String, UpdateRoleRequestData>();
            req.data = reqData;

            BaseResponse<String> rsp = new BaseResponse<String>();

            int iRet = SDKBase.RequestV8<String, UpdateRoleRequestData, BaseResponse<String>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode != 1000)
                {
                    iRet = iCode;
                }
            }    
            return iRet;
        }
        //Add user
        public static int AddUser(ref AddUserRequestData reqData, ref AddUserResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();         
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = false;
            baseReq.strMethod = "POST";
            baseReq.strUri = "/brms/api/v1.1/user";

            BaseProV8Request<String, AddUserRequestData> req = new BaseProV8Request<String, AddUserRequestData>();
            req.data = reqData;

            BaseResponse<AddUserResponseData> rsp = new BaseResponse<AddUserResponseData>();
            rsp.data = new AddUserResponseData();

            int iRet = SDKBase.RequestV8<String, AddUserRequestData, BaseResponse<AddUserResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }           
            return iRet;
        }
        // Delete user
        // ids: User ID, separated by commas
        public static int DeleteUser(ref DeleteUserRequestUrlParam reqUrlParam)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = false;
            baseReq.bEmptyBody = true;
            baseReq.strMethod = "DELETE";
            baseReq.strUri = "/brms/api/v1.1/user?";

            BaseProV8Request<DeleteUserRequestUrlParam, String> req = new BaseProV8Request<DeleteUserRequestUrlParam, String>();
            req.urlParam = reqUrlParam;

            BaseResponse<String> rsp = new BaseResponse<String>();

            int iRet = SDKBase.RequestV8<DeleteUserRequestUrlParam, String, BaseResponse<String>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode != 1000)
                {
                    iRet = iCode;
                }
            }  
            return iRet;
        }
        // Get user
        // id: User ID
        public static int GetUser(ref string id, ref GetUserResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = true;
            baseReq.strMethod = "GET";
            baseReq.strUri = string.Format("/brms/api/v1.1/user/{0}", id);

            BaseProV8Request<String, String> req = new BaseProV8Request<String, String>();

            BaseResponse<GetUserResponseData> rsp = new BaseResponse<GetUserResponseData>();
            rsp.data = new GetUserResponseData();

            int iRet = SDKBase.RequestV8<String, String, BaseResponse<GetUserResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }         
            return iRet;
        }
        // Get user list
        public static int GetUserList(ref GetUsersRequestUrlParam reqUrlParam, ref GetUsersResponseData rspData)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = false;
            baseReq.bEmptyBody = true;
            baseReq.strMethod = "GET";
            baseReq.strUri = "/brms/api/v1.1/user/list?";

            BaseProV8Request<GetUsersRequestUrlParam, String> req = new BaseProV8Request<GetUsersRequestUrlParam, String>();
            req.urlParam = reqUrlParam;

            BaseResponse<GetUsersResponseData> rsp = new BaseResponse<GetUsersResponseData>();
            rsp.data = new GetUsersResponseData();

            int iRet = SDKBase.RequestV8<GetUsersRequestUrlParam, String, BaseResponse<GetUsersResponseData>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode == 1000)
                {
                    rspData = rsp.data;
                }
                else
                {
                    iRet = iCode;
                }
            }          
            return iRet;
        }
        // Save user
        // id: User ID
        public static int UpdateUser(ref UpdateUserRequestData reqData, ref string id)
        {
            BaseRequest baseReq = new BaseRequest();
            baseReq.bEmptyUrlParam = true;
            baseReq.bEmptyBody = false;
            baseReq.strMethod = "PUT";
            baseReq.strUri = string.Format("/brms/api/v1.1/user/{0}", id);

            BaseProV8Request<String, UpdateUserRequestData> req = new BaseProV8Request<String, UpdateUserRequestData>();
            req.data = reqData;

            BaseResponse<String> rsp = new BaseResponse<String>();

            int iRet = SDKBase.RequestV8<String, UpdateUserRequestData, BaseResponse<String>>(ref baseReq, ref req, ref rsp);
            if (iRet == 0)
            {
                int iCode = rsp.code;
                if (iCode != 1000)
                {
                    iRet = iCode;
                }
            }  
            return iRet;
        }
    }
}
