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 OrgTeamMatch struct {
	service.Service
}

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

	err = e.Orm.Table("org_match as om").
		Select("om.id, ol.league_name, od.division_name, os.season_name,om.grouping,"+
			"om.rounds, om.match_start_time, om.match_end_time, om.status,"+
			"ota.team_name as team_a_name, om.team_a_id, om.team_b_id, otb.team_name as team_b_name").
		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").
		Joins("left join org_team as ota on ota.id = om.team_a_id").
		Joins("left join org_team as otb on otb.id = om.team_b_id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
			cDto.PassDel("om"),
		).
		Where("team_a_id = ? or team_b_id = ?", c.TeamId, c.TeamId).
		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
}

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

	err = e.Orm.Table("org_match as om").
		Select("om.id, ol.league_name, od.division_name, os.season_name,om.grouping,"+
			"om.rounds, om.match_start_time, om.match_end_time, om.status,"+
			"ota.team_name as team_a_name, otb.team_name as team_b_name").
		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").
		Joins("left join org_team as ota on ota.id = om.team_a_id").
		Joins("left join org_team as otb on otb.id = om.team_b_id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		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
}

// Get 获取OrgMatch对象
func (e *OrgTeamMatch) Get(id string, p *actions.DataPermission, model *models.OrgMatch) error {
	var data models.OrgMatch

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