优化配送订单合同气瓶候选显示
This commit is contained in:
@@ -62,9 +62,32 @@ func filterGasorderContractCandidates(query *gorm.DB, candidate string, now time
|
||||
)
|
||||
}
|
||||
func GetGasorderContract(ctx *gin.Context) { getGasorderContract(ctx) }
|
||||
|
||||
// ListGasorderContractProduct 在订单候选场景按合同收窄未解绑气瓶,未传合同时保留完整历史列表。
|
||||
func ListGasorderContractProduct(ctx *gin.Context) {
|
||||
common.ListResource(ctx, &models.GasorderContractProduct{})
|
||||
contractIdentity := strings.TrimSpace(ctx.Query("contract_identity"))
|
||||
if contractIdentity == "" {
|
||||
common.ListResource(ctx, &models.GasorderContractProduct{})
|
||||
return
|
||||
}
|
||||
contractID, err := common.ResolveIdentityID(&models.GasorderContract{}, contractIdentity, true)
|
||||
if err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
common.ListPageFiltered[models.GasorderContractProduct](ctx, func(query *gorm.DB) *gorm.DB {
|
||||
return filterGasorderContractProductCandidates(query, contractID)
|
||||
})
|
||||
}
|
||||
|
||||
// filterGasorderContractProductCandidates 仅保留当前合同仍有效绑定的气瓶,并按类型和编码排序。
|
||||
func filterGasorderContractProductCandidates(query *gorm.DB, contractID uint64) *gorm.DB {
|
||||
return query.
|
||||
Where("gasorder_contract_id = ? AND unbound_at IS NULL", contractID).
|
||||
Order("product_type_name asc").
|
||||
Order("product_code asc")
|
||||
}
|
||||
|
||||
func GetGasorderContractProduct(ctx *gin.Context) {
|
||||
common.GetResource(ctx, &models.GasorderContractProduct{})
|
||||
}
|
||||
|
||||
@@ -71,6 +71,34 @@ func TestFilterGasorderContractCandidates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestFilterGasorderContractProductCandidates 验证订单候选仅包含当前合同未解绑气瓶并按业务字段排序。
|
||||
func TestFilterGasorderContractProductCandidates(t *testing.T) {
|
||||
sqlDatabase, _, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("创建模拟数据库失败:%v", err)
|
||||
}
|
||||
defer sqlDatabase.Close()
|
||||
database, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDatabase}), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("创建 GORM 数据库失败:%v", err)
|
||||
}
|
||||
statement := database.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||
var products []models.GasorderContractProduct
|
||||
return filterGasorderContractProductCandidates(
|
||||
tx.Model(&models.GasorderContractProduct{}), 27,
|
||||
).Find(&products)
|
||||
})
|
||||
for _, fragment := range []string{
|
||||
"gasorder_contract_id = 27",
|
||||
"unbound_at IS NULL",
|
||||
"ORDER BY product_type_name asc,product_code asc",
|
||||
} {
|
||||
if !strings.Contains(statement, fragment) {
|
||||
t.Fatalf("合同气瓶候选缺少 %q:%s", fragment, statement)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGasorderCreatorTypesCoverEveryConfirmedOrigin(t *testing.T) {
|
||||
for _, creatorType := range []string{"user", "staff", "delivery", "gas"} {
|
||||
if gasorderCreatorModels[creatorType] == nil {
|
||||
|
||||
Reference in New Issue
Block a user