package live_charge_service

import (
	"bwallet/models"
)

type LiveCharge struct {
	Id         int
	Unit       string
	UnitPrice  float64
	PlatformId int64

	PageNum  int
	PageSize int
}

func (l *LiveCharge) GetAll() ([]*models.LiveCharge, error) {
	var charge []*models.LiveCharge
	charge, err := models.GetLiveCharge(l.PageNum, l.PageSize, l.getMaps())
	if err != nil {
		return nil, err
	}

	return charge, nil
}

func (l *LiveCharge) Add() error {
	charge := map[string]interface{}{
		"unit":        l.Unit,
		"unit_price":  l.UnitPrice,
		"platform_id": l.PlatformId,
	}

	if err := models.AddLiveCharge(charge); err != nil {
		return err
	}

	return nil
}

func (l *LiveCharge) Edit() error {
	return models.EditLiveCharge(l.Id, map[string]interface{}{
		"unit":       l.Unit,
		"unit_price": l.UnitPrice,
	})
}

func (l *LiveCharge) ExistById() (bool, error) {
	return models.ExistLiveChargeById(l.Id)
}

func (l *LiveCharge) GetOneLiveInfo() (*models.LiveCharge, error) {
	charge, err := models.GetOneLiveCharge(l.Id)
	if err != nil {
		return nil, err
	}

	return charge, nil
}

func (l *LiveCharge) Count() (int, error) {
	return models.GetLiveChargeTotal(l.getMaps())
}

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

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

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

	return maps
}
