172 lines
4.6 KiB
Go
172 lines
4.6 KiB
Go
package smdl
|
|
|
|
import (
|
|
"admin/apps/game/domain/entity"
|
|
"admin/apps/game/domain/projects/smdl/internal"
|
|
"admin/apps/game/model"
|
|
"admin/internal/errcode"
|
|
dto2 "admin/internal/model/dto"
|
|
"admin/lib/httpclient"
|
|
"admin/lib/utils"
|
|
"net/url"
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
type Role struct {
|
|
RoleId string `json:"roleId"`
|
|
Account string `json:"account"`
|
|
ServerConfId string `json:"serverId"`
|
|
Name string `json:"roleName"`
|
|
Status string `json:"status"`
|
|
Level int `json:"roleLevel"`
|
|
Profession string `json:"profession"`
|
|
LatestLoginTime int64 `json:"latestLoginAt"`
|
|
CreateTime int64 `json:"createAt"`
|
|
Items []struct {
|
|
ID int64 `json:"item_id"`
|
|
Name string `json:"item_name"`
|
|
Num int64 `json:"num"`
|
|
}
|
|
}
|
|
|
|
type RoleHook struct {
|
|
}
|
|
|
|
func (hook *RoleHook) List(projectInfo *entity.Project, resource string, params *dto2.CommonListReq,
|
|
fields []*dto2.CommonDtoFieldDesc, totalCount int, rows []dto2.CommonDtoValues) (
|
|
int, []*dto2.CommonDtoFieldDesc, []dto2.CommonDtoValues, error) {
|
|
alisrvAddr := projectInfo.GetApiAddr()
|
|
if alisrvAddr == "" {
|
|
return 0, nil, nil, errcode.New(errcode.ServerError, "项目%v没有配置api服务器地址", projectInfo.Po.Name)
|
|
}
|
|
|
|
serverList, err := getAllRunningServers(projectInfo)
|
|
if err != nil {
|
|
return 0, nil, nil, err
|
|
}
|
|
|
|
totalCount, reqPageServerList, err := getPaginationServerReqList(projectInfo, serverList, params, func(info *internal.ServerInfo) int { return info.TotalRoleCount })
|
|
if err != nil {
|
|
return 0, nil, nil, err
|
|
}
|
|
|
|
type RspData struct {
|
|
State struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
} `json:"state"`
|
|
Data []*Role `json:"data"`
|
|
}
|
|
|
|
body := &url.Values{}
|
|
|
|
for _, cond := range params.ParsedWhereConditions.Conditions {
|
|
if cond.Key == "Account" {
|
|
// 按账号查询
|
|
body.Set("userId", cond.Value1.(string))
|
|
} else if cond.Key == "RoleId" {
|
|
body.Set("roleId", cond.Value1.(string))
|
|
} else if cond.Key == "Name" {
|
|
body.Set("name", cond.Value1.(string))
|
|
}
|
|
}
|
|
|
|
for _, reqServer := range reqPageServerList {
|
|
body.Set("serverid", reqServer.RawInfo.ServerId)
|
|
body.Set("offset", strconv.Itoa(reqServer.Offset))
|
|
body.Set("count", strconv.Itoa(reqServer.Count))
|
|
|
|
rsp := &RspData{}
|
|
err := httpclient.Request(alisrvAddr+"/rolelist", "get", body, rsp)
|
|
if err != nil {
|
|
return 0, nil, nil, err
|
|
}
|
|
|
|
for _, role := range rsp.Data {
|
|
rolePo := &model.Role{
|
|
RoleId: role.RoleId,
|
|
Account: role.Account,
|
|
ServerConfId: role.ServerConfId,
|
|
Name: role.Name,
|
|
Status: role.Status,
|
|
Level: role.Level,
|
|
Profession: role.Profession,
|
|
LatestLoginTime: utils.ParseUnixTimestamp2LocalTime(role.LatestLoginTime),
|
|
CreatedAt: utils.ParseUnixTimestamp2LocalTime(role.CreateTime),
|
|
}
|
|
et := (&entity.CommonResource{}).FromPo(rolePo)
|
|
rows = append(rows, et.ToCommonDto())
|
|
}
|
|
}
|
|
|
|
sort.SliceStable(rows, func(i, j int) bool {
|
|
return rows[i]["Status"].(string) < rows[j]["Status"].(string)
|
|
})
|
|
|
|
return totalCount, fields, rows, nil
|
|
}
|
|
|
|
func (hook *RoleHook) GetDetail(projectInfo *entity.Project, params *dto2.GetRoleDetailReq) (*dto2.GetRoleDetailRsp, error) {
|
|
alisrvAddr := projectInfo.GetApiAddr()
|
|
if alisrvAddr == "" {
|
|
return nil, errcode.New(errcode.ServerError, "项目%v没有配置api服务器地址", projectInfo.Po.Name)
|
|
}
|
|
|
|
type RspData struct {
|
|
State struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
} `json:"state"`
|
|
Data []*Role `json:"data"`
|
|
}
|
|
|
|
body := &url.Values{}
|
|
|
|
body.Set("roleId", params.RoleId)
|
|
body.Set("serverid", params.ServerConfId)
|
|
body.Set("offset", "0")
|
|
body.Set("count", "1")
|
|
|
|
rsp := &RspData{}
|
|
err := httpclient.Request(alisrvAddr+"/rolelist", "get", body, rsp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(rsp.Data) <= 0 {
|
|
return nil, errcode.New(errcode.ServerError, "找不到服务器%v的角色信息:%v", params.ServerConfId, params.RoleId)
|
|
}
|
|
|
|
detailRsp := &dto2.GetRoleDetailRsp{
|
|
RoleInfo: &dto2.RoleDetailInfo{
|
|
ServerId: rsp.Data[0].ServerConfId,
|
|
Name: rsp.Data[0].Name,
|
|
RoleId: rsp.Data[0].RoleId,
|
|
Level: rsp.Data[0].Level,
|
|
Items: make([]*dto2.ItemInfo, 0),
|
|
},
|
|
}
|
|
for _, item := range rsp.Data[0].Items {
|
|
|
|
find := false
|
|
for _, v := range detailRsp.RoleInfo.Items {
|
|
if v.ItemID == int(item.ID) {
|
|
v.ItemNum += item.Num
|
|
find = true
|
|
break
|
|
}
|
|
}
|
|
if !find {
|
|
itemInfo := &dto2.ItemInfo{
|
|
ItemID: int(item.ID),
|
|
ItemNum: item.Num,
|
|
Desc: item.Name,
|
|
}
|
|
|
|
detailRsp.RoleInfo.Items = append(detailRsp.RoleInfo.Items, itemInfo)
|
|
}
|
|
}
|
|
return detailRsp, nil
|
|
}
|