package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	cDto "go-admin/common/dto"
	"go-admin/common/utils"
	"gorm.io/gorm"
	"sort"
	"strconv"

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

type OrgTeamPlayer struct {
	service.Service
}

// GetPage 获取OrgMatchEvaluate列表
func (e *OrgTeamPlayer) GetPage(c *dto.OrgTeamPlayerGetPageReq, p *actions.DataPermission, list *[]dto.OrgTeamPlayerGetPageReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("omtp.id, omtp.player_id, ol.league_name, om.season_id, os.season_name, om.rounds, omtp.player_name").
		Joins("left join org_match as om on omtp.match_id = om.id and omtp.rounds = om.rounds").
		Joins("left join org_league as ol on ol.id = om.league_id").
		Joins("left join org_season as os on os.id = om.season_id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission("omtp", p),
			cDto.PassDel("omtp"),
		).
		Where("omtp.team_id=?", c.TeamId).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgClubPlayer GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// GetPage 获取OrgMatchEvaluate列表
func (e *OrgTeamPlayer) GetMatchInfo(id string, p *actions.DataPermission, data *cDto.PageMatchInfo) error {
	var err error

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("omtp.id, omtp.match_id, omtp.team_id, ol.league_name, os.season_name,"+
			"oc.club_name, ot.team_name, omtp.player_name, omtp.player_number, omtp.position,"+
			"omtp.player_id, om.rounds, om.season_id").
		Joins("left join org_club as oc on omtp.club_id = oc.id").
		Joins("left join org_team as ot on omtp.team_id = ot.id").
		Joins("left join org_match as om on omtp.match_id = om.id and omtp.rounds = om.rounds").
		Joins("left join org_league as ol on ol.id = om.league_id").
		Joins("left join org_season as os on os.id = om.season_id").
		Scopes(
			actions.Permission("omtp", p),
		).
		First(data, id).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetMatchInfo error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *OrgTeamPlayer) GetRoundsScoring(playerId string, rounds string, teamPlayerId string, data *[]cDto.RoundsScoring) error {
	var err error

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("omtp.id, om.match_start_time, om.rounds, om.grouping, ot.team_name as other_team, "+
			"omtp.scoring, omtp.scoring, omtp.rebound, omtp.assist, omtp.steal, omtp.free_throw, "+
			"omtp.block_shot, omtp.foul, omtp.two_point_shot, omtp.three_point_shot").
		Joins("left join org_match as om on omtp.match_id = om.id and omtp.rounds = om.rounds").
		Joins("left join org_team as ot on omtp.other_team_id = ot.id").
		Scopes(
			cDto.SetWhere("omtp", "player_id", playerId),
			cDto.SetWhere("omtp", "rounds", rounds),
			cDto.SetWhere("omtp", "id", teamPlayerId),
		).
		Find(data).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetRoundsScoring error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *OrgTeamPlayer) statisticsScoring(playerId string, rounds string, seasonId string, statistics string, data *cDto.TotalScoring) error {
	var err error
	field := ""

	if statistics == "sum" {
		field += "sum(omtp.scoring)as scoring," +
			"sum(omtp.rebound)as rebound," +
			"sum(omtp.assist)as assist," +
			"sum(omtp.steal)as steal," +
			"sum(omtp.free_throw)as free_throw," +
			"sum(omtp.block_shot)as block_shot," +
			"sum(omtp.foul)as foul," +
			"sum(omtp.two_point_shot)as two_point_shot," +
			"sum(omtp.three_point_shot)as three_point_shot"
	}

	if statistics == "avg" {
		field += "avg(omtp.scoring)as scoring," +
			"avg(omtp.rebound)as rebound," +
			"avg(omtp.assist)as assist," +
			"avg(omtp.steal)as steal," +
			"avg(omtp.free_throw)as free_throw," +
			"avg(omtp.block_shot)as block_shot," +
			"avg(omtp.foul)as foul," +
			"avg(omtp.two_point_shot)as two_point_shot," +
			"avg(omtp.three_point_shot)as three_point_shot"
	}

	err = e.Orm.Table("org_match_team_player as omtp").
		Joins("left join org_match as om on omtp.match_id = om.id and omtp.rounds = om.rounds").
		Select(field).
		Where("omtp.player_id = ?", playerId).
		Where("omtp.rounds = ?", rounds).
		Scopes(cDto.SetWhere("om", "season_id", seasonId)).
		First(data).Error

	if err != nil {
		e.Log.Errorf("OrgMatchService GetTotalScoring error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取OrgMatchEvaluate对象
func (e *OrgTeamPlayer) GetTotalScoring(playerId string, rounds string, seasonId string, data *[]cDto.TotalScoring) error {
	replyGetTotalScoring1 := new(cDto.TotalScoring)
	e.statisticsScoring(playerId, rounds, "0", "sum", replyGetTotalScoring1)
	*data = append(*data, *replyGetTotalScoring1)

	replyGetTotalScoring2 := new(cDto.TotalScoring)
	e.statisticsScoring(playerId, rounds, "0", "avg", replyGetTotalScoring2)
	*data = append(*data, *replyGetTotalScoring2)

	replyGetTotalScoring3 := new(cDto.TotalScoring)
	e.statisticsScoring(playerId, rounds, seasonId, "sum", replyGetTotalScoring3)
	*data = append(*data, *replyGetTotalScoring3)

	replyGetTotalScoring4 := new(cDto.TotalScoring)
	e.statisticsScoring(playerId, rounds, seasonId, "avg", replyGetTotalScoring4)
	*data = append(*data, *replyGetTotalScoring4)

	replyGetTotalScoring5 := new(cDto.TotalScoring)
	e.statisticsRankingList(playerId, seasonId, replyGetTotalScoring5)
	*data = append(*data, *replyGetTotalScoring5)

	return nil
}

func (e *OrgTeamPlayer) statisticsRankingList(playerId string, seasonId string, scoring *cDto.TotalScoring) error {

	replyGetPlayerRank := make([]models.OrgMatchTeamPlayer, 0)
	err := e.statisticsRanking(seasonId, &replyGetPlayerRank)
	if err != nil {
		e.Log.Errorf("OrgMatchService GetTotalScoring error:%s \r\n", err)
		return err
	}
	e.rankingScoring(replyGetPlayerRank, playerId, scoring)
	e.rankingRebound(replyGetPlayerRank, playerId, scoring)
	e.rankingAssist(replyGetPlayerRank, playerId, scoring)
	e.rankingSteal(replyGetPlayerRank, playerId, scoring)
	e.rankingFreeThrow(replyGetPlayerRank, playerId, scoring)
	e.rankingBlockShot(replyGetPlayerRank, playerId, scoring)
	e.rankingFoul(replyGetPlayerRank, playerId, scoring)
	e.rankingTwoPointShot(replyGetPlayerRank, playerId, scoring)
	e.rankingThreePointShot(replyGetPlayerRank, playerId, scoring)

	return nil
}

func (e *OrgTeamPlayer) rankingScoring(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].Scoring) > utils.StringToFloat(replyGetPlayerRank[j].Scoring)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.Scoring = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingRebound(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].Rebound) > utils.StringToFloat(replyGetPlayerRank[j].Rebound)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.Rebound = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingAssist(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].Assist) > utils.StringToFloat(replyGetPlayerRank[j].Assist)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.Assist = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingSteal(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].Steal) > utils.StringToFloat(replyGetPlayerRank[j].Steal)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.Steal = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingFreeThrow(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].FreeThrow) > utils.StringToFloat(replyGetPlayerRank[j].FreeThrow)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.FreeThrow = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingBlockShot(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].BlockShot) > utils.StringToFloat(replyGetPlayerRank[j].BlockShot)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.BlockShot = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingFoul(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].Foul) > utils.StringToFloat(replyGetPlayerRank[j].Foul)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.Foul = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingTwoPointShot(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].TwoPointShot) > utils.StringToFloat(replyGetPlayerRank[j].TwoPointShot)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.TwoPointShot = strconv.Itoa(k + 1)
		}
	}
}
func (e *OrgTeamPlayer) rankingThreePointShot(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string, scoring *cDto.TotalScoring) {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].ThreePointShot) > utils.StringToFloat(replyGetPlayerRank[j].ThreePointShot)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			scoring.ThreePointShot = strconv.Itoa(k + 1)
		}
	}
}

