package typeManage

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

type GameChannelService struct {

}


//添加游戏渠道
func (g *GameChannelService) CreateGameChannel(channel typeManage.GameChannel) (err error)  {
	if !errors.Is(global.GVA_DB.Table("game_channel").Where("channel_name = ?",channel.ChannelName).First(&typeManage.GameChannel{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("已存在相同渠道，请勿重复添加")
	}
	return global.GVA_DB.Table("game_channel").Omit("create_time", "update_time").Create(&channel).Error
}


//删除游戏渠道
func (g *GameChannelService) DeleteGameChannel(channel typeManage.GameChannel) (err error)  {
	var entity typeManage.GameChannel
	err = global.GVA_DB.Table("game_channel").Where("id = ?", channel.Id).First(&entity).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}
	return global.GVA_DB.Table("game_channel").Delete(&entity).Error
}


//批量删除
func (g *GameChannelService) DeleteGameChannelsByIds(ids request.IdsReq) (err error)  {

	err = global.GVA_DB.Table("game_channel").Delete(&[]typeManage.GameChannel{}, "id in ?", ids.Ids).Error
	return err
}



//更改渠道信息
func (g *GameChannelService) UpdateGameChannel(channel typeManage.GameChannel) (err error) {
	if !errors.Is(global.GVA_DB.Table("game_channel").Where("channel_name = ? and id != ?", channel.ChannelName, channel.Id).First(&typeManage.GameChannel{}).Error,gorm.ErrRecordNotFound) {
		return errors.New("已存在相同渠道，无法更新")
	}
	return global.GVA_DB.Table("game_channel").Updates(&channel).Error
}

//通过id查询
func (g *GameChannelService) GetGameChannelById(id int) (channel typeManage.GameChannel, err error)  {
	err = global.GVA_DB.Table("game_channel").Where("id = ?", id).First(&channel).Error
	return
}

//条件查询
func (g *GameChannelService) GetGameChannelInfoList(channel typeManage.GameChannel, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error)  {
	limit := info.PageSize
	offset := (info.Page - 1) * info.PageSize

	//先更新游戏数量的值
	global.GVA_DB.Exec("update `game_channel` gc set game_counts = (SELECT COUNT(gl.game_port) FROM game_list gl WHERE gc.channel_name = gl.game_port);")

	//对结构体进行操作
	db := global.GVA_DB.Table("game_channel").Model(&typeManage.GameChannel{})
	var channelList []typeManage.GameChannel

	if channel.ChannelName != "" {
		db = db.Where("channel_name like ?", "%" + channel.ChannelName + "%")
	}

	//查询是否有错误
	err = db.Count(&total).Error
	if err != nil {
		return channelList, total, err
	} else {
		//如果没有出错，先分页，再排序
		db = db.Limit(limit).Offset(offset)
		//对字段进行排序，要求是页面显示出来的所有字段，否则默认按照厂商名称来进行排序
		//首先新建一个集合，用来防止恶意注入
		if order != "" {
			var orderStr string
			orderMap := make(map[string]bool, 6)
			orderMap["channel_name"] = true
			orderMap["channel_desc"] = true
			orderMap["display_sequence"] = true
			orderMap["game_counts"] = 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 channelList, total, err
			}
			//按照给定的排序字段以及排序规则来进行排序
			err = db.Order(orderStr).Find(&channelList).Error
		} else {
			//没有传入排序字段，直接按照厂商名称默认排序
			err = db.Order("channel_name").Find(&channelList).Error
		}
	}
	return channelList, total, err
}