package service

import (
	"errors"
	"strconv"

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

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

	err = e.Orm.Model(&data).
		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("OrgClubUserService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

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

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

func (e *OrgClubUser) GetUserId(clubId int) (error, string) {
	data := new(models.OrgClubUser)

	e.Orm.Table("org_club_user").
		Select("user_id,club_id").
		Where("club_id=?", clubId).
		First(&data)

	return nil, data.UserId
}

// Get 获取OrgClubUser对象
func (e *OrgClubUser) GetUserIds(clubId []int) (error, []int) {
	data := make([]models.OrgClubUser, 0)

	e.Orm.Table("org_club_user").
		Select("user_id,club_id").
		Where("club_id IN(?)", clubId).
		First(&data)

	ids := make([]int, 0)
	id := 0
	for _, v := range data {
		id, _ = strconv.Atoi(v.UserId)
		ids = append(ids, id)
	}
	return nil, ids
}

// Get 获取OrgClubUser对象
func (e *OrgClubUser) GetClubId(userId int) (error, string) {
	data := new(models.OrgClubUser)

	err := e.Orm.Table("org_club_user").
		Select("club_id").
		Where("user_id=?", userId).
		First(&data).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err, ""
	}
	return nil, data.ClubId
}

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

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

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

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