func (e *OrgTeamPlayer) statisticsRanking(seasonId string, data *[]models.OrgMatchTeamPlayer) error {
	var err error
	field := "player_id," +
		"sum(omtp.scoring)as scoring," +
		"sum(omtp.rebound)as rebound," +
		"sum(omtp.assist)as assist," +
		"sum(omtp.steal)as steal," +
		"sum(omtp.free_throw)as free_throw," +
		"sum(omtp.block_shot)as block_shot," +
		"sum(omtp.foul)as foul," +
		"sum(omtp.two_point_shot)as two_point_shot," +
		"sum(omtp.three_point_shot)as three_point_shot"
	err = e.Orm.Table("org_match_team_player as omtp").
		Select(field).
		Joins("left join org_match as om on omtp.match_id = om.id").
		Where("om.season_id = ?", seasonId).
		Order("player_id asc").
		Group("player_id").
		Find(&data).Error

	if err != nil {
		e.Log.Errorf("OrgMatchService GetTotalScoring error:%s \r\n", err)
		return err
	}
	return nil
}

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

	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 GetOrgMatchEvaluate error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

func (e *OrgTeamPlayer) GetRankForGetPage(seasonId string, playerId string) string {
	replyGetPlayerRank := make([]models.OrgMatchTeamPlayer, 0)
	err := e.statisticsRanking(seasonId, &replyGetPlayerRank)
	if err != nil {
		e.Log.Errorf("OrgMatchService GetTotalScoring error:%s \r\n", err)
		return "0"
	}
	return e.rankingForGetPage(replyGetPlayerRank, playerId)
}

func (e *OrgTeamPlayer) rankingForGetPage(replyGetPlayerRank []models.OrgMatchTeamPlayer, playerId string) string {
	sort.Slice(replyGetPlayerRank, func(i, j int) bool {
		return utils.StringToFloat(replyGetPlayerRank[i].Scoring) > utils.StringToFloat(replyGetPlayerRank[j].Scoring)
	})
	for k, v := range replyGetPlayerRank {
		if v.PlayerId == playerId {
			return strconv.Itoa(k + 1)
		}
	}
	return "0"
}
