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"
	"go-admin/common/const/rediskey"
	cDto "go-admin/common/dto"
	"go-admin/common/helper"
)

type LineReverseSetting struct {
	service.Service
}

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

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

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

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

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

	err = e.SaveCache(data)
	if err != nil {
		return err
	}
	return nil
}

// 缓存数据
func (e *LineReverseSetting) SaveCache(data models.LineReverseSetting) error {
	if data.Id == 0 {
		if err := e.Orm.First(&data).Error; err != nil {
			return err
		}
	}

	if err := helper.DefaultRedis.SetHashWithTags(rediskey.ReverseSetting, &data); err != nil {
		e.Log.Errorf("redis error:%s", err)
		return err
	}
	return nil
}

// Update 修改LineReverseSetting对象
func (e *LineReverseSetting) Update(c *dto.LineReverseSettingUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.LineReverseSetting{}
	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("LineReverseSettingService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	if err = helper.DefaultRedis.SetHashWithTags(rediskey.ReverseSetting, data); err != nil {
		e.Log.Errorf("redis error:%s", err)
		return err
	}

	return nil
}

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

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