浏览代码

add dup_key

yansen 6 年前
父节点
当前提交
dd9cdf501a
共有 4 个文件被更改,包括 51 次插入2 次删除
  1. 5
    2
      models/goods/orders.go
  2. 1
    0
      models/model/ta_goods_orders.go
  3. 15
    0
      utils/utils.go
  4. 30
    0
      utils/utils_test.go

+ 5
- 2
models/goods/orders.go 查看文件

42
 		return err
42
 		return err
43
 	}
43
 	}
44
 
44
 
45
+	now := time.Now().Local()
46
+
45
 	// order.OrdersId = guid.NewGUIDString()
47
 	// order.OrdersId = guid.NewGUIDString()
46
 	order.OrgId = caseInfo.OrgId
48
 	order.OrgId = caseInfo.OrgId
47
-	order.CreateDate = time.Now().Local()
49
+	order.CreateDate = now
48
 	order.Status = models.STATUS_NORMAL
50
 	order.Status = models.STATUS_NORMAL
49
 	order.IsPay = models.BOOL_TRUE // 默认已支付
51
 	order.IsPay = models.BOOL_TRUE // 默认已支付
50
 	order.CouponAmount = "0"
52
 	order.CouponAmount = "0"
51
 	order.IsIntimidate = INTIMIDATE_NO
53
 	order.IsIntimidate = INTIMIDATE_NO
54
+	order.DupKey = utils.GetFiveSeconds(now)
52
 
55
 
53
 	// 当前订单号的随机方式 = 时间 + 个人ID
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
 	if len(ordersno) > 32 {
58
 	if len(ordersno) > 32 {
56
 		ordersno = string([]byte(ordersno)[:32])
59
 		ordersno = string([]byte(ordersno)[:32])
57
 	}
60
 	}

+ 1
- 0
models/model/ta_goods_orders.go 查看文件

28
 	ActualAmount string `xorm:"DECIMAL(8,2)"`
28
 	ActualAmount string `xorm:"DECIMAL(8,2)"`
29
 	CouponAmount string `xorm:"DECIMAL(8,2)"`
29
 	CouponAmount string `xorm:"DECIMAL(8,2)"`
30
 	IsIntimidate int    `xorm:"SMALLINT(6)"`
30
 	IsIntimidate int    `xorm:"SMALLINT(6)"`
31
+	DupKey       string `xorm:"VARCHAR(32)"`
31
 }
32
 }

+ 15
- 0
utils/utils.go 查看文件

141
 	var code string = "66" + strconv.Itoa(temp1) + temp4 + strconv.Itoa(temp2) + temp3 + strconv.Itoa(temp5)
141
 	var code string = "66" + strconv.Itoa(temp1) + temp4 + strconv.Itoa(temp2) + temp3 + strconv.Itoa(temp5)
142
 	return code
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 查看文件

4
 	"spaceofcheng/services/utils"
4
 	"spaceofcheng/services/utils"
5
 	"strings"
5
 	"strings"
6
 	"testing"
6
 	"testing"
7
+	"time"
7
 )
8
 )
8
 
9
 
9
 func TestStrSliceIndexOf(t *testing.T) {
10
 func TestStrSliceIndexOf(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
+}