Create & Init Project...
This commit is contained in:
58
app/job/main/usersuit/dao/pendant/BUILD
Normal file
58
app/job/main/usersuit/dao/pendant/BUILD
Normal file
@ -0,0 +1,58 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_test",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"dao_test.go",
|
||||
"mysql_test.go",
|
||||
"redis_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
rundir = ".",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/job/main/usersuit/conf:go_default_library",
|
||||
"//vendor/github.com/go-sql-driver/mysql:go_default_library",
|
||||
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"dao.go",
|
||||
"mysql.go",
|
||||
"redis.go",
|
||||
],
|
||||
importpath = "go-common/app/job/main/usersuit/dao/pendant",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/job/main/usersuit/conf:go_default_library",
|
||||
"//app/job/main/usersuit/model:go_default_library",
|
||||
"//library/cache/redis: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"],
|
||||
)
|
54
app/job/main/usersuit/dao/pendant/dao.go
Normal file
54
app/job/main/usersuit/dao/pendant/dao.go
Normal file
@ -0,0 +1,54 @@
|
||||
package pendant
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-common/app/job/main/usersuit/conf"
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/database/sql"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
)
|
||||
|
||||
// Dao struct info of Dao.
|
||||
type Dao struct {
|
||||
db *sql.DB
|
||||
c *conf.Config
|
||||
client *bm.Client
|
||||
// redis
|
||||
redis *redis.Pool
|
||||
|
||||
notifyURL string
|
||||
}
|
||||
|
||||
// New new a Dao and return.
|
||||
func New(c *conf.Config) (d *Dao) {
|
||||
d = &Dao{
|
||||
c: c,
|
||||
db: sql.NewMySQL(c.Mysql),
|
||||
client: bm.NewClient(c.HTTPClient),
|
||||
// redis
|
||||
redis: redis.NewPool(c.PendantRedis.Config),
|
||||
notifyURL: c.NotifyURL,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Ping ping health.
|
||||
func (d *Dao) Ping(c context.Context) (err error) {
|
||||
return d.pingRedis(c)
|
||||
}
|
||||
|
||||
// Close close connections of mc, redis, db.
|
||||
func (d *Dao) Close() {
|
||||
if d.db != nil {
|
||||
d.db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Dao) pingRedis(c context.Context) (err error) {
|
||||
conn := d.redis.Get(c)
|
||||
_, err = conn.Do("SET", "PING", "PONG")
|
||||
conn.Close()
|
||||
return
|
||||
}
|
42
app/job/main/usersuit/dao/pendant/dao_test.go
Normal file
42
app/job/main/usersuit/dao/pendant/dao_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
package pendant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"go-common/app/job/main/usersuit/conf"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
var d *Dao
|
||||
|
||||
func init() {
|
||||
dir, _ := filepath.Abs("../cmd/convey-test.toml")
|
||||
flag.Set("conf", dir)
|
||||
conf.Init()
|
||||
d = New(conf.Conf)
|
||||
}
|
||||
|
||||
func Test_Ping(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
err := d.Ping(context.TODO())
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Close(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
d.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func Test_pingRedis(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
err := d.pingRedis(context.TODO())
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
73
app/job/main/usersuit/dao/pendant/mysql.go
Normal file
73
app/job/main/usersuit/dao/pendant/mysql.go
Normal file
@ -0,0 +1,73 @@
|
||||
package pendant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"go-common/app/job/main/usersuit/model"
|
||||
xsql "go-common/library/database/sql"
|
||||
)
|
||||
|
||||
const (
|
||||
_upEquipSQL = "UPDATE user_pendant_equip SET pid = 0 AND expires = 0 WHERE mid = ?"
|
||||
_upEquipExpiresSQL = "UPDATE user_pendant_equip SET expires = ? WHERE mid = ?"
|
||||
_selEquipSQL = "SELECT mid FROM user_pendant_equip WHERE expires =< ?"
|
||||
_selEquipMIDSQL = "SELECT mid,pid,expires FROM user_pendant_equip WHERE mid = ?"
|
||||
_selGidPidSQL = "SELECT gid FROM pendant_group_ref WHERE pid = ?"
|
||||
)
|
||||
|
||||
// UpEquipMID update equip empty by mid
|
||||
func (d *Dao) UpEquipMID(c context.Context, mid int64) (affected int64, err error) {
|
||||
var res sql.Result
|
||||
if res, err = d.db.Exec(c, _upEquipSQL, mid); err != nil {
|
||||
return
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// UpEquipExpires update equip expires by mid
|
||||
func (d *Dao) UpEquipExpires(c context.Context, mid, expires int64) (affected int64, err error) {
|
||||
var res sql.Result
|
||||
if res, err = d.db.Exec(c, _upEquipExpiresSQL, expires, mid); err != nil {
|
||||
return
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// PendantEquipMID get user equip pendant by mid.
|
||||
func (d *Dao) PendantEquipMID(c context.Context, mid int64) (pe *model.PendantEquip, err error) {
|
||||
row := d.db.QueryRow(c, _selEquipMIDSQL, mid)
|
||||
pe = new(model.PendantEquip)
|
||||
if err = row.Scan(&pe.Mid, &pe.Pid, &pe.Expires); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ExpireEquipPendant get expire equip pendant
|
||||
func (d *Dao) ExpireEquipPendant(c context.Context, expires int64) (res []int64, err error) {
|
||||
var (
|
||||
row *xsql.Rows
|
||||
mid int64
|
||||
)
|
||||
if row, err = d.db.Query(c, _selEquipSQL, expires); err != nil {
|
||||
return
|
||||
}
|
||||
defer row.Close()
|
||||
for row.Next() {
|
||||
if err = row.Scan(&mid); err != nil {
|
||||
return
|
||||
}
|
||||
res = append(res, mid)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// PendantEquipGidPid get gid of its equip pendant by pid.
|
||||
func (d *Dao) PendantEquipGidPid(c context.Context, pid int64) (gid int64, err error) {
|
||||
row := d.db.QueryRow(c, _selGidPidSQL, pid)
|
||||
if err = row.Scan(&gid); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
48
app/job/main/usersuit/dao/pendant/mysql_test.go
Normal file
48
app/job/main/usersuit/dao/pendant/mysql_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package pendant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestDaoUpEquipMID(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
res, err := d.UpEquipMID(context.Background(), 11)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDaoUpEquipExpires(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
res, err := d.UpEquipExpires(context.TODO(), 11, 123121)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDaoPendantEquipMID(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
res, err := d.PendantEquipMID(context.TODO(), 11)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDaoExpireEquipPendant(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
res, err := d.ExpireEquipPendant(context.TODO(), 11)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDaoPendantEquipGidPid(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
res, err := d.PendantEquipGidPid(context.TODO(), 11)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
})
|
||||
}
|
37
app/job/main/usersuit/dao/pendant/redis.go
Normal file
37
app/job/main/usersuit/dao/pendant/redis.go
Normal file
@ -0,0 +1,37 @@
|
||||
package pendant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
const (
|
||||
_pendantPKG = "pkg_" // key of
|
||||
_pendantEquip = "pe_"
|
||||
)
|
||||
|
||||
// DelPKGCache del package cache
|
||||
func (d *Dao) DelPKGCache(c context.Context, mid int64) (err error) {
|
||||
key := _pendantPKG + strconv.FormatInt(mid, 10)
|
||||
conn := d.redis.Get(c)
|
||||
defer conn.Close()
|
||||
if err = conn.Send("DEL", key); err != nil {
|
||||
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DelEquipCache set pendant info cache
|
||||
func (d *Dao) DelEquipCache(c context.Context, mid int64) (err error) {
|
||||
key := _pendantEquip + strconv.FormatInt(mid, 10)
|
||||
conn := d.redis.Get(c)
|
||||
defer conn.Close()
|
||||
if err = conn.Send("DEL", key); err != nil {
|
||||
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
22
app/job/main/usersuit/dao/pendant/redis_test.go
Normal file
22
app/job/main/usersuit/dao/pendant/redis_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package pendant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func Test_DelPKGCache(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
err := d.DelPKGCache(context.TODO(), 11)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DelEquipCache(t *testing.T) {
|
||||
Convey("should return err be nil", t, func() {
|
||||
err := d.DelEquipCache(context.TODO(), 11)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user