package service

import (
	"errors"

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

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

type MemberBalance struct {
	service.Service
}

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

	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("MemberBalanceService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

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

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

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

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

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

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

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

// 添加默认余额
func (e *MemberBalance) CreateDefaultBalance(user *models.LineUser) error {
	data := models.MemberBalance{
		UserId: user.Id,
	}

	if err := e.Orm.Create(&data).Error; err != nil {
		return err
	}
	return nil
}

// GetBalance 获取余额
func (e *MemberBalance) GetBalance(userId int) (models.MemberBalance, error) {
	data := models.MemberBalance{}

	if err := e.Orm.Model(&data).Where("user_id =?", userId).First(&data).Error; err != nil {
		return data, err
	}

	return data, nil
}
