fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -21,6 +22,10 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func manualEntryTenantScope(ctx context.Context, db *gorm.DB, column string) *gorm.DB {
|
||||
return db.Where(column+" = ?", tenancy.IDFromContext(ctx))
|
||||
}
|
||||
|
||||
// FindAdminSnapshot 读取客服的昵称与登录名,用于录单时绑定快照。
|
||||
func (r *Repository) FindAdminSnapshot(ctx context.Context, adminID uint64) (name, username string, err error) {
|
||||
var admin model.AdminUser
|
||||
@@ -36,6 +41,7 @@ func (r *Repository) FindAdminSnapshot(ctx context.Context, adminID uint64) (nam
|
||||
// Create 写入一条录单记录。
|
||||
func (r *Repository) Create(ctx context.Context, adminID uint64, adminName, adminUsername, entryNo, entryGroup, platform, operator, remark string) (*EntryDTO, error) {
|
||||
row := model.CSManualEntry{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
EntryNo: entryNo,
|
||||
EntryGroup: entryGroup,
|
||||
Platform: platform,
|
||||
@@ -58,7 +64,7 @@ func (r *Repository) Create(ctx context.Context, adminID uint64, adminName, admi
|
||||
// FindByID 按 ID 查询单条录单记录。
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64) (*EntryDTO, error) {
|
||||
var row model.CSManualEntry
|
||||
if err := r.db.WithContext(ctx).First(&row, id).Error; err != nil {
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").First(&row, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrEntryNotFound
|
||||
}
|
||||
@@ -70,7 +76,7 @@ func (r *Repository) FindByID(ctx context.Context, id uint64) (*EntryDTO, error)
|
||||
|
||||
// Update 修正录单的编号、组别、平台、操作人与备注。同组内编号唯一冲突返回 ErrEntryNoExists。
|
||||
func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup, platform, operator, remark string) (*EntryDTO, error) {
|
||||
result := r.db.WithContext(ctx).Model(&model.CSManualEntry{}).
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id").
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{
|
||||
"entry_no": entryNo,
|
||||
@@ -87,7 +93,8 @@ func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup,
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.CSManualEntry{}).Where("id = ?", id).Count(&count).Error; err != nil {
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id").
|
||||
Where("id = ?", id).Count(&count).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
@@ -99,7 +106,8 @@ func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup,
|
||||
|
||||
// Delete 删除一条录单记录。
|
||||
func (r *Repository) Delete(ctx context.Context, id uint64) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.CSManualEntry{}, id)
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").
|
||||
Delete(&model.CSManualEntry{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -111,8 +119,8 @@ func (r *Repository) Delete(ctx context.Context, id uint64) error {
|
||||
|
||||
// List 按条件分页查询录单记录。
|
||||
func (r *Repository) List(ctx context.Context, query ListQuery) (*PaginatedResult, error) {
|
||||
db := r.db.WithContext(ctx).Model(&model.CSManualEntry{})
|
||||
countDB := r.db.WithContext(ctx).Model(&model.CSManualEntry{})
|
||||
db := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id")
|
||||
countDB := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id")
|
||||
|
||||
// 编号查询:精确匹配该编号,并同时展示其带后缀的变体(如 202611060124-0、202611060124/1)。
|
||||
if query.EntryNo != "" {
|
||||
@@ -189,7 +197,7 @@ func (r *Repository) TodayAgentStats(ctx context.Context, day time.Time) ([]Agen
|
||||
SellCount int64
|
||||
}
|
||||
rows := make([]row, 0)
|
||||
if err := r.db.WithContext(ctx).Table("cs_manual_entries").
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Table("cs_manual_entries"), "tenant_id").
|
||||
Select(`admin_id, MAX(admin_name) AS admin_name, COUNT(*) AS count,
|
||||
SUM(CASE WHEN entry_group = 'collect' THEN 1 ELSE 0 END) AS collect_count,
|
||||
SUM(CASE WHEN entry_group = 'sell' THEN 1 ELSE 0 END) AS sell_count`).
|
||||
@@ -225,7 +233,7 @@ func (r *Repository) ListOptions(ctx context.Context, optionType string) ([]Opti
|
||||
return nil, ErrOptionTypeInvalid
|
||||
}
|
||||
rows := make([]model.CSManualEntryOption, 0)
|
||||
if err := r.db.WithContext(ctx).
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").
|
||||
Where("type = ?", optionType).
|
||||
Order("sort_order ASC, id ASC").
|
||||
Find(&rows).Error; err != nil {
|
||||
@@ -265,7 +273,12 @@ func (r *Repository) CreateOption(ctx context.Context, optionType, name, groupTa
|
||||
if !IsValidOptionType(optionType) {
|
||||
return nil, ErrOptionTypeInvalid
|
||||
}
|
||||
row := model.CSManualEntryOption{Type: optionType, Name: name, GroupTag: groupTag}
|
||||
row := model.CSManualEntryOption{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
Type: optionType,
|
||||
Name: name,
|
||||
GroupTag: groupTag,
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Create(&row).Error; err != nil {
|
||||
if isDuplicateEntry(err) {
|
||||
return nil, ErrOptionExists
|
||||
@@ -279,13 +292,13 @@ func (r *Repository) CreateOption(ctx context.Context, optionType, name, groupTa
|
||||
// UpdateOption 重命名一个选项并可更新组别标记,重名返回 ErrOptionExists。
|
||||
func (r *Repository) UpdateOption(ctx context.Context, id uint64, name, groupTag string) (*OptionDTO, error) {
|
||||
var row model.CSManualEntryOption
|
||||
if err := r.db.WithContext(ctx).First(&row, id).Error; err != nil {
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").First(&row, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrOptionNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
result := r.db.WithContext(ctx).Model(&model.CSManualEntryOption{}).
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntryOption{}), "tenant_id").
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{
|
||||
"name": name,
|
||||
@@ -305,7 +318,8 @@ func (r *Repository) UpdateOption(ctx context.Context, id uint64, name, groupTag
|
||||
|
||||
// DeleteOption 删除一个选项(历史录单里的名称快照不受影响)。
|
||||
func (r *Repository) DeleteOption(ctx context.Context, id uint64) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.CSManualEntryOption{}, id)
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").
|
||||
Delete(&model.CSManualEntryOption{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -340,6 +354,7 @@ func isDuplicateEntry(err error) bool {
|
||||
func toDTO(row model.CSManualEntry) EntryDTO {
|
||||
return EntryDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
EntryNo: row.EntryNo,
|
||||
EntryGroup: row.EntryGroup,
|
||||
Platform: row.Platform,
|
||||
@@ -355,6 +370,7 @@ func toDTO(row model.CSManualEntry) EntryDTO {
|
||||
func toOptionDTO(row model.CSManualEntryOption) OptionDTO {
|
||||
return OptionDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
Type: row.Type,
|
||||
Name: row.Name,
|
||||
GroupTag: row.GroupTag,
|
||||
|
||||
Reference in New Issue
Block a user