MarketingSystemDataExportTool/server/internal/api/fields.go

63 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"database/sql"
"net/http"
"sort"
)
// FieldsHandler 返回指定数据源的所有表字段,包含 hidden 默认值(全部 true可前端精确控制显示/隐藏)
func FieldsHandler(marketing, ymt *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ds := r.URL.Query().Get("datasource")
db := marketing
if ds == "ymt" {
db = ymt
}
hiddenDefaults := getHiddenFieldsFromCode(ds)
var tables []string
if ds == "ymt" {
tables = []string{"order_info", "order_cash", "order_voucher", "order_digit", "goods_voucher_batch", "goods_voucher_subject_config", "merchant", "activity"}
} else {
tables = []string{"order", "order_detail", "order_cash", "order_voucher", "plan", "key_batch", "code_batch", "voucher", "voucher_batch", "merchant_key_send"}
}
out := []map[string]interface{}{}
for _, tbl := range tables {
cols := getColumns(db, tbl)
fields := []map[string]interface{}{}
for _, c := range cols {
tCanonical, fCanonical := canonicalField(ds, tbl, c.Name)
if tCanonical == "" || fCanonical == "" {
continue
}
label := c.Comment
if label == "" {
label = fCanonical
}
hidden := isFieldHidden(hiddenDefaults, tCanonical, fCanonical)
fields = append(fields, map[string]interface{}{
"key": tCanonical + "." + fCanonical,
"field": fCanonical,
"label": label,
"hidden": hidden,
})
}
tDisplay := displayTable(ds, tbl)
out = append(out, map[string]interface{}{
"table": tDisplay,
"label": tableLabel(tDisplay),
"fields": fields,
})
}
sort.Slice(out, func(i, j int) bool { return out[i]["table"].(string) < out[j]["table"].(string) })
ok(w, r, map[string]interface{}{
"datasource": ds,
"tables": out,
})
})
}