You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
3.2 KiB
142 lines
3.2 KiB
package internal |
|
|
|
import ( |
|
"fmt" |
|
"github.com/gogf/gf/v2/database/gdb" |
|
"go.mongodb.org/mongo-driver/bson" |
|
"math" |
|
"math/rand" |
|
"strconv" |
|
"strings" |
|
) |
|
|
|
func CompareType(model *gdb.Model, recharge int, compareType int) *gdb.Model { |
|
if recharge > 0 { |
|
switch compareType { |
|
case 1: |
|
model = model.Where("amount=?", recharge) |
|
case 2: |
|
model = model.Where("amount<=?", recharge) |
|
case 3: |
|
model = model.Where("amount<?", recharge) |
|
case 4: |
|
model = model.Where("amount>?", recharge) |
|
case 5: |
|
model = model.Where("amount>=?", recharge) |
|
} |
|
} |
|
return model |
|
} |
|
|
|
func CompareSqlType(sql string, recharge int, name string, compareType int) string { |
|
if recharge > 0 { |
|
switch compareType { |
|
case 1: |
|
sql += fmt.Sprintf(" and %s=%d ", name, recharge) |
|
case 2: |
|
sql += fmt.Sprintf(" and %s<=%d ", name, recharge) |
|
case 3: |
|
sql += fmt.Sprintf(" and %s<%d ", name, recharge) |
|
case 4: |
|
sql += fmt.Sprintf(" and %s>%d ", name, recharge) |
|
case 5: |
|
sql += fmt.Sprintf(" and %s>=%d ", name, recharge) |
|
} |
|
} |
|
return sql |
|
} |
|
|
|
func checkCompareType(compareType int, data int) any { |
|
switch compareType { |
|
case 1: |
|
return data |
|
case 2: |
|
return bson.M{"$lte": data} |
|
case 3: |
|
return bson.M{"$lt": data} |
|
case 4: |
|
return bson.M{"$gt": data} |
|
case 5: |
|
return bson.M{"$gte": data} |
|
} |
|
return data |
|
} |
|
|
|
func checkDaoIntCompareType(model *gdb.Model, compareType int, target string, data int) *gdb.Model { |
|
switch compareType { |
|
case 1: |
|
model = model.Where(fmt.Sprintf("%s=?", target), data) |
|
case 2: |
|
model = model.Where(fmt.Sprintf("%s<=?", target), data) |
|
case 3: |
|
model = model.Where(fmt.Sprintf("%s<?", target), data) |
|
case 4: |
|
model = model.Where(fmt.Sprintf("%s>?", target), data) |
|
case 5: |
|
model = model.Where(fmt.Sprintf("%s>=?", target), data) |
|
} |
|
|
|
return model |
|
} |
|
|
|
func generateOne(charset string, lenfrom, lento int) string { |
|
result := "" |
|
if (lenfrom <= lento) && (len(charset) > 0) && (lenfrom >= 0) { |
|
length := math.Round(randomFloat(lenfrom, lento)) |
|
for { |
|
if float64(len(result)) < length { |
|
Float64Data := math.Floor(rand.Float64() * float64(len(charset))) |
|
Int, _ := strconv.Atoi(fmt.Sprint(Float64Data)) |
|
result += string(getChar(charset, Int)) |
|
} else { |
|
break |
|
} |
|
} |
|
} |
|
|
|
return result |
|
} |
|
|
|
func getChar(str string, index int) rune { |
|
return []rune(str)[index] |
|
} |
|
|
|
// int |
|
func randInt(min int, max int) int { |
|
return min + rand.Intn(max-min) |
|
} |
|
|
|
// float |
|
func randomFloat(min, max int) float64 { |
|
if max == min { |
|
return float64(max) |
|
} |
|
return decimal(rand.Float64()*(float64(max)-float64(min)) + float64(min)) |
|
} |
|
|
|
func decimal(v float64) float64 { |
|
val, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", v), 64) |
|
return val |
|
} |
|
|
|
func inArrayString(arr []string, target string) bool { |
|
for _, v := range arr { |
|
if v == target { |
|
return true |
|
} |
|
} |
|
return false |
|
} |
|
|
|
func cdKeyFilename(preName string, id int64) string { |
|
return fmt.Sprintf("%s%d.xlsx", preName, id) |
|
} |
|
|
|
// ReplaceLastOccurrence 替换最后一个匹配的子字符串 |
|
func ReplaceLastOccurrence(s, oldStr, newStr string) string { |
|
lastIndex := strings.LastIndex(s, oldStr) |
|
if lastIndex == -1 { |
|
return s // 未找到,直接返回原字符串 |
|
} |
|
return s[:lastIndex] + newStr + s[lastIndex+len(oldStr):] |
|
}
|
|
|