package service

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

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

type OrgPlayer struct {
	service.Service
}

// GetPage 获取OrgPlayer列表
func (e *OrgPlayer) GetPage(c *dto.OrgPlayerGetPageReq, p *actions.DataPermission, list *[]dto.OrgPlayerGetPageReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_player as op").
		Select("op.id,op.player_name,op.player_number,op.player_img,op.position,op.sex,op.status").
		Scopes(
			actions.Permission("op", p),
			cDto.PassDel("op"),
			cDto.SetWhere("op", "player_user_id", c.PlayerUserId),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgPlayerService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

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

	err := e.Orm.Table("org_player as op").
		Select("op.id,op.club_id,op.player_name,op.player_name_en,op.player_number,"+
			"op.position,op.sex,op.player_img,op.share_conf,op.team_id,"+
			"oc.club_name,oc.club_logo,oc.club_qrcode,"+
			"ot.team_name").
		Joins("left join org_club as oc on op.club_id=oc.id").
		Joins("left join org_team as ot on op.team_id=ot.id").
		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 GetOrgPlayer error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Get 获取OrgPlayer对象
func (e *OrgPlayer) GetData(d *dto.OrgPlayerDataGetReq, p *actions.DataPermission, model *dto.OrgPlayerDataGetReply) error {
	var err error
	field := "if(sum(omtp.scoring)=='',0,sum(omtp.scoring)) as scoring"

	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 and omtp.rounds = om.rounds").
		Scopes(
			actions.Permission("omtp", p),
			cDto.SetWhere("om", "season_id", d.SeasonId),
			cDto.PassDel("omtp"),
			cDto.PassDel("om"),
		).
		Where("omtp.player_id = ?", d.PlayerId).
		First(model).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Get 获取OrgPlayer对象
func (e *OrgPlayer) GetDataSeason(d *dto.OrgPlayerDataGetReq, p *actions.DataPermission, model *dto.OrgPlayerDataGetReply) error {
	var err error

	field := "count(om.id) as scoring"

	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 and omtp.rounds = om.rounds").
		Scopes(
			actions.Permission("omtp", p),
			cDto.PassDel("omtp"),
			cDto.PassDel("om"),
		).
		Where("omtp.player_id = ?", d.PlayerId).
		Group("om.season_id").
		First(model).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Get 获取OrgPlayer对象
func (e *OrgPlayer) GetDataMatch(d *dto.OrgPlayerDataGetReq, p *actions.DataPermission, model *dto.OrgPlayerDataGetReply) error {
	var err error

	field := "count(om.id) as count_match"

	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 and omtp.rounds = om.rounds").
		Scopes(
			actions.Permission("omtp", p),
			cDto.SetWhere("om", "season_id", d.SeasonId),
			cDto.PassDel("omtp"),
			cDto.PassDel("om"),
		).
		Where("omtp.player_id = ?", d.PlayerId).
		Group("omtp.match_id and omtp.rounds").
		First(model).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

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

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("ol.league_name, os.season_name, om.season_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),
		).
		Where("om.season_id = ?", seasonId).
		Where("omtp.player_id = ?", playerId).
		Group("om.season_id").
		First(data).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetMatchInfo error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *OrgPlayer) StatisticsScoringAvg(playerId string, seasonId string, statistics string, data *dto.OrgMatchInfo) error {
	var err error
	field := ""

	field += "omtp.id, ol.league_name, os.season_name, om.season_id, count(omtp.id)as count_match, ot.team_name," +
		"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 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").
		Joins("left join org_team as ot on ot.id = omtp.team_id").
		Scopes(cDto.SetWhere("omtp", "player_id", playerId)).
		Where("om.season_id = ?", seasonId).
		Group("om.season_id").
		First(data).Error

	if statistics == "avg" {
		teamCount, _ := strconv.ParseInt(data.CountMatch, 10, 64)

		data.Scoring = utils.GetAvg(data.Scoring, teamCount)
		data.Rebound = utils.GetAvg(data.Rebound, teamCount)
		data.Assist = utils.GetAvg(data.Assist, teamCount)
		data.Steal = utils.GetAvg(data.Steal, teamCount)
		data.FreeThrow = utils.GetAvg(data.FreeThrow, teamCount)
		data.BlockShot = utils.GetAvg(data.BlockShot, teamCount)
		data.Foul = utils.GetAvg(data.Foul, teamCount)
		data.TwoPointShot = utils.GetAvg(data.TwoPointShot, teamCount)
		data.ThreePointShot = utils.GetAvg(data.ThreePointShot, teamCount)
	}

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

//参加比赛id
func (e *OrgPlayer) GetMatchId(playerId string, seasonId string) (error, []string) {
	var err error
	data := make([]dto.MatchId, 0)

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("distinct(om.id) as id").
		Joins("left join org_match as om on omtp.match_id = om.id and omtp.rounds = om.rounds").
		Scopes(
			cDto.SetWhere("om", "season_id", seasonId),
			cDto.PassDel("omtp"),
			cDto.PassDel("om"),
		).
		Where("omtp.player_id = ?", playerId).
		Order("om.id desc").
		Find(&data).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetRoundsScoring error:%s \r\n", err)
		return err, nil
	}

	matchId := make([]string, 0)
	for _, v := range data {
		matchId = append(matchId, v.Id)
	}
	return nil, matchId
}

func (e *OrgPlayer) GetRoundsScoring(c *dto.OrgPlayerGetInfoReq, matchId []string, data *[]dto.OrgPlayerRoundsScoring, count *int64) error {
	var err error

	field := "om.id, om.rounds, om.grouping," +
		"om.team_a_id,om.team_b_id,om.team_a_score,om.team_b_score," +
		"om.match_start_time, om.match_end_time, ot.team_name as other_team," +
		"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 as om").
		Select(field).
		Joins("left join org_match_team_player as omtp 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").
		Where("omtp.match_id in (?)", matchId).
		Where("omtp.player_id = ? ", c.PlayerId).
		Scopes(
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			cDto.PassDel("om"),
		).
		Group("omtp.match_id").
		Find(data).Limit(-1).Offset(-1).
		Count(count).Error

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

func (e *OrgPlayer) GetSeasonRoundsScoring(c *dto.OrgPlayerGetInfoReq, matchId []string, data *[]dto.OrgPlayerRoundsScoring, count *int64) error {
	var err error

	field := "om.season_id as id, omtp.team_id, ot.team_name, ol.league_name, os.season_name, count(omtp.id)as count_match," +
		"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 as om").
		Select(field).
		Joins("left join org_match_team_player as omtp on omtp.match_id = om.id and omtp.rounds = om.rounds").
		Joins("left join org_team as ot on omtp.team_id = ot.id").
		Joins("left join org_league as ol on om.league_id = ol.id").
		Joins("left join org_season as os on om.season_id = os.id").
		Scopes(
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			cDto.PassDel("om"),
		).
		Where("omtp.match_id in (?)", matchId).
		Where("omtp.player_id = ? ", c.PlayerId).
		Group("om.season_id").
		Find(data).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetRoundsScoring error:%s \r\n", err)
		return err
	}
	return nil
}
func (e *OrgPlayer) GetOrgRounds(c *dto.GetRoundsReq, list *[]dto.GetRoundsReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("omtp.id,omtp.match_id,om.rounds,om.league_id,om.division_id,om.season_id,ol.league_name,od.division_name,os.season_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_division as od on od.id = om.division_id").
		Joins("left join org_season as os on os.id = om.season_id").
		Scopes(
			cDto.PassDel("omtp"),
			cDto.SetWhere("omtp", "player_id", c.PlayerId),
		).
		Find(list).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgPlayerService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}
func (e *OrgPlayer) GetOrgMatchEvaluate(c *dto.OrgPlayerGetOrgMatchEvaluateReq, list *[]dto.OrgPlayerGetOrgMatchEvaluateReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_match_evaluate_wonderful as omew").
		Select("omew.id, omew.wonderful_url, omew.wonderful_title, omew.file_id").
		Joins("left join org_match_evaluate as ome on ome.id = omew.match_evaluate_id").
		Joins("left join org_match as om on om.id = ome.match_id").
		Scopes(
			cDto.PassDel("omew"),
			cDto.SetWhere("om", "league_id", c.LeagueId),
			cDto.SetWhere("om", "season_id", c.SeasonId),
			cDto.SetWhere("ome", "match_id", c.MatchId),
			cDto.SetWhere("ome", "rounds", c.Rounds),
		).
		Where("omew.type=?", c.Type).
		Where("ome.player_id=?", c.PlayerId).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *OrgPlayer) GetOrgMatchEvaluateContent(c *dto.OrgPlayerGetOrgMatchEvaluateContentReq, list *[]dto.OrgPlayerGetOrgMatchEvaluateContentReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_match_evaluate as ome").
		Select("ome.id, ome.content, ome.content_date, ome.content_img, ol.league_name,"+
			"os.season_name, od.division_name, ome.match_id, ome.rounds").
		Joins("left join org_match as om on om.id = ome.match_id").
		Joins("left join org_league as ol on om.league_id = ol.id").
		Joins("left join org_season as os on om.season_id = os.id").
		Joins("left join org_division as od on om.division_id = od.id").
		Scopes(
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			cDto.PassDel("ome"),
		).
		Where("ome.player_id=?", c.PlayerId).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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 *OrgPlayer) 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
}

func (e *OrgPlayer) 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 *OrgPlayer) 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"
}

func (e *OrgPlayer) GetPageWonderful(evaluateId string, list *[]cDto.Wonderful, wType int) error {
	var err error

	err = e.Orm.Table("org_match_evaluate_wonderful as omew").
		Select("omew.id, omew.wonderful_url, omew.wonderful_title").
		Scopes(
			cDto.PassDel("omew"),
		).
		Where("match_evaluate_id=?", evaluateId).
		Where("type=?", wType).
		Find(list).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *OrgPlayer) GetNowSeasonId() string {
	reply := new(models.OrgSeason)
	now := time.Now()
	//negativeM, _ := time.ParseDuration("-5m")
	//nowBefore := now.Add(negativeM)

	err := e.Orm.Table("org_season").
		Select("id").
		Scopes(cDto.PassDel("org_season")).
		Where("start_time<?", now).
		Where("end_time>?", now).
		Order("id desc").
		First(&reply).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetOrgMsg error:%s \r\n", err)
		return "0"
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return "0"
	}
	return strconv.Itoa(reply.Id)
}
