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,69 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"business_test.go",
"dao_test.go",
"mc_test.go",
"permit_test.go",
"reason_test.go",
"tag_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/manager/conf:go_default_library",
"//app/admin/main/manager/model:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster/middleware/permit:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"business.go",
"dao.go",
"journal.go",
"mc.go",
"permit.go",
"reason.go",
"tag.go",
],
importpath = "go-common/app/admin/main/manager/dao",
tags = ["automanaged"],
deps = [
"//app/admin/main/manager/conf:go_default_library",
"//app/admin/main/manager/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/orm:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/permit:go_default_library",
"//vendor/github.com/jinzhu/gorm: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"],
)

View File

@@ -0,0 +1,244 @@
package dao
import (
"context"
"fmt"
"strconv"
"strings"
"go-common/app/admin/main/manager/model"
"github.com/jinzhu/gorm"
)
// AddBusiness .
func (d *Dao) AddBusiness(c context.Context, b *model.Business) (err error) {
maxBid := int64(-1)
if b.PID != 0 {
if maxBid, err = d.maxBid(c); err != nil {
return
}
}
valueStrings := []string{}
valueArgs := []interface{}{}
for _, name := range b.Names {
maxBid++
valueStrings = append(valueStrings, "(?,?,?,?,?,?)")
valueArgs = append(valueArgs, b.PID, maxBid, name, b.Flow, b.FlowState, b.State)
}
stmt := fmt.Sprintf("INSERT INTO manager_business(pid,bid,name,flow,flow_state,state) VALUES %s ON DUPLICATE KEY UPDATE pid=values(pid),name=values(name),flow=values(flow),flow_state=values(flow_state),state=values(state)", strings.Join(valueStrings, ","))
err = d.db.Exec(stmt, valueArgs...).Error
return
}
// maxBidByPid .
func (d *Dao) maxBid(c context.Context) (maxBid int64, err error) {
err = d.db.Table("manager_business").Select("max(bid)").Row().Scan(&maxBid)
return
}
// UpdateBusiness .
func (d *Dao) UpdateBusiness(c context.Context, b *model.Business) error {
return d.db.Table("manager_business").Where("id = ?", b.ID).
Update(map[string]interface{}{
"name": b.Name,
"flow": b.Flow,
}).Error
}
// BusinessChilds .
func (d *Dao) BusinessChilds(c context.Context, pid int64) (res []*model.Business, err error) {
err = d.db.Table("manager_business").Where("pid = ?", pid).Find(&res).Error
return
}
// AddRole .
func (d *Dao) AddRole(c context.Context, br *model.BusinessRole) (err error) {
stmt := fmt.Sprintf("INSERT INTO manager_business_role(bid,rid,name,type,state) VALUES %s ON DUPLICATE KEY UPDATE bid=values(bid),name=values(name),type=values(type),state=values(state)", "(?,?,?,?,?)")
err = d.db.Exec(stmt, br.BID, br.RID, br.Name, br.Type, br.State).Error
return
}
// MaxRidByBid .
func (d *Dao) MaxRidByBid(c context.Context, bid int64) (maxRid interface{}, err error) {
err = d.db.Table("manager_business_role").Select("max(rid) as mrid").Where("bid = ?", bid).Row().Scan(&maxRid)
return
}
// UpdateRole .
func (d *Dao) UpdateRole(c context.Context, br *model.BusinessRole) error {
return d.db.Table("manager_business_role").Where("id = ?", br.ID).
Update("name", br.Name).Error
}
// RoleByRIDs .
func (d *Dao) RoleByRIDs(c context.Context, bid int64, rids []int64) (res map[int64]*model.BusinessRole, err error) {
t := []*model.BusinessRole{}
res = make(map[int64]*model.BusinessRole)
err = d.db.Where("rid IN (?)", rids).Where("bid = ?", bid).Find(&t).Error
for _, r := range t {
res[r.RID] = r
}
return
}
// AddUser add users with associated roles .
func (d *Dao) AddUser(c context.Context, bur *model.BusinessUserRole) error {
valueStrings := []string{}
valueArgs := []interface{}{}
for _, uid := range bur.UIDs {
valueStrings = append(valueStrings, "(?,?,?,?)")
valueArgs = append(valueArgs, uid, bur.CUID, bur.BID, bur.Role)
}
stmt := fmt.Sprintf("INSERT INTO manager_business_user_role(uid, cuid, bid, role) VALUES %s ON DUPLICATE KEY UPDATE uid=values(uid),cuid=values(cuid),bid=values(bid),role=values(role)", strings.Join(valueStrings, ","))
return d.db.Exec(stmt, valueArgs...).Error
}
// UpdateUser .
func (d *Dao) UpdateUser(c context.Context, bur *model.BusinessUserRole) error {
return d.db.Table("manager_business_user_role").Where("id = ?", bur.ID).
Update("role", bur.Role).Error
}
// UpdateBusinessState .
func (d *Dao) UpdateBusinessState(c context.Context, su *model.StateUpdate) error {
return d.db.Table("manager_business").Where("id = ?", su.ID).
Update("state", su.State).Error
}
// BatchUpdateChildState .
func (d *Dao) BatchUpdateChildState(c context.Context, pid int64, flowStates []int64) error {
return d.db.Table("manager_business").Where("flow_state NOT IN (?)", flowStates).Where("pid = ?", pid).
Update("state", 0).Error
}
// UpdateBusinessRoleState .
func (d *Dao) UpdateBusinessRoleState(c context.Context, su *model.StateUpdate) error {
return d.db.Table("manager_business_role").Where("id = ?", su.ID).
Update("state", su.State).Error
}
// ParentBusiness .
func (d *Dao) ParentBusiness(c context.Context, state int64) (res map[int64]*model.BusinessList, err error) {
temp := []*model.BusinessList{}
res = make(map[int64]*model.BusinessList)
db := d.db.Where("pid = ?", 0)
if state != -1 {
db = db.Where("state = ?", state)
}
if err = db.Order("id asc", true).Find(&temp).Error; err != nil {
return
}
for _, t := range temp {
res[t.ID] = t
}
return
}
// ChildBusiness .
func (d *Dao) ChildBusiness(c context.Context, bp *model.BusinessListParams) (res map[int64]*model.BusinessList, err error) {
temp := []*model.BusinessList{}
res = make(map[int64]*model.BusinessList)
db := d.db.Where("pid != ?", 0)
if bp.State != -1 {
db = db.Where("state = ?", bp.State)
}
if bp.Flow != 0 {
db = db.Where("flow_state = ?", bp.Flow)
}
err = db.Order("ctime desc").Find(&temp).Error
for _, t := range temp {
res[t.ID] = t
}
return
}
// ChildBusinessByPIDs .
func (d *Dao) ChildBusinessByPIDs(c context.Context, pids []int64) (res map[int64][]*model.BusinessList, err error) {
res = make(map[int64][]*model.BusinessList)
temp := []*model.BusinessList{}
if err = d.db.Where("pid IN (?)", pids).Find(&temp).Error; err != nil {
return
}
for _, t := range temp {
res[t.PID] = append(res[t.PID], t)
}
return
}
// FlowList .
func (d *Dao) FlowList(c context.Context, bp *model.BusinessListParams) (res []*model.BusinessList, err error) {
db := d.db.Where("pid != ?", 0)
if bp.Flow > 0 {
db = db.Where("flow_state = ?", bp.Flow)
}
if bp.State != -1 {
db = db.Where("state = ?", bp.State)
}
err = db.Find(&res).Error
return
}
// RoleListByBID .
func (d *Dao) RoleListByBID(c context.Context, br *model.BusinessRole) (res []*model.BusinessRole, err error) {
db := d.db
if br.BID > 0 {
db = db.Where("bid = ?", br.BID)
}
if br.Type != -1 {
db = db.Where("type = ?", br.Type)
}
if br.State != -1 {
db = db.Where("state = ?", br.State)
}
err = db.Order("rid desc").Find(&res).Error
return
}
// RoleListByRIDs .
func (d *Dao) RoleListByRIDs(c context.Context, bid int64, rids []int64) (res []*model.BusinessRole, err error) {
err = d.DB().Table("manager_business_role").Where("bid = ? AND rid IN (?)", bid, rids).Where("state = ?", 1).Find(&res).Error
return
}
// UserList .
func (d *Dao) UserList(c context.Context, u *model.UserListParams) (res []*model.BusinessUserRoleList, err error) {
db := d.db.Table("manager_business_user_role")
if u.BID > 0 {
db = db.Where("bid = ?", u.BID)
}
if u.UID > 0 {
db = db.Where("uid = ?", u.UID)
}
if u.Role != -1 {
db = db.Where("find_in_set(?, role) > 0", strconv.FormatInt(u.Role, 10))
}
err = db.Where("state = ?", model.UserOnState).Find(&res).Error
return
}
// DeleteUser .
func (d *Dao) DeleteUser(c context.Context, bur *model.BusinessUserRole) error {
return d.db.Table("manager_business_user_role").Where("id = ?", bur.ID).Update("state", model.UserOffState).Error
}
// BusinessByID .
func (d *Dao) BusinessByID(c context.Context, id int64) (res *model.BusinessList, err error) {
res = &model.BusinessList{}
if err = d.db.Where("id = ?", id).First(res).Error; err == gorm.ErrRecordNotFound {
err = nil
}
return
}
// UserRoles .
func (d *Dao) UserRoles(c context.Context, uid int64) (res []*model.BusinessUserRoleList, err error) {
err = d.db.Table("manager_business_user_role").Where("uid = ?", uid).Find(&res).Error
return
}
// UserRoleByBIDs .
func (d *Dao) UserRoleByBIDs(c context.Context, uid int64, bids []int64) (res []*model.BusinessUserRoleList, err error) {
err = d.DB().Table("manager_business_user_role").Where("bid IN (?)", bids).Where("uid = ? AND state = ?", uid, 1).Find(&res).Error
return
}

View File

@@ -0,0 +1,50 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/main/manager/model"
"github.com/smartystreets/goconvey/convey"
)
func TestMaxBid(t *testing.T) {
convey.Convey("maxBid", t, func() {
_, err := d.maxBid(context.Background())
convey.So(err, convey.ShouldBeNil)
})
}
func TestUpdateBusiness(t *testing.T) {
convey.Convey("UpdateBusiness", t, func() {
p := &model.Business{
ID: 1,
Name: "test",
Flow: 1,
}
err := d.UpdateBusiness(context.Background(), p)
convey.So(err, convey.ShouldBeNil)
})
}
func TestBusinessChilds(t *testing.T) {
convey.Convey("BusinessChilds", t, func() {
_, err := d.BusinessChilds(context.Background(), 1)
convey.So(err, convey.ShouldBeNil)
})
}
func TestMaxRidByBid(t *testing.T) {
convey.Convey("MaxRidByBid", t, func() {
_, err := d.MaxRidByBid(context.Background(), 1)
convey.So(err, convey.ShouldBeNil)
})
}
func TestParentBusiness(t *testing.T) {
convey.Convey("ParentBusiness", t, func() {
_, err := d.ParentBusiness(context.Background(), int64(1))
convey.So(err, convey.ShouldBeNil)
})
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"go-common/app/admin/main/manager/conf"
"go-common/library/cache/memcache"
"go-common/library/database/orm"
bm "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
// Dao .
type Dao struct {
db *gorm.DB
mc *memcache.Pool
httpClient *bm.Client
dsbClient *bm.Client
}
// New new a instance
func New(c *conf.Config) (d *Dao) {
d = &Dao{
db: orm.NewMySQL(c.ORM),
mc: memcache.NewPool(c.Memcache),
httpClient: bm.NewClient(c.HTTPClient),
dsbClient: bm.NewClient(c.DsbClient),
}
d.initORM()
return
}
func (d *Dao) initORM() {
d.db.LogMode(true)
}
// DB .
func (d *Dao) DB() *gorm.DB {
return d.db
}
// Ping check connection of db , mc.
func (d *Dao) Ping(c context.Context) (err error) {
if d.db != nil {
err = d.db.DB().PingContext(c)
}
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,37 @@
package dao
import (
"flag"
"go-common/app/admin/main/manager/conf"
"os"
"path/filepath"
"testing"
)
var d *Dao
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.manager.manager-admin")
flag.Set("conf_token", "b661550b1a2cce530787e00ed1625581")
flag.Set("tree_id", "11038")
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 {
dir, _ := filepath.Abs("../cmd/manager-admin-test.toml")
if err := flag.Set("conf", dir); err != nil {
panic(err)
}
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"bufio"
"net/http"
"strings"
"go-common/app/admin/main/manager/model"
bm "go-common/library/net/http/blademaster"
)
const (
_searchAuditURL = "http://manager.bilibili.co/x/admin/search/log/audit"
_searchActionURL = "http://manager.bilibili.co/x/admin/search/log/user_action"
)
// SearchLogAudit .
func (d *Dao) SearchLogAudit(c *bm.Context) (res *model.LogRes, err error) {
res, err = d.combination(c, _searchAuditURL)
return
}
// SearchLogAction .
func (d *Dao) SearchLogAction(c *bm.Context) (res *model.LogRes, err error) {
res, err = d.combination(c, _searchActionURL)
return
}
// combination .
func (d *Dao) combination(c *bm.Context, preURL string) (res *model.LogRes, err error) {
params := c.Request.URL.RawQuery
url := preURL + "?" + params
cookie := c.Request.Header.Get("Cookie")
req, err := http.NewRequest(http.MethodGet, url, bufio.NewReader(strings.NewReader(params)))
if err != nil {
return
}
req.Header.Set("Cookie", cookie)
err = d.httpClient.Do(c, req, &res)
return
}

View File

@@ -0,0 +1,43 @@
package dao
import (
"context"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/net/http/blademaster/middleware/permit"
)
// Session .
func (d *Dao) Session(ctx context.Context, sid string) (res *permit.Session, err error) {
conn := d.mc.Get(ctx)
defer conn.Close()
r, err := conn.Get(sid)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", sid, err)
return
}
res = &permit.Session{}
if err = conn.Scan(r, res); err != nil {
log.Error("conn.Scan(%s) error(%v)", string(r.Value), err)
}
return
}
// SetSession .
func (d *Dao) SetSession(ctx context.Context, p *permit.Session) (err error) {
conn := d.mc.Get(ctx)
defer conn.Close()
item := &memcache.Item{
Key: p.Sid,
Object: p,
Flags: memcache.FlagJSON,
Expiration: int32(_sessionLife),
}
err = conn.Set(item)
return
}

View File

@@ -0,0 +1,28 @@
package dao
import (
"context"
"testing"
"go-common/library/net/http/blademaster/middleware/permit"
"github.com/smartystreets/goconvey/convey"
)
func TestSession(t *testing.T) {
convey.Convey("Session", t, func() {
sid := "1234567890"
_, err := d.Session(context.Background(), sid)
convey.So(err, convey.ShouldBeNil)
})
}
func TestSetSession(t *testing.T) {
convey.Convey("SetSession", t, func() {
p := &permit.Session{
Sid: "1234567890",
}
err := d.SetSession(context.Background(), p)
convey.So(err, convey.ShouldBeNil)
})
}

View File

@@ -0,0 +1,53 @@
package dao
import (
"context"
"crypto/rand"
"encoding/hex"
"net/url"
"go-common/library/ecode"
"go-common/library/net/http/blademaster/middleware/permit"
)
const (
_sessionlen = 32
_sessionLife = 2592000
_dsbCaller = "manager-go"
_dsbVerifyURL = "http://dashboard-mng.bilibili.co/api/session/verify"
)
// VerifyDsb .
func (d *Dao) VerifyDsb(ctx context.Context, sid string) (res string, err error) {
params := url.Values{}
params.Set("session_id", sid)
params.Set("encrypt", "md5")
params.Set("caller", _dsbCaller)
var dsbRes struct {
Code int `json:"code"`
UserName string `json:"username"`
}
if err = d.dsbClient.Get(ctx, _dsbVerifyURL, "", params, &dsbRes); err != nil {
return
}
if ecode.Int(dsbRes.Code) != ecode.OK {
err = ecode.Int(dsbRes.Code)
return
}
res = dsbRes.UserName
return
}
// NewSession .
func (d *Dao) NewSession(ctx context.Context) (res *permit.Session) {
b := make([]byte, _sessionlen)
n, err := rand.Read(b)
if n != len(b) || err != nil {
return
}
res = &permit.Session{
Sid: hex.EncodeToString(b),
Values: make(map[string]interface{}),
}
return
}

View File

@@ -0,0 +1,27 @@
package dao
import (
"context"
"go-common/library/ecode"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestVerifyDsb(t *testing.T) {
convey.Convey("VerifyDsb", t, func() {
sid := "1234567890"
_, err := d.VerifyDsb(context.Background(), sid)
if err == ecode.NoLogin {
err = nil
}
convey.So(err, convey.ShouldBeNil)
})
}
func TestNewSession(t *testing.T) {
convey.Convey("NewSession", t, func() {
res := d.NewSession(context.Background())
convey.So(res, convey.ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,143 @@
package dao
import (
"context"
"go-common/app/admin/main/manager/model"
)
// AddCateSecExt .
func (d *Dao) AddCateSecExt(c context.Context, e *model.CateSecExt) (err error) {
str := "INSERT INTO manager_reason_catesecext (bid,name,type) VALUES (?,?,?) ON DUPLICATE KEY UPDATE bid=values(bid),name=values(name),type=values(type)"
return d.db.Exec(str, e.BusinessID, e.Name, e.Type).Error
}
// UpdateCateSecExt .
func (d *Dao) UpdateCateSecExt(c context.Context, e *model.CateSecExt) (err error) {
return d.db.Table("manager_reason_catesecext").Where("id = ?", e.ID).Update("name", e.Name).Error
}
// BanCateSecExt .
func (d *Dao) BanCateSecExt(c context.Context, e *model.CateSecExt) (err error) {
return d.db.Table("manager_reason_catesecext").Where("id = ?", e.ID).Update("state", e.State).Error
}
// AddAssociation .
func (d *Dao) AddAssociation(c context.Context, e *model.Association) (err error) {
return d.db.Table("manager_reason_association").Create(&e).Error
}
// UpdateAssociation .
func (d *Dao) UpdateAssociation(c context.Context, e *model.Association) (err error) {
return d.db.Table("manager_reason_association").Where("id = ?", e.ID).Updates(
map[string]interface{}{
"rid": e.RoleID,
"cid": e.CategoryID,
"sids": e.SecondIDs,
}).Error
}
// BanAssociation .
func (d *Dao) BanAssociation(c context.Context, e *model.Association) (err error) {
return d.db.Table("manager_reason_association").Where("id = ?", e.ID).Update("state", e.State).Error
}
// AddReason .
func (d *Dao) AddReason(c context.Context, e *model.Reason) (err error) {
return d.db.Table("manager_reason").Create(e).Error
}
// UpdateReason .
func (d *Dao) UpdateReason(c context.Context, e *model.Reason) (err error) {
return d.db.Table("manager_reason").Where("id = ?", e.ID).
Updates(map[string]interface{}{
"rid": e.RoleID,
"cid": e.CategoryID,
"sid": e.SecondID,
"state": e.State,
"common": e.Common,
"uid": e.UID,
"description": e.Description,
"weight": e.Weight,
"flag": e.Flag,
"lid": e.LinkID,
"type_id": e.TypeID,
"tid": e.TagID,
}).Error
}
// ReasonList .
func (d *Dao) ReasonList(c context.Context, e *model.SearchReasonParams) (res []*model.Reason, err error) {
db := d.db.Table("manager_reason")
if e.BusinessID != -1 {
db = db.Where("bid = ?", e.BusinessID)
}
if e.KeyWord != "" {
db = db.Where("description LIKE ?", "%"+e.KeyWord+"%")
}
if e.RoleID != 0 {
db = db.Where("rid = ?", e.RoleID)
}
if e.CategoryID != 0 {
db = db.Where("cid = ?", e.CategoryID)
}
if e.SecondID != 0 {
db = db.Where("sid = ?", e.SecondID)
}
if e.State != -1 {
db = db.Where("state = ?", e.State)
}
if e.UName != "" {
db = db.Where("uid = ?", e.UID)
}
if e.Order != "" {
db = db.Order(e.Order+" "+e.Sort, true)
}
err = db.Find(&res).Error
return
}
// CateSecByIDs .
func (d *Dao) CateSecByIDs(c context.Context, ids []int64) (res map[int64]string, err error) {
r := []*model.CateSecExt{}
res = make(map[int64]string)
if err = d.db.Table("manager_reason_catesecext").Where("id IN (?)", ids).Find(&r).Error; err != nil {
return
}
for _, cn := range r {
res[cn.ID] = cn.Name
}
return
}
// BatchUpdateReasonState .
func (d *Dao) BatchUpdateReasonState(c context.Context, b *model.BatchUpdateReasonState) (err error) {
return d.db.Table("manager_reason").Where("id IN (?)", b.IDs).Update("state", b.State).Error
}
// CateSecExtList .
func (d *Dao) CateSecExtList(c context.Context, e *model.CateSecExt) (res []*model.CateSecExt, err error) {
// Display all record
db := d.db.Table("manager_reason_catesecext").Where("bid = ? and type = ?", e.BusinessID, e.Type)
if e.State != -1 {
db = db.Where("state = ?", e.State)
}
err = db.Find(&res).Error
return
}
// CateSecList .
func (d *Dao) CateSecList(c context.Context, bid int64) (res []*model.CateSecExt, err error) {
err = d.db.Table("manager_reason_catesecext").Where("bid = ?", bid).Find(&res).Error
return
}
// AssociationList .
func (d *Dao) AssociationList(c context.Context, state int64, bid int64) (res []*model.Association, err error) {
db := d.db.Table("manager_reason_association").Where("bid = ?", bid)
if state != -1 {
db = db.Where("state = ?", state)
}
err = db.Find(&res).Error
return
}

View File

@@ -0,0 +1,250 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/main/manager/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddCateSecExt(t *testing.T) {
var (
c = context.TODO()
e = &model.CateSecExt{
BusinessID: 1,
Type: 1,
Name: "测试type1",
}
)
convey.Convey("AddCateSecExt", t, func(ctx convey.C) {
err := d.AddCateSecExt(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoUpdateCateSecExt(t *testing.T) {
var (
c = context.TODO()
e = &model.CateSecExt{
ID: 75,
Name: "测试更新下",
}
)
convey.Convey("UpdateCateSecExt", t, func(ctx convey.C) {
err := d.UpdateCateSecExt(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoBanCateSecExt(t *testing.T) {
var (
c = context.TODO()
e = &model.CateSecExt{
ID: 75,
State: 0,
}
)
convey.Convey("BanCateSecExt", t, func(ctx convey.C) {
err := d.BanCateSecExt(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoAddAssociation(t *testing.T) {
var (
c = context.TODO()
e = &model.Association{
BusinessID: 1,
RoleID: 121,
CategoryID: 212,
SecondIDs: "3,7",
}
)
convey.Convey("AddAssociation", t, func(ctx convey.C) {
err := d.AddAssociation(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoUpdateAssociation(t *testing.T) {
var (
c = context.TODO()
e = &model.Association{
ID: 41,
RoleID: 1111,
CategoryID: 2222,
SecondIDs: "6,5,4,3",
}
)
convey.Convey("UpdateAssociation", t, func(ctx convey.C) {
err := d.UpdateAssociation(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoBanAssociation(t *testing.T) {
var (
c = context.TODO()
e = &model.Association{
ID: 41,
State: 1,
}
)
convey.Convey("BanAssociation", t, func(ctx convey.C) {
err := d.BanAssociation(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoAddReason(t *testing.T) {
var (
c = context.TODO()
e = &model.Reason{
BusinessID: 1,
RoleID: 99,
CategoryID: 98,
SecondID: 97,
Common: 0,
UID: 1,
Description: "随便测试",
Weight: 96,
Flag: 0,
}
)
convey.Convey("AddReason", t, func(ctx convey.C) {
err := d.AddReason(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoUpdateReason(t *testing.T) {
var (
c = context.TODO()
e = &model.Reason{
ID: 18,
RoleID: 100,
CategoryID: 99,
SecondID: 98,
Common: 1,
Description: "随便测试试试",
Weight: 97,
Flag: 1,
BusinessID: 2,
TypeID: 3,
TagID: 4,
}
)
convey.Convey("UpdateReason", t, func(ctx convey.C) {
err := d.UpdateReason(c, e)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoReasonList(t *testing.T) {
var (
c = context.TODO()
e = &model.SearchReasonParams{
BusinessID: 1,
}
)
convey.Convey("ReasonList", t, func(ctx convey.C) {
res, err := d.ReasonList(c, e)
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 TestDaoCateSecByIDs(t *testing.T) {
var (
c = context.TODO()
ids = []int64{1, 2, 3}
)
convey.Convey("CateSecByIDs", t, func(ctx convey.C) {
res, err := d.CateSecByIDs(c, ids)
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 TestDaoBatchUpdateReasonState(t *testing.T) {
var (
c = context.TODO()
b = &model.BatchUpdateReasonState{
IDs: []int64{1, 2, 3},
}
)
convey.Convey("BatchUpdateReasonState", t, func(ctx convey.C) {
err := d.BatchUpdateReasonState(c, b)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoCateSecExtList(t *testing.T) {
var (
c = context.TODO()
e = &model.CateSecExt{
BusinessID: 1,
Type: 1,
}
)
convey.Convey("CateSecExtList", t, func(ctx convey.C) {
res, err := d.CateSecExtList(c, e)
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 TestDaoCateSecList(t *testing.T) {
var (
c = context.TODO()
lid = int64(0)
)
convey.Convey("CateSecList", t, func(ctx convey.C) {
res, err := d.CateSecList(c, lid)
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 TestDaoAssociationList(t *testing.T) {
var (
c = context.TODO()
state = int64(0)
lid = int64(0)
)
convey.Convey("AssociationList", t, func(ctx convey.C) {
res, err := d.AssociationList(c, state, lid)
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,220 @@
package dao
import (
"context"
"fmt"
"strings"
"go-common/app/admin/main/manager/model"
"go-common/library/ecode"
)
// AddType .
func (d *Dao) AddType(c context.Context, tt *model.TagType) (err error) {
tx := d.db.Begin()
if tx.Error != nil {
return
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
if err = tx.Create(tt).Error; err != nil {
return
}
if len(tt.Rids) > 0 {
values := []string{}
valueArgs := []interface{}{}
for _, rid := range tt.Rids {
values = append(values, "(?,?)")
valueArgs = append(valueArgs, tt.ID, rid)
}
stmt := fmt.Sprintf("INSERT INTO manager_tag_type_role(tid,rid) VALUES %s ON DUPLICATE KEY UPDATE tid=values(tid),rid=values(rid)", strings.Join(values, ","))
if err = tx.Exec(stmt, valueArgs...).Error; err != nil {
return
}
}
err = tx.Commit().Error
return
}
// UpdateTypeName .
func (d *Dao) UpdateTypeName(c context.Context, tt *model.TagType) error {
return d.db.Table("manager_tag_type").Where("id = ?", tt.ID).Update("name", tt.Name).Error
}
// UpdateType .
func (d *Dao) UpdateType(c context.Context, tt *model.TagType) (err error) {
if len(tt.Rids) <= 0 {
return
}
values := []string{}
valueArgs := []interface{}{}
for _, rid := range tt.Rids {
values = append(values, "(?,?)")
valueArgs = append(valueArgs, tt.ID, rid)
}
stmt := fmt.Sprintf("INSERT INTO manager_tag_type_role(tid,rid) VALUES %s ON DUPLICATE KEY UPDATE tid=values(tid),rid=values(rid)", strings.Join(values, ","))
err = d.db.Exec(stmt, valueArgs...).Error
return
}
// DeleteNonRole .
func (d *Dao) DeleteNonRole(c context.Context, tt *model.TagType) error {
if len(tt.Rids) <= 0 {
stmt := "DELETE FROM manager_tag_type_role WHERE tid=?"
return d.db.Exec(stmt, tt.ID).Error
}
stmt := "DELETE FROM manager_tag_type_role WHERE id IN (SELECT id FROM (select id from manager_tag_type_role WHERE tid=? AND rid NOT IN (?)) as temp)"
return d.db.Exec(stmt, tt.ID, tt.Rids).Error
}
// DeleteType .
func (d *Dao) DeleteType(c context.Context, td *model.TagTypeDel) error {
return d.db.Where("id = ?", td.ID).Delete(&model.TagType{}).Error
}
// AddTag .
func (d *Dao) AddTag(c context.Context, t *model.Tag) (err error) {
maxTagID := int64(0)
if maxTagID, err = d.maxTagIDByBid(c, t.Bid); err != nil {
return
}
maxTagID++
t.TagID = maxTagID
err = d.db.Create(t).Error
return
}
// maxTagIDByBid .
func (d *Dao) maxTagIDByBid(c context.Context, bid int64) (maxTagID int64, err error) {
var tagID interface{}
err = d.db.Table("manager_tag").Select("max(tag_id) as max_tag_id").Where("bid = ?", bid).Row().Scan(&tagID)
if tagID == nil {
maxTagID = int64(0)
return
}
maxTagID = tagID.(int64)
return
}
// UpdateTag .
func (d *Dao) UpdateTag(c context.Context, t *model.Tag) error {
return d.db.Table("manager_tag").Where("id = ?", t.ID).
Updates(map[string]interface{}{"bid": t.Bid, "tid": t.Tid, "rid": t.Rid, "name": t.Name, "weight": t.Weight, "description": t.Description}).Error
}
// AddControl .
func (d *Dao) AddControl(c context.Context, tc *model.TagControl) error {
return d.db.Create(tc).Error
}
// UpdateControl .
func (d *Dao) UpdateControl(c context.Context, tc *model.TagControl) error {
return d.db.Table("manager_tag_control").Where("id = ?", tc.ID).
Update(map[string]interface{}{
"tid": tc.Tid,
"name": tc.Name,
"title": tc.Title,
"weight": tc.Weight,
"component": tc.Component,
"placeholder": tc.Placeholder,
"required": tc.Required,
}).Error
}
// BatchUpdateState .
func (d *Dao) BatchUpdateState(c context.Context, b *model.BatchUpdateState) (err error) {
return d.db.Table("manager_tag").Where("id IN (?)", b.IDs).Update("state", b.State).Error
}
// TagList .
func (d *Dao) TagList(c context.Context, t *model.SearchTagParams) (res []*model.Tag, err error) {
db := d.db.Table("manager_tag")
if t.Bid != -1 {
db = db.Where("bid = ?", t.Bid)
}
if t.KeyWord != "" {
db = db.Where("name LIKE ?", "%"+t.KeyWord+"%")
}
if t.Tid != -1 {
db = db.Where("tid = ?", t.Tid)
}
if t.Rid != -1 {
db = db.Where("rid = ?", t.Rid)
}
if t.State != -1 {
db = db.Where("state = ?", t.State)
}
if t.UName != "" {
db = db.Where("uid = ?", t.UID)
}
if t.Order != "" {
db = db.Order(t.Order+" "+t.Sort, true)
}
err = db.Find(&res).Error
return
}
// TagControl .
func (d *Dao) TagControl(c context.Context, tc *model.TagControlParam) (res []*model.TagControl, err error) {
db := d.db.Table("manager_tag_control")
if tc.BID != 0 {
db = db.Where("bid = ?", tc.BID)
}
if tc.TID != 0 {
db = db.Where("tid = ?", tc.TID)
}
err = db.Find(&res).Error
return
}
// AttrList .
func (d *Dao) AttrList(c context.Context, bid int64) (res *model.TagBusinessAttr, err error) {
res = &model.TagBusinessAttr{}
err = d.db.Where("bid = ?", bid).First(res).Error
if err == ecode.NothingFound {
err = nil
}
return
}
// InsertAttr .
func (d *Dao) InsertAttr(c context.Context, tba *model.TagBusinessAttr) error {
return d.db.Create(tba).Error
}
// AttrUpdate .
func (d *Dao) AttrUpdate(c context.Context, tba *model.TagBusinessAttr) error {
return d.db.Table("manager_tag_business_attr").Where("bid = ?", tba.Bid).Update("button", tba.Button).Error
}
// TypeByIDs .
func (d *Dao) TypeByIDs(c context.Context, ids []int64) (res map[int64]*model.TagType, err error) {
t := []*model.TagType{}
res = make(map[int64]*model.TagType)
err = d.db.Where("id IN (?)", ids).Find(&t).Error
for _, r := range t {
res[r.ID] = r
}
return
}
// TagTypeByBID .
func (d *Dao) TagTypeByBID(c context.Context, bid int64) (res []*model.TagType, err error) {
err = d.db.Where("bid = ?", bid).Find(&res).Error
return
}
// TagTypeRoleByTids .
func (d *Dao) TagTypeRoleByTids(c context.Context, tids []int64) (res []*model.TagTypeRole, err error) {
err = d.db.Where("tid IN (?)", tids).Find(&res).Error
return
}
// TagByType accross type get tag list .
func (d *Dao) TagByType(c context.Context, tid int64) (res []*model.Tag, err error) {
err = d.db.Where("tid = ?", tid).Find(&res).Error
return
}

View File

@@ -0,0 +1,282 @@
package dao
import (
"context"
"math/rand"
"testing"
"go-common/app/admin/main/manager/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddType(t *testing.T) {
var (
c = context.TODO()
tt = &model.TagType{}
)
convey.Convey("AddType", t, func(ctx convey.C) {
err := d.AddType(c, tt)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoUpdateTypeName(t *testing.T) {
var (
c = context.TODO()
tt = &model.TagType{}
)
convey.Convey("UpdateTypeName", t, func(ctx convey.C) {
err := d.UpdateTypeName(c, tt)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoUpdateType(t *testing.T) {
var (
c = context.TODO()
tt = &model.TagType{}
)
convey.Convey("UpdateType", t, func(ctx convey.C) {
err := d.UpdateType(c, tt)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDeleteNonRole(t *testing.T) {
var (
c = context.TODO()
tt = &model.TagType{}
)
convey.Convey("DeleteNonRole", t, func(ctx convey.C) {
err := d.DeleteNonRole(c, tt)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDeleteType(t *testing.T) {
var (
c = context.TODO()
td = &model.TagTypeDel{}
)
convey.Convey("DeleteType", t, func(ctx convey.C) {
err := d.DeleteType(c, td)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoAddTag(t *testing.T) {
var (
c = context.TODO()
no = &model.Tag{}
)
convey.Convey("AddTag", t, func(ctx convey.C) {
err := d.AddTag(c, no)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaomaxTagIDByBid(t *testing.T) {
var (
c = context.TODO()
bid = int64(0)
)
convey.Convey("maxTagIDByBid", t, func(ctx convey.C) {
maxTagID, err := d.maxTagIDByBid(c, bid)
ctx.Convey("Then err should be nil.maxTagID should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(maxTagID, convey.ShouldNotBeNil)
})
})
}
func TestDaoUpdateTag(t *testing.T) {
var (
c = context.TODO()
no = &model.Tag{}
)
convey.Convey("UpdateTag", t, func(ctx convey.C) {
err := d.UpdateTag(c, no)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoAddControl(t *testing.T) {
var (
c = context.TODO()
tc = &model.TagControl{}
)
convey.Convey("AddControl", t, func(ctx convey.C) {
err := d.AddControl(c, tc)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoUpdateControl(t *testing.T) {
var (
c = context.TODO()
tc = &model.TagControl{}
)
convey.Convey("UpdateControl", t, func(ctx convey.C) {
err := d.UpdateControl(c, tc)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoBatchUpdateState(t *testing.T) {
var (
c = context.TODO()
b = &model.BatchUpdateState{}
)
convey.Convey("BatchUpdateState", t, func(ctx convey.C) {
err := d.BatchUpdateState(c, b)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoTagList(t *testing.T) {
var (
c = context.TODO()
no = &model.SearchTagParams{}
)
convey.Convey("TagList", t, func(ctx convey.C) {
res, err := d.TagList(c, no)
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 TestDaoTagControl(t *testing.T) {
var (
c = context.TODO()
tc = &model.TagControlParam{
BID: 1,
TID: 2,
}
)
convey.Convey("TagControl", t, func(ctx convey.C) {
res, err := d.TagControl(c, tc)
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 TestDaoAttrList(t *testing.T) {
var (
c = context.TODO()
bid = int64(0)
)
convey.Convey("AttrList", t, func(ctx convey.C) {
res, err := d.AttrList(c, bid)
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 TestDaoInsertAttr(t *testing.T) {
bid := rand.Intn(1000)
tba := &model.TagBusinessAttr{
Bid: int64(bid),
}
convey.Convey("InsertAttr", t, func(ctx convey.C) {
err := d.InsertAttr(context.Background(), tba)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoAttrUpdate(t *testing.T) {
var (
c = context.TODO()
tba = &model.TagBusinessAttr{}
)
convey.Convey("AttrUpdate", t, func(ctx convey.C) {
err := d.AttrUpdate(c, tba)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoTypeByIDs(t *testing.T) {
var (
c = context.TODO()
ids = []int64{}
)
convey.Convey("TypeByIDs", t, func(ctx convey.C) {
res, err := d.TypeByIDs(c, ids)
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 TestDaoTagTypeByBID(t *testing.T) {
var (
c = context.TODO()
bid = int64(0)
)
convey.Convey("TagTypeByBID", t, func(ctx convey.C) {
res, err := d.TagTypeByBID(c, bid)
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 TestDaoTagTypeRoleByTids(t *testing.T) {
var (
c = context.TODO()
tids = []int64{}
)
convey.Convey("TagTypeRoleByTids", t, func(ctx convey.C) {
res, err := d.TagTypeRoleByTids(c, tids)
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 TestDaoTagByType(t *testing.T) {
var (
c = context.TODO()
tid = int64(0)
)
convey.Convey("TagByType", t, func(ctx convey.C) {
res, err := d.TagByType(c, tid)
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)
})
})
}