feat: 迁移商务旗下渠道查询和抽屉
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package business
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
"kra/internal/biz/business"
|
||||
)
|
||||
|
||||
type businessPromoteRepo struct{ data Provider }
|
||||
|
||||
func NewBusinessPromoteRepo(data Provider) business.BusinessPromoteRepo {
|
||||
return &businessPromoteRepo{data: data}
|
||||
}
|
||||
|
||||
type directPromotePO struct {
|
||||
ID uint64
|
||||
Account string
|
||||
SourceDB string `gorm:"column:source_db"`
|
||||
BusierID uint64
|
||||
}
|
||||
|
||||
func (r *businessPromoteRepo) ListDirect(ctx context.Context, filter business.BusinessPromoteFilter, options ...business.ListOption) (*business.BusinessPromotePage, error) {
|
||||
page := &business.BusinessPromotePage{Rows: []*business.BusinessPromote{}, Available: true}
|
||||
db := r.data.DB().WithContext(ctx)
|
||||
bindings := func() *gorm.DB {
|
||||
q := db.Table("sys_user_bind_platform b").Where("b.user_id = ? AND b.status = ?", filter.UserID, "0")
|
||||
if filter.SourceDB != "" {
|
||||
q = q.Where("b.source_db = ?", filter.SourceDB)
|
||||
}
|
||||
if filter.BusierID != 0 {
|
||||
q = q.Where("b.platform_busier_id = ?", filter.BusierID)
|
||||
}
|
||||
return q
|
||||
}
|
||||
var count int64
|
||||
if err := bindings().Count(&count).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
if count == 0 {
|
||||
return page, nil
|
||||
}
|
||||
available, err := businessTableAvailable(db, "tab_promote")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !available {
|
||||
page.Available = false
|
||||
return page, nil
|
||||
}
|
||||
q := db.Table("tab_promote p").Where("p.promote_level = ?", 1).Where("EXISTS (?)", bindings().Select("1").Where("b.source_db = p.source_db AND b.platform_busier_id = p.busier_id"))
|
||||
if err := q.Count(&page.Total).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
o := business.NewListOptions(options...)
|
||||
var pos []directPromotePO
|
||||
if err := q.Select("p.id,p.account,p.source_db,p.busier_id").Order("p.id DESC,p.source_db ASC").Offset(o.Offset).Limit(o.Limit).Scan(&pos).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
for _, po := range pos {
|
||||
page.Rows = append(page.Rows, &business.BusinessPromote{ID: po.ID, Account: po.Account, SourceDB: po.SourceDB, BusierID: po.BusierID})
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package business
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kra/internal/biz/business"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDirectPromotesUseApprovedBindingPairsAndStablePaging(t *testing.T) {
|
||||
db := prepareAccounts(t)
|
||||
for _, sql := range []string{
|
||||
"ALTER TABLE tab_promote ADD COLUMN account TEXT",
|
||||
"INSERT INTO sys_user_bind_platform(user_id,source_db,platform_busier_id,status) VALUES(7,'lx',10,'0'),(7,'hw',10,'0'),(7,'yq',10,'3'),(8,'lx',20,'0')",
|
||||
"INSERT INTO tab_promote(id,source_db,busier_id,promote_level,account) VALUES(1,'lx',10,1,'a'),(2,'lx',10,2,'sub'),(1,'hw',10,1,'b'),(3,'yq',10,1,'pending'),(4,'lx',20,1,'other'),(5,'lx',10,1,'c')",
|
||||
} {
|
||||
if err := db.Exec(sql).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
r := NewBusinessPromoteRepo(testData{db})
|
||||
ctx := context.Background()
|
||||
page, err := r.ListDirect(ctx, business.BusinessPromoteFilter{UserID: 7})
|
||||
if err != nil || !page.Available || page.Total != 3 || len(page.Rows) != 3 {
|
||||
t.Fatalf("直属渠道范围错误: %+v %v", page, err)
|
||||
}
|
||||
if page.Rows[0].ID != 5 || page.Rows[1].SourceDB != "hw" {
|
||||
t.Fatal("同号跨平台分页未使用稳定排序")
|
||||
}
|
||||
page, err = r.ListDirect(ctx, business.BusinessPromoteFilter{UserID: 7, SourceDB: "hw", BusierID: 10})
|
||||
if err != nil || page.Total != 1 || page.Rows[0].Account != "b" {
|
||||
t.Fatal("平台与商务筛选未同时生效")
|
||||
}
|
||||
page, err = r.ListDirect(ctx, business.BusinessPromoteFilter{UserID: 7}, business.ListOffset(1), business.ListLimit(1))
|
||||
if err != nil || page.Total != 3 || len(page.Rows) != 1 || page.Rows[0].SourceDB != "hw" {
|
||||
t.Fatal("分页或总数不正确")
|
||||
}
|
||||
if err := db.Exec("DROP TABLE tab_promote").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page, err = r.ListDirect(ctx, business.BusinessPromoteFilter{UserID: 7})
|
||||
if err != nil || page.Available {
|
||||
t.Fatal("未接入的渠道数据不能返回正常空列表")
|
||||
}
|
||||
page, err = r.ListDirect(ctx, business.BusinessPromoteFilter{UserID: 999})
|
||||
if err != nil || !page.Available || page.Total != 0 {
|
||||
t.Fatal("无绑定用户的直属渠道应为确定的空集合")
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ var ProviderSet = wire.NewSet(
|
||||
databusiness.NewBindingRepo,
|
||||
databusiness.NewPlatformAccountRepo,
|
||||
databusiness.NewPasswordResetRepo,
|
||||
databusiness.NewBusinessPromoteRepo,
|
||||
NewIntegrationRuntime,
|
||||
wire.Bind(new(datasystem.Provider), new(*Data)),
|
||||
wire.Bind(new(datasystem.DatabaseProvider), new(*Data)),
|
||||
|
||||
Reference in New Issue
Block a user