76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
package rechargereward
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type rechargeRewardPeriod struct {
|
|
CycleKey string
|
|
RechargeDate int
|
|
StartAt time.Time
|
|
EndAt time.Time
|
|
}
|
|
|
|
func (s *Service) currentPeriod(now time.Time, location *time.Location) (rechargeRewardPeriod, bool) {
|
|
startAt := rechargeRewardStartAt(s.cfg.RechargeReward.StartDate, location)
|
|
return currentRechargeRewardPeriod(now, startAt, location)
|
|
}
|
|
|
|
func (s *Service) displayPeriod(now time.Time, location *time.Location) rechargeRewardPeriod {
|
|
startAt := rechargeRewardStartAt(s.cfg.RechargeReward.StartDate, location)
|
|
period, ok := currentRechargeRewardPeriod(now, startAt, location)
|
|
if ok {
|
|
return period
|
|
}
|
|
monthStart := time.Date(startAt.Year(), startAt.Month(), 1, 0, 0, 0, 0, location)
|
|
return rechargeRewardPeriod{
|
|
CycleKey: startAt.Format("20060102"),
|
|
RechargeDate: startAt.Year()*100 + int(startAt.Month()),
|
|
StartAt: startAt,
|
|
EndAt: monthStart.AddDate(0, 1, 0),
|
|
}
|
|
}
|
|
|
|
func currentRechargeRewardPeriod(now time.Time, startAt time.Time, location *time.Location) (rechargeRewardPeriod, bool) {
|
|
local := now.In(location)
|
|
start := startAt.In(location)
|
|
if local.Before(start) {
|
|
return rechargeRewardPeriod{}, false
|
|
}
|
|
monthStart := time.Date(local.Year(), local.Month(), 1, 0, 0, 0, 0, location)
|
|
periodStart := monthStart
|
|
if periodStart.Before(start) {
|
|
periodStart = start
|
|
}
|
|
periodEnd := monthStart.AddDate(0, 1, 0)
|
|
if !local.Before(periodEnd) {
|
|
return rechargeRewardPeriod{}, false
|
|
}
|
|
return rechargeRewardPeriod{
|
|
CycleKey: periodStart.Format("20060102"),
|
|
RechargeDate: periodStart.Year()*100 + int(periodStart.Month()),
|
|
StartAt: periodStart,
|
|
EndAt: periodEnd,
|
|
}, true
|
|
}
|
|
|
|
func rechargeRewardStartAt(raw string, location *time.Location) time.Time {
|
|
value := strings.TrimSpace(raw)
|
|
if value == "" {
|
|
value = "2026-05-21"
|
|
}
|
|
parsed, err := time.ParseInLocation("2006-01-02", value, location)
|
|
if err != nil {
|
|
parsed, _ = time.ParseInLocation("2006-01-02", "2026-05-21", location)
|
|
}
|
|
return time.Date(parsed.Year(), parsed.Month(), parsed.Day(), 0, 0, 0, 0, location)
|
|
}
|
|
|
|
func rechargeRewardStorageTime(t time.Time, storageLocation *time.Location) string {
|
|
if storageLocation == nil {
|
|
storageLocation = t.Location()
|
|
}
|
|
return t.In(storageLocation).Format("2006-01-02 15:04:05")
|
|
}
|