tools.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  6. "net/http"
  7. "sort"
  8. )
  9. // getUniqueFields 获取切片中标题和项目名称的唯一值
  10. func getUniqueFields(slice []map[string]interface{}) (map[string]bool, map[string]bool) {
  11. titles := make(map[string]bool)
  12. projects := make(map[string]bool)
  13. for _, item := range slice {
  14. title, titleExists := item["title"].(string)
  15. project, projectExists := item["projectname"].(string)
  16. if titleExists {
  17. titles[title] = true
  18. }
  19. if projectExists {
  20. projects[project] = true
  21. }
  22. }
  23. return titles, projects
  24. }
  25. // countMatches 统计切片A中的元素在切片B中存在的数量和总数量
  26. func countMatches(sliceA []map[string]interface{}, titlesInB, projectsInB map[string]bool) int {
  27. count := 0
  28. for _, itemA := range sliceA {
  29. title, titleExists := itemA["title"].(string)
  30. project, projectExists := itemA["projectname"].(string)
  31. if titleExists && titlesInB[title] {
  32. count++
  33. } else if projectExists && projectsInB[project] {
  34. count++
  35. }
  36. }
  37. return count
  38. }
  39. // SendMail 发送邮件
  40. func SendMail(title, content string) {
  41. url := fmt.Sprintf("%s?to=%s&title=%s&body=%s", GF.Email.Api, GF.Email.To, title, content)
  42. fmt.Println("url=>", url)
  43. res, err := http.Get(url)
  44. if err != nil {
  45. log.Info("SendMail", zap.Any("err", err))
  46. } else {
  47. log.Info("SendMail", zap.Any("res", res))
  48. }
  49. }
  50. // IsInStringArray 判断数组中是否存在字符串
  51. func IsInStringArray(str string, arr []string) bool {
  52. // 先对字符串数组进行排序
  53. sort.Strings(arr)
  54. // 使用二分查找算法查找字符串
  55. pos := sort.SearchStrings(arr, str)
  56. // 如果找到了则返回 true,否则返回 false
  57. return pos < len(arr) && arr[pos] == str
  58. }