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("已存在相同游戏，请勿重复添加")
	}
	//数据导入测试
	//var lists = []typeManage.GameList{{Id:44,Remark:"",GameName:"龙腾传世",GamePort:"魅族",LoginType:"魅族"},{Id:76,Remark:"",GameName:"龙皇传说",GamePort:"酷派",LoginType:"小绵羊"},{Id:138,Remark:"",GameName:"龙之国物语",GamePort:"魅族",LoginType:"魅族"},{Id:75,Remark:"",GameName:"黑月",GamePort:"酷派",LoginType:"小绵羊"},{Id:115,Remark:"",GameName:"麻将来了",GamePort:"腾讯",LoginType:"微信"},{Id:154,Remark:"",GameName:"鸿途归来",GamePort:"腾讯",LoginType:"微信+QQ"},{Id:74,Remark:"魔力宝贝2酷派",GameName:"魔力宝贝觉醒（2）",GamePort:"酷派",LoginType:"小绵羊"},{Id:73,Remark:"",GameName:"魔力宝贝归来",GamePort:"酷派",LoginType:"小绵羊"},{Id:163,Remark:"",GameName:"魂之纪元",GamePort:"酷派",LoginType:"小绵羊"},{Id:143,Remark:"",GameName:"驯龙物语",GamePort:"酷派",LoginType:"小绵羊"},{Id:173,Remark:"",GameName:"风色传说",GamePort:"酷派",LoginType:"小绵羊"},{Id:43,Remark:"",GameName:"青云诀2",GamePort:"魅族",LoginType:"魅族"},{Id:72,Remark:"",GameName:"青云诀",GamePort:"魅族",LoginType:"魅族"},{Id:70,Remark:"",GameName:"零之战线",GamePort:"酷派",LoginType:"小绵羊"},{Id:69,Remark:"",GameName:"阴阳师",GamePort:"酷派",LoginType:"小绵羊"},{Id:85,Remark:"",GameName:"问道",GamePort:"腾讯",LoginType:"QQ"},{Id:185,Remark:"1000826",GameName:"长安幻想",GamePort:"酷派",LoginType:"小绵羊"},{Id:68,Remark:"",GameName:"镇魔曲",GamePort:"网易",LoginType:"小绵羊"},{Id:184,Remark:"",GameName:"镇魂街:天生为王酷派",GamePort:"酷派",LoginType:"小绵羊"},{Id:67,Remark:"",GameName:"铁杆三国",GamePort:"酷派",LoginType:"小绵羊"},{Id:118,Remark:"",GameName:"重返帝国",GamePort:"腾讯",LoginType:"微信+QQ"},{Id:66,Remark:"",GameName:"远征2",GamePort:"酷派",LoginType:"小绵羊"},{Id:98,Remark:"",GameName:"超凡之路",GamePort:"酷派",LoginType:"小绵羊"},{Id:122,Remark:"",GameName:"诺亚之心",GamePort:"腾讯",LoginType:"微信+QQ"},{Id:65,Remark:"",GameName:"诛仙",GamePort:"酷派",LoginType:"小绵羊"},{Id:137,Remark:"",GameName:"西游伏魔记酷派",GamePort:"酷派",LoginType:"小绵羊"},{Id:127,Remark:"",GameName:"街机之三国战记",GamePort:"酷派",LoginType:"小绵羊"},{Id:87,Remark:"",GameName:"蜀门",GamePort:"腾讯",LoginType:"QQ"},{Id:119,Remark:"",GameName:"蜀门",GamePort:"酷派",LoginType:"小绵羊"},{Id:64,Remark:"",GameName:"蚁族崛起",GamePort:"酷派",LoginType:"小绵羊"},{Id:63,Remark:"",GameName:"荣耀大天使",GamePort:"酷派",LoginType:"小绵羊"},{Id:78,Remark:"",GameName:"英雄联盟手游",GamePort:"腾讯",LoginType:"QQ"},{Id:113,Remark:"",GameName:"英雄杀",GamePort:"腾讯",LoginType:"微信"},{Id:95,Remark:"",GameName:"苍之骑士团2",GamePort:"酷派",LoginType:"小绵羊"},{Id:62,Remark:"",GameName:"花与剑",GamePort:"酷派",LoginType:"小绵羊"},{Id:170,Remark:"",GameName:"芒果斗地主",GamePort:"酷派",LoginType:"小绵羊"},{Id:61,Remark:"",GameName:"航海王：燃烧意志",GamePort:"酷派",LoginType:"小绵羊"},{Id:165,Remark:"",GameName:"胡莱三国5",GamePort:"酷派",LoginType:"小绵羊"},{Id:157,Remark:"",GameName:"胡莱三国4",GamePort:"酷派",LoginType:"小绵羊"},{Id:60,Remark:"",GameName:"胡莱三国3",GamePort:"酷派",LoginType:"小绵羊"},{Id:59,Remark:"",GameName:"聊斋搜灵录",GamePort:"酷派",LoginType:"小绵羊"},{Id:86,Remark:"",GameName:"绿色征途",GamePort:"腾讯",LoginType:"QQ"},{Id:58,Remark:"",GameName:"绝世武林",GamePort:"酷派",LoginType:"小绵羊"},{Id:148,Remark:"",GameName:"纯白和弦",GamePort:"酷派",LoginType:"小绵羊"},{Id:104,Remark:"",GameName:"精灵盛典黎明",GamePort:"腾讯",LoginType:"QQ"},{Id:141,Remark:"",GameName:"筑梦公馆",GamePort:"酷派",LoginType:"小绵羊"},{Id:179,Remark:"",GameName:"空之要塞：启航",GamePort:"酷派",LoginType:"小绵羊"},{Id:57,Remark:"",GameName:"神雕侠侣2",GamePort:"酷派",LoginType:"小绵羊"},{Id:56,Remark:"",GameName:"神仙道高清重制版",GamePort:"酷派",LoginType:"小绵羊"},{Id:145,Remark:"",GameName:"破天一剑",GamePort:"魅族",LoginType:"魅族"},{Id:99,Remark:"",GameName:"盟重英雄",GamePort:"腾讯",LoginType:"QQ"},{Id:180,Remark:"",GameName:"皇座战争酷派",GamePort:"酷派",LoginType:"小绵羊"},{Id:174,Remark:"",GameName:"百分之一",GamePort:"酷派",LoginType:"小绵羊"},{Id:55,Remark:"",GameName:"疯狂原始人2",GamePort:"酷派",LoginType:"小绵羊"},{Id:77,Remark:"",GameName:"王者荣耀",GamePort:"腾讯",LoginType:"QQ"},{Id:53,Remark:"",GameName:"王者传奇",GamePort:"酷派",LoginType:"小绵羊"},{Id:54,Remark:"",GameName:"王者传奇",GamePort:"魅族",LoginType:"魅族"},{Id:52,Remark:"",GameName:"率土之滨",GamePort:"酷派",LoginType:"小绵羊"},{Id:100,Remark:"",GameName:"玄中记",GamePort:"腾讯",LoginType:"QQ"},{Id:51,Remark:"",GameName:"狂暴传奇",GamePort:"酷派",LoginType:"小绵羊"},{Id:129,Remark:"",GameName:"爱琳诗篇",GamePort:"魅族",LoginType:"魅族"},{Id:88,Remark:"",GameName:"热血江湖手游",GamePort:"腾讯",LoginType:"QQ"},{Id:50,Remark:"",GameName:"热血合击",GamePort:"酷派",LoginType:"小绵羊"},{Id:102,Remark:"",GameName:"热血合击",GamePort:"腾讯",LoginType:"QQ"},{Id:133,Remark:"",GameName:"热血合击",GamePort:"魅族",LoginType:"魅族"},{Id:124,Remark:"",GameName:"点击冒险之旅",GamePort:"酷派",LoginType:"小绵羊"},{Id:49,Remark:"",GameName:"灌篮高手正版授权手游",GamePort:"酷派",LoginType:"小绵羊"},{Id:48,Remark:"",GameName:"潘多拉的回响",GamePort:"酷派",LoginType:"小绵羊"},{Id:172,Remark:"",GameName:"永恒联盟",GamePort:"酷派",LoginType:"小绵羊"},{Id:93,Remark:"",GameName:"武林闲侠",GamePort:"酷派",LoginType:"小绵羊"},{Id:45,Remark:"",GameName:"梦幻西游",GamePort:"网易",LoginType:"小绵羊"},{Id:46,Remark:"",GameName:"梦幻西游",GamePort:"酷派",LoginType:"小绵羊"},{Id:178,Remark:"",GameName:"梦幻新诛仙",GamePort:"酷派",LoginType:"小绵羊"},{Id:110,Remark:"",GameName:"梦三国",GamePort:"腾讯",LoginType:"微信"},{Id:71,Remark:"",GameName:"桃花源记",GamePort:"魅族",LoginType:"魅族"},{Id:176,Remark:"",GameName:"极无双2",GamePort:"酷派",LoginType:"小绵羊"},{Id:41,Remark:"",GameName:"最终幻想：勇气启示录 幻影战争",GamePort:"酷派",LoginType:"小绵羊"},{Id:168,Remark:"测试游戏",GameName:"暴击联盟",GamePort:"酷派",LoginType:"小绵羊"},{Id:186,Remark:"",GameName:"春秋封神",GamePort:"酷派",LoginType:"小绵羊"},{Id:160,Remark:"",GameName:"星际卫队",GamePort:"酷派",LoginType:"小绵羊"},{Id:112,Remark:"",GameName:"星辰奇缘",GamePort:"腾讯",LoginType:"微信"},{Id:116,Remark:"",GameName:"星辰变",GamePort:"腾讯",LoginType:"微信"},{Id:40,Remark:"",GameName:"明日之后",GamePort:"酷派",LoginType:"小绵羊"},{Id:158,Remark:"",GameName:"时空猎人3",GamePort:"魅族",LoginType:"魅族"},{Id:125,Remark:"",GameName:"无神之界",GamePort:"酷派",LoginType:"小绵羊"},{Id:120,Remark:"",GameName:"无悔华夏",GamePort:"酷派",LoginType:"小绵羊"},{Id:39,Remark:"",GameName:"新笑傲江湖",GamePort:"酷派",LoginType:"小绵羊"},{Id:38,Remark:"",GameName:"新神魔大陆",GamePort:"酷派",LoginType:"小绵羊"},{Id:37,Remark:"",GameName:"新盗墓笔记",GamePort:"酷派",LoginType:"小绵羊"},{Id:36,Remark:"",GameName:"新斗罗大陆",GamePort:"酷派",LoginType:"小绵羊"},{Id:35,Remark:"",GameName:"新射雕群侠传之铁血丹心",GamePort:"酷派",LoginType:"小绵羊"},{Id:132,Remark:"",GameName:"新射雕群侠传之铁血丹心",GamePort:"魅族",LoginType:"魅族"},{Id:106,Remark:"",GameName:"新凡人修仙传",GamePort:"酷派",LoginType:"小绵羊"},{Id:181,Remark:"",GameName:"新倚天屠龙记",GamePort:"酷派",LoginType:"小绵羊"},{Id:34,Remark:"",GameName:"斩魔无双",GamePort:"酷派",LoginType:"小绵羊"},{Id:31,Remark:"",GameName:"斗罗大陆：魂师对决",GamePort:"酷派",LoginType:"小绵羊"},{Id:32,Remark:"",GameName:"斗罗大陆",GamePort:"酷派",LoginType:"小绵羊"},{Id:121,Remark:"",GameName:"散人无双",GamePort:"酷派",LoginType:"小绵羊"},{Id:128,Remark:"",GameName:"捕鱼大世界",GamePort:"酷派",LoginType:"小绵羊"},{Id:30,Remark:"",GameName:"战神遗迹",GamePort:"酷派",LoginType:"小绵羊"},{Id:123,Remark:"",GameName:"战神蚩尤",GamePort:"酷派",LoginType:"小绵羊"},{Id:151,Remark:"",GameName:"战争怒吼",GamePort:"酷派",LoginType:"小绵羊"},{Id:108,Remark:"",GameName:"我本千金",GamePort:"酷派",LoginType:"小绵羊"},{Id:29,Remark:"",GameName:"我是大东家",GamePort:"酷派",LoginType:"小绵羊"},{Id:42,Remark:"",GameName:"我是大东家",GamePort:"魅族",LoginType:"魅族"},{Id:159,Remark:"",GameName:"快点三国",GamePort:"酷派",LoginType:"小绵羊"},{Id:103,Remark:"",GameName:"征途2",GamePort:"腾讯",LoginType:"QQ"},{Id:182,Remark:"",GameName:"弹弹堂大冒险酷派",GamePort:"酷派",LoginType:"小绵羊"},{Id:105,Remark:"",GameName:"异能都市",GamePort:"酷派",LoginType:"小绵羊"},{Id:117,Remark:"",GameName:"庆余年",GamePort:"腾讯",LoginType:"微信+QQ"},{Id:80,Remark:"",GameName:"幻塔",GamePort:"酷派",LoginType:"小绵羊"},{Id:81,Remark:"",GameName:"幻塔",GamePort:"腾讯",LoginType:"QQ"},{Id:79,Remark:"",GameName:"帝王三国",GamePort:"腾讯",LoginType:"QQ"},{Id:27,Remark:"",GameName:"少年三国志：零",GamePort:"酷派",LoginType:"小绵羊"},{Id:153,Remark:"",GameName:"少年三国志2",GamePort:"酷派",LoginType:"小绵羊"},{Id:175,Remark:"",GameName:"小小蚁国",GamePort:"酷派",LoginType:"小绵羊"},{Id:96,Remark:"",GameName:"完美世界：诸神之战",GamePort:"酷派",LoginType:"小绵羊"},{Id:94,Remark:"",GameName:"奶块",GamePort:"腾讯",LoginType:"QQ"},{Id:156,Remark:"",GameName:"奶块",GamePort:"魅族",LoginType:"魅族"},{Id:134,Remark:"",GameName:"奥特曼集结",GamePort:"酷派",LoginType:"小绵羊"},{Id:84,Remark:"",GameName:"天龙八部",GamePort:"腾讯",LoginType:"QQ"},{Id:82,Remark:"",GameName:"天龙3D",GamePort:"腾讯",LoginType:"QQ"},{Id:111,Remark:"",GameName:"天天酷跑",GamePort:"腾讯",LoginType:"微信"},{Id:107,Remark:"",GameName:"天使之战",GamePort:"酷派",LoginType:"小绵羊"},{Id:114,Remark:"",GameName:"天使之战",GamePort:"腾讯",LoginType:"微信"},{Id:26,Remark:"",GameName:"天下",GamePort:"酷派",LoginType:"小绵羊"},{Id:24,Remark:"",GameName:"大话西游",GamePort:"网易",LoginType:"小绵羊"},{Id:25,Remark:"",GameName:"大话西游",GamePort:"酷派",LoginType:"小绵羊"},{Id:23,Remark:"",GameName:"大秦帝国之帝国烽烟",GamePort:"酷派",LoginType:"小绵羊"},{Id:21,Remark:"",GameName:"大唐无双",GamePort:"网易",LoginType:"小绵羊"},{Id:22,Remark:"",GameName:"大唐无双",GamePort:"酷派",LoginType:"小绵羊"},{Id:162,Remark:"",GameName:"坦克前线",GamePort:"腾讯",LoginType:"微信+QQ"},{Id:20,Remark:"",GameName:"坦克世界",GamePort:"魅族",LoginType:"魅族"},{Id:144,Remark:"",GameName:"坍塌世界",GamePort:"酷派",LoginType:"小绵羊"},{Id:135,Remark:"",GameName:"圣灵之境",GamePort:"酷派",LoginType:"小绵羊"},{Id:19,Remark:"",GameName:"圣斗士星矢：正义传说",GamePort:"酷派",LoginType:"小绵羊"},{Id:18,Remark:"",GameName:"哥哥捕鱼",GamePort:"酷派",LoginType:"小绵羊"},{Id:17,Remark:"",GameName:"叫我大掌柜",GamePort:"酷派",LoginType:"小绵羊"},{Id:28,Remark:"",GameName:"口袋觉醒",GamePort:"魅族",LoginType:"魅族"},{Id:16,Remark:"",GameName:"双生幻想",GamePort:"酷派",LoginType:"小绵羊"},{Id:139,Remark:"",GameName:"原始征途",GamePort:"酷派",LoginType:"小绵羊"},{Id:15,Remark:"",GameName:"原始传奇",GamePort:"酷派",LoginType:"小绵羊"},{Id:109,Remark:"",GameName:"原始传奇",GamePort:"腾讯",LoginType:"微信"},{Id:155,Remark:"",GameName:"原始传奇",GamePort:"魅族",LoginType:"魅族"},{Id:14,Remark:"",GameName:"劲舞时代",GamePort:"酷派",LoginType:"小绵羊"},{Id:13,Remark:"",GameName:"劲舞团",GamePort:"360",LoginType:"小绵羊"},{Id:91,Remark:"",GameName:"剑侠世界3",GamePort:"酷派",LoginType:"小绵羊"},{Id:101,Remark:"",GameName:"剑侠世界3",GamePort:"腾讯",LoginType:"QQ"},{Id:90,Remark:"",GameName:"创造与魔法",GamePort:"腾讯",LoginType:"QQ"},{Id:126,Remark:"",GameName:"凤凰传奇",GamePort:"酷派",LoginType:"小绵羊"},{Id:12,Remark:"",GameName:"光明大陆",GamePort:"网易",LoginType:"小绵羊"},{Id:11,Remark:"",GameName:"傲世龙城",GamePort:"酷派",LoginType:"小绵羊"},{Id:9,Remark:"",GameName:"倩女幽魂",GamePort:"酷派",LoginType:"小绵羊"},{Id:10,Remark:"",GameName:"倩女幽魂",GamePort:"360",LoginType:"小绵羊"},{Id:8,Remark:"",GameName:"侠客风云传online",GamePort:"酷派",LoginType:"小绵羊"},{Id:7,Remark:"",GameName:"侠客道",GamePort:"酷派",LoginType:"小绵羊"},{Id:6,Remark:"",GameName:"仙凡幻想",GamePort:"360",LoginType:"小绵羊"},{Id:131,Remark:"",GameName:"云上城之歌",GamePort:"魅族",LoginType:"魅族"},{Id:149,Remark:"",GameName:"乱世逐鹿",GamePort:"腾讯",LoginType:"微信+QQ"},{Id:146,Remark:"",GameName:"乐高无限",GamePort:"腾讯",LoginType:"微信"},{Id:152,Remark:"",GameName:"世界onlineN",GamePort:"腾讯",LoginType:"QQ"},{Id:167,Remark:"",GameName:"不良人3",GamePort:"酷派",LoginType:"小绵羊"},{Id:166,Remark:"",GameName:"三国杀名将传hp",GamePort:"酷派",LoginType:"小绵羊"},{Id:5,Remark:"",GameName:"三国杀名将传",GamePort:"酷派",LoginType:"小绵羊"},{Id:142,Remark:"",GameName:"三国战争",GamePort:"魅族",LoginType:"魅族"},{Id:4,Remark:"",GameName:"三国志战略版",GamePort:"酷派",LoginType:"小绵羊"},{Id:89,Remark:"",GameName:"三国志战略版",GamePort:"腾讯",LoginType:"QQ"},{Id:3,Remark:"",GameName:"万魂觉醒",GamePort:"酷派",LoginType:"小绵羊"},{Id:2,Remark:"",GameName:"一梦江湖",GamePort:"酷派",LoginType:"小绵羊"},{Id:47,Remark:"",GameName:"一梦江湖",GamePort:"网易",LoginType:"小绵羊"},{Id:150,Remark:"",GameName:"一梦江湖",GamePort:"魅族",LoginType:"魅族"},{Id:1,Remark:"",GameName:"一拳超人",GamePort:"酷派",LoginType:"小绵羊"}}
	//fmt.Print(lists)
	//return global.GVA_DB.Table("game_list").Create(&lists).Error
	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
	}
}
