|
|
@@ -1,12 +1,15 @@
|
|
|
package typeManage
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"gorm.io/gorm"
|
|
|
"log-server/global"
|
|
|
"log-server/model/common/request"
|
|
|
"log-server/model/typeManage"
|
|
|
+ "strconv"
|
|
|
)
|
|
|
|
|
|
type GameListService struct{}
|
|
|
@@ -17,7 +20,11 @@ func (g *GameListService) CreateGameList(game typeManage.GameList) (err error)
|
|
|
if !errors.Is(global.GVA_DB.Table("game_list").Where("game_name = ? and game_port = ? and login_type = ?",game.GameName, game.GamePort, game.LoginType).First(&typeManage.GameChannel{}).Error, gorm.ErrRecordNotFound) {
|
|
|
return errors.New("已存在相同游戏,请勿重复添加")
|
|
|
}
|
|
|
- return global.GVA_DB.Table("game_list").Omit("create_time", "update_time").Create(&game).Error
|
|
|
+ //添加数据库记录
|
|
|
+ err = global.GVA_DB.Table("game_list").Omit("create_time", "update_time").Create(&game).Error
|
|
|
+ //更新redis
|
|
|
+ UpdateGameRedis(game)
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
//删除游戏列表
|
|
|
@@ -27,12 +34,18 @@ func (g *GameListService) DeleteGameList(game typeManage.GameList) (err error)
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
return err
|
|
|
}
|
|
|
- return global.GVA_DB.Table("game_list").Delete(&entity).Error
|
|
|
+ //先执行删除操作,再删除redis缓存
|
|
|
+ err = global.GVA_DB.Table("game_list").Delete(&entity).Error
|
|
|
+ //删除redis缓存
|
|
|
+ DeleteGameRedis(entity)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
//批量删除
|
|
|
func (g *GameListService) DeleteGameListsByIds(ids request.IdsReq) (err error) {
|
|
|
err = global.GVA_DB.Table("game_list").Delete(&[]typeManage.GameList{}, "id in ?", ids.Ids).Error
|
|
|
+ //批量删除redis
|
|
|
+ BulkDeletionGame(ids.Ids)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
@@ -45,7 +58,11 @@ func (g *GameListService) UpdateGameList(game typeManage.GameList) (err error) {
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
return errors.New("已存在相同游戏,无法更新")
|
|
|
}
|
|
|
- return global.GVA_DB.Table("game_list").Updates(&game).Error
|
|
|
+ //先执行更新操作,再删除redis缓存
|
|
|
+ err = global.GVA_DB.Table("game_list").Updates(&game).Error
|
|
|
+ //更新redis缓存
|
|
|
+ UpdateGameRedis(game)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -56,6 +73,8 @@ func (g *GameListService) UpdateGameList(game typeManage.GameList) (err error) {
|
|
|
//单个查询
|
|
|
func (g *GameListService) GetGameListById(id int) (gameList typeManage.GameList, err error) {
|
|
|
err = global.GVA_DB.Table("game_list").Where("id = ?", id).First(&gameList).Error
|
|
|
+ //更改redis缓存
|
|
|
+ UpdateGameRedis(gameList)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -65,29 +84,44 @@ func (g *GameListService) GetGameList(game typeManage.SearchGameListParams, info
|
|
|
//计算limit和offset
|
|
|
limit := info.PageSize
|
|
|
offset := (info.Page - 1) * limit
|
|
|
-
|
|
|
- //登录方式和游戏厂商获取
|
|
|
+ //通过redis获取登录方式和游戏厂商
|
|
|
option := typeManage.Option{}
|
|
|
-
|
|
|
- //首先判断登录方式和游戏厂商存不存在,如果不存在,就不必find了
|
|
|
- //判断是否有记录,如果有就查询,如果没有,报错
|
|
|
- //有记录
|
|
|
- //err = global.GVA_DB.Table("game_channel").Model(&typeManage.GameChannel{}).Count(&total).Error
|
|
|
- //if err != nil {
|
|
|
- //
|
|
|
- //}
|
|
|
- global.GVA_DB.Table("game_channel").Select("channel_name").Find(&option.GamePortOptions)
|
|
|
-
|
|
|
- //判断是否有记录,如果有就查询,如果没有,报错
|
|
|
- //有记录
|
|
|
- global.GVA_DB.Table("login_type").Select("login_name").Find(&option.LoginTypeOptions)
|
|
|
-
|
|
|
-
|
|
|
- //fmt.Println(option)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ gamePortOption := GamePortOptionQuery()
|
|
|
+ loginTypeOption := LoginTypeOptionQuery()
|
|
|
+ if gamePortOption == nil || loginTypeOption == nil {
|
|
|
+ //首先判断登录方式和游戏厂商存不存在,如果不存在,就不必find了
|
|
|
+ //判断是否有记录,如果有就查询,如果没有,报错
|
|
|
+ //有记录
|
|
|
+ //err = global.GVA_DB.Table("game_channel").Model(&typeManage.GameChannel{}).Count(&total).Error
|
|
|
+ //if err != nil {
|
|
|
+ //
|
|
|
+ //}
|
|
|
+ global.GVA_DB.Table("game_channel").Select("channel_name").Find(&option.GamePortOptions)
|
|
|
+ //判断是否有记录,如果有就查询,如果没有,报错
|
|
|
+ //有记录
|
|
|
+ global.GVA_DB.Table("login_type").Select("login_name").Find(&option.LoginTypeOptions)
|
|
|
+ //fmt.Println(option)
|
|
|
+ //将option写入redis
|
|
|
+ //序列化option对象
|
|
|
+ //datas, _ := json.Marshal(option)
|
|
|
+ //global.GVA_REDIS.Set(ctx, key, datas, 30* time.Minute)
|
|
|
+ } else {
|
|
|
+ //找到redis记录
|
|
|
+ //先将map序列化,然后反序列化成结构体,最后传输
|
|
|
+ //var objectChannel typeManage.GamePortOption
|
|
|
+ //m := map[string]interface{}{
|
|
|
+ // "id":"hahaha",
|
|
|
+ // "info":"heheeh",
|
|
|
+ //}
|
|
|
+ //var object typeManage.TestStruct
|
|
|
+ //fmt.Println(m)
|
|
|
+ //bytes, _ := json.Marshal(m)
|
|
|
+ //err = json.Unmarshal(bytes, &object)
|
|
|
+ //fmt.Println(err)
|
|
|
+ //fmt.Println(object)
|
|
|
+ option.GamePortOptions = gamePortOption
|
|
|
+ option.LoginTypeOptions = loginTypeOption
|
|
|
+ }
|
|
|
|
|
|
//绑定操作model
|
|
|
db := global.GVA_DB.Table("game_list").Model(&typeManage.GameList{})
|
|
|
@@ -141,3 +175,77 @@ func (g *GameListService) GetGameList(game typeManage.SearchGameListParams, info
|
|
|
return gameList,option, total, err
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+//更改redis记录
|
|
|
+func UpdateGameRedis(game typeManage.GameList) {
|
|
|
+ ctx := context.Background()
|
|
|
+ key := "typeManage:gameList"
|
|
|
+ field := strconv.FormatInt(game.Id, 10)
|
|
|
+ //序列化
|
|
|
+ datas, _ := json.Marshal(game)
|
|
|
+ global.GVA_REDIS.HSet(ctx, key, field, datas)
|
|
|
+}
|
|
|
+
|
|
|
+//删除redis记录
|
|
|
+func DeleteGameRedis(game typeManage.GameList) {
|
|
|
+ //删除redis记录
|
|
|
+ ctx := context.Background()
|
|
|
+ key := "typeManage:gameList"
|
|
|
+ field := strconv.FormatInt(game.Id, 10)
|
|
|
+ global.GVA_REDIS.HDel(ctx, key, field)
|
|
|
+}
|
|
|
+
|
|
|
+//批量删除命令
|
|
|
+func BulkDeletionGame(ids []int) {
|
|
|
+ //批量删除redis
|
|
|
+ ctx := context.Background()
|
|
|
+ key := "typeManage:gameList"
|
|
|
+ for i := range ids{
|
|
|
+ delKey := strconv.Itoa(ids[i])
|
|
|
+ global.GVA_REDIS.HDel(ctx, key, delKey)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//定义游戏厂商结构体,以及返回游戏厂商结构体
|
|
|
+func GamePortOptionQuery() (channelOptionReturn []typeManage.GamePortOption){
|
|
|
+ var channelOption []typeManage.GamePortOption //回传前端的信息结构体
|
|
|
+ var objectChannel typeManage.GamePortOption //用于反序列化的结构体
|
|
|
+ ctx := context.Background() //redis上下文环境
|
|
|
+ key := "typeManage:gameChannel" //查询的redis表
|
|
|
+ channelKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result() //hash表中的所有key
|
|
|
+ return RedisQueryChannel(ctx, channelOption, objectChannel, key, channelKeys)
|
|
|
+}
|
|
|
+
|
|
|
+//定义登录方式结构体,以及返回登录方式结构体
|
|
|
+func LoginTypeOptionQuery() (loginOptionReturn []typeManage.LoginTypeOption){
|
|
|
+ var loginOption []typeManage.LoginTypeOption //回传前端的信息结构体
|
|
|
+ var objectLogin typeManage.LoginTypeOption //用于反序列化的结构体
|
|
|
+ ctx := context.Background() //redis上下文环境
|
|
|
+ key := "typeManage:loginType" //查询的redis表
|
|
|
+ loginKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result() //hash表中的所有key
|
|
|
+ return RedisQueryLogin(ctx, loginOption, objectLogin, key, loginKeys)
|
|
|
+}
|
|
|
+
|
|
|
+//查询游戏厂商具体数据
|
|
|
+func RedisQueryChannel(ctx context.Context, channelOption []typeManage.GamePortOption, objectChannel typeManage.GamePortOption, keyName string, keys []string) (channelOptionReturn []typeManage.GamePortOption){
|
|
|
+ for k := range keys{
|
|
|
+ //fmt.Println(keys[k])
|
|
|
+ bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
|
|
|
+ json.Unmarshal(bytes, &objectChannel)
|
|
|
+ fmt.Println(objectChannel)
|
|
|
+ channelOption = append(channelOption, objectChannel)
|
|
|
+ }
|
|
|
+ return channelOption
|
|
|
+}
|
|
|
+
|
|
|
+//查询登录方式具体数据
|
|
|
+func RedisQueryLogin(ctx context.Context, loginOption []typeManage.LoginTypeOption, objectLogin typeManage.LoginTypeOption, keyName string, keys []string) (loginOptionReturn []typeManage.LoginTypeOption){
|
|
|
+ for k := range keys{
|
|
|
+ //fmt.Println(keys[k])
|
|
|
+ bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
|
|
|
+ json.Unmarshal(bytes, &objectLogin)
|
|
|
+ fmt.Println(objectLogin)
|
|
|
+ loginOption = append(loginOption, objectLogin)
|
|
|
+ }
|
|
|
+ return loginOption
|
|
|
+}
|