package live_category_service

import (
	"bwallet/models"
)

type LiveCategory struct {
	Id         int
	Name       string
	Sort       uint8
	Logo       string
	PlatformId int64

	PageNum  int
	PageSize int
}

func (l *LiveCategory) GetOneLiveCategory() (*models.LiveCategory, error) {
	category, err := models.GetOneLiveCategory(l.Id)
	if err != nil {
		return nil, err
	}

	return category, nil
}

func (l *LiveCategory) GetAll() ([]*models.LiveCategory, error) {
	var article []*models.LiveCategory

	article, err := models.GetLiveCategory(l.PageNum, l.PageSize, l.getMaps())
	if err != nil {
		return nil, err
	}

	return article, nil
}

func (l *LiveCategory) Add() error {
	category := map[string]interface{}{
		"name":        l.Name,
		"sort":        l.Sort,
		"logo":        l.Logo,
		"platform_id": l.PlatformId,
	}

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

	return nil
}

func (l *LiveCategory) Edit() error {
	return models.EditLiveCategory(l.Id, map[string]interface{}{
		"name":        l.Name,
		"sort":        l.Sort,
		"logo":        l.Logo,
		"platform_id": l.PlatformId,
	})
}

func (l *LiveCategory) ExistById() (bool, error) {
	return models.ExistLiveCategoryById(l.Id)
}

func (l *LiveCategory) Count() (int, error) {
	return models.GetLiveCategoryTotal(l.getMaps())
}

func (l *LiveCategory) Delete() error {
	return models.DeleteLiveCategory(l.Id)
}

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

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

	if l.Name != "" {
		maps["name"] = l.Name
	}

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

	return maps
}
