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

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

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

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

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

// Insert 创建LineSymbolPrice对象
func (e *LineSymbolPrice) Insert(c *dto.LineSymbolPriceInsertReq) error {
	var err error
	var data models.LineSymbolPrice
	c.Generate(&data)
	var count int64

	e.Orm.Model(&models.LineSymbolPrice{}).Where("symbol = ?", c.Symbol).Count(&count)

	if count > 0 {
		return errors.New("交易对已存在，请不要重复添加")
	}

	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("LineSymbolPriceService Insert error:%s \r\n", err)
		return err
	}

	e.cacheList()

	return nil
}

func (e *LineSymbolPrice) cacheList() {
	symbols := make([]string, 0)
	if err := e.Orm.Model(&models.LineSymbolPrice{}).Where("status =1").Pluck("symbol", &symbols).Error; err != nil {
		e.Log.Errorf("获取可用交易对失败,err:", err)
	}

	if len(symbols) > 0 {
		if err := helper.DefaultRedis.SetListCache(rediskey.CacheSymbolLastPrice, 0, symbols...); err != nil {
			e.Log.Errorf("设置可缓存价格交易对失败")
		}
	}
}

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

	e.Orm.Model(&models.LineSymbolPrice{}).Where("symbol = ? and id !=?", c.Symbol, c.GetId()).Count(&count)
	if count > 0 {
		return errors.New("交易对已存在，请不要重复添加")
	}

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

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

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

func (e *LineSymbolPrice) InitCache() {
	e.cacheList()
}
