db_mysql.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "gitee.com/go-package/carbon"
  6. _ "github.com/go-sql-driver/mysql"
  7. )
  8. var mysqlDb *sql.DB
  9. func regMysql(brand string) {
  10. //const mySqlUrl = "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&loc=Local"
  11. url := ""
  12. switch brand {
  13. case "ugg":
  14. url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/uggwb?charset=utf8mb4&loc=Local", brand)
  15. case "hdugg":
  16. url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/hduggwb?charset=utf8mb4&loc=Local", brand)
  17. case "ecco":
  18. url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/weimob?charset=utf8mb4&loc=Local", brand)
  19. }
  20. if len(url) == 0 {
  21. panic("mysql brand error")
  22. }
  23. db, err := sql.Open("mysql", url)
  24. if err != nil {
  25. panic(err)
  26. }
  27. err = db.Ping()
  28. if err != nil {
  29. panic(err)
  30. }
  31. mysqlDb = db
  32. }
  33. type Vip struct {
  34. Wid int64
  35. Phone string
  36. Name string
  37. }
  38. func GetAllWid(ago int) ([]Vip, error) {
  39. var rows *sql.Rows
  40. var err error
  41. if ago > 0 {
  42. now := carbon.Now().SubDays(ago).StartOfDay().ToDateTimeString()
  43. sqlStr := `select distinct wid,phone,name from user where created_at >= ? or updated_at >= ? order by wid `
  44. rows, err = mysqlDb.Query(sqlStr, now, now)
  45. if err != nil {
  46. return nil, err
  47. }
  48. defer rows.Close()
  49. } else if ago == 0 {
  50. now := carbon.Now().StartOfDay().ToDateTimeString()
  51. sqlStr := `select distinct wid,phone,name from user where created_at >= ? or updated_at >= ? order by wid `
  52. rows, err = mysqlDb.Query(sqlStr, now, now)
  53. if err != nil {
  54. return nil, err
  55. }
  56. defer rows.Close()
  57. } else {
  58. sqlStr := `select distinct wid,phone,name from user order by wid `
  59. rows, err = mysqlDb.Query(sqlStr)
  60. if err != nil {
  61. return nil, err
  62. }
  63. defer rows.Close()
  64. }
  65. var vips []Vip
  66. for rows.Next() {
  67. var vip Vip
  68. err := rows.Scan(&vip.Wid, &vip.Phone, &vip.Name)
  69. if err != nil {
  70. return nil, err
  71. }
  72. vips = append(vips, vip)
  73. }
  74. return vips, nil
  75. }