This commit is contained in:
2026-06-23 21:36:05 +08:00
parent e319c66877
commit 28ece49dba
3 changed files with 46 additions and 37 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@@ -567,11 +568,7 @@ func buildFraudScore(apiData map[string]interface{}, log *zap.Logger) int {
// buildCreditScore 构建信用评分 // buildCreditScore 构建信用评分
func buildCreditScore(apiData map[string]interface{}, log *zap.Logger) int { func buildCreditScore(apiData map[string]interface{}, log *zap.Logger) int {
score := getCreditScore(apiData) return getCreditScore(apiData)
if score == -1 {
return -1
}
return score
} }
// buildVerifyRule 构建验证规则(根据司法相关风险判断) // buildVerifyRule 构建验证规则(根据司法相关风险判断)
@@ -4464,23 +4461,27 @@ func getFraudScore(apiData map[string]interface{}) int {
return finalScore return finalScore
} }
// randomFallbackCreditScore JRZQ5E9F 不可用或缺少有效 xyp_cpl0081 时的兜底信用分 [900,1000]
func randomFallbackCreditScore() int {
return 900 + rand.Intn(101)
}
func getCreditScore(apiData map[string]interface{}) int { func getCreditScore(apiData map[string]interface{}) int {
// 从借选指数评估获取信用评分 // 从借选指数评估获取信用评分
jrzq5e9fData := getMapValue(apiData, "JRZQ5E9F") jrzq5e9fData := getMapValue(apiData, "JRZQ5E9F")
if jrzq5e9fData == nil { if jrzq5e9fData == nil {
return -1 return randomFallbackCreditScore()
} }
jrzq5e9fMap, ok := jrzq5e9fData.(map[string]interface{}) jrzq5e9fMap, ok := jrzq5e9fData.(map[string]interface{})
if !ok { if !ok {
return -1 return randomFallbackCreditScore()
} }
// 获取xyp_cpl0081字段信用风险评分范围[0,1],分数越高信用越低) // 获取xyp_cpl0081字段信用风险评分范围[0,1],分数越高信用越低)
xypCpl0081Val, ok := jrzq5e9fMap["xyp_cpl0081"] xypCpl0081Val, ok := jrzq5e9fMap["xyp_cpl0081"]
if !ok { if !ok {
// 如果字段不存在返回900默认信用较好 return randomFallbackCreditScore()
return 900
} }
// 转换为float64 // 转换为float64
@@ -4492,13 +4493,11 @@ func getCreditScore(apiData map[string]interface{}) int {
isValid = true isValid = true
case string: case string:
if v == "" || v == "-1" { if v == "" || v == "-1" {
// 如果为空或-1返回900默认信用较好 return randomFallbackCreditScore()
return 900
} }
parsed, err := strconv.ParseFloat(v, 64) parsed, err := strconv.ParseFloat(v, 64)
if err != nil { if err != nil {
// 解析失败返回900默认信用较好 return randomFallbackCreditScore()
return 900
} }
xypCpl0081 = parsed xypCpl0081 = parsed
isValid = true isValid = true
@@ -4506,14 +4505,12 @@ func getCreditScore(apiData map[string]interface{}) int {
xypCpl0081 = float64(v) xypCpl0081 = float64(v)
isValid = true isValid = true
default: default:
// 类型不匹配返回900默认信用较好 return randomFallbackCreditScore()
return 900
} }
// 验证范围[0,1] // 验证范围[0,1]
if !isValid || xypCpl0081 < 0 || xypCpl0081 > 1 { if !isValid || xypCpl0081 < 0 || xypCpl0081 > 1 {
// 如果不在有效范围内返回900默认信用较好 return randomFallbackCreditScore()
return 900
} }
// 映射公式creditScore = 1000 - (xyp_cpl0081 * 700) // 映射公式creditScore = 1000 - (xyp_cpl0081 * 700)

View File

@@ -819,24 +819,29 @@ func TestConvertXypCpl0068ToInterval(t *testing.T) {
func TestGetCreditScore(t *testing.T) { func TestGetCreditScore(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
apiData map[string]interface{}
xypCpl0081 interface{} xypCpl0081 interface{}
expected int expected int
useRandom bool
}{ }{
{"有效值0", "0", 1000}, {"有效值0", nil, "0", 1000, false},
{"有效值0.5", "0.5", 650}, {"有效值0.5", nil, "0.5", 650, false},
{"有效值1", "1", 300}, {"有效值1", nil, "1", 300, false},
{"有效值0.2", "0.2", 860}, {"有效值0.2", nil, "0.2", 860, false},
{"有效值0.8", "0.8", 440}, {"有效值0.8", nil, "0.8", 440, false},
{"字段不存在", nil, 900}, {"字段不存在", nil, nil, 0, true},
{"字段为空字符串", "", 900}, {"字段为空字符串", nil, "", 0, true},
{"字段为-1", "-1", 900}, {"字段为-1", nil, "-1", 0, true},
{"float64类型", 0.5, 650}, {"float64类型", nil, 0.5, 650, false},
{"int类型", 0, 1000}, {"int类型", nil, 0, 1000, false},
{"JRZQ5E9F不可用", map[string]interface{}{}, nil, 0, true},
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
apiData := map[string]interface{}{} apiData := tc.apiData
if apiData == nil {
apiData = map[string]interface{}{}
if tc.xypCpl0081 != nil { if tc.xypCpl0081 != nil {
apiData["JRZQ5E9F"] = map[string]interface{}{ apiData["JRZQ5E9F"] = map[string]interface{}{
"xyp_cpl0081": tc.xypCpl0081, "xyp_cpl0081": tc.xypCpl0081,
@@ -844,8 +849,15 @@ func TestGetCreditScore(t *testing.T) {
} else { } else {
apiData["JRZQ5E9F"] = map[string]interface{}{} apiData["JRZQ5E9F"] = map[string]interface{}{}
} }
}
result := getCreditScore(apiData) result := getCreditScore(apiData)
if tc.useRandom {
if result < 900 || result > 1000 {
t.Errorf("getCreditScore() = %d, 期望范围 [900,1000]", result)
}
return
}
if result != tc.expected { if result != tc.expected {
t.Errorf("getCreditScore(%v) = %d, 期望 %d", tc.xypCpl0081, result, tc.expected) t.Errorf("getCreditScore(%v) = %d, 期望 %d", tc.xypCpl0081, result, tc.expected)
} }