Kaynağa Gözat

add dup_key

yansen 6 yıl önce
ebeveyn
işleme
dd9cdf501a

+ 5
- 2
models/goods/orders.go Dosyayı Görüntüle

@@ -42,16 +42,19 @@ func (m *GoodsDAO) SaveOrders(order *model.TaGoodsOrders) error {
42 42
 		return err
43 43
 	}
44 44
 
45
+	now := time.Now().Local()
46
+
45 47
 	// order.OrdersId = guid.NewGUIDString()
46 48
 	order.OrgId = caseInfo.OrgId
47
-	order.CreateDate = time.Now().Local()
49
+	order.CreateDate = now
48 50
 	order.Status = models.STATUS_NORMAL
49 51
 	order.IsPay = models.BOOL_TRUE // 默认已支付
50 52
 	order.CouponAmount = "0"
51 53
 	order.IsIntimidate = INTIMIDATE_NO
54
+	order.DupKey = utils.GetFiveSeconds(now)
52 55
 
53 56
 	// 当前订单号的随机方式 = 时间 + 个人ID
54
-	ordersno := "G-" + time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.UserId), "")
57
+	ordersno := "G-" + now.Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.UserId), "")
55 58
 	if len(ordersno) > 32 {
56 59
 		ordersno = string([]byte(ordersno)[:32])
57 60
 	}

+ 1
- 0
models/model/ta_goods_orders.go Dosyayı Görüntüle

@@ -28,4 +28,5 @@ type TaGoodsOrders struct {
28 28
 	ActualAmount string `xorm:"DECIMAL(8,2)"`
29 29
 	CouponAmount string `xorm:"DECIMAL(8,2)"`
30 30
 	IsIntimidate int    `xorm:"SMALLINT(6)"`
31
+	DupKey       string `xorm:"VARCHAR(32)"`
31 32
 }

+ 15
- 0
utils/utils.go Dosyayı Görüntüle

@@ -141,3 +141,18 @@ func GenerateQRCode() string {
141 141
 	var code string = "66" + strconv.Itoa(temp1) + temp4 + strconv.Itoa(temp2) + temp3 + strconv.Itoa(temp5)
142 142
 	return code
143 143
 }
144
+
145
+// GetFiveSeconds 获取 5 秒钟的时间
146
+func GetFiveSeconds(t time.Time) string {
147
+	str := t.Format("20060102150405")
148
+	strLen := len(str)
149
+
150
+	lastNum, _ := strconv.Atoi(str[strLen-1:])
151
+	if lastNum < 5 {
152
+		lastNum = 0
153
+	} else {
154
+		lastNum = 5
155
+	}
156
+
157
+	return str[:strLen-1] + strconv.Itoa(lastNum)
158
+}

+ 30
- 0
utils/utils_test.go Dosyayı Görüntüle

@@ -4,6 +4,7 @@ import (
4 4
 	"spaceofcheng/services/utils"
5 5
 	"strings"
6 6
 	"testing"
7
+	"time"
7 8
 )
8 9
 
9 10
 func TestStrSliceIndexOf(t *testing.T) {
@@ -86,3 +87,32 @@ func TestDecodeBase64NoTail(t *testing.T) {
86 87
 		}
87 88
 	}
88 89
 }
90
+
91
+func TestGetFiveSeconds(t *testing.T) {
92
+	cases := []map[string]string{
93
+		map[string]string{
94
+			"case":     "2018-10-07 13:21:40",
95
+			"expected": "20181007132140",
96
+		},
97
+		map[string]string{
98
+			"case":     "2018-10-07 13:21:42",
99
+			"expected": "20181007132140",
100
+		},
101
+		map[string]string{
102
+			"case":     "2018-10-07 13:21:45",
103
+			"expected": "20181007132145",
104
+		},
105
+		map[string]string{
106
+			"case":     "2018-10-07 13:21:48",
107
+			"expected": "20181007132145",
108
+		},
109
+	}
110
+
111
+	for _, cs := range cases {
112
+		dt, _ := time.Parse("2006-01-02 15:04:05", cs["case"])
113
+		res := utils.GetFiveSeconds(dt)
114
+		if res != cs["expected"] {
115
+			t.Fatalf("TestGetFiveSeconds fail : %s", cs["case"])
116
+		}
117
+	}
118
+}