package service

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

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

type OrgLeague struct {
	service.Service
}

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

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("ol.id, ol.league_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").
		Where("ol.status=?", 1).
		Where("omtp.player_id=?", c.PlayerId).
		Group("om.league_id").
		Find(list).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgLeagueService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// GetPage 获取OrgLeague列表
func (e *OrgLeague) GetPageForOwn(c *dto.OrgLeagueGetPageReq, p *actions.DataPermission, list *[]dto.OrgLeagueGetPageReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("ol.id, ol.league_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").
		Where("ol.status=?", 1).
		Where("omtp.player_id=?", c.PlayerId).
		Group("om.league_id").
		Find(list).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgLeagueService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

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

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

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

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

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

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

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, d.GetId())
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveOrgLeague error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}
