fix: 补齐渠道全量合计筛选导出及有效端目标遗漏
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"kra/internal/biz/business"
|
||||
@@ -18,11 +19,13 @@ type channelPO struct {
|
||||
ParentID, TopPromoteID, BusierID uint64
|
||||
CreateTime *int64
|
||||
LastLoginTime *int64 `gorm:"column:last_login_time"`
|
||||
MobilePhone string
|
||||
SettlementDayPeriod *int
|
||||
}
|
||||
|
||||
func (channelPO) TableName() string { return "tab_promote" }
|
||||
func toBizChannel(p channelPO) *business.Channel {
|
||||
return &business.Channel{ChannelKey: business.ChannelKey{SourceDB: p.SourceDB, ID: p.ID}, Account: p.Account, Nickname: p.Nickname, ParentName: p.ParentName, RealName: p.RealName, Level: p.PromoteLevel, RegisterType: p.RegisterType, Status: p.Status, LockStatus: p.LockStatus, ParentID: p.ParentID, TopPromoteID: p.TopPromoteID, BusierID: p.BusierID, CreatedUnix: p.CreateTime, LastLoginUnix: p.LastLoginTime, QualifiedAt: p.QualifiedAt}
|
||||
return &business.Channel{ChannelKey: business.ChannelKey{SourceDB: p.SourceDB, ID: p.ID}, Account: p.Account, Nickname: p.Nickname, ParentName: p.ParentName, RealName: p.RealName, Level: p.PromoteLevel, RegisterType: p.RegisterType, Status: p.Status, LockStatus: p.LockStatus, ParentID: p.ParentID, TopPromoteID: p.TopPromoteID, BusierID: p.BusierID, CreatedUnix: p.CreateTime, LastLoginUnix: p.LastLoginTime, QualifiedAt: p.QualifiedAt, Phone: p.MobilePhone, SettlementDays: p.SettlementDayPeriod}
|
||||
}
|
||||
|
||||
type channelRepo struct{ data Provider }
|
||||
@@ -58,7 +61,7 @@ func (r *channelRepo) Bindings(ctx context.Context, scope business.UserScope, so
|
||||
}
|
||||
func (r *channelRepo) List(ctx context.Context, scope business.UserScope, filter business.ChannelFilter, options ...business.ListOption) (*business.ChannelPage, error) {
|
||||
db := r.data.DB().WithContext(ctx)
|
||||
page := &business.ChannelPage{Rows: []*business.Channel{}, Available: true}
|
||||
page := &business.ChannelPage{Rows: []*business.Channel{}, Available: true, StatusCounts: &business.ChannelStatusCounts{}}
|
||||
bindings := channelBindingQuery(db, scope, filter)
|
||||
constrain := !scope.All || filter.BusierUserID != nil || filter.BusierAccount != ""
|
||||
if constrain {
|
||||
@@ -89,6 +92,26 @@ func (r *channelRepo) List(ctx context.Context, scope business.UserScope, filter
|
||||
keyword := strings.ToLower(strings.TrimSpace(filter.Keyword))
|
||||
q = q.Where("INSTR(LOWER(p.account),?)>0 OR INSTR(LOWER(p.nickname),?)>0 OR INSTR(LOWER(p.real_name),?)>0", keyword, keyword, keyword)
|
||||
}
|
||||
if filter.Phone != "" {
|
||||
q = q.Where("INSTR(p.mobile_phone,?) > 0", filter.Phone)
|
||||
}
|
||||
if filter.SettlementDays != nil {
|
||||
q = q.Where("p.settlement_day_period = ?", *filter.SettlementDays)
|
||||
}
|
||||
for _, period := range []struct {
|
||||
column string
|
||||
begin, end *time.Time
|
||||
}{
|
||||
{"p.create_time", filter.CreatedBegin, filter.CreatedEnd},
|
||||
{"p.last_login_time", filter.LoginBegin, filter.LoginEnd},
|
||||
} {
|
||||
if period.begin != nil {
|
||||
q = q.Where(period.column+" >= ?", period.begin.Unix())
|
||||
}
|
||||
if period.end != nil {
|
||||
q = q.Where(period.column+" < ?", period.end.AddDate(0, 0, 1).Unix())
|
||||
}
|
||||
}
|
||||
for _, field := range []struct {
|
||||
column string
|
||||
value *int
|
||||
@@ -105,14 +128,21 @@ func (r *channelRepo) List(ctx context.Context, scope business.UserScope, filter
|
||||
q = q.Where("e.qualified_at IS NULL")
|
||||
}
|
||||
}
|
||||
if err := q.Count(&page.Total).Error; err != nil {
|
||||
var counts struct{ Total, Normal, Locked, Terminated int64 }
|
||||
if err := q.Session(&gorm.Session{}).Select("COUNT(*) AS total,COALESCE(SUM(CASE WHEN COALESCE(p.lock_status,0)=0 THEN 1 ELSE 0 END),0) AS normal,COALESCE(SUM(CASE WHEN COALESCE(p.lock_status,0) NOT IN (0,-2) THEN 1 ELSE 0 END),0) AS locked,COALESCE(SUM(CASE WHEN p.lock_status=-2 THEN 1 ELSE 0 END),0) AS terminated").Scan(&counts).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
page.Total = counts.Total
|
||||
page.StatusCounts = &business.ChannelStatusCounts{Normal: counts.Normal, Locked: counts.Locked, Terminated: counts.Terminated}
|
||||
var pos []channelPO
|
||||
o := business.NewListOptions(options...)
|
||||
keysQuery := q.Select("p.id,p.source_db").Order("p.create_time DESC,p.id DESC,p.source_db ASC").Offset(o.Offset).Limit(o.Limit)
|
||||
order := "p.create_time DESC,p.id DESC,p.source_db ASC"
|
||||
if filter.SortField == "createdAt" && filter.Ascending {
|
||||
order = "p.create_time ASC,p.id ASC,p.source_db ASC"
|
||||
}
|
||||
keysQuery := q.Select("p.id,p.source_db").Order(order).Offset(o.Offset).Limit(o.Limit)
|
||||
pageQuery := joinEffectiveness(db.Table("tab_promote p").Joins("JOIN (?) page_ids ON page_ids.id=p.id AND page_ids.source_db=p.source_db", keysQuery))
|
||||
if err := pageQuery.Select("p.id,p.source_db,p.account,p.nickname,p.parent_name,p.real_name,p.promote_level,p.register_type,p.status,p.lock_status,p.parent_id,p.top_promote_id,p.busier_id,p.create_time,e.qualified_at").Order("p.create_time DESC,p.id DESC,p.source_db ASC").Scan(&pos).Error; err != nil {
|
||||
if err := pageQuery.Select("p.*,e.qualified_at").Order(order).Scan(&pos).Error; err != nil {
|
||||
return nil, business.ErrStorage
|
||||
}
|
||||
if len(pos) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user