优化配送订单合同标题显示
This commit is contained in:
@@ -158,8 +158,17 @@ func getGasorderBasic(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
}
|
||||
contractNames, err := loadGasorderContractNames([]models.GasorderBasic{order})
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
displayOrder := gasorderDetailDisplay{
|
||||
GasorderBasic: order,
|
||||
ContractDisplayName: contractDisplayName(contractNames[order.GasorderContractID]),
|
||||
}
|
||||
response, err := common.PublicResourceResponse(gin.H{
|
||||
"order": order, "items": items, "assignments": assignments, "statuses": statuses,
|
||||
"order": displayOrder, "items": items, "assignments": assignments, "statuses": statuses,
|
||||
"tracks": tracks, "confirmations": confirmations, "payments": payments,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -17,9 +17,16 @@ import (
|
||||
type gasorderListDisplay struct {
|
||||
models.GasorderBasic
|
||||
CreatorDisplayName string `json:"creator_display_name"`
|
||||
ContractDisplayName string `json:"contract_display_name"`
|
||||
ContractProductsSummary string `json:"contract_products_summary"`
|
||||
}
|
||||
|
||||
// gasorderDetailDisplay 为订单详情补充合同当前标题和编号,不改变订单持久化快照。
|
||||
type gasorderDetailDisplay struct {
|
||||
models.GasorderBasic
|
||||
ContractDisplayName string `json:"contract_display_name"`
|
||||
}
|
||||
|
||||
// ListGasorderBasic 返回平台订单分页列表,并补充只用于管理端展示的可读摘要。
|
||||
func ListGasorderBasic(ctx *gin.Context) {
|
||||
page, size := common.PageSize(ctx)
|
||||
@@ -58,6 +65,10 @@ func buildGasorderListDisplays(orders []models.GasorderBasic) ([]gasorderListDis
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contractNames, err := loadGasorderContractNames(orders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
displays := make([]gasorderListDisplay, 0, len(orders))
|
||||
for _, order := range orders {
|
||||
creatorName := strings.TrimSpace(creatorNames[creatorLookupKey(order.CreatorType, order.CreatorID)])
|
||||
@@ -67,12 +78,49 @@ func buildGasorderListDisplays(orders []models.GasorderBasic) ([]gasorderListDis
|
||||
displays = append(displays, gasorderListDisplay{
|
||||
GasorderBasic: order,
|
||||
CreatorDisplayName: creatorName,
|
||||
ContractDisplayName: contractDisplayName(contractNames[order.GasorderContractID]),
|
||||
ContractProductsSummary: productSummaries[order.ID],
|
||||
})
|
||||
}
|
||||
return displays, nil
|
||||
}
|
||||
|
||||
// loadGasorderContractNames 批量读取订单关联合同的当前标题与唯一合同编号。
|
||||
func loadGasorderContractNames(orders []models.GasorderBasic) (map[uint64]models.GasorderContract, error) {
|
||||
ids := make([]uint64, 0, len(orders))
|
||||
for _, order := range orders {
|
||||
ids = append(ids, order.GasorderContractID)
|
||||
}
|
||||
contracts := map[uint64]models.GasorderContract{}
|
||||
if len(ids) == 0 {
|
||||
return contracts, nil
|
||||
}
|
||||
var records []models.GasorderContract
|
||||
if err := common.ActiveRecords(impl.DBService).Where("id IN ? AND status <> ?", ids, common.StatusArchived).Find(&records).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, contract := range records {
|
||||
contracts[contract.ID] = contract
|
||||
}
|
||||
return contracts, nil
|
||||
}
|
||||
|
||||
// contractDisplayName 组合可读标题和唯一合同编号,关联失效时返回明确文案。
|
||||
func contractDisplayName(contract models.GasorderContract) string {
|
||||
title := strings.TrimSpace(contract.Title)
|
||||
contractNo := strings.TrimSpace(contract.ContractNo)
|
||||
if contract.ID == 0 {
|
||||
return "记录已失效"
|
||||
}
|
||||
if title == "" {
|
||||
return firstReadableName(contractNo, "未命名合同")
|
||||
}
|
||||
if contractNo == "" {
|
||||
return title
|
||||
}
|
||||
return fmt.Sprintf("%s(%s)", title, contractNo)
|
||||
}
|
||||
|
||||
func creatorLookupKey(creatorType string, creatorID uint64) string {
|
||||
return fmt.Sprintf("%s:%d", creatorType, creatorID)
|
||||
}
|
||||
|
||||
@@ -36,3 +36,23 @@ func TestRestoreGasorderListAddresses(t *testing.T) {
|
||||
t.Fatalf("空地址应保持为空,实际为 %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestContractDisplayName 验证合同标题、编号和失效记录使用稳定可读文案。
|
||||
func TestContractDisplayName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
contract models.GasorderContract
|
||||
want string
|
||||
}{
|
||||
{"标题和编号", models.GasorderContract{Entity: models.Entity{ID: 1}, Title: "居民供气合同", ContractNo: "HT-001"}, "居民供气合同(HT-001)"},
|
||||
{"仅编号", models.GasorderContract{Entity: models.Entity{ID: 2}, ContractNo: "HT-002"}, "HT-002"},
|
||||
{"失效记录", models.GasorderContract{}, "记录已失效"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := contractDisplayName(test.contract); got != test.want {
|
||||
t.Fatalf("合同展示名称 = %q,期望 %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user