From 24b86e3b4d4885b5e7577b6a47c1911f2a1483b7 Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Wed, 3 Jan 2024 16:39:06 +0800 Subject: [PATCH] fix import cycle --- admin/user.go | 1 + auth/analysis.go | 31 +++++++++++++++++++++++++++++++ auth/payment.go | 3 +-- auth/redeem.go | 3 +-- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 auth/analysis.go diff --git a/admin/user.go b/admin/user.go index 51bf4f7..df8c646 100644 --- a/admin/user.go +++ b/admin/user.go @@ -127,5 +127,6 @@ func UpdateRootPassword(db *sql.DB, cache *redis.Client, password string) error } cache.Del(context.Background(), fmt.Sprint("nio:user:root")) + return nil } diff --git a/auth/analysis.go b/auth/analysis.go new file mode 100644 index 0000000..27017a5 --- /dev/null +++ b/auth/analysis.go @@ -0,0 +1,31 @@ +package auth + +import ( + "chat/utils" + "fmt" + "github.com/go-redis/redis/v8" + "time" +) + +func getMonth() string { + date := time.Now() + return date.Format("2006-01") +} + +func getDay() string { + date := time.Now() + return date.Format("2006-01-02") +} + +func getBillingFormat(t string) string { + return fmt.Sprintf("nio:billing-analysis-%s", t) +} + +func getMonthBillingFormat(t string) string { + return fmt.Sprintf("nio:billing-analysis-%s", t) +} + +func incrBillingRequest(cache *redis.Client, amount int64) { + utils.IncrWithExpire(cache, getBillingFormat(getDay()), amount, time.Hour*24*30*2) + utils.IncrWithExpire(cache, getMonthBillingFormat(getMonth()), amount, time.Hour*24*30*2) +} diff --git a/auth/payment.go b/auth/payment.go index 40c800b..5aa1458 100644 --- a/auth/payment.go +++ b/auth/payment.go @@ -1,7 +1,6 @@ package auth import ( - "chat/admin" "chat/utils" "database/sql" "errors" @@ -79,7 +78,7 @@ func (u *User) Pay(db *sql.DB, cache *redis.Client, amount float32) bool { if useDeeptrain() { state := Pay(u.Username, amount) if state { - admin.IncrBillingRequest(cache, int64(amount*100)) + incrBillingRequest(cache, int64(amount*100)) } return state } diff --git a/auth/redeem.go b/auth/redeem.go index c1c129a..8be8492 100644 --- a/auth/redeem.go +++ b/auth/redeem.go @@ -1,7 +1,6 @@ package auth import ( - "chat/admin" "chat/utils" "database/sql" "errors" @@ -106,7 +105,7 @@ func (u *User) UseRedeem(db *sql.DB, cache *redis.Client, code string) (float32, return 0, fmt.Errorf("failed to use redeem code: %w", err) } - admin.IncrBillingRequest(cache, int64(redeem.GetQuota()*10)) + incrBillingRequest(cache, int64(redeem.GetQuota()*10)) return redeem.GetQuota(), nil } }