package typeManage

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

type LoginTypeService struct {}


//创建登录类型
func (l *LoginTypeService) CreateLoginType(login typeManage.LoginType) (err error) {
	//查找是否有同名登录名称
	err = global.GVA_DB.Table("login_type").Where("login_name = ?", login.LoginName).First(&typeManage.LoginType{}).Error
	if !errors.Is(err, gorm.ErrRecordNotFound){
		//找到同名登录名称
		return errors.New("已存在相同登录方式，请勿重复添加")
	}
	return global.GVA_DB.Table("login_type").Omit("create_time", "update_time").Create(&login).Error
}

//单个删除
func (l *LoginTypeService) DeleteLoginType(login typeManage.LoginType) (err error) {
	//单个删除的时候实例化一个实体
	//为什么实例化一个实体，推测是以求保险
	//先用这个实体接收合适的id值所对应的记录，再直接删除这个实体，防止误操作
	var entity typeManage.LoginType
	//查询是否有这条记录存在
	err = global.GVA_DB.Table("login_type").Where("id = ?", login.Id).First(&entity).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}
	return global.GVA_DB.Table("login_type").Delete(&entity).Error
}




//批量删除
func (l *LoginTypeService) DeleteLoginTypesByIds(ids request.IdsReq) (err error)  {
	err = global.GVA_DB.Table("login_type").Delete(&[]typeManage.LoginType{}, "id in ?", ids.Ids).Error
	return err
}



//更改登录类型
func (l *LoginTypeService) UpdateLoginType(login typeManage.LoginType) (err error)  {
	err = global.GVA_DB.Table("login_type").Where("id != ? and login_name = ?", login.Id, login.LoginName).First(&typeManage.LoginType{}).Error
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("已存在同名登录方式，修改失败")
	}
	return global.GVA_DB.Table("login_type").Updates(&login).Error
}




//通过id查询单个登录类型
func (l *LoginTypeService) GetLoginTypeById(id int) (loginType typeManage.LoginType, err error) {
	err = global.GVA_DB.Table("login_type").Where("id = ?", id).First(&loginType).Error
	return
}


//条件查询
func (l *LoginTypeService) GetLoginType(loginType typeManage.LoginType, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
	//先求offset和limit
	limit := info.PageSize
	offset := (info.Page - 1) * info.PageSize

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


	//确定操作的model
	db := global.GVA_DB.Table("login_type").Model(&typeManage.LoginType{})
	var loginList []typeManage.LoginType
	//先过滤(查询条件)再排序
	if loginType.LoginName != "" {
		db.Where("login_name like ?", "%"+loginType.LoginName+"%")
	}
	//记录总数
	if err = db.Count(&total).Error;err != nil {
		//没有复合条件，直接返回
		return loginList, total, err
	} else {
		//如果有符合条件的，先分页，再排序
		db.Limit(limit).Offset(offset)
		if order != "" {
			//传入排序字段,为避免sql注入，需要一个数组
			var orderStr string
			orderMap := make(map[string]bool, 5)
			orderMap["login_name"] = 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 loginList, total, err
			}
			err = db.Order(orderStr).Find(&loginList).Error
		} else {
			//未传入排序字段,默认按登录名称降序
			err = db.Order("login_name desc").Find(&loginList).Error
		}
	}
	return loginList, total, err
}