package service

import (
	"errors"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"

	"go-admin/app/operate/models"
	"go-admin/app/operate/service/dto"
	"go-admin/common/actions"
	cDto "go-admin/common/dto"
)

type OrgMatchTeamPlayer struct {
	service.Service
}

// GetPage 获取OrgMatchTeamPlayer列表
func (e *OrgMatchTeamPlayer) GetPage(c *dto.OrgMatchTeamPlayerGetPageReq, p *actions.DataPermission, list *[]models.OrgMatchTeamPlayer, count *int64) error {
	var err error
	var data models.OrgMatchTeamPlayer

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
			cDto.SetWhere("", "team_id", c.TeamId),
			cDto.SetWhere("", "match_id", c.MatchId),
			cDto.SetWhere("", "rounds", c.Rounds),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgMatchTeamPlayerService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取OrgMatchTeamPlayer对象
func (e *OrgMatchTeamPlayer) Get(d *dto.OrgMatchTeamPlayerGetReq, p *actions.DataPermission, model *models.OrgMatchTeamPlayer) error {
	var data models.OrgMatchTeamPlayer

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetOrgMatchTeamPlayer error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建OrgMatchTeamPlayer对象
func (e *OrgMatchTeamPlayer) Insert(c *dto.OrgMatchTeamPlayerInsertReq) error {
	var err error
	var data models.OrgMatchTeamPlayer
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("OrgMatchTeamPlayerService Insert error:%s \r\n", err)
		return err
	}
	return nil
}

// Update 修改OrgMatchTeamPlayer对象
func (e *OrgMatchTeamPlayer) Update(c *dto.OrgMatchTeamPlayerUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.OrgMatchTeamPlayer{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Updates(&data)
	if db.Error != nil {
		e.Log.Errorf("OrgMatchTeamPlayerService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

// Remove 删除OrgMatchTeamPlayer
func (e *OrgMatchTeamPlayer) Remove(d *dto.OrgMatchTeamPlayerDeleteReq, p *actions.DataPermission) error {
	var data models.OrgMatchTeamPlayer

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
			cDto.SetWhere("", "match_id", d.MatchId),
			cDto.SetWhere("", "rounds", d.Rounds),
			cDto.SetWhere("", "team_id", d.TeamId),
			cDto.SetWhereNotIn("", "player_id", d.Ids),
		).Delete(&data)
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveOrgMatchTeamPlayer error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// Insert 创建OrgMatchTeamPlayer对象
func (e *OrgMatchTeamPlayer) IsBeing(matchId string, rounds string, playerId string) (error, int) {
	var data models.OrgMatchTeamPlayer

	err := e.Orm.Model(&data).
		Where("match_id=?", matchId).
		Where("rounds=?", rounds).
		Where("player_id=?", playerId).
		First(&data).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetOrgMatchTeamPlayer error:%s \r\n", err)
		return err, 0
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err, 0
	}
	return nil, data.Id
}
