package wallet_recommend_category

import (
	"bwallet/models"
	"encoding/json"
)

type WalletRecommendCategory struct {
	Id         int
	Name       json.RawMessage
	Sort       uint32
	WalletType uint8
	Status     uint8
	PlatformId int

	PageNum  int
	PageSize int
}

func (c *WalletRecommendCategory) Get() (*models.WalletRecommendCategory, error) {
	currency, err := models.GetWalletRecommendCategory(c.Id)
	if err != nil {
		return nil, err
	}

	return currency, nil
}

func (c *WalletRecommendCategory) GetAll() ([]*models.WalletRecommendCategory, error) {
	var categories []*models.WalletRecommendCategory

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

	return categories, nil
}

func (c *WalletRecommendCategory) Add() error {
	category := map[string]interface{}{
		"name":        c.Name,
		"sort":        c.Sort,
		"wallet_type": c.WalletType,
		"status":      c.Status,
		"platform_id": c.PlatformId,
	}

	if err := models.AddWalletRecommendCategory(category); err != nil {
		return err
	}

	return nil
}

func (c *WalletRecommendCategory) Edit() error {
	return models.EditWalletRecommendCategory(c.Id, map[string]interface{}{
		"name":        c.Name,
		"sort":        c.Sort,
		"wallet_type": c.WalletType,
		"status":      c.Status,
	})
}

func (c *WalletRecommendCategory) ExistById() (bool, error) {
	return models.ExistCategoryById(c.Id)
}

func (c *WalletRecommendCategory) Count() (int, error) {
	return models.GetRecommendCategoryTotal(c.getMaps())
}

func (c *WalletRecommendCategory) Delete() error {
	return models.DeleteWalletRecommendCategory(c.Id)
}

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

	if c.Id != 0 {
		maps["id"] = c.Id
	}

	if c.WalletType != 0 {
		maps["wallet_type"] = c.WalletType
	}

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

	return maps
}
