package recommend_coin_service

import (
	"bwallet/models"
)

type RecommendCoin struct {
	Id         int
	Cid        int
	Recommend  int
	PlatformId int
	Sort       int
	Type       int

	PageNum  int
	PageSize int
}

func (c *RecommendCoin) Get() (*models.RecommendCoin, error) {

	coinRecommend, err := models.GetRecommendCoin(c.Id)
	if err != nil {
		return nil, err
	}

	return coinRecommend, nil
}

func (c *RecommendCoin) GetAll() ([]*models.RecommendCoin, error) {
	var coinsRecommend []*models.RecommendCoin

	coinsRecommend, err := models.GetRecommendCoins(c.PageNum, c.PageSize, c.getMaps())
	if err != nil {
		return nil, err
	}

	return coinsRecommend, nil
}

func (c *RecommendCoin) Add() error {
	coinRecommend := map[string]interface{}{
		"cid":         c.Cid,
		"recommend":   c.Recommend,
		"platform_id": c.PlatformId,
		"sort":        c.Sort,
		"type":        c.Type,
	}

	if err := models.AddRecommendCoin(coinRecommend); err != nil {
		return err
	}

	return nil
}

func (c *RecommendCoin) Edit() error {
	return models.EditRecommendCoin(c.Id, map[string]interface{}{
		"sort": c.Sort,
	})
}

func (c *RecommendCoin) ExistById() (bool, error) {
	return models.ExistRecommendCoinById(c.Id)
}

func (c *RecommendCoin) Count() (int, error) {
	return models.GetRecommendCoinTotal(c.getMaps())
}

func (c *RecommendCoin) Delete() error {
	return models.DeleteRecommendCoin(c.Id)
}

func (c *RecommendCoin) getMaps() (map[string]interface{}) {
	maps := make(map[string]interface{})

	if c.PlatformId != 0 {
		maps["platform_id"] = c.PlatformId
	}

	if c.Cid != 0 {
		maps["cid"] = c.Cid
	}

	if c.Type != 0 {
		maps["type"] = c.Type
	}

	return maps
}
