Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/apps/redenvelope/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func Create(c *gin.Context) {

// 查询今日已发送的红包数量
var todayCount int64
today := time.Now().Truncate(24 * time.Hour)
today := startOfDay(time.Now())
if err := db.DB(c.Request.Context()).Model(&model.RedEnvelope{}).
Where("creator_id = ? AND created_at >= ?", currentUser.ID, today).
Count(&todayCount).Error; err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/apps/redenvelope/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package redenvelope

import (
"math/rand"
"time"

"github.com/shopspring/decimal"
)

func startOfDay(now time.Time) time.Time {
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
}

// calculateRandomAmount 二倍均值算法计算随机红包金额
func calculateRandomAmount(remaining decimal.Decimal, count int) decimal.Decimal {
// 如果是最后一个红包,返回所有剩余金额(避免舍入误差)
Expand Down
32 changes: 32 additions & 0 deletions internal/apps/redenvelope/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2025 linux.do

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package redenvelope

import (
"testing"
"time"
)

func TestStartOfDayUsesLocalMidnight(t *testing.T) {
location := time.FixedZone("UTC+8", 8*60*60)
now := time.Date(2026, time.September, 4, 1, 30, 0, 0, location)
want := time.Date(2026, time.September, 4, 0, 0, 0, 0, location)

if got := startOfDay(now); !got.Equal(want) {
t.Fatalf("startOfDay(%v) = %v, want %v", now, got, want)
}
}
Loading