package live_banner_service

import (
	"bwallet/models"
)

type LiveBanner struct {
	Id         int
	TargetLink string
	ImageUrl   string
	Title      string
	PlatformId int
	Sort       int32
	Status     uint8

	PageNum  int
	PageSize int
}

func (b *LiveBanner) Get() (*models.LiveBanner, error) {

	banner, err := models.GetLiveBanner(b.Id)
	if err != nil {
		return nil, err
	}

	return banner, nil
}

func (b *LiveBanner) GetAll() ([]*models.LiveBanner, error) {
	var banners []*models.LiveBanner

	banners, err := models.GetLiveBanners(b.PageNum, b.PageSize, b.getMaps())
	if err != nil {
		return nil, err
	}

	return banners, nil
}

func (b *LiveBanner) Add() error {
	banner := map[string]interface{}{
		"target_link": b.TargetLink,
		"image_url":   b.ImageUrl,
		"title":       b.Title,
		"sort":        b.Sort,
		"status":      b.Status,
		"platform_id": b.PlatformId,
	}

	if err := models.AddLiveBanner(banner); err != nil {
		return err
	}

	return nil
}

func (b *LiveBanner) Edit() error {
	return models.EditLiveBanner(b.Id, map[string]interface{}{
		"target_link": b.TargetLink,
		"image_url":   b.ImageUrl,
		"title":       b.Title,
		"sort":        b.Sort,
		"status":      b.Status,
		"platform_id": b.PlatformId,
	})
}

func (b *LiveBanner) ExistById() (bool, error) {
	return models.ExistLiveBannerById(b.Id)
}

func (b *LiveBanner) Count() (int, error) {
	return models.GetLiveBannerTotal(b.getMaps())
}

func (b *LiveBanner) Delete() error {
	return models.DeleteLiveBanner(b.Id)
}

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

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

	if b.Status != 0 {
		maps["status"] = b.Status
	}

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

	return maps
}
