修复气站用户配送点归属展示
This commit is contained in:
@@ -11,19 +11,30 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// gasUserListItem 在用户主档上附加当前气站服务关系中的配送点归属。
|
||||
type gasUserListItem struct {
|
||||
models.UserAccount
|
||||
DeliveryBasicID uint64 `gorm:"column:delivery_basic_id" json:"delivery_basic_id"`
|
||||
}
|
||||
|
||||
// scopedUserList 将用户列表与当前气站唯一有效服务关系绑定,避免返回跨站归属。
|
||||
func scopedUserList(databaseService *gorm.DB, gasBasicID uint64) *gorm.DB {
|
||||
return common.ActiveRecords(databaseService.Model(&models.UserAccount{})).
|
||||
Select("user_account.*, user_service_relation.delivery_basic_id AS delivery_basic_id").
|
||||
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = user_account.id AND user_service_relation.status <> ?", common.StatusArchived).
|
||||
Where("user_service_relation.gas_basic_id = ?", gasBasicID)
|
||||
}
|
||||
|
||||
func ListUser(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, size := common.PageSize(ctx)
|
||||
var list []models.UserAccount
|
||||
var list []gasUserListItem
|
||||
var total int64
|
||||
query := common.ApplyKeywordFilter(ctx,
|
||||
common.ActiveRecords(impl.DBService.Model(&models.UserAccount{})).
|
||||
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = user_account.id AND user_service_relation.status <> ?",
|
||||
common.StatusArchived).
|
||||
Where("user_service_relation.gas_basic_id = ?", station.ID),
|
||||
scopedUserList(impl.DBService, station.ID),
|
||||
&models.UserAccount{})
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
|
||||
32
backend/api/internal/logic/gas/user_test.go
Normal file
32
backend/api/internal/logic/gas/user_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package gas
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestScopedUserListIncludesDeliveryRelation 验证用户列表带出当前气站服务关系的配送点主键。
|
||||
func TestScopedUserListIncludesDeliveryRelation(t *testing.T) {
|
||||
connection, _, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create SQL mock: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = connection.Close() })
|
||||
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
|
||||
if err != nil {
|
||||
t.Fatalf("open GORM database: %v", err)
|
||||
}
|
||||
|
||||
statement := scopedUserList(databaseService, 42).
|
||||
Find(&[]gasUserListItem{}).Statement.SQL.String()
|
||||
if !strings.Contains(statement, "user_service_relation.delivery_basic_id AS delivery_basic_id") {
|
||||
t.Fatalf("user list must select delivery relation, got: %s", statement)
|
||||
}
|
||||
if !strings.Contains(statement, "user_service_relation.gas_basic_id =") {
|
||||
t.Fatalf("user list must keep gas scope, got: %s", statement)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user