package trusteeship_coin_service

import (
	"bwallet/models"
)

type TrusteeshipCoin struct {
	Id         int
	Cid        int
	Sort       int
	PlatformId int
	CategoryId uint8

	PageNum  int
	PageSize int
}

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

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

	return coinTrusteeship, nil
}

func (c *TrusteeshipCoin) GetAll() ([]*models.TrusteeshipCoin, error) {
	var coinsTrusteeship []*models.TrusteeshipCoin

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

	return coinsTrusteeship, nil
}

func (c *TrusteeshipCoin) Add() error {
	coinTrusteeship := map[string]interface{}{
		"cid":         c.Cid,
		"sort":        c.Sort,
		"platform_id": c.PlatformId,
	}

	if err := models.AddTrusteeshipCoin(coinTrusteeship); err != nil {
		return err
	}

	return nil
}

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

func (c *TrusteeshipCoin) ExistById() (bool, error) {
	return models.ExistTrusteeshipCoinById(c.Id)
}

func (c *TrusteeshipCoin) Count() (int, error) {
	return models.GetTrusteeshipCoinTotal(c.getMaps())
}

func (c *TrusteeshipCoin) Delete() error {
	return models.DeleteTrusteeshipCoin(c.Id)
}

func (c *TrusteeshipCoin) DeleteTrusteeshipByCondition() error {
	return models.DeleteTrusteeshipByCondition(c.getMaps())
}

func (c *TrusteeshipCoin) 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
	}

	return maps
}
