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 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
	var data models.OrgPlayer

	err = e.Orm.Table("org_player as op").
		Select("op.id,op.club_id,oc.club_name,op.team_id,ot.team_name,"+
			"op.player_name,op.player_number,op.position,op.sex,op.status").
		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").
		Joins("left join org_player_user as opu on op.player_user_id=opu.id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
			cDto.SetWhere("op", "team_id", c.TeamId),
			cDto.PassDel("op"),
		).
		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,oc.club_name,op.team_id,ot.team_name,"+
			"op.player_name,op.player_name_en,op.id_card,op.player_number,"+
			"op.position,op.sex,op.player_img,opu.username,op.share_conf,op.status").
		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").
		Joins("left join org_player_user as opu on op.player_user_id=opu.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
}

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

// Update 修改OrgPlayer对象
func (e *OrgPlayer) Update(c *dto.OrgPlayerUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.OrgPlayer{}
	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("OrgPlayerService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

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

	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 RemoveOrgPlayer error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}
