12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package main
- import (
- "database/sql"
- "fmt"
- "gitee.com/go-package/carbon"
- _ "github.com/go-sql-driver/mysql"
- )
- var mysqlDb *sql.DB
- func regMysql(brand string) {
- //const mySqlUrl = "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&loc=Local"
- url := ""
- switch brand {
- case "ugg":
- url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/uggwb?charset=utf8mb4&loc=Local", brand)
- case "hdugg":
- url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/hduggwb?charset=utf8mb4&loc=Local", brand)
- case "ecco":
- url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/weimob?charset=utf8mb4&loc=Local", brand)
- }
- if len(url) == 0 {
- panic("mysql brand error")
- }
- db, err := sql.Open("mysql", url)
- if err != nil {
- panic(err)
- }
- err = db.Ping()
- if err != nil {
- panic(err)
- }
- mysqlDb = db
- }
- type Vip struct {
- Wid int64
- Phone string
- Name string
- }
- func GetAllWid(ago int) ([]Vip, error) {
- var rows *sql.Rows
- var err error
- if ago > 0 {
- now := carbon.Now().SubDays(ago).StartOfDay().ToDateTimeString()
- sqlStr := `select distinct wid,phone,name from user where created_at >= ? or updated_at >= ? order by wid `
- rows, err = mysqlDb.Query(sqlStr, now, now)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- } else if ago == 0 {
- now := carbon.Now().StartOfDay().ToDateTimeString()
- sqlStr := `select distinct wid,phone,name from user where created_at >= ? or updated_at >= ? order by wid `
- rows, err = mysqlDb.Query(sqlStr, now, now)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- } else {
- sqlStr := `select distinct wid,phone,name from user order by wid `
- rows, err = mysqlDb.Query(sqlStr)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- }
- var vips []Vip
- for rows.Next() {
- var vip Vip
- err := rows.Scan(&vip.Wid, &vip.Phone, &vip.Name)
- if err != nil {
- return nil, err
- }
- vips = append(vips, vip)
- }
- return vips, nil
- }
|