package recommend_coin_service

import (
	"bwallet/models"
)

type RecommendCoin struct {
	Id              int
	Cid             int
	Sort            int
	PlatformId      int
	CategoryId      uint8
	Type            uint8

	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,
		"sort":        c.Sort,
		"platform_id": c.PlatformId,
		"category_id": c.CategoryId,
		"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) DeleteByCondition() error {
	return models.DeleteByCondition(c.getMaps())
}

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.CategoryId != 0 {
		maps["category_id"] = c.CategoryId
	}

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

	return maps
}
