150 lines
3.5 KiB
Go
150 lines
3.5 KiB
Go
package game
|
|
|
|
import (
|
|
"context"
|
|
"server/internal/dao"
|
|
"server/internal/model"
|
|
"server/internal/model/do"
|
|
"server/internal/service"
|
|
"server/utility/ecode"
|
|
)
|
|
|
|
type sGame struct {
|
|
}
|
|
|
|
func New() service.IGame {
|
|
return &sGame{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterGame(New())
|
|
}
|
|
|
|
func (s *sGame) GameList(ctx context.Context, in *model.GameListIn) (out *model.GameListOut, err error) {
|
|
|
|
list := make([]model.Game, 0)
|
|
var total int
|
|
err = dao.Games.Ctx(ctx).Page(in.Page, in.Size).ScanAndCount(&list, &total, false)
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("游戏列表获取失败")
|
|
}
|
|
return &model.GameListOut{
|
|
List: list,
|
|
Total: total,
|
|
}, nil
|
|
}
|
|
|
|
func (s *sGame) CreateGame(ctx context.Context, in *model.AddGameIn) (out *model.AddGameOut, err error) {
|
|
|
|
// 判断游戏是否存在
|
|
exist, err := dao.Games.Ctx(ctx).Where(do.Games{GameId: in.GameId}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("新增游戏异常")
|
|
}
|
|
|
|
if exist {
|
|
return nil, ecode.Params.Sub("游戏ID已存在")
|
|
}
|
|
|
|
exist, err = dao.Games.Ctx(ctx).Where(do.Games{GameName: in.GameName}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("新增游戏异常")
|
|
}
|
|
|
|
if exist {
|
|
return nil, ecode.Params.Sub("游戏名称已存在")
|
|
}
|
|
|
|
exist, err = dao.Games.Ctx(ctx).Where(do.Games{GameCode: in.GameCode}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("新增游戏异常")
|
|
}
|
|
|
|
if exist {
|
|
return nil, ecode.Params.Sub("游戏编码已存在")
|
|
}
|
|
|
|
_, err = dao.Games.Ctx(ctx).Insert(do.Games{
|
|
GameId: in.GameId,
|
|
GameName: in.GameName,
|
|
GameCode: in.GameCode,
|
|
Avatar: in.Avatar,
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("新增游戏失败")
|
|
}
|
|
|
|
return &model.AddGameOut{Success: true}, nil
|
|
}
|
|
|
|
func (s *sGame) UpdateGame(ctx context.Context, in *model.UpdateGameIn) (out *model.UpdateGameOut, err error) {
|
|
|
|
// 判断游戏是否存在
|
|
exist, err := dao.Games.Ctx(ctx).Where(do.Games{Id: in.Id}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("修改游戏异常")
|
|
}
|
|
|
|
if !exist {
|
|
return nil, ecode.Params.Sub("该游戏不存在")
|
|
}
|
|
|
|
exist, err = dao.Games.Ctx(ctx).Where(do.Games{GameId: in.GameId}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("修改游戏异常")
|
|
}
|
|
|
|
if exist {
|
|
return nil, ecode.Params.Sub("游戏ID已存在")
|
|
}
|
|
|
|
exist, err = dao.Games.Ctx(ctx).Where(do.Games{GameName: in.GameName}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("修改游戏异常")
|
|
}
|
|
|
|
if exist {
|
|
return nil, ecode.Params.Sub("游戏名称已存在")
|
|
}
|
|
|
|
exist, err = dao.Games.Ctx(ctx).Where(do.Games{GameCode: in.GameCode}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("修改游戏异常")
|
|
}
|
|
|
|
if exist {
|
|
return nil, ecode.Params.Sub("游戏编码已存在")
|
|
}
|
|
|
|
_, err = dao.Games.Ctx(ctx).Where(do.Games{Id: in.Id}).Update(do.Games{
|
|
GameId: in.GameId,
|
|
GameName: in.GameName,
|
|
GameCode: in.GameCode,
|
|
Avatar: in.Avatar,
|
|
})
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("修改游戏失败")
|
|
}
|
|
return &model.UpdateGameOut{Success: true}, nil
|
|
}
|
|
|
|
func (s *sGame) DeleteGame(ctx context.Context, in *model.DeleteGameIn) (out *model.DeleteGameOut, err error) {
|
|
|
|
// 判断是否存在
|
|
exist, err := dao.Games.Ctx(ctx).Where(do.Games{Id: in.Id}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("删除该游戏异常")
|
|
}
|
|
|
|
if !exist {
|
|
return nil, ecode.Params.Sub("游戏不存在")
|
|
}
|
|
|
|
_, err = dao.Games.Ctx(ctx).Delete(do.Games{Id: in.Id})
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("删除游戏失败")
|
|
}
|
|
return &model.DeleteGameOut{Success: true}, nil
|
|
}
|