uniugm/admin/apps/game/domain/comm_resource_selection.go
2025-07-09 10:55:26 +08:00

189 lines
6.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package domain
import (
"admin/apps/game/domain/entity"
"admin/apps/game/domain/projects"
"admin/apps/game/model"
"admin/internal/errcode"
dto2 "admin/internal/model/dto"
"admin/lib/cdn"
"admin/lib/dfs"
"admin/lib/xlog"
"bytes"
"fmt"
"strings"
"time"
)
func (svc *CommonResourceService) handleServerUpOrDown(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
if hook, ok := projects.GetProjectResourceHook(projectEt, resourceName).(projects.IPostResourceOpRowsHook); ok {
rsp, err := hook.RowsSelection(projectEt, resourceName, params.BtnKey, params.Rows)
return rsp, err
}
return nil, nil
}
func (svc *CommonResourceService) handleServerExportCdn(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
// 导出cdn聚合公告信息一起导
serverList, err := svc.serverRepo.List(projectEt.GetProjectPo().ID)
if err != nil {
return nil, err
}
noticeList, err := svc.noticeRepo.List(projectEt.GetProjectPo().ID)
if err != nil {
return nil, err
}
content, err := genCdnServerListContent(projectEt, serverList, noticeList)
if err != nil {
return nil, err
}
secretId := "AKIDqZ6SbXGsX6UffS4VnskRvEQXWLzQ2Xpu"
secretKey := "BDBlGajfQ2ShjZCbnv581jEZiy3vZ4MR"
region := projectEt.Po.DfsRegion
cosClient := dfs.NewDfsCos(secretId, secretKey, projectEt.Po.DfsBucket, region)
opFilePathList := []string{
"Default_Update/AndroidTest/REMAIN-ServerList.bytes",
"Default_Update/iPhoneTest/REMAIN-ServerList.bytes",
}
for _, opFile := range opFilePathList {
oldFileContent, err := cosClient.GetObj(opFile)
if err != nil {
xlog.Errorf("get dfs file %v content error:%v", opFile, err)
return nil, errcode.New(errcode.DBError, "get obj %v error:%v", opFile, err)
}
xlog.Infof("prepare write dfs server list file %v content:%v", opFile, string(oldFileContent))
}
xlog.Infof("new dfs server list file content:%v", content)
for _, opFile := range opFilePathList {
err := cosClient.PutObj(opFile, []byte(content))
if err != nil {
xlog.Errorf("write dfs file %v content error:%v", opFile, err)
return nil, errcode.New(errcode.DBError, "write obj %v error:%v", opFile, err)
}
}
time.Sleep(2 * time.Second)
err = cdn.PurgeTxCdnPath(secretId, secretKey, region, projectEt.Po.CdnPath)
if err != nil {
return nil, errcode.New(errcode.DBError, "purge cdn path:%v error:%v", projectEt.Po.CdnPath)
}
return &dto2.CommonRowsSelectionRsp{
Msg: "推送服务器列表文件到分布式存储成功!",
NeedRefresh: false,
FileName: "",
}, nil
}
func (svc *CommonResourceService) handleServerPrepareShowExportCdn(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
// 导出cdn聚合公告信息一起导
serverList, err := svc.serverRepo.List(projectEt.GetProjectPo().ID)
if err != nil {
return nil, err
}
noticeList, err := svc.noticeRepo.List(projectEt.GetProjectPo().ID)
if err != nil {
return nil, err
}
content, err := genCdnServerListContent(projectEt, serverList, noticeList)
if err != nil {
return nil, err
}
return &dto2.CommonRowsSelectionRsp{
Msg: content,
NeedRefresh: false,
FileName: projectEt.GetProjectPo().Name + "-区服公告信息.txt",
}, nil
}
func (svc *CommonResourceService) handleNoticeDisable(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
var err error
if len(params.Rows) == 0 {
// 禁用所有
err = svc.noticeRepo.DisableAll(projectEt.GetProjectID())
} else {
ids := make([]int, 0, len(params.Rows))
for _, v := range params.Rows {
ids = append(ids, int(v["ID"].(float64)))
}
err = svc.noticeRepo.DisableSome(projectEt.GetProjectID(), ids)
}
if err != nil {
return nil, err
}
return &dto2.CommonRowsSelectionRsp{
Msg: "禁用成功!",
NeedRefresh: true,
}, nil
}
func (svc *CommonResourceService) handleNoticeEnable(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
var err error
if len(params.Rows) == 0 {
// 禁用所有
err = svc.noticeRepo.EnableAll(projectEt.GetProjectID())
} else {
ids := make([]int, 0, len(params.Rows))
for _, v := range params.Rows {
ids = append(ids, int(v["ID"].(float64)))
}
err = svc.noticeRepo.EnableSome(projectEt.GetProjectID(), ids)
}
if err != nil {
return nil, err
}
return &dto2.CommonRowsSelectionRsp{
Msg: "启用成功!",
NeedRefresh: true,
}, nil
}
func (svc *CommonResourceService) handleGenAccountExport(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
fileContent := bytes.NewBuffer(nil)
getContentFun := func(po *model.GenAccount) {
buf := bytes.NewBuffer(nil)
accounts := strings.Split(po.AccountList, ",")
buf.WriteString(fmt.Sprintf("id:%v\n", po.ID))
buf.WriteString(fmt.Sprintf("区服:%v\n", po.ServerConfID))
buf.WriteString(fmt.Sprintf("创建时间:%v\n", po.CreatedAt.Format(time.DateTime)))
buf.WriteString(fmt.Sprintf("账号数量:%v\n", len(accounts)))
buf.WriteString(fmt.Sprintf("账号列表:\n"))
for _, account := range accounts {
buf.WriteString(fmt.Sprintf("%v\n", account))
}
buf.WriteString(fmt.Sprintf("\n"))
fileContent.WriteString(buf.String())
}
if len(params.Rows) == 0 {
poList, err := svc.genAccountRepo.ListAll(projectEt.GetProjectID())
if err != nil {
return nil, err
}
for _, po := range poList {
getContentFun(po)
}
} else {
for _, dtoObj := range params.Rows {
et := new(entity.CommonResource).FromPo(&model.GenAccount{}).FromDto(dtoObj)
po := et.Po.(*model.GenAccount)
getContentFun(po)
}
}
return &dto2.CommonRowsSelectionRsp{
Msg: fileContent.String(),
NeedRefresh: false,
FileName: projectEt.GetProjectPo().Name + "-登录白名单账号列表.txt",
}, nil
}