common.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. package utils
  2. import (
  3. "bufio"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "image"
  13. "image/png"
  14. "io"
  15. "math"
  16. "math/rand"
  17. "net"
  18. "net/http"
  19. "os"
  20. "os/exec"
  21. "path"
  22. "regexp"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "time"
  27. "unicode"
  28. )
  29. // 随机数种子
  30. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  31. func GetRandString(size int) string {
  32. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
  33. randomSb := ""
  34. digitSize := len(allLetterDigit)
  35. for i := 0; i < size; i++ {
  36. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  37. }
  38. return randomSb
  39. }
  40. func GetRandStringNoSpecialChar(size int) string {
  41. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  42. randomSb := ""
  43. digitSize := len(allLetterDigit)
  44. for i := 0; i < size; i++ {
  45. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  46. }
  47. return randomSb
  48. }
  49. func StringsToJSON(str string) string {
  50. rs := []rune(str)
  51. jsons := ""
  52. for _, r := range rs {
  53. rint := int(r)
  54. if rint < 128 {
  55. jsons += string(r)
  56. } else {
  57. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  58. }
  59. }
  60. return jsons
  61. }
  62. // 序列化
  63. func ToString(v interface{}) string {
  64. data, _ := json.Marshal(v)
  65. return string(data)
  66. }
  67. // md5加密
  68. func MD5(data string) string {
  69. m := md5.Sum([]byte(data))
  70. return hex.EncodeToString(m[:])
  71. }
  72. // 获取数字随机字符
  73. func GetRandDigit(n int) string {
  74. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  75. }
  76. // 获取随机数
  77. func GetRandNumber(n int) int {
  78. return rnd.Intn(n)
  79. }
  80. func GetRandInt(min, max int) int {
  81. if min >= max || min == 0 || max == 0 {
  82. return max
  83. }
  84. return rand.Intn(max-min) + min
  85. }
  86. func GetToday(format string) string {
  87. today := time.Now().Format(format)
  88. return today
  89. }
  90. // 获取今天剩余秒数
  91. func GetTodayLastSecond() time.Duration {
  92. today := GetToday(FormatDate) + " 23:59:59"
  93. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  94. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  95. }
  96. // 处理出生日期函数
  97. func GetBrithDate(idcard string) string {
  98. l := len(idcard)
  99. var s string
  100. if l == 15 {
  101. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  102. return s
  103. }
  104. if l == 18 {
  105. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  106. return s
  107. }
  108. return GetToday(FormatDate)
  109. }
  110. // 处理性别
  111. func WhichSexByIdcard(idcard string) string {
  112. var sexs = [2]string{"女", "男"}
  113. length := len(idcard)
  114. if length == 18 {
  115. sex, _ := strconv.Atoi(string(idcard[16]))
  116. return sexs[sex%2]
  117. } else if length == 15 {
  118. sex, _ := strconv.Atoi(string(idcard[14]))
  119. return sexs[sex%2]
  120. }
  121. return "男"
  122. }
  123. // 截取小数点后几位
  124. func SubFloatToString(f float64, m int) string {
  125. n := strconv.FormatFloat(f, 'f', -1, 64)
  126. if n == "" {
  127. return ""
  128. }
  129. if m >= len(n) {
  130. return n
  131. }
  132. newn := strings.Split(n, ".")
  133. if m == 0 {
  134. return newn[0]
  135. }
  136. if len(newn) < 2 || m >= len(newn[1]) {
  137. return n
  138. }
  139. return newn[0] + "." + newn[1][:m]
  140. }
  141. // 截取小数点后几位
  142. func SubFloatToFloat(f float64, m int) float64 {
  143. newn := SubFloatToString(f, m)
  144. newf, _ := strconv.ParseFloat(newn, 64)
  145. return newf
  146. }
  147. // 截取小数点后几位
  148. func SubFloatToFloatStr(f float64, m int) string {
  149. newn := SubFloatToString(f, m)
  150. return newn
  151. }
  152. // 获取相差时间-年
  153. func GetYearDiffer(start_time, end_time string) int {
  154. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  155. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  156. age := t2.Year() - t1.Year()
  157. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  158. age--
  159. }
  160. return age
  161. }
  162. // 获取相差时间-秒
  163. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  164. diff := end_time.Unix() - start_time.Unix()
  165. return diff
  166. }
  167. func FixFloat(f float64, m int) float64 {
  168. newn := SubFloatToString(f+0.00000001, m)
  169. newf, _ := strconv.ParseFloat(newn, 64)
  170. return newf
  171. }
  172. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  173. func StrListToString(strList []string) (str string) {
  174. if len(strList) > 0 {
  175. for k, v := range strList {
  176. if k == 0 {
  177. str = v
  178. } else {
  179. str = str + "," + v
  180. }
  181. }
  182. return
  183. }
  184. return ""
  185. }
  186. // Token
  187. func GetToken() string {
  188. randStr := GetRandString(64)
  189. token := MD5(randStr + Md5Key)
  190. tokenLen := 64 - len(token)
  191. return strings.ToUpper(token + GetRandString(tokenLen))
  192. }
  193. // 数据没有记录
  194. func ErrNoRow() string {
  195. return "<QuerySeter> no row found"
  196. }
  197. // 判断文件是否存在
  198. func FileIsExist(filePath string) bool {
  199. _, err := os.Stat(filePath)
  200. return err == nil || os.IsExist(err)
  201. }
  202. // 获取图片扩展名
  203. func GetImgExt(file string) (ext string, err error) {
  204. var headerByte []byte
  205. headerByte = make([]byte, 8)
  206. fd, err := os.Open(file)
  207. if err != nil {
  208. return "", err
  209. }
  210. defer fd.Close()
  211. _, err = fd.Read(headerByte)
  212. if err != nil {
  213. return "", err
  214. }
  215. xStr := fmt.Sprintf("%x", headerByte)
  216. switch {
  217. case xStr == "89504e470d0a1a0a":
  218. ext = ".png"
  219. case xStr == "0000010001002020":
  220. ext = ".ico"
  221. case xStr == "0000020001002020":
  222. ext = ".cur"
  223. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  224. ext = ".gif"
  225. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  226. ext = ".tga"
  227. case xStr[:8] == "464f524d":
  228. ext = ".iff"
  229. case xStr[:8] == "52494646":
  230. ext = ".ani"
  231. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  232. ext = ".tiff"
  233. case xStr[:4] == "424d":
  234. ext = ".bmp"
  235. case xStr[:4] == "ffd8":
  236. ext = ".jpg"
  237. case xStr[:2] == "0a":
  238. ext = ".pcx"
  239. default:
  240. ext = ""
  241. }
  242. return ext, nil
  243. }
  244. // 保存图片
  245. func SaveImage(path string, img image.Image) (err error) {
  246. //需要保持的文件
  247. imgfile, err := os.Create(path)
  248. defer imgfile.Close()
  249. // 以PNG格式保存文件
  250. err = png.Encode(imgfile, img)
  251. return err
  252. }
  253. // 下载图片
  254. func DownloadImage(imgUrl string) (filePath string, err error) {
  255. imgPath := "./static/imgs/"
  256. fileName := path.Base(imgUrl)
  257. res, err := http.Get(imgUrl)
  258. if err != nil {
  259. fmt.Println("A error occurred!")
  260. return
  261. }
  262. defer res.Body.Close()
  263. // 获得get请求响应的reader对象
  264. reader := bufio.NewReaderSize(res.Body, 32*1024)
  265. filePath = imgPath + fileName
  266. file, err := os.Create(filePath)
  267. if err != nil {
  268. return
  269. }
  270. // 获得文件的writer对象
  271. writer := bufio.NewWriter(file)
  272. written, _ := io.Copy(writer, reader)
  273. fmt.Printf("Total length: %d \n", written)
  274. return
  275. }
  276. // 保存base64数据为文件
  277. func SaveBase64ToFile(content, path string) error {
  278. data, err := base64.StdEncoding.DecodeString(content)
  279. if err != nil {
  280. return err
  281. }
  282. f, err := os.Create(path)
  283. defer f.Close()
  284. if err != nil {
  285. return err
  286. }
  287. f.Write(data)
  288. return nil
  289. }
  290. func SaveBase64ToFileBySeek(content, path string) (err error) {
  291. data, err := base64.StdEncoding.DecodeString(content)
  292. exist, err := PathExists(path)
  293. if err != nil {
  294. return
  295. }
  296. if !exist {
  297. f, err := os.Create(path)
  298. if err != nil {
  299. return err
  300. }
  301. n, _ := f.Seek(0, 2)
  302. // 从末尾的偏移量开始写入内容
  303. _, err = f.WriteAt([]byte(data), n)
  304. defer f.Close()
  305. } else {
  306. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  307. if err != nil {
  308. return err
  309. }
  310. n, _ := f.Seek(0, 2)
  311. // 从末尾的偏移量开始写入内容
  312. _, err = f.WriteAt([]byte(data), n)
  313. defer f.Close()
  314. }
  315. return nil
  316. }
  317. func PathExists(path string) (bool, error) {
  318. _, err := os.Stat(path)
  319. if err == nil {
  320. return true, nil
  321. }
  322. if os.IsNotExist(err) {
  323. return false, nil
  324. }
  325. return false, err
  326. }
  327. func StartIndex(page, pagesize int) int {
  328. if page > 1 {
  329. return (page - 1) * pagesize
  330. }
  331. return 0
  332. }
  333. func PageCount(count, pagesize int) int {
  334. if count%pagesize > 0 {
  335. return count/pagesize + 1
  336. } else {
  337. return count / pagesize
  338. }
  339. }
  340. func TrimHtml(src string) string {
  341. //将HTML标签全转换成小写
  342. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  343. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  344. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  345. src = re.ReplaceAllString(src, "[图片]")
  346. re, _ = regexp.Compile("class[\\S\\s]+?>")
  347. src = re.ReplaceAllString(src, "")
  348. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  349. src = re.ReplaceAllString(src, "")
  350. return strings.TrimSpace(src)
  351. }
  352. //1556164246 -> 2019-04-25 03:50:46 +0000
  353. //timestamp
  354. func TimeToTimestamp() {
  355. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  356. }
  357. func ToUnicode(text string) string {
  358. textQuoted := strconv.QuoteToASCII(text)
  359. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  360. return textUnquoted
  361. }
  362. func VersionToInt(version string) int {
  363. version = strings.Replace(version, ".", "", -1)
  364. n, _ := strconv.Atoi(version)
  365. return n
  366. }
  367. func IsCheckInList(list []int, s int) bool {
  368. for _, v := range list {
  369. if v == s {
  370. return true
  371. }
  372. }
  373. return false
  374. }
  375. func round(num float64) int {
  376. return int(num + math.Copysign(0.5, num))
  377. }
  378. func toFixed(num float64, precision int) float64 {
  379. output := math.Pow(10, float64(precision))
  380. return float64(round(num*output)) / output
  381. }
  382. // GetWilsonScore returns Wilson Score
  383. func GetWilsonScore(p, n float64) float64 {
  384. if p == 0 && n == 0 {
  385. return 0
  386. }
  387. return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
  388. }
  389. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  390. func ChangeWordsToNum(str string) (numStr string) {
  391. words := ([]rune)(str)
  392. num := 0
  393. n := 0
  394. for i := 0; i < len(words); i++ {
  395. word := string(words[i : i+1])
  396. switch word {
  397. case "万":
  398. if n == 0 {
  399. n = 1
  400. }
  401. n = n * 10000
  402. num = num*10000 + n
  403. n = 0
  404. case "千":
  405. if n == 0 {
  406. n = 1
  407. }
  408. n = n * 1000
  409. num += n
  410. n = 0
  411. case "百":
  412. if n == 0 {
  413. n = 1
  414. }
  415. n = n * 100
  416. num += n
  417. n = 0
  418. case "十":
  419. if n == 0 {
  420. n = 1
  421. }
  422. n = n * 10
  423. num += n
  424. n = 0
  425. case "一":
  426. n += 1
  427. case "二":
  428. n += 2
  429. case "三":
  430. n += 3
  431. case "四":
  432. n += 4
  433. case "五":
  434. n += 5
  435. case "六":
  436. n += 6
  437. case "七":
  438. n += 7
  439. case "八":
  440. n += 8
  441. case "九":
  442. n += 9
  443. case "零":
  444. default:
  445. if n > 0 {
  446. num += n
  447. n = 0
  448. }
  449. if num == 0 {
  450. numStr += word
  451. } else {
  452. numStr += strconv.Itoa(num) + word
  453. num = 0
  454. }
  455. }
  456. }
  457. if n > 0 {
  458. num += n
  459. n = 0
  460. }
  461. if num != 0 {
  462. numStr += strconv.Itoa(num)
  463. }
  464. return
  465. }
  466. func Sha1(data string) string {
  467. sha1 := sha1.New()
  468. sha1.Write([]byte(data))
  469. return hex.EncodeToString(sha1.Sum([]byte("")))
  470. }
  471. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  472. cmd := `ffmpeg -i ` + videoPath + ` 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
  473. out, err := exec.Command("bash", "-c", cmd).Output()
  474. if err != nil {
  475. return
  476. }
  477. outTimes := string(out)
  478. fmt.Println("outTimes:", outTimes)
  479. if outTimes != "" {
  480. timeArr := strings.Split(outTimes, ":")
  481. h := timeArr[0]
  482. m := timeArr[1]
  483. s := timeArr[2]
  484. hInt, err := strconv.Atoi(h)
  485. if err != nil {
  486. return playSeconds, err
  487. }
  488. mInt, err := strconv.Atoi(m)
  489. if err != nil {
  490. return playSeconds, err
  491. }
  492. s = strings.Trim(s, " ")
  493. s = strings.Trim(s, "\n")
  494. sInt, err := strconv.ParseFloat(s, 64)
  495. if err != nil {
  496. return playSeconds, err
  497. }
  498. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  499. }
  500. return
  501. }
  502. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  503. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  504. tradeCode = strings.Trim(tradeCode, " ")
  505. tradeCodeInt, err := strconv.Atoi(tradeCode)
  506. if err != nil {
  507. return
  508. }
  509. tradeCodeInt = tradeCodeInt + 1
  510. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  511. return
  512. }
  513. // excel日期字段格式化 yyyy-mm-dd
  514. func ConvertToFormatDay(excelDaysString string) string {
  515. // 2006-01-02 距离 1900-01-01的天数
  516. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  517. curDiffDay := excelDaysString
  518. b, _ := strconv.Atoi(curDiffDay)
  519. // 获取excel的日期距离2006-01-02的天数
  520. realDiffDay := b - baseDiffDay
  521. //fmt.Println("realDiffDay:",realDiffDay)
  522. // 距离2006-01-02 秒数
  523. realDiffSecond := realDiffDay * 24 * 3600
  524. //fmt.Println("realDiffSecond:",realDiffSecond)
  525. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  526. baseOriginSecond := 1136185445
  527. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  528. return resultTime
  529. }
  530. func CheckPwd(pwd string) bool {
  531. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  532. reg := regexp.MustCompile(compile)
  533. flag := reg.MatchString(pwd)
  534. return flag
  535. }
  536. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  537. // 数字月份必须前置补零
  538. if len(myMonth) == 1 {
  539. myMonth = "0" + myMonth
  540. }
  541. yInt, _ := strconv.Atoi(myYear)
  542. timeLayout := "2006-01-02 15:04:05"
  543. loc, _ := time.LoadLocation("Local")
  544. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  545. newMonth := theTime.Month()
  546. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  547. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  548. return t1, t2
  549. }
  550. // 移除字符串中的空格
  551. func TrimStr(str string) (str2 string) {
  552. if str == "" {
  553. return str
  554. }
  555. return strings.Replace(str, " ", "", -1)
  556. }
  557. // 字符串转换为time
  558. func StrTimeToTime(strTime string) time.Time {
  559. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  560. loc, _ := time.LoadLocation("Local") //重要:获取时区
  561. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  562. return resultTime
  563. }
  564. // 字符串类型时间转周几
  565. func StrDateTimeToWeek(strTime string) string {
  566. var WeekDayMap = map[string]string{
  567. "Monday": "周一",
  568. "Tuesday": "周二",
  569. "Wednesday": "周三",
  570. "Thursday": "周四",
  571. "Friday": "周五",
  572. "Saturday": "周六",
  573. "Sunday": "周日",
  574. }
  575. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  576. startday, _ := time.ParseInLocation("2006-01-02", ctime, time.Local)
  577. staweek_int := startday.Weekday().String()
  578. return WeekDayMap[staweek_int]
  579. }
  580. // 时间格式转年月日字符串
  581. func TimeToStrYmd(time2 time.Time) string {
  582. var Ymd string
  583. year := time2.Year()
  584. month := time2.Format("1")
  585. day1 := time.Now().Day()
  586. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  587. return Ymd
  588. }
  589. // 时间格式去掉时分秒
  590. func TimeRemoveHms(strTime string) string {
  591. var Ymd string
  592. var resultTime = StrTimeToTime(strTime)
  593. year := resultTime.Year()
  594. month := resultTime.Format("01")
  595. day1 := resultTime.Day()
  596. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  597. return Ymd
  598. }
  599. // 时间格式去掉时分秒
  600. func TimeRemoveHms2(strTime string) string {
  601. var Ymd string
  602. var resultTime = StrTimeToTime(strTime)
  603. year := resultTime.Year()
  604. month := resultTime.Format("01")
  605. day1 := resultTime.Day()
  606. Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
  607. return Ymd
  608. }
  609. // 文章上一次编辑时间
  610. func ArticleLastTime(strTime string) string {
  611. var newTime string
  612. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  613. diffTime := time.Now().Unix() - stamp.Unix()
  614. if diffTime <= 60 {
  615. newTime = "当前"
  616. } else if diffTime < 60*60 {
  617. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  618. } else if diffTime < 24*60*60 {
  619. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  620. } else if diffTime < 30*24*60*60 {
  621. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  622. } else if diffTime < 12*30*24*60*60 {
  623. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  624. } else {
  625. newTime = "1年前"
  626. }
  627. return newTime
  628. }
  629. // 人民币小写转大写
  630. func ConvertNumToCny(num float64) (str string, err error) {
  631. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  632. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  633. // log.Println(sliceUnit[:len(sliceUnit)-2])
  634. s := sliceUnit[len(sliceUnit)-len(strNum):]
  635. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  636. for k, v := range strNum[:] {
  637. str = str + upperDigitUnit[string(v)] + s[k]
  638. }
  639. reg, err := regexp.Compile(`零角零分$`)
  640. str = reg.ReplaceAllString(str, "整")
  641. reg, err = regexp.Compile(`零角`)
  642. str = reg.ReplaceAllString(str, "零")
  643. reg, err = regexp.Compile(`零分$`)
  644. str = reg.ReplaceAllString(str, "整")
  645. reg, err = regexp.Compile(`零[仟佰拾]`)
  646. str = reg.ReplaceAllString(str, "零")
  647. reg, err = regexp.Compile(`零{2,}`)
  648. str = reg.ReplaceAllString(str, "零")
  649. reg, err = regexp.Compile(`零亿`)
  650. str = reg.ReplaceAllString(str, "亿")
  651. reg, err = regexp.Compile(`零万`)
  652. str = reg.ReplaceAllString(str, "万")
  653. reg, err = regexp.Compile(`零*元`)
  654. str = reg.ReplaceAllString(str, "元")
  655. reg, err = regexp.Compile(`亿零{0, 3}万`)
  656. str = reg.ReplaceAllString(str, "^元")
  657. reg, err = regexp.Compile(`零元`)
  658. str = reg.ReplaceAllString(str, "零")
  659. return
  660. }
  661. // GetNowWeekMonday 获取本周周一的时间
  662. func GetNowWeekMonday() time.Time {
  663. offset := int(time.Monday - time.Now().Weekday())
  664. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  665. offset = -6
  666. }
  667. mondayTime := time.Now().AddDate(0, 0, offset)
  668. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  669. return mondayTime
  670. }
  671. // GetNowWeekLastDay 获取本周最后一天的时间
  672. func GetNowWeekLastDay() time.Time {
  673. offset := int(time.Monday - time.Now().Weekday())
  674. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  675. offset = -6
  676. }
  677. firstDayTime := time.Now().AddDate(0, 0, offset)
  678. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  679. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  680. return lastDayTime
  681. }
  682. // GetNowMonthFirstDay 获取本月第一天的时间
  683. func GetNowMonthFirstDay() time.Time {
  684. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  685. return nowMonthFirstDay
  686. }
  687. // GetNowMonthLastDay 获取本月最后一天的时间
  688. func GetNowMonthLastDay() time.Time {
  689. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  690. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  691. return nowMonthLastDay
  692. }
  693. // GetNowQuarterFirstDay 获取本季度第一天的时间
  694. func GetNowQuarterFirstDay() time.Time {
  695. month := int(time.Now().Month())
  696. var nowQuarterFirstDay time.Time
  697. if month >= 1 && month <= 3 {
  698. //1月1号
  699. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  700. } else if month >= 4 && month <= 6 {
  701. //4月1号
  702. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  703. } else if month >= 7 && month <= 9 {
  704. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  705. } else {
  706. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  707. }
  708. return nowQuarterFirstDay
  709. }
  710. // GetNowQuarterLastDay 获取本季度最后一天的时间
  711. func GetNowQuarterLastDay() time.Time {
  712. month := int(time.Now().Month())
  713. var nowQuarterLastDay time.Time
  714. if month >= 1 && month <= 3 {
  715. //03-31 23:59:59
  716. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  717. } else if month >= 4 && month <= 6 {
  718. //06-30 23:59:59
  719. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  720. } else if month >= 7 && month <= 9 {
  721. //09-30 23:59:59
  722. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  723. } else {
  724. //12-31 23:59:59
  725. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  726. }
  727. return nowQuarterLastDay
  728. }
  729. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  730. func GetNowHalfYearFirstDay() time.Time {
  731. month := int(time.Now().Month())
  732. var nowHalfYearLastDay time.Time
  733. if month >= 1 && month <= 6 {
  734. //03-31 23:59:59
  735. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  736. } else {
  737. //12-31 23:59:59
  738. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  739. }
  740. return nowHalfYearLastDay
  741. }
  742. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  743. func GetNowHalfYearLastDay() time.Time {
  744. month := int(time.Now().Month())
  745. var nowHalfYearLastDay time.Time
  746. if month >= 1 && month <= 6 {
  747. //03-31 23:59:59
  748. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  749. } else {
  750. //12-31 23:59:59
  751. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  752. }
  753. return nowHalfYearLastDay
  754. }
  755. // GetNowYearFirstDay 获取当前年的最后一天的时间
  756. func GetNowYearFirstDay() time.Time {
  757. //12-31 23:59:59
  758. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  759. return nowYearFirstDay
  760. }
  761. // GetNowYearLastDay 获取当前年的最后一天的时间
  762. func GetNowYearLastDay() time.Time {
  763. //12-31 23:59:59
  764. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  765. return nowYearLastDay
  766. }
  767. // CalculationDate 计算两个日期之间相差n年m月y天
  768. // FormatPrice 格式化展示金额数字(财务金额展示,小数点前,每三位用,隔开) 1,234,567,898.55
  769. func FormatPrice(price float64) (str string) {
  770. str = decimal.NewFromFloat(price).String()
  771. length := len(str)
  772. if length < 4 {
  773. return str
  774. }
  775. arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
  776. length1 := len(arr[0])
  777. if length1 < 4 {
  778. return str
  779. }
  780. count := (length1 - 1) / 3
  781. for i := 0; i < count; i++ {
  782. arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
  783. }
  784. return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
  785. }
  786. // getMonthDay 获取某年某月有多少天
  787. func getMonthDay(year, month int) (days int) {
  788. if month != 2 {
  789. if month == 4 || month == 6 || month == 9 || month == 11 {
  790. days = 30
  791. } else {
  792. days = 31
  793. }
  794. } else {
  795. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  796. days = 29
  797. } else {
  798. days = 28
  799. }
  800. }
  801. return
  802. }
  803. func SaveToFile(content, path string) error {
  804. f, err := os.Create(path)
  805. defer f.Close()
  806. if err != nil {
  807. return err
  808. }
  809. f.Write([]byte(content))
  810. return nil
  811. }
  812. // HideString 给字段加***(从字符串中间替换,少于需要替换的长度,那么就补全*的长度)
  813. // src 待*字符串
  814. // hideLen 需要加*的长度
  815. func HideString(src string, hideLen int) string {
  816. if src == "" {
  817. return src
  818. }
  819. str := []rune(src)
  820. if hideLen == 0 {
  821. hideLen = 4
  822. }
  823. hideStr := ""
  824. for i := 0; i < hideLen; i++ {
  825. hideStr += "*"
  826. }
  827. strLen := len(str)
  828. // 字符长度是1
  829. if strLen == 1 {
  830. return string(str[:1]) + hideStr
  831. }
  832. //字符长度大于1,但是小于等于需要隐藏的字符长度,那么就隐藏中间,保留前后各一位字符
  833. if strLen <= hideLen+2 {
  834. return string(str[:1]) + hideStr + string(str[strLen-1:])
  835. }
  836. subLen := strLen - hideLen //剩余需要展示的字符长度
  837. decimal.NewFromFloat(2)
  838. frontLenDecimal := decimal.NewFromInt(int64(subLen)).Div(decimal.NewFromInt(2)) //前面需要展示的字符的长度
  839. frontLen := frontLenDecimal.Floor().IntPart()
  840. return string(str[:frontLen]) + hideStr + string(str[frontLen+int64(hideLen):])
  841. }
  842. // 用户参会时间转换
  843. func GetAttendanceDetailSeconds(secondNum int) string {
  844. var timeStr string
  845. if secondNum <= 60 {
  846. if secondNum < 10 {
  847. timeStr = "0" + strconv.Itoa(secondNum) + "''"
  848. } else {
  849. timeStr = strconv.Itoa(secondNum) + "''"
  850. }
  851. } else {
  852. var remainderStr string
  853. remainderNum := secondNum % 60
  854. minuteNum := secondNum / 60
  855. if remainderNum < 10 {
  856. remainderStr = "0" + strconv.Itoa(remainderNum) + "''"
  857. } else {
  858. remainderStr = strconv.Itoa(remainderNum) + "''"
  859. }
  860. if minuteNum < 10 {
  861. timeStr = "0" + strconv.Itoa(minuteNum) + "'" + remainderStr
  862. } else {
  863. timeStr = strconv.Itoa(minuteNum) + "'" + remainderStr
  864. }
  865. }
  866. return timeStr
  867. }
  868. // SubStr 截取字符串(中文)
  869. func SubStr(str string, subLen int) string {
  870. strRune := []rune(str)
  871. bodyRuneLen := len(strRune)
  872. if bodyRuneLen > subLen {
  873. bodyRuneLen = subLen
  874. }
  875. str = string(strRune[:bodyRuneLen])
  876. return str
  877. }
  878. func GetLocalIP() (ip string, err error) {
  879. addrs, err := net.InterfaceAddrs()
  880. if err != nil {
  881. return
  882. }
  883. for _, addr := range addrs {
  884. ipAddr, ok := addr.(*net.IPNet)
  885. if !ok {
  886. continue
  887. }
  888. if ipAddr.IP.IsLoopback() {
  889. continue
  890. }
  891. if !ipAddr.IP.IsGlobalUnicast() {
  892. continue
  893. }
  894. return ipAddr.IP.String(), nil
  895. }
  896. return
  897. }
  898. func PrintLog(params ...string) {
  899. _, file, line, ok := runtime.Caller(1)
  900. fmt.Println(file, line, ok, params)
  901. }
  902. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  903. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  904. for _, id := range idStrList {
  905. if id == searchId {
  906. has = true
  907. return
  908. }
  909. }
  910. return
  911. }
  912. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该Int值)
  913. func InArrayByInt(idStrList []int, searchId int) (has bool) {
  914. for _, id := range idStrList {
  915. if id == searchId {
  916. has = true
  917. return
  918. }
  919. }
  920. return
  921. }
  922. // GetOrmInReplace 获取orm的in查询替换?的方法
  923. func GetOrmInReplace(num int) string {
  924. template := make([]string, num)
  925. for i := 0; i < num; i++ {
  926. template[i] = "?"
  927. }
  928. return strings.Join(template, ",")
  929. }
  930. // GetTimeSubDay 计算两个时间的自然日期差
  931. func GetTimeSubDay(t1, t2 time.Time) int {
  932. var day int
  933. swap := false
  934. if t1.Unix() > t2.Unix() {
  935. t1, t2 = t2, t1
  936. swap = true
  937. }
  938. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  939. day = int(t2.Sub(t1).Hours() / 24)
  940. // 计算在t1+两个时间的余数之后天数是否有变化
  941. if t1_.Day() != t1.Day() {
  942. day += 1
  943. }
  944. if swap {
  945. day = -day
  946. }
  947. return day
  948. }
  949. // GetFrequencyEndDay 根据当前时间和频度,获取该频度下最后一天的日期
  950. func GetFrequencyEndDay(currDate time.Time, frequency string) (endDate time.Time) {
  951. switch frequency {
  952. case "周度":
  953. // 如果当前就是最后一天,那么就直接返回本日期就好了
  954. if currDate.Weekday() == 0 {
  955. endDate = currDate
  956. } else {
  957. endDate = currDate.AddDate(0, 0, 7-int(currDate.Weekday()))
  958. }
  959. case "旬度":
  960. nextDay := currDate.AddDate(0, 0, 1)
  961. if nextDay.Day() == 1 || currDate.Day() == 10 || currDate.Day() == 20 {
  962. //如果是每月10、20、最后一天,那么就直接返回本日期就好了
  963. endDate = currDate
  964. } else {
  965. if currDate.Day() < 10 { // 每月10号
  966. endDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, time.Local)
  967. } else if currDate.Day() < 20 { // 每月10号
  968. endDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, time.Local)
  969. } else {
  970. // 下旬,多种可能,最大天数可能存在8天,9天,10天,11天,
  971. tmpNextMonth := currDate.AddDate(0, 0, 13)
  972. endDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  973. }
  974. }
  975. case "月度":
  976. nextDay := currDate.AddDate(0, 0, 1)
  977. if nextDay.Day() == 1 {
  978. //如果是每月的最后一天,那么就直接返回本日期就好了
  979. endDate = currDate
  980. } else {
  981. endDate = time.Date(nextDay.Year(), nextDay.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  982. }
  983. case "季度":
  984. nextDay := currDate.AddDate(0, 0, 1)
  985. if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
  986. //如果是每季的最后一天,那么就直接返回本日期就好了
  987. endDate = currDate
  988. } else {
  989. if currDate.Month() < 4 { // 1季度
  990. endDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, time.Local)
  991. } else if currDate.Month() < 7 { // 2季度
  992. endDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  993. } else if currDate.Month() < 10 { // 3季度
  994. endDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, time.Local)
  995. } else {
  996. // 4季度
  997. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  998. }
  999. }
  1000. case "年度":
  1001. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  1002. default:
  1003. endDate = currDate
  1004. return
  1005. }
  1006. return
  1007. }
  1008. // CheckFrequency 获取两个频度之间是否相对低高频
  1009. // 大于0,代表左侧是高频(例:左侧:日度,右侧:周度)
  1010. // 等于0,代表同频
  1011. // 小于0,代表右侧是高频(例:左侧:周度,右侧:日度)
  1012. func CheckFrequency(leftFrequency, rightFrequency string) int {
  1013. frequencyMap := map[string]int{
  1014. "年度": 0,
  1015. "半年度": 1,
  1016. "季度": 2,
  1017. "月度": 3,
  1018. "旬度": 4,
  1019. "周度": 5,
  1020. "日度": 6,
  1021. }
  1022. return frequencyMap[leftFrequency] - frequencyMap[rightFrequency]
  1023. }
  1024. // 将下划线命名转为驼峰命名
  1025. func SnakeToCamel(s string) string {
  1026. var result string
  1027. upper := true
  1028. for _, c := range s {
  1029. if c == '_' {
  1030. upper = true
  1031. continue
  1032. }
  1033. if upper {
  1034. result += string(unicode.ToUpper(c))
  1035. upper = false
  1036. } else {
  1037. result += string(c)
  1038. }
  1039. }
  1040. return result
  1041. }
  1042. // 将驼峰命名转为下划线命名
  1043. func CamelToSnake(s string) string {
  1044. var result string
  1045. for i, c := range s {
  1046. if unicode.IsUpper(c) {
  1047. if i > 0 {
  1048. result += "_"
  1049. }
  1050. result += string(unicode.ToLower(c))
  1051. } else {
  1052. result += string(c)
  1053. }
  1054. }
  1055. return result
  1056. }
  1057. func TimeTransferString(format string, t time.Time) string {
  1058. str := t.Format(format)
  1059. if t.IsZero() {
  1060. return ""
  1061. }
  1062. return str
  1063. }
  1064. // GetEdbRefreshStartDate
  1065. // @Description: 获取开始刷新时间
  1066. // @author: Roc
  1067. // @datetime 2024-02-05 11:23:55
  1068. // @param startDate string
  1069. // @return string
  1070. func GetEdbRefreshStartDate(startDate string) string {
  1071. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1072. if startDate == `` || strings.Contains(startDate, "0000-") {
  1073. return "1990-01-01"
  1074. }
  1075. return startDate
  1076. }
  1077. // GetEdbRefreshEndDate
  1078. // @Description: 获取结束刷新时间
  1079. // @author: Roc
  1080. // @datetime 2024-02-05 11:23:55
  1081. // @param startDate string
  1082. // @return string
  1083. func GetEdbRefreshEndDate(endDate string) string {
  1084. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1085. if endDate == `` || strings.Contains(endDate, "0000-") {
  1086. return time.Now().Format(FormatDate)
  1087. }
  1088. return endDate
  1089. }
  1090. func DealExcelDate(date string) (newDate time.Time, err error) {
  1091. /*newDate, err = dateparse.ParseAny(date)
  1092. if err != nil {
  1093. return
  1094. }
  1095. newDate = time.Date(newDate.Year(), newDate.Month(), newDate.Day(), 0, 0, 0, 0, time.Local)*/
  1096. if strings.Contains(date, "/") {
  1097. newDate, err = time.ParseInLocation("2006/01/02", date, time.Local)
  1098. return
  1099. } else {
  1100. newDate, err = time.ParseInLocation(FormatDate, date, time.Local)
  1101. return
  1102. }
  1103. return
  1104. }
  1105. // FloatAlmostEqual 判断两个浮点数是否相等
  1106. func FloatAlmostEqual(a, b float64) bool {
  1107. epsilon := 1e-9 // 容差值
  1108. return math.Abs(a-b) <= epsilon
  1109. }
  1110. // VerifyFrequency
  1111. // @Description: 校验频度是否合规
  1112. // @author: Roc
  1113. // @datetime 2024-04-26 13:30:22
  1114. // @param frequency string 待校验的频度
  1115. // @return bool
  1116. func VerifyFrequency(frequency string) bool {
  1117. return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
  1118. }
  1119. // DateConvMysqlConvMongo
  1120. // @Description: 将mysql中的日期比较符转换成mongo中的日期比较符
  1121. // @author: Roc
  1122. // @datetime 2024-05-08 11:03:26
  1123. // @param dateCon string
  1124. func DateConvMysqlConvMongo(dateCon string) string {
  1125. cond := ""
  1126. switch dateCon {
  1127. case "=":
  1128. cond = "$eq"
  1129. case "<":
  1130. cond = "$lt"
  1131. case "<=":
  1132. cond = "$lte"
  1133. case ">":
  1134. cond = "$gt"
  1135. case ">=":
  1136. cond = "$gte"
  1137. }
  1138. return cond
  1139. }
  1140. // GenerateEdbCodeMap 当前已经生成的指标编码map(暂时不做定时数据清理了,因为数据不大,我们至少每个月会重启一次,所以暂时不做定时数据清理)
  1141. var GenerateEdbCodeMap = map[string]bool{}
  1142. // GenerateEdbCode
  1143. // @Description: 生成指标编码
  1144. // @author: Roc
  1145. // @datetime 2024-06-05 09:49:53
  1146. // @param num int
  1147. // @param pre string 前缀
  1148. // @return edbCode string
  1149. // @return err error
  1150. func GenerateEdbCode(num int, pre string) (edbCode string, err error) {
  1151. if num >= 10 {
  1152. err = errors.New("指标编码生成失败,请重新生成")
  1153. return
  1154. }
  1155. // 4位随机数
  1156. randStr := GetRandDigit(4)
  1157. // 年月日时分秒+4位随机数
  1158. edbCode = `C` + pre + time.Now().Format(FormatShortDateTimeUnSpace) + randStr
  1159. if _, ok := GenerateEdbCodeMap[edbCode]; ok {
  1160. num++
  1161. edbCode, err = GenerateEdbCode(num, pre)
  1162. }
  1163. GenerateEdbCodeMap[edbCode] = true
  1164. return
  1165. }
  1166. // InsertStr2StrIdx 可分隔的字符串中插入指定字符串, 例如: CO1 Comdty插入V => CO1 V Comdty
  1167. func InsertStr2StrIdx(str, sep string, idx int, value string) string {
  1168. str = strings.TrimSpace(str)
  1169. // 默认以空格作为分隔符
  1170. if sep == "" {
  1171. sep = " "
  1172. }
  1173. slice := strings.Split(str, sep)
  1174. if len(slice) < 2 {
  1175. return str
  1176. }
  1177. // 如果idx不在切片的有效范围内,直接返回原字符串
  1178. if idx < 0 || idx > len(slice) {
  1179. return str
  1180. }
  1181. slice = append(slice[:idx], append([]string{value}, slice[idx:]...)...)
  1182. return strings.Join(slice, sep)
  1183. }
  1184. // FormatFloatPlaces 格式化浮点数位数
  1185. func FormatFloatPlaces(val float64, places int32) (newVal float64, err error) {
  1186. if places <= 0 {
  1187. places = 4
  1188. }
  1189. strNewVal := decimal.NewFromFloat(val).Round(places).String()
  1190. di, e := decimal.NewFromString(strNewVal)
  1191. if e != nil {
  1192. err = fmt.Errorf("NewFromString err: %v", e)
  1193. return
  1194. }
  1195. newVal, _ = di.Float64()
  1196. return
  1197. }
  1198. // handleSystemAppointDateT
  1199. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  1200. // @author: Roc
  1201. // @datetime2023-10-27 09:31:35
  1202. // @param Frequency string
  1203. // @param Day string
  1204. // @return date string
  1205. // @return err error
  1206. // @return errMsg string
  1207. func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  1208. //currDate := time.Now()
  1209. switch frequency {
  1210. case "本周":
  1211. day := int(currDate.Weekday())
  1212. if day == 0 { // 周日
  1213. day = 7
  1214. }
  1215. num := 0
  1216. switch appointDay {
  1217. case "周一":
  1218. num = 1
  1219. case "周二":
  1220. num = 2
  1221. case "周三":
  1222. num = 3
  1223. case "周四":
  1224. num = 4
  1225. case "周五":
  1226. num = 5
  1227. case "周六":
  1228. num = 6
  1229. case "周日":
  1230. num = 7
  1231. }
  1232. day = num - day
  1233. date = currDate.AddDate(0, 0, day).Format(FormatDate)
  1234. case "本旬":
  1235. day := currDate.Day()
  1236. var tmpDate time.Time
  1237. switch appointDay {
  1238. case "第一天":
  1239. if day <= 10 {
  1240. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1241. } else if day <= 20 {
  1242. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  1243. } else {
  1244. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  1245. }
  1246. case "最后一天":
  1247. if day <= 10 {
  1248. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  1249. } else if day <= 20 {
  1250. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  1251. } else {
  1252. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1253. }
  1254. }
  1255. date = tmpDate.Format(FormatDate)
  1256. case "本月":
  1257. var tmpDate time.Time
  1258. switch appointDay {
  1259. case "第一天":
  1260. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1261. case "最后一天":
  1262. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1263. }
  1264. date = tmpDate.Format(FormatDate)
  1265. case "本季":
  1266. month := currDate.Month()
  1267. var tmpDate time.Time
  1268. switch appointDay {
  1269. case "第一天":
  1270. if month <= 3 {
  1271. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1272. } else if month <= 6 {
  1273. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  1274. } else if month <= 9 {
  1275. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1276. } else {
  1277. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  1278. }
  1279. case "最后一天":
  1280. if month <= 3 {
  1281. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  1282. } else if month <= 6 {
  1283. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1284. } else if month <= 9 {
  1285. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  1286. } else {
  1287. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1288. }
  1289. }
  1290. date = tmpDate.Format(FormatDate)
  1291. case "本半年":
  1292. month := currDate.Month()
  1293. var tmpDate time.Time
  1294. switch appointDay {
  1295. case "第一天":
  1296. if month <= 6 {
  1297. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1298. } else {
  1299. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1300. }
  1301. case "最后一天":
  1302. if month <= 6 {
  1303. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1304. } else {
  1305. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1306. }
  1307. }
  1308. date = tmpDate.Format(FormatDate)
  1309. case "本年":
  1310. var tmpDate time.Time
  1311. switch appointDay {
  1312. case "第一天":
  1313. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1314. case "最后一天":
  1315. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1316. }
  1317. date = tmpDate.Format(FormatDate)
  1318. default:
  1319. errMsg = "错误的日期频度:" + frequency
  1320. err = errors.New(errMsg)
  1321. return
  1322. }
  1323. return
  1324. }
  1325. func CompareFloatByOpStrings(op string, a, b float64) bool {
  1326. switch op {
  1327. case "=":
  1328. return a == b
  1329. case ">":
  1330. return a > b
  1331. case ">=":
  1332. return a >= b
  1333. case "<=":
  1334. return a <= b
  1335. case "<":
  1336. return a < b
  1337. }
  1338. return false
  1339. }
  1340. // IsDivideZero
  1341. // @Description: 判断是否分母为0的bug
  1342. // @author: Roc
  1343. // @datetime 2024-08-23 11:21:25
  1344. // @param err error
  1345. // @return bool
  1346. func IsDivideZero(err error) bool {
  1347. if err == nil {
  1348. return false
  1349. }
  1350. //if strings.Contains(err.Error(), "divide by zero") {
  1351. // return true
  1352. //}
  1353. if strings.Contains(err.Error(), "division by zero") {
  1354. return true
  1355. }
  1356. return false
  1357. }