优惠券相关逻辑

This commit is contained in:
yan.y
2024-08-12 13:37:43 +08:00
parent e2fb325452
commit 3cd4e4b335
11 changed files with 369 additions and 84 deletions
+19
View File
@@ -6,6 +6,7 @@ import (
"pet-house.com/business/models"
"strconv"
"testing"
"time"
)
func TestGetStrDays(t *testing.T) {
@@ -54,3 +55,21 @@ func TestMath(t *testing.T) {
var a = int(RoundToOneDecimalPlace(float64(150)*(95/100.0)) * 10)
fmt.Println(a)
}
func TestMap(t *testing.T) {
var c1 = make(map[int]models.Coupons)
c1[1] = models.Coupons{
Id: 1,
Name: "111",
GoodsSubType: 0,
Discount: 0,
Source: "",
PeriodType: 0,
Period: "",
PeriodInfo: nil,
ExpireTime: 0,
CreateTime: time.Time{},
UpdateTime: time.Time{},
Status: 0,
}
print("12345", c1[2].Id)
}
+23
View File
@@ -0,0 +1,23 @@
package utils
import "sync"
// KeyedMutex 提供基于键的锁定
type KeyedMutex struct {
mutexes sync.Map
}
// Lock 锁定指定键
func (km *KeyedMutex) Lock(key string) {
mu, _ := km.mutexes.LoadOrStore(key, &sync.Mutex{})
mu.(*sync.Mutex).Lock()
}
// Unlock 解锁指定键
func (km *KeyedMutex) Unlock(key string) {
mu, ok := km.mutexes.Load(key)
if ok {
mu.(*sync.Mutex).Unlock()
km.mutexes.Delete(key)
}
}