package typeManage

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"log-server/global"
	"log-server/model/common/request"
	"log-server/model/typeManage"
)

type GameListService struct{}

//创建游戏列表
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
}

//删除游戏列表
func (g *GameListService) DeleteGameList(game typeManage.GameList) (err error)  {
	var entity typeManage.GameList
	err = global.GVA_DB.Table("game_list").Where("id = ?", game.Id).First(&entity).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}
	return global.GVA_DB.Table("game_list").Delete(&entity).Error
}

//批量删除
func (g *GameListService) DeleteGameListsByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Table("game_list").Delete(&[]typeManage.GameList{}, "id in ?", ids.Ids).Error
	return err
}


//更改游戏列表
func (g *GameListService) UpdateGameList(game typeManage.GameList) (err error) {
	//查询是否有相同游戏
	err = global.GVA_DB.Table("game_list").Where("id != ? and game_name = ? and game_port = ? and login_type = ?",
		game.Id, game.GameName, game.GamePort, game.LoginType).First(&typeManage.GameList{}).Error
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("已存在相同游戏，无法更新")
	}
	return global.GVA_DB.Table("game_list").Updates(&game).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
	return
}


//条件查询
func (g *GameListService) GetGameList(game typeManage.SearchGameListParams, info request.PageInfo, order string, desc bool) (list interface{}, options interface{}, total int64, err error) {
	//计算limit和offset
	limit := info.PageSize
	offset := (info.Page - 1) * limit

	//登录方式和游戏厂商获取
	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)





	//绑定操作model
	db := global.GVA_DB.Table("game_list").Model(&typeManage.GameList{})
	var gameList []typeManage.GameList
	//通过传输的前端信息进行like查询过滤，此后总记录就不会再变，只会进行分页排序等软业务。所以查找总数
	//if game.GameName != "" {
	//	db.Where("game_name like ?", "%"+game.GameName+"%")
	//}
	//if game.GamePort != "" {
	//	db.Where("game_port like ?", "%"+game.GamePort+"%")
	//}
	//if game.LoginType != "" {
	//	db.Where("login_type like ?", "%"+game.LoginType+"%")
	//}
	if game.SearchValue != "" {
		db.Where(game.SearchKey + " like ?", "%"+game.SearchValue+"%")
	}
	//记录总数
	err = db.Count(&total).Error
	//分页
	if err != nil {
		return gameList,option, total, err
	} else {
		db.Limit(limit).Offset(offset)
		//如果传入排序规则，则按照排序规则排序，否则默认按照游戏名称排序
		if order != "" {
			//为了防止sql注入，本次采取初始化map方法
			var orderStr string
			orderMap := make(map[string]bool, 3)
			orderMap["game_name"] = true
			orderMap["create_time"] = true
			orderMap["update_time"] = true
			if orderMap[order] {
				if desc {
					orderStr = order + " desc"
				} else {
					orderStr = order
				}
			} else {
				//传入排序字段非法
				err = fmt.Errorf("传入非法字段 %v", order)
				return gameList,option, total, err
			}
			//通过排序规则加以排序
			err = db.Order(orderStr).Find(&gameList).Error
		} else {
			//没有传入排序字段，按默认排序
			err = db.Order("game_name").Find(&gameList).Error
		}
		//返回
		return gameList,option, total, err
	}
}
