Files
chis_osi/handler/health_record.go
T

90 lines
2.7 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"chis_osi/osi"
)
// HealthRecordHandler 暴露健康档案查询的 HTTP 入口。
type HealthRecordHandler struct {
client *osi.Client
}
func NewHealthRecordHandler(client *osi.Client) *HealthRecordHandler {
return &HealthRecordHandler{client: client}
}
// Find 处理 GET /api/health-record/find?idCard=..|phrid=..|personName=..|empiId=..
// 直接回写平台完整响应({code,message,data:[...]}),不做字段裁剪——查询响应可能含
// Go 结构体未建模的字段,回写原始 JSON 才能看到完整档案内容。
func (h *HealthRecordHandler) Find(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSONError(w, http.StatusMethodNotAllowed, "only GET is supported")
return
}
q := r.URL.Query()
query := osi.FindHealthRecordQuery{
IDCard: q.Get("idCard"),
PHRID: q.Get("phrid"),
PersonName: q.Get("personName"),
EMPIID: q.Get("empiId"),
}
if query.IDCard == "" && query.PHRID == "" && query.PersonName == "" && query.EMPIID == "" {
writeJSONError(w, http.StatusBadRequest, "provide one of: idCard, phrid, personName, empiId")
return
}
_, result, err := h.client.FindHealthRecord(r.Context(), query)
// 拿到平台响应就原样回写(成功或业务错误都含 code/message),便于查看完整内容。
if len(result.Raw) > 0 {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, _ = w.Write(result.Raw)
return
}
if err != nil {
writeJSONError(w, http.StatusBadGateway, err.Error())
return
}
writeJSONError(w, http.StatusBadGateway, "empty response from OSI")
}
// Crowd 处理 GET /api/health-record/crowd?idCard=..|phrid=..
// 原样回写 JKDA00005 人群分类与子档案标记响应。
func (h *HealthRecordHandler) Crowd(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSONError(w, http.StatusMethodNotAllowed, "only GET is supported")
return
}
q := r.URL.Query()
query := osi.FindRqbjQuery{
IDCard: q.Get("idCard"),
PHRID: q.Get("phrid"),
}
if query.IDCard == "" && query.PHRID == "" {
writeJSONError(w, http.StatusBadRequest, "provide one of: idCard, phrid")
return
}
_, result, err := h.client.FindRqbj(r.Context(), query)
if len(result.Raw) > 0 {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, _ = w.Write(result.Raw)
return
}
if err != nil {
writeJSONError(w, http.StatusBadGateway, err.Error())
return
}
writeJSONError(w, http.StatusBadGateway, "empty response from OSI")
}
func writeJSONError(w http.ResponseWriter, status int, msg string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": msg})
}