utils.go
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package utils
import (
"encoding/json"
"fmt"
"time"
)
/**
* @Author SteveChan
* @Description //TODO
* @Date 16:50 2020/12/10
* @Param
* @return
**/
func StringSliceEqualBCE(a, b []int) bool {
if len(a) != len(b) {
return false
}
if (a == nil) != (b == nil) {
return false
}
b = b[:len(a)]
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
/**
* @Author SteveChan
* @Description //TODO
* @Date 16:50 2020/12/10
* @Param
* @return
**/
func IsContain(items []interface{}, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
/**
* @Author SteveChan
* @Description //TODO
* @Date 16:50 2020/12/10
* @Param
* @return
**/
func IsContainInt(items map[int64]interface{}, item int64) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
type LocalDate time.Time
/**
* @Author SteveChan
* @Description // MarshalJSON satify the json marshal interface
* @Date 16:49 2020/12/10
* @Param
* @return
**/
func (l LocalDate) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", time.Time(l).Format("2006-01-02"))
return []byte(stamp), nil
}
/**
* @Author SteveChan
* @Description //TODO
* @Date 16:49 2020/12/10
* @Param
* @return
**/
func JsonToMap(jsonStr string) (map[string]string, error) {
m := make(map[string]string)
err := json.Unmarshal([]byte(jsonStr), &m)
if err != nil {
fmt.Printf("Unmarshal with error: %+v\n", err)
return nil, err
}
for k, v := range m {
fmt.Printf("%v: %v\n", k, v)
}
return m, nil
}
/**
* @Author SteveChan
* @Description 插入切片指定位置
* @Date 16:46 2020/12/10
* @Param index int 插入的位置
* @Param newStr []string 待插入的切片
* @Param str []string 源切片
* @return ns []string 返回新的切片
**/
func InsertSlice(index int, newstr []string, src []string) (ns []string) {
ns = append(ns, src[:index]...) // 切片后加..., 相当于拆包成单个元素
ns = append(ns, newstr...)
ns = append(ns, src[index:]...)
return
}
/**
* @Author SteveChan
* @Description 查找相同的手机号
* @Date 16:45 2020/12/10
* @Param nums []int 数组
* @return int
**/
func findRepeatNumber(nums []int) int {
maps := make(map[int]bool)
for _,num:=range nums{
if maps[num]{
return num
}else{
maps[num]=true
}
}
return -1
}
/**
* @Author SteveChan
* @Description //
* @Date 16:49 2020/12/10
* @Param
* @return
**/
//RankPeriodCheckTime 设置赛季时检查时间范围的合法性
//func RankPeriodCheckTime(rankTypeId int64, beginTime int64, endTime int64, idNot int64) bool {
// sql := `SELECT count(*) FROM rank_period
// WHERE rank_type_id = %d
// AND id <> %d
// AND
// (
// (UNIX_TIMESTAMP(begin_time) BETWEEN %d AND %d)
// OR
// (UNIX_TIMESTAMP(end_time) BETWEEN %d AND %d)
// OR
// (%d BETWEEN UNIX_TIMESTAMP(begin_time) AND UNIX_TIMESTAMP(end_time))
// OR
// (%d BETWEEN UNIX_TIMESTAMP(begin_time) AND UNIX_TIMESTAMP(end_time))
// )
// LIMIT 1 `
// sql = fmt.Sprintf(sql, rankTypeId, idNot, beginTime, endTime, beginTime, endTime, beginTime, endTime)
// var cnt int
// err := utils.ExecuteQueryOne(&cnt, sql)
// if err != nil {
// log.Error("SQL Execute err:%s", err)
// return false
// }
// if cnt > 0 {
// return false
// }
// return true
//}