Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"duplicate.go",
"origin.go",
"secret.go",
"user.go",
"wechat.go",
],
importpath = "go-common/app/job/main/passport-user-compare/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/passport-user-compare/conf:go_default_library",
"//app/job/main/passport-user-compare/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"duplicate_test.go",
"origin_test.go",
"secret_test.go",
"user_test.go",
"wechat_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/passport-user-compare/conf:go_default_library",
"//app/job/main/passport-user-compare/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,63 @@
package dao
import (
"context"
"go-common/app/job/main/passport-user-compare/conf"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
const (
safeQuestionSegment = 100
)
// Dao dao
type Dao struct {
c *conf.Config
httpClient *bm.Client
originDB *sql.DB
userDB *sql.DB
secretDB *sql.DB
}
// New new dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
originDB: sql.NewMySQL(c.DB.Origin),
userDB: sql.NewMySQL(c.DB.User),
secretDB: sql.NewMySQL(c.DB.Secret),
httpClient: bm.NewClient(c.HTTPClient),
}
return
}
// Ping check dao ok.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.originDB.Ping(c); err != nil {
log.Info("dao.originDB.Ping() error(%v)", err)
}
if err = d.userDB.Ping(c); err != nil {
log.Info("dao.newDB.Ping() error(%v)", err)
}
if err = d.secretDB.Ping(c); err != nil {
log.Info("dao.secretDB.Ping() error(%v)", err)
}
return
}
// Close close connections.
func (d *Dao) Close() (err error) {
if d.originDB != nil {
d.originDB.Close()
}
if d.userDB != nil {
d.userDB.Close()
}
if d.secretDB != nil {
d.secretDB.Close()
}
return
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"flag"
"go-common/app/job/main/passport-user-compare/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.passport.passport-user-compare-job")
flag.Set("conf_token", "5b735bbc35fa33c84fd2c89a54b6470c")
flag.Set("tree_id", "61394")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/passport-user-compare-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,77 @@
package dao
import (
"context"
"database/sql"
"go-common/app/job/main/passport-user-compare/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_getUserTelDuplicateSQL = "SELECT id,mid,tel,cid,tel_bind_time,status,ts FROM user_tel_duplicate WHERE status = 0 order by ts"
_getUserEmailDuplicateSQL = "SELECT id,mid,email,verified,email_bind_time,status,ts FROM user_email_duplicate WHERE status = 0 order by ts"
_updateTelDuplicateStatusSQL = "UPDATE user_tel_duplicate SET status = 1 WHERE id = ?"
_updateEmailDuplicateStatusSQL = "UPDATE user_email_duplicate SET status = 1 WHERE id = ?"
)
// UserTelDuplicate get user tel duplicate.
func (d *Dao) UserTelDuplicate(c context.Context) (res []*model.UserTelDuplicate, err error) {
var rows *xsql.Rows
if rows, err = d.userDB.Query(c, _getUserTelDuplicateSQL); err != nil {
log.Error("fail to get UserTelDuplicate, dao.userDB.Query(%s) error(%v)", _getUserTelDuplicateSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.UserTelDuplicate)
if err = rows.Scan(&r.ID, &r.Mid, &r.Tel, &r.Cid, &r.TelBindTime, &r.Status, &r.Timestamp); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}
// UserEmailDuplicate get user email duplicate.
func (d *Dao) UserEmailDuplicate(c context.Context) (res []*model.UserEmailDuplicate, err error) {
var rows *xsql.Rows
if rows, err = d.userDB.Query(c, _getUserEmailDuplicateSQL); err != nil {
log.Error("fail to get UserEmailDuplicate, dao.userDB.Query(%s) error(%v)", _getUserEmailDuplicateSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.UserEmailDuplicate)
if err = rows.Scan(&r.ID, &r.Mid, &r.Email, &r.Verified, &r.EmailBindTime, &r.Status, &r.Timestamp); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}
// UpdateUserTelDuplicateStatus update user tel duplicate status.
func (d *Dao) UpdateUserTelDuplicateStatus(c context.Context, id int64) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, _updateTelDuplicateStatusSQL, id); err != nil {
log.Error("fail to update tel duplicate status, id(%d) dao.userDB.Exec() error(%+v)", id, err)
return
}
return res.RowsAffected()
}
// UpdateUserEmailDuplicateStatus update user email duplicate status.
func (d *Dao) UpdateUserEmailDuplicateStatus(c context.Context, id int64) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, _updateEmailDuplicateStatusSQL, id); err != nil {
log.Error("fail to update email duplicate status, id(%d) dao.userDB.Exec() error(%+v)", id, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,70 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUserTelDuplicate(t *testing.T) {
convey.Convey("UserTelDuplicate", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.UserTelDuplicate(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoUserEmailDuplicate(t *testing.T) {
convey.Convey("UserEmailDuplicate", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.UserEmailDuplicate(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoUpdateUserTelDuplicateStatus(t *testing.T) {
convey.Convey("UpdateUserTelDuplicateStatus", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserTelDuplicateStatus(c, id)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateUserEmailDuplicateStatus(t *testing.T) {
convey.Convey("UpdateUserEmailDuplicateStatus", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserEmailDuplicateStatus(c, id)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,291 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/app/job/main/passport-user-compare/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
var (
selectAccountSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE mid > ? limit ? "
selectAccountByMidSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE mid = ?"
selectAccountByTelSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE tel = ?"
selectAccountByMailSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE email = ?"
selectAccountByTimeSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE modify_time >= ? AND modify_time < ?"
selectAccountInfoSQL = "SELECT id, mid, spacesta, safe_question, safe_answer, join_time, join_ip, active_time, modify_time FROM aso_account_info%d WHERE id > ? limit ?"
selectAccountInfoByMidSQL = "SELECT id, mid, spacesta, safe_question, safe_answer, join_time, join_ip, active_time, modify_time FROM aso_account_info%d WHERE mid = ? "
selectAccountInfoByTimeSQL = "SELECT id, mid, spacesta, safe_question, safe_answer, join_time, join_ip, active_time, modify_time FROM aso_account_info%d WHERE modify_time >= ? AND modify_time < ? "
selectAccountSnsSQL = "SELECT mid, sina_uid, sina_access_token, sina_access_expires, qq_openid, qq_access_token, qq_access_expires FROM aso_account_sns WHERE mid > ? limit ?"
selectAccountSnsByMidSQL = "SELECT mid, sina_uid, sina_access_token, sina_access_expires, qq_openid, qq_access_token, qq_access_expires FROM aso_account_sns WHERE mid = ? "
selectOriginTelBindLogSQL = "SELECT timestamp FROM aso_telephone_bind_log WHERE mid = ? order by timestamp limit 1"
selectAccountRegByMidSQL = "SELECT id, mid, origintype, regtype, appid, ctime, mtime FROM aso_account_reg_origin_%d WHERE mid = ? "
selectAccountRegByTimeSQL = "SELECT id, mid, origintype, regtype, appid, ctime, mtime FROM aso_account_reg_origin_%d WHERE mtime >= ? AND mtime < ? "
)
// BatchQueryAccount batch query account
func (d *Dao) BatchQueryAccount(c context.Context, start, limit int64) (res []*model.OriginAccount, err error) {
var rows *xsql.Rows
if rows, err = d.originDB.Query(c, selectAccountSQL, start, limit); err != nil {
log.Error("fail to get BatchQueryAccount, dao.originDB.Query(%s) error(%v)", selectAccountSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var telPtr, emailPtr *string
r := new(model.OriginAccount)
if err = rows.Scan(&r.Mid, &r.UserID, &r.Uname, &r.Pwd, &r.Salt, &emailPtr, &telPtr, &r.MobileVerified, &r.Isleak, &r.CountryID, &r.MTime); err != nil {
log.Error("BatchQueryAccount row.Scan() error(%v)", err)
res = nil
return
}
if telPtr != nil {
r.Tel = *telPtr
}
if emailPtr != nil {
r.Email = *emailPtr
}
res = append(res, r)
}
return
}
// QueryAccountByMid query account by mid
func (d *Dao) QueryAccountByMid(c context.Context, mid int64) (res *model.OriginAccount, err error) {
row := d.originDB.QueryRow(c, selectAccountByMidSQL, mid)
res = new(model.OriginAccount)
var telPtr, emailPtr *string
if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("QueryAccountByMid row.Scan() error(%v)", err)
}
return
}
if telPtr != nil {
res.Tel = *telPtr
}
if emailPtr != nil {
res.Email = *emailPtr
}
return
}
// QueryAccountByTel query account by mid
func (d *Dao) QueryAccountByTel(c context.Context, tel string) (res *model.OriginAccount, err error) {
row := d.originDB.QueryRow(c, selectAccountByTelSQL, tel)
res = new(model.OriginAccount)
var telPtr, emailPtr *string
if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("QueryAccountByTel row.Scan() error(%v)", err)
}
return
}
if telPtr != nil {
res.Tel = *telPtr
}
if emailPtr != nil {
res.Email = *emailPtr
}
return
}
// QueryAccountByMail query account by mid
func (d *Dao) QueryAccountByMail(c context.Context, mail string) (res *model.OriginAccount, err error) {
row := d.originDB.QueryRow(c, selectAccountByMailSQL, mail)
res = new(model.OriginAccount)
var telPtr, emailPtr *string
if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("QueryAccountByMail row.Scan() error(%v)", err)
}
return
}
if telPtr != nil {
res.Tel = *telPtr
}
if emailPtr != nil {
res.Email = *emailPtr
}
return
}
// BatchQueryAccountByTime batch query by time
func (d *Dao) BatchQueryAccountByTime(c context.Context, start, end time.Time) (res []*model.OriginAccount, err error) {
var rows *xsql.Rows
if rows, err = d.originDB.Query(c, selectAccountByTimeSQL, start, end); err != nil {
log.Error("fail to get BatchQueryAccountByTime, dao.originDB.Query(%s) error(%v)", selectAccountSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var telPtr, emailPtr *string
r := new(model.OriginAccount)
if err = rows.Scan(&r.Mid, &r.UserID, &r.Uname, &r.Pwd, &r.Salt, &emailPtr, &telPtr, &r.MobileVerified, &r.Isleak, &r.CountryID, &r.MTime); err != nil {
log.Error("BatchQueryAccountByTime row.Scan() error(%v)", err)
res = nil
return
}
if telPtr != nil {
r.Tel = *telPtr
}
if emailPtr != nil {
r.Email = *emailPtr
}
res = append(res, r)
}
return
}
// BatchQueryAccountInfo batch query account info
func (d *Dao) BatchQueryAccountInfo(c context.Context, start, limit int64, suffix int) (res []*model.OriginAccountInfo, err error) {
var rows *xsql.Rows
if rows, err = d.originDB.Query(c, fmt.Sprintf(selectAccountInfoSQL, suffix), start, limit); err != nil {
log.Error("fail to get BatchQueryAccountInfo, dao.originDB.Query(%s) error(%v)", selectAccountInfoSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.OriginAccountInfo)
if err = rows.Scan(&r.ID, &r.Mid, &r.Spacesta, &r.SafeQuestion, &r.SafeAnswer, &r.JoinTime, &r.JoinIP, &r.ActiveTime, &r.MTime); err != nil {
log.Error("BatchQueryAccountInfo row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}
// QueryAccountInfoByMid query account info by mid
func (d *Dao) QueryAccountInfoByMid(c context.Context, mid int64) (res *model.OriginAccountInfo, err error) {
row := d.originDB.QueryRow(c, fmt.Sprintf(selectAccountInfoByMidSQL, mid%30), mid)
res = new(model.OriginAccountInfo)
if err = row.Scan(&res.ID, &res.Mid, &res.Spacesta, &res.SafeQuestion, &res.SafeAnswer, &res.JoinTime, &res.JoinIP, &res.ActiveTime, &res.MTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("QueryAccountInfoByMid row.Scan() error(%v)", err)
}
return
}
return
}
// BatchQueryAccountInfoByTime batch query account info
func (d *Dao) BatchQueryAccountInfoByTime(c context.Context, start, end time.Time, suffix int) (res []*model.OriginAccountInfo, err error) {
var rows *xsql.Rows
if rows, err = d.originDB.Query(c, fmt.Sprintf(selectAccountInfoByTimeSQL, suffix), start, end); err != nil {
log.Error("fail to get BatchQueryAccountInfoByTime, dao.originDB.Query(%s) error(%v)", selectAccountInfoSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.OriginAccountInfo)
if err = rows.Scan(&r.ID, &r.Mid, &r.Spacesta, &r.SafeQuestion, &r.SafeAnswer, &r.JoinTime, &r.JoinIP, &r.ActiveTime, &r.MTime); err != nil {
log.Error("BatchQueryAccountInfoByTime row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}
// BatchQueryAccountSns batch query account sns
func (d *Dao) BatchQueryAccountSns(c context.Context, start, limit int64) (res []*model.OriginAccountSns, err error) {
var rows *xsql.Rows
if rows, err = d.originDB.Query(c, selectAccountSnsSQL, start, limit); err != nil {
log.Error("fail to get BatchQueryAccountSns, dao.originDB.Query(%s) error(%v)", selectAccountSnsSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.OriginAccountSns)
if err = rows.Scan(&r.Mid, &r.SinaUID, &r.SinaAccessToken, &r.SinaAccessExpires, &r.QQOpenid, &r.QQAccessToken, &r.QQAccessExpires); err != nil {
log.Error("BatchQueryAccountSns row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}
// QueryAccountSnsByMid query account sns by mid
func (d *Dao) QueryAccountSnsByMid(c context.Context, mid int64) (res *model.OriginAccountSns, err error) {
row := d.originDB.QueryRow(c, selectAccountSnsByMidSQL, mid)
res = new(model.OriginAccountSns)
if err = row.Scan(&res.Mid, &res.SinaUID, &res.SinaAccessToken, &res.SinaAccessExpires, &res.QQOpenid, &res.QQAccessToken, &res.QQAccessExpires); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("QueryAccountSnsByMid row.Scan() error(%v)", err)
}
return
}
return
}
// QueryTelBindLog get aso tel bind log.
func (d *Dao) QueryTelBindLog(c context.Context, mid int64) (res int64, err error) {
if err = d.originDB.QueryRow(c, selectOriginTelBindLogSQL, mid).Scan(&res); err != nil {
if err == xsql.ErrNoRows {
err = nil
} else {
log.Error("fail to get AsoTelBindLog, dao.originDB.QueryRow(%s) error(%v)", selectOriginTelBindLogSQL, err)
}
return
}
return
}
// QueryAccountRegByMid query account reg by mid
func (d *Dao) QueryAccountRegByMid(c context.Context, mid int64) (res *model.OriginAccountReg, err error) {
row := d.originDB.QueryRow(c, fmt.Sprintf(selectAccountRegByMidSQL, mid%20), mid)
res = new(model.OriginAccountReg)
if err = row.Scan(&res.ID, &res.Mid, &res.OriginType, &res.RegType, &res.AppID, &res.CTime, &res.MTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("QueryAccountRegByMid row.Scan() error(%v)", err)
}
return
}
return
}
// BatchQueryAccountRegByTime batch query account info
func (d *Dao) BatchQueryAccountRegByTime(c context.Context, start, end time.Time, suffix int) (res []*model.OriginAccountReg, err error) {
var rows *xsql.Rows
if rows, err = d.originDB.Query(c, fmt.Sprintf(selectAccountRegByTimeSQL, suffix), start, end); err != nil {
log.Error("fail to get BatchQueryAccountRegByTime, dao.originDB.Query(%s) error(%v)", selectAccountInfoSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.OriginAccountReg)
if err = rows.Scan(&r.ID, &r.Mid, &r.OriginType, &r.RegType, &r.AppID, &r.CTime, &r.MTime); err != nil {
log.Error("BatchQueryAccountInfoByTime row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}

View File

@@ -0,0 +1,226 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoBatchQueryAccount(t *testing.T) {
convey.Convey("BatchQueryAccount", t, func(ctx convey.C) {
var (
c = context.Background()
start = int64(0)
limit = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchQueryAccount(c, start, limit)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoQueryAccountByMid(t *testing.T) {
convey.Convey("QueryAccountByMid", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryAccountByMid(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoQueryAccountByTel(t *testing.T) {
convey.Convey("QueryAccountByTel", t, func(ctx convey.C) {
var (
c = context.Background()
tel = "13122111111"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryAccountByTel(c, tel)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoQueryAccountByMail(t *testing.T) {
convey.Convey("QueryAccountByMail", t, func(ctx convey.C) {
var (
c = context.Background()
mail = "598717394@qq.com"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryAccountByMail(c, mail)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoBatchQueryAccountByTime(t *testing.T) {
convey.Convey("BatchQueryAccountByTime", t, func(ctx convey.C) {
var (
c = context.Background()
start = time.Now()
end = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchQueryAccountByTime(c, start, end)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoBatchQueryAccountInfo(t *testing.T) {
convey.Convey("BatchQueryAccountInfo", t, func(ctx convey.C) {
var (
c = context.Background()
start = int64(0)
limit = int64(0)
suffix = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchQueryAccountInfo(c, start, limit, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoQueryAccountInfoByMid(t *testing.T) {
convey.Convey("QueryAccountInfoByMid", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryAccountInfoByMid(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoBatchQueryAccountInfoByTime(t *testing.T) {
convey.Convey("BatchQueryAccountInfoByTime", t, func(ctx convey.C) {
var (
c = context.Background()
start = time.Now()
end = time.Now()
suffix = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchQueryAccountInfoByTime(c, start, end, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoBatchQueryAccountSns(t *testing.T) {
convey.Convey("BatchQueryAccountSns", t, func(ctx convey.C) {
var (
c = context.Background()
start = int64(0)
limit = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchQueryAccountSns(c, start, limit)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoQueryAccountSnsByMid(t *testing.T) {
convey.Convey("QueryAccountSnsByMid", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryAccountSnsByMid(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryTelBindLog(t *testing.T) {
convey.Convey("QueryTelBindLog", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryTelBindLog(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryAccountRegByMid(t *testing.T) {
convey.Convey("QueryAccountRegByMid", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(22222222)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryAccountRegByMid(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoBatchQueryAccountRegByTime(t *testing.T) {
convey.Convey("BatchQueryAccountRegByTime", t, func(ctx convey.C) {
var (
c = context.Background()
start = time.Now()
end = time.Now()
suffix = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchQueryAccountRegByTime(c, start, end, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,33 @@
package dao
import (
"context"
"go-common/app/job/main/passport-user-compare/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
var (
secretSQL = "SELECT us.key_type, us.key FROM user_secret us"
)
// LoadSecret load secret
func (d *Dao) LoadSecret(c context.Context) (res []*model.Secret, err error) {
var rows *xsql.Rows
if rows, err = d.secretDB.Query(c, secretSQL); err != nil {
log.Error("fail to get secretSQL, dao.secretDB.Query(%s) error(%v)", secretSQL, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.Secret)
if err = rows.Scan(&r.KeyType, &r.Key); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}

View File

@@ -0,0 +1,23 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoLoadSecret(t *testing.T) {
convey.Convey("LoadSecret", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.LoadSecret(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,352 @@
package dao
import (
"context"
"database/sql"
"fmt"
"go-common/app/job/main/passport-user-compare/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
var (
insertUserBaseSQL = "INSERT INTO user_base (mid,userid,pwd,salt,status,deleted,mtime) VALUES(?,?,?,?,?,?,?)"
selectUserBaseSQL = "SELECT mid, userid, pwd, salt, status FROM user_base WHERE mid = ?"
updateUserBaseSQL = "UPDATE user_base SET userid = ?, pwd = ? ,salt = ? ,status = ? WHERE mid = ?"
insertUserTelSQL = "INSERT INTO user_tel (mid,tel,cid,tel_bind_time,mtime) VALUES(?,?,?,?,?)"
selectUserTelSQL = "SELECT mid, tel, cid, tel_bind_time FROM user_tel WHERE mid = ?"
updateUserTelSQL = "UPDATE user_tel SET tel = ? ,cid = ? WHERE mid = ?"
insertUserMailSQL = "INSERT INTO user_email (mid,email,verified,email_bind_time,mtime) VALUES(?,?,?,?,?)"
selectUserMailSQL = "SELECT mid,email,email_bind_time FROM user_email WHERE mid = ?"
updateUserMailSQL = "UPDATE user_email SET email = ? WHERE mid = ?"
updateUserMailVerifiedSQL = "UPDATE user_email SET verified = ? WHERE mid = ?"
insertUserSafeQuestionSQL = "INSERT INTO user_safe_question%02d (mid,safe_question,safe_answer,safe_bind_time) VALUES(?,?,?,?)"
selectUserSafeQuestionSQL = "SELECT mid, safe_question, safe_answer FROM user_safe_question%02d WHERE mid = ?"
updateUserSafeQuestionSQL = "UPDATE user_safe_question%02d SET safe_question = ? ,safe_answer = ? WHERE mid = ? "
insertThirdBindSQL = "INSERT INTO user_third_bind (mid,openid,platform,token,expires) VALUES(?,?,?,?,?)"
selectThirdBindSQL = "SELECT mid, openid, platform, token FROM user_third_bind WHERE mid = ? AND platform = ?"
updateThirdBindSQL = "UPDATE user_third_bind SET openid = ? ,token = ? WHERE mid = ? AND platform = ? "
queryCountryCodeSQL = "SELECT id,code FROM country_code"
queryMidByTelSQL = "SELECT mid FROM user_tel WHERE tel = ? and cid = ?"
queryMidByEmailSQL = "SELECT mid FROM user_email WHERE email = ?"
getUnverifiedEmail = "SELECT mid FROM user_email where mid > ? and verified = 0 limit 20000"
_getUserRegOriginByMidSQL = "SELECT mid,join_ip,join_time,origin,reg_type,appid from user_reg_origin%02d where mid = ?"
_insertUpdateUserRegOriginTypeSQL = "INSERT INTO user_reg_origin%02d (mid,join_ip,join_time,origin,reg_type,appid,ctime,mtime) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE join_ip=?,join_time=?,origin=?,reg_type=?,appid=?,ctime=?,mtime=?"
)
// QueryUserBase query user basic info by mid
func (d *Dao) QueryUserBase(c context.Context, mid int64) (res *model.UserBase, err error) {
row := d.userDB.QueryRow(c, selectUserBaseSQL, mid)
res = new(model.UserBase)
if err = row.Scan(&res.Mid, &res.UserID, &res.Pwd, &res.Salt, &res.Status); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
return
}
// UpdateUserBase update user base
func (d *Dao) UpdateUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, updateUserBaseSQL, a.UserID, a.Pwd, a.Salt, a.Status, a.Mid); err != nil {
log.Error("failed to update user base, dao.userDB.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUserBase add user base.
func (d *Dao) InsertUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, insertUserBaseSQL, a.Mid, a.UserID, a.Pwd, a.Salt, a.Status, a.Deleted, a.MTime); err != nil {
log.Error("fail to add user base, userBase(%+v) dao.userDB.Exec() error(%+v)", a, err)
return
}
return res.RowsAffected()
}
// QueryUserTel query user tel info by mid
func (d *Dao) QueryUserTel(c context.Context, mid int64) (res *model.UserTel, err error) {
row := d.userDB.QueryRow(c, selectUserTelSQL, mid)
res = new(model.UserTel)
var cidPtr *string
if err = row.Scan(&res.Mid, &res.Tel, &cidPtr, &res.TelBindTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
if cidPtr != nil {
res.Cid = *cidPtr
}
return
}
// UpdateUserTel update user tel
func (d *Dao) UpdateUserTel(c context.Context, a *model.UserTel) (affected int64, err error) {
var (
res sql.Result
telPtr *[]byte
cidPtr string
)
if len(a.Tel) != 0 {
telPtr = &a.Tel
cidPtr = a.Cid
}
if res, err = d.userDB.Exec(c, updateUserTelSQL, telPtr, cidPtr, a.Mid); err != nil {
log.Error("failed to update user tel, dao.userDB.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUserTel insert user tel
func (d *Dao) InsertUserTel(c context.Context, a *model.UserTel) (affected int64, err error) {
var (
res sql.Result
telPtr *[]byte
cidPtr string
)
if len(a.Tel) != 0 {
telPtr = &a.Tel
cidPtr = a.Cid
}
if res, err = d.userDB.Exec(c, insertUserTelSQL, a.Mid, telPtr, cidPtr, a.TelBindTime, a.MTime); err != nil {
log.Error("fail to add user tel, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
return
}
return res.RowsAffected()
}
// QueryUserMail query user mail info by mid
func (d *Dao) QueryUserMail(c context.Context, mid int64) (res *model.UserEmail, err error) {
row := d.userDB.QueryRow(c, selectUserMailSQL, mid)
res = new(model.UserEmail)
if err = row.Scan(&res.Mid, &res.Email, &res.EmailBindTime); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
return
}
// UpdateUserMail update user tel
func (d *Dao) UpdateUserMail(c context.Context, a *model.UserEmail) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, updateUserMailSQL, a.Email, a.Mid); err != nil {
log.Error("failed to update user mail, dao.userDB.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// UpdateUserMailVerified update user email verified
func (d *Dao) UpdateUserMailVerified(c context.Context, a *model.UserEmail) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, updateUserMailVerifiedSQL, a.Verified, a.Mid); err != nil {
log.Error("failed to update user mail verified, dao.userDB.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUserEmail add user email.
func (d *Dao) InsertUserEmail(c context.Context, a *model.UserEmail) (affected int64, err error) {
var (
res sql.Result
emailPtr *[]byte
)
if len(a.Email) != 0 {
emailPtr = &a.Email
}
if res, err = d.userDB.Exec(c, insertUserMailSQL, a.Mid, emailPtr, a.Verified, a.EmailBindTime, a.MTime); err != nil {
log.Error("fail to add user email, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
return
}
return res.RowsAffected()
}
// QueryUserSafeQuestion query user safe question by mid
func (d *Dao) QueryUserSafeQuestion(c context.Context, mid int64) (res *model.UserSafeQuestion, err error) {
row := d.userDB.QueryRow(c, fmt.Sprintf(selectUserSafeQuestionSQL, mid%safeQuestionSegment), mid)
res = new(model.UserSafeQuestion)
if err = row.Scan(&res.Mid, &res.SafeQuestion, &res.SafeAnswer); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
return
}
// UpdateUserSafeQuestion update user tel
func (d *Dao) UpdateUserSafeQuestion(c context.Context, a *model.UserSafeQuestion) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, fmt.Sprintf(updateUserSafeQuestionSQL, a.Mid%safeQuestionSegment), a.SafeQuestion, a.SafeAnswer, a.Mid); err != nil {
log.Error("failed to update user safe question, dao.userDB.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUserSafeQuestion insert user safe question
func (d *Dao) InsertUserSafeQuestion(c context.Context, a *model.UserSafeQuestion) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, fmt.Sprintf(insertUserSafeQuestionSQL, a.Mid%safeQuestionSegment), a.Mid, a.SafeQuestion, a.SafeAnswer, a.SafeBindTime); err != nil {
log.Error("fail to add user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
return
}
return res.RowsAffected()
}
// QueryUserThirdBind query user third bind by mid and platform
func (d *Dao) QueryUserThirdBind(c context.Context, mid, platform int64) (res *model.UserThirdBind, err error) {
row := d.userDB.QueryRow(c, selectThirdBindSQL, mid, platform)
res = new(model.UserThirdBind)
if err = row.Scan(&res.Mid, &res.OpenID, &res.PlatForm, &res.Token); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
return
}
// UpdateUserThirdBind update user third bind
func (d *Dao) UpdateUserThirdBind(c context.Context, a *model.UserThirdBind) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, updateThirdBindSQL, a.OpenID, a.Token, a.Mid, a.PlatForm); err != nil {
log.Error("failed to update user third bind sql, dao.userDB.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUserThirdBind insert user third bind.
func (d *Dao) InsertUserThirdBind(c context.Context, a *model.UserThirdBind) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, insertThirdBindSQL, a.Mid, a.OpenID, a.PlatForm, a.Token, a.Expires); err != nil {
log.Error("fail to add user third bind, userThirdBind(%+v) dao.userDB.Exec() error(%+v)", a, err)
return
}
return res.RowsAffected()
}
// QueryCountryCode query country code
func (d *Dao) QueryCountryCode(c context.Context) (res map[int64]string, err error) {
var rows *xsql.Rows
if rows, err = d.userDB.Query(c, queryCountryCodeSQL); err != nil {
log.Error("fail to get CountryCodeMap, dao.originDB.Query(%s) error(%v)", queryCountryCodeSQL, err)
return
}
defer rows.Close()
res = make(map[int64]string)
for rows.Next() {
var (
id int64
code string
)
if err = rows.Scan(&id, &code); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
res[id] = code
}
return
}
// GetMidByTel get mid by tel.
func (d *Dao) GetMidByTel(c context.Context, a *model.UserTel) (mid int64, err error) {
if err = d.userDB.QueryRow(c, queryMidByTelSQL, a.Tel, a.Cid).Scan(&mid); err != nil {
log.Error("fail to get mid by tel, dao.userDB.QueryRow(%s) error(%+v)", queryMidByTelSQL, err)
return
}
return
}
// GetMidByEmail get mid by email.
func (d *Dao) GetMidByEmail(c context.Context, a *model.UserEmail) (mid int64, err error) {
if err = d.userDB.QueryRow(c, queryMidByEmailSQL, a.Email).Scan(&mid); err != nil {
log.Error("fail to get mid by email, dao.userDB.QueryRow(%s) error(%+v)", queryMidByEmailSQL, err)
return
}
return
}
// GetUnverifiedEmail get unverified email.
func (d *Dao) GetUnverifiedEmail(c context.Context, start int64) (res []*model.UserEmail, err error) {
var rows *xsql.Rows
if rows, err = d.userDB.Query(c, getUnverifiedEmail, start); err != nil {
log.Error("fail to get UnverifiedEmail, dao.userDB.Query(%s) error(%v)", getUnverifiedEmail, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.UserEmail)
if err = rows.Scan(&r.Mid); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
return
}
// GetUserRegOriginByMid get user reg origin by mid.
func (d *Dao) GetUserRegOriginByMid(c context.Context, mid int64) (res *model.UserRegOrigin, err error) {
res = &model.UserRegOrigin{}
if err = d.userDB.QueryRow(c, fmt.Sprintf(_getUserRegOriginByMidSQL, tableIndex(mid)), mid).Scan(&res.Mid, &res.JoinIP, &res.JoinTime, &res.Origin, &res.RegType, &res.AppID); err != nil {
if err == xsql.ErrNoRows {
err = nil
res = nil
} else {
log.Error("fail to get UserRegOrigin by mid(%d), dao.userDB.QueryRow(%s) error(%+v)", mid, _getUserRegOriginByMidSQL, err)
}
return
}
return
}
// InsertUpdateUserRegOriginType insert update user reg origin type.
func (d *Dao) InsertUpdateUserRegOriginType(c context.Context, a *model.UserRegOrigin) (affected int64, err error) {
var res sql.Result
if res, err = d.userDB.Exec(c, fmt.Sprintf(_insertUpdateUserRegOriginTypeSQL, tableIndex(a.Mid)), a.Mid, a.JoinIP, a.JoinTime, a.Origin, a.RegType, a.AppID, a.CTime, a.MTime,
a.JoinIP, a.JoinTime, a.Origin, a.RegType, a.AppID, a.CTime, a.MTime); err != nil {
log.Error("fail to insert update user reg origin type, userRegOrigin(%+v) dao.userDB.Exec() error(%+v)", a, err)
return
}
return res.RowsAffected()
}
func tableIndex(mid int64) int64 {
return mid % 100
}

View File

@@ -0,0 +1,411 @@
package dao
import (
"context"
"testing"
"go-common/app/job/main/passport-user-compare/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoQueryUserBase(t *testing.T) {
convey.Convey("QueryUserBase", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryUserBase(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateUserBase(t *testing.T) {
convey.Convey("UpdateUserBase", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserBase{
Mid: 1111111,
UserID: "test_0000_0001",
Pwd: []byte{},
Salt: "",
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserBase(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUserBase(t *testing.T) {
convey.Convey("InsertUserBase", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserBase{
Mid: 1111111111,
UserID: "test_0000_0002",
Pwd: []byte{},
Salt: "",
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.InsertUserBase(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUserTel(t *testing.T) {
convey.Convey("QueryUserTel", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1111111111)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryUserTel(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateUserTel(t *testing.T) {
convey.Convey("UpdateUserTel", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserTel{
Mid: 1111111111,
Tel: []byte{},
Cid: "",
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserTel(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUserTel(t *testing.T) {
convey.Convey("InsertUserTel", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserTel{
Mid: 1111111111,
Tel: []byte{},
Cid: "",
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.InsertUserTel(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUserMail(t *testing.T) {
convey.Convey("QueryUserMail", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1111111111)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryUserMail(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateUserMail(t *testing.T) {
convey.Convey("UpdateUserMail", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserEmail{
Mid: 1111111111,
Email: []byte{},
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserMail(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateUserMailVerified(t *testing.T) {
convey.Convey("UpdateUserMailVerified", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserEmail{
Mid: 1111111111,
Email: []byte{},
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserMailVerified(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUserEmail(t *testing.T) {
convey.Convey("InsertUserEmail", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserEmail{
Mid: 1111111111,
Email: []byte{},
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.InsertUserEmail(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUserSafeQuestion(t *testing.T) {
convey.Convey("QueryUserSafeQuestion", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1111111111)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryUserSafeQuestion(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoUpdateUserSafeQuestion(t *testing.T) {
convey.Convey("UpdateUserSafeQuestion", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserSafeQuestion{
Mid: 1111111111,
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserSafeQuestion(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUserSafeQuestion(t *testing.T) {
convey.Convey("InsertUserSafeQuestion", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserSafeQuestion{
Mid: 1111111111,
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.InsertUserSafeQuestion(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUserThirdBind(t *testing.T) {
convey.Convey("QueryUserThirdBind", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1111111111)
platform = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryUserThirdBind(c, mid, platform)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoUpdateUserThirdBind(t *testing.T) {
convey.Convey("UpdateUserThirdBind", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserThirdBind{
Mid: 1111111111,
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.UpdateUserThirdBind(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUserThirdBind(t *testing.T) {
convey.Convey("InsertUserThirdBind", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserThirdBind{
Mid: 1111111111,
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.InsertUserThirdBind(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryCountryCode(t *testing.T) {
convey.Convey("QueryCountryCode", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.QueryCountryCode(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetMidByTel(t *testing.T) {
convey.Convey("GetMidByTel", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserTel{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
mid, err := d.GetMidByTel(c, a)
ctx.Convey("Then err should be nil.mid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(mid, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetMidByEmail(t *testing.T) {
convey.Convey("GetMidByEmail", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserEmail{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
mid, err := d.GetMidByEmail(c, a)
ctx.Convey("Then err should be nil.mid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(mid, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUnverifiedEmail(t *testing.T) {
convey.Convey("GetUnverifiedEmail", t, func(ctx convey.C) {
var (
c = context.Background()
start = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetUnverifiedEmail(c, start)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUserRegOriginByMid(t *testing.T) {
convey.Convey("GetUserRegOriginByMid", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetUserRegOriginByMid(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUpdateUserRegOriginType(t *testing.T) {
convey.Convey("InsertUpdateUserRegOriginType", t, func(ctx convey.C) {
var (
c = context.Background()
a = &model.UserRegOrigin{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.InsertUpdateUserRegOriginType(c, a)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaotableIndex(t *testing.T) {
convey.Convey("tableIndex", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := tableIndex(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strconv"
"time"
"go-common/library/log"
)
type wechatResp struct {
Status int `json:"status"`
Msg string `json:"msg"`
}
const (
_url = "http://bap.bilibili.co/api/v1/message/add"
)
// SendWechat send stat to wechat
func (d *Dao) SendWechat(param map[string]int64) (err error) {
var stat []byte
if stat, err = json.Marshal(param); err != nil {
log.Error("json.Marshal error ,error is (%+v)", err)
return
}
params := map[string]string{
"content": string(stat),
"timestamp": strconv.FormatInt(time.Now().Unix(), 10),
"token": d.c.WeChat.Token,
"type": "wechat",
"username": d.c.WeChat.Username,
"url": "",
}
params["signature"] = d.sign(params)
b, err := json.Marshal(params)
if err != nil {
log.Error("SendWechat json.Marshal error(%v)", err)
return
}
req, err := http.NewRequest(http.MethodPost, _url, bytes.NewReader(b))
if err != nil {
log.Error("SendWechat NewRequest error(%v), params(%s)", err, string(b))
return
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := wechatResp{}
if err = d.httpClient.Do(context.TODO(), req, &res); err != nil {
log.Error("SendWechat Do error(%v), params(%s)", err, string(b))
return
}
if res.Status != 0 {
err = fmt.Errorf("status(%d) msg(%s)", res.Status, res.Msg)
log.Error("SendWechat response error(%v), params(%s)", err, string(b))
}
return
}
func (d *Dao) sign(params map[string]string) string {
var keys []string
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
buf := bytes.Buffer{}
for _, k := range keys {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(url.QueryEscape(k) + "=")
buf.WriteString(url.QueryEscape(params[k]))
}
h := md5.New()
io.WriteString(h, buf.String()+d.c.WeChat.Secret)
return fmt.Sprintf("%x", h.Sum(nil))
}

View File

@@ -0,0 +1,35 @@
package dao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSendWechat(t *testing.T) {
convey.Convey("SendWechat", t, func(ctx convey.C) {
var (
param map[string]int64
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SendWechat(param)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaosign(t *testing.T) {
convey.Convey("sign", t, func(ctx convey.C) {
var (
params map[string]string
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.sign(params)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}