module_user.go 13.2 KB
package allied_creation_user

import (
	"encoding/json"
	"fmt"
	"strconv"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)

//UserSearch 搜索用户列表
func (gateway HttplibAlliedCreationUser) UserSearch(param ReqUserSearch) (*DataUserSearch, error) {
	url := gateway.baseUrL + "/user/search"
	method := "post"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:搜索用户列表。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("搜索用户列表失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取搜索用户列表失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:搜索用户列表。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析搜索用户列表:%w", err)
	}
	var data DataUserSearch
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserCreate 创建用户
func (gateway HttplibAlliedCreationUser) UserCreate(param ReqCreateUser) (*DataCreateUser, error) {
	url := gateway.baseUrL + "/user"
	method := "post"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:创建用户。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("创建用户失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取创建用户失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:创建用户。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析创建用户:%w", err)
	}
	var data DataCreateUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserUpdate 更新用户
func (gateway HttplibAlliedCreationUser) UserUpdate(param ReqUpdateUser) (*DataUpdateUser, error) {
	url := gateway.baseUrL + "/user/" + strconv.Itoa(int(param.UserId))
	method := "put"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:更新用户。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求更新用户失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取更新用户失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:更新用户。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析更新用户:%w", err)
	}
	var data DataUpdateUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserGet 获取用户
func (gateway HttplibAlliedCreationUser) UserGet(param ReqGetUser) (*DataGateUser, error) {
	url := fmt.Sprintf("%s%s%d", gateway.baseUrL, "/user/", param.UserId)
	method := "get"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:获取用户。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求获取用户失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取获取用户失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:获取用户。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析获取用户:%w", err)
	}
	var data DataGateUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserDelete 删除用户
func (gateway HttplibAlliedCreationUser) UserDelete(param ReqDeleteUser) (*DataDeleteUser, error) {
	url := fmt.Sprintf("%s%s%d", gateway.baseUrL, "/user/", param.UserId)
	method := "delete"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:删除用户。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求删除用户失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取删除用户失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:删除用户。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析删除用户:%w", err)
	}
	var data DataDeleteUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserBatchEnable 批量修改用户启用状态
func (gateway HttplibAlliedCreationUser) UserBatchEnable(param ReqBatchEnableUser) (*DataBatchEnableUser, error) {
	url := fmt.Sprintf("%s%s", gateway.baseUrL, "/user/batch-enable")
	method := "post"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:批量修改用户启用状态。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求批量修改用户启用状态失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取批量修改用户启用状态失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:批量修改用户启用状态。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析批量修改用户启用状态:%w", err)
	}
	var data DataBatchEnableUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserBatchResetPassword 批量重置密码
func (gateway HttplibAlliedCreationUser) UserBatchResetPassword(param ReqBatchResetPasswordUser) (*DataBatchResetPasswordUser, error) {
	url := fmt.Sprintf("%s%s", gateway.baseUrL, "/user/batch-reset-password")
	method := "post"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:批量重置密码。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求批量重置密码失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取批量重置密码失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:批量重置密码。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析批量重置密码:%w", err)
	}
	var data DataBatchResetPasswordUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//CooperatorUserCreate 创建共创用户
func (gateway HttplibAlliedCreationUser) CooperatorUserCreate(param ReqCreateCooperatorUser) (*DataCreateCooperatorUser, error) {
	url := fmt.Sprintf("%s%s", gateway.baseUrL, "/user/cooperator")
	method := "post"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:创建共创用户。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求创建共创用户失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取创建共创用户失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:创建共创用户。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析创建共创用户:%w", err)
	}
	var data DataCreateCooperatorUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//CooperatorUserUpdate 更新共创用户
func (gateway HttplibAlliedCreationUser) CooperatorUserUpdate(param ReqUpdateCooperatorUser) (*DataUpdateCooperatorUser, error) {
	url := fmt.Sprintf("%s%s%d", gateway.baseUrL, "/user/cooperator/", param.UserId)
	method := "put"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:更新共创用户。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求更新共创用户失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取更新共创用户失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:更新共创用户。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析更新共创用户:%w", err)
	}
	var data DataUpdateCooperatorUser
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserProfile 获取用户概要数据
func (gateway HttplibAlliedCreationUser) UserProfile(param ReqUserProfile) (*DataUserProfile, error) {
	url := fmt.Sprintf("%s%s%d%s", gateway.baseUrL, "/user/", param.UserId, "/profile")
	method := "get"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:获取用户概要数据。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求获取用户概要数据失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取用户概要数据失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:获取用户概要数据。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析获取用户概要数据:%w", err)
	}
	var data DataUserProfile
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserUpdateBaseInfo 更新用户基础数据
func (gateway HttplibAlliedCreationUser) UserUpdateBaseInfo(param ReqUserUpdateBaseInfo) (*DataUserUpdateBaseInfo, error) {
	url := fmt.Sprintf("%s%s%d%s", gateway.baseUrL, "/user/", param.UserId, "/base-info")
	method := "put"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:更新用户基础数据。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求更新用户基础数据失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取更新用户基础数据失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:更新用户基础数据。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析更新用户基础数据:%w", err)
	}
	var data DataUserUpdateBaseInfo
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

//UserUpdateBaseInfo 返回用户有权限的菜单
func (gateway HttplibAlliedCreationUser) UserAccessMenus(param ReqUserAccessMenus) (*DataUserAccessMenus, error) {
	url := fmt.Sprintf("%s%s%d%s", gateway.baseUrL, "/user/", param.UserId, "/access-menus")
	url += fmt.Sprintf("?menuCategory=%v&&allDisableMenu=%v", param.MenuCategory, param.ALLDisableMenu)
	method := "get"
	req := gateway.CreateRequest(url, method)
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:返回用户有权限的菜单。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求返回用户有权限的菜单失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取返回用户有权限的菜单失败:%w", err)
	}
	log.Logger.Debug("获取用户模块请求数据:返回用户有权限的菜单。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析返回用户有权限的菜单:%w", err)
	}
	var data DataUserAccessMenus
	err = gateway.GetResponseData(result, &data)
	return &data, err
}