This commit is contained in:
2026-01-13 18:30:10 +08:00
parent 0206710e29
commit 04df8460a4
29 changed files with 2472 additions and 111 deletions

View File

@@ -32,6 +32,61 @@ func generateAuthDateRange() string {
return fmt.Sprintf("%s-%s", start, end)
}
// callTianyuanApiWithLog 调用天元API并记录日志
func (a *ApiRequestService) callTianyuanApiWithLog(ctx context.Context, featureID, apiID string, params map[string]interface{}) (*tianyuanapi.Response, error) {
startTime := time.Now()
resp, err := a.tianyuanapi.CallInterface(apiID, params)
responseTime := time.Since(startTime).Milliseconds()
// 如果没有提供featureID尝试从缓存中获取
if featureID == "" {
a.apiFeatureMapMutex.RLock()
featureID = a.apiFeatureMapCache[apiID]
a.apiFeatureMapMutex.RUnlock()
}
// 构建调用记录选项
callStatus := int64(0) // 默认失败
errorCode := ""
errorMessage := ""
responseData := interface{}(nil)
transactionID := ""
if err != nil {
// 调用失败
errorMessage = err.Error()
// 尝试从错误信息中提取错误码
if code := tianyuanapi.GetCodeByError(err); code != -1 {
errorCode = fmt.Sprintf("%d", code)
}
} else {
// 调用成功
callStatus = 1
responseData = resp.Data
transactionID = resp.TransactionID
}
// 异步记录调用日志(避免影响主流程)
go func() {
logOpts := CallLogOptions{
FeatureID: featureID,
ApiID: apiID,
CallStatus: callStatus,
ResponseTime: responseTime,
ErrorCode: errorCode,
ErrorMessage: errorMessage,
RequestParams: params,
ResponseData: responseData,
TransactionID: transactionID,
}
if recordErr := a.tianyuanapiCallLogService.RecordCall(context.Background(), logOpts); recordErr != nil {
logx.Errorf("记录天元API调用日志失败api_id=%s, err=%v", apiID, recordErr)
}
}()
return resp, err
}
type ApiRequestService struct {
config config.Config
featureModel model.FeatureModel
@@ -39,16 +94,22 @@ type ApiRequestService struct {
userFeatureWhitelistModel model.UserFeatureWhitelistModel
whitelistService *WhitelistService
tianyuanapi *tianyuanapi.Client
tianyuanapiCallLogService *TianyuanapiCallLogService
apiFeatureMapCache map[string]string // apiID -> featureID 缓存
apiFeatureMapMutex sync.RWMutex
}
// NewApiRequestService 是一个构造函数,用于初始化 ApiRequestService
func NewApiRequestService(c config.Config, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel, userFeatureWhitelistModel model.UserFeatureWhitelistModel, tianyuanapi *tianyuanapi.Client) *ApiRequestService {
func NewApiRequestService(c config.Config, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel, userFeatureWhitelistModel model.UserFeatureWhitelistModel, tianyuanapi *tianyuanapi.Client, tianyuanapiCallLogService *TianyuanapiCallLogService, whitelistService *WhitelistService) *ApiRequestService {
return &ApiRequestService{
config: c,
featureModel: featureModel,
productFeatureModel: productFeatureModel,
userFeatureWhitelistModel: userFeatureWhitelistModel,
tianyuanapi: tianyuanapi,
tianyuanapiCallLogService: tianyuanapiCallLogService,
whitelistService: whitelistService,
apiFeatureMapCache: make(map[string]string),
}
}
@@ -61,15 +122,21 @@ type APIResponseData struct {
}
// ProcessRequests 处理请求
func (a *ApiRequestService) ProcessRequests(params []byte, productID string) ([]byte, error) {
var ctx, cancel = context.WithCancel(context.Background())
func (a *ApiRequestService) ProcessRequests(ctx context.Context, params []byte, productID string) ([]byte, error) {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()
// 从params中提取id_card用于白名单检查
idCard := gjson.GetBytes(params, "id_card").String()
// 查询白名单如果提供了id_card集中由 WhitelistService 处理
whitelistedFeatureApiIds, _ := a.whitelistService.GetWhitelistedFeatureApisByIdCard(ctx, idCard)
var whitelistedFeatureApiIds map[string]bool
if a.whitelistService != nil {
whitelistedFeatureApiIds, _ = a.whitelistService.GetWhitelistedFeatureApisByIdCard(ctx, idCard)
} else {
whitelistedFeatureApiIds = make(map[string]bool)
}
build := a.productFeatureModel.SelectBuilder().Where(squirrel.Eq{
"product_id": productID,
@@ -95,6 +162,13 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID string) ([]
if len(featureList) == 0 {
return nil, errors.New("处理请求错误,产品无对应接口功能")
}
// 缓存apiID到featureID的映射关系供后续调用记录使用
a.apiFeatureMapMutex.Lock()
for _, feature := range featureList {
a.apiFeatureMapCache[feature.ApiId] = feature.Id
}
a.apiFeatureMapMutex.Unlock()
var (
wg sync.WaitGroup
resultsCh = make(chan APIResponseData, len(featureList))
@@ -140,7 +214,7 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID string) ([]
tryCount := 0
for {
tryCount++
resp, preprocessErr = a.PreprocessRequestApi(params, feature.ApiId)
resp, preprocessErr = a.PreprocessRequestApi(ctx, params, feature.ApiId)
if preprocessErr == nil {
break
}
@@ -197,7 +271,7 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID string) ([]
}
// ------------------------------------请求处理器--------------------------
var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, error){
var requestProcessors = map[string]func(*ApiRequestService, context.Context, []byte) ([]byte, error){
"PersonEnterprisePro": (*ApiRequestService).ProcessPersonEnterpriseProRequest,
"BehaviorRiskScan": (*ApiRequestService).ProcessBehaviorRiskScanRequest,
"YYSYBE08": (*ApiRequestService).ProcessYYSYBE08Request,
@@ -232,16 +306,16 @@ var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, err
}
// PreprocessRequestApi 调用指定的请求处理函数
func (a *ApiRequestService) PreprocessRequestApi(params []byte, apiID string) ([]byte, error) {
func (a *ApiRequestService) PreprocessRequestApi(ctx context.Context, params []byte, apiID string) ([]byte, error) {
if processor, exists := requestProcessors[apiID]; exists {
return processor(a, params) // 调用 ApiRequestService 方法
return processor(a, ctx, params) // 调用 ApiRequestService 方法
}
return nil, errors.New("api请求, 未找到相应的处理程序")
}
// PersonEnterprisePro 人企业关系加强版
func (a *ApiRequestService) ProcessPersonEnterpriseProRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessPersonEnterpriseProRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
// 设置最大调用次数上限
maxApiCalls := 20 // 允许最多查询20个企业
@@ -250,7 +324,7 @@ func (a *ApiRequestService) ProcessPersonEnterpriseProRequest(params []byte) ([]
return nil, errors.New("api请求, PersonEnterprisePro, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("QYGLB4C0", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "QYGLB4C0", map[string]interface{}{
"id_card": idCard.String(),
})
if err != nil {
@@ -452,7 +526,7 @@ func (a *ApiRequestService) ProcessPersonEnterpriseProRequest(params []byte) ([]
}
// 调用QYGL8271接口获取企业涉诉信息
lawsuitResp, err := a.tianyuanapi.CallInterface("QYGL8271", map[string]interface{}{
lawsuitResp, err := a.callTianyuanApiWithLog(ctx, "", "QYGL8271", map[string]interface{}{
"ent_name": orgName.String(),
"ent_code": creditCode.String(),
"auth_date": generateAuthDateRange(),
@@ -576,14 +650,14 @@ func (a *ApiRequestService) ProcessPersonEnterpriseProRequest(params []byte) ([]
}
// ProcesFLXG0V4BRequest 个人司法涉诉(详版)
func (a *ApiRequestService) ProcessFLXG0V4BRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessFLXG0V4BRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
if !name.Exists() || !idCard.Exists() {
return nil, errors.New("api请求, FLXG0V4B, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("FLXG0V4B", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "FLXG0V4B", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"auth_date": generateAuthDateRange(),
@@ -599,13 +673,13 @@ func (a *ApiRequestService) ProcessFLXG0V4BRequest(params []byte) ([]byte, error
}
// ProcessFLXG0687Request 反诈反赌核验
func (a *ApiRequestService) ProcessFLXG0687Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessFLXG0687Request(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
if !idCard.Exists() {
return nil, errors.New("api请求, FLXG0687, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("FLXG0687", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "FLXG0687", map[string]interface{}{
"id_card": idCard.String(),
})
@@ -627,7 +701,7 @@ func (a *ApiRequestService) ProcessFLXG0687Request(params []byte) ([]byte, error
}
// ProcessFLXG3D56Request 违约失信
func (a *ApiRequestService) ProcessFLXG3D56Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessFLXG3D56Request(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -635,7 +709,7 @@ func (a *ApiRequestService) ProcessFLXG3D56Request(params []byte) ([]byte, error
return nil, errors.New("api请求, FLXG3D56, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("FLXG3D56", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "FLXG3D56", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -682,14 +756,14 @@ func (a *ApiRequestService) ProcessFLXG3D56Request(params []byte) ([]byte, error
}
// ProcessIVYZ5733Request 婚姻状况
func (a *ApiRequestService) ProcessIVYZ5733Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessIVYZ5733Request(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
if !idCard.Exists() || !name.Exists() {
return nil, errors.New("api请求, IVYZ5733, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("IVYZ5733", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "IVYZ5733", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
})
@@ -738,14 +812,14 @@ func (a *ApiRequestService) ProcessIVYZ5733Request(params []byte) ([]byte, error
}
// ProcessIVYZ9A2BRequest 学历查询
func (a *ApiRequestService) ProcessIVYZ9A2BRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessIVYZ9A2BRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
if !idCard.Exists() || !name.Exists() {
return nil, errors.New("api请求, IVYZ9A2B, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("IVYZ9A2B", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "IVYZ9A2B", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
})
@@ -809,14 +883,14 @@ func (a *ApiRequestService) ProcessIVYZ9A2BRequest(params []byte) ([]byte, error
}
// ProcessYYSYBE08Request 二要素
func (a *ApiRequestService) ProcessYYSYBE08Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessYYSYBE08Request(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
if !name.Exists() || !idCard.Exists() {
return nil, errors.New("api请求, YYSYBE08, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("YYSYBE08", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "YYSYBE08", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
})
@@ -850,7 +924,7 @@ func (a *ApiRequestService) ProcessYYSYBE08Request(params []byte) ([]byte, error
}
// ProcessJRZQ0A03Request 借贷申请
func (a *ApiRequestService) ProcessJRZQ0A03Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ0A03Request(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -858,7 +932,7 @@ func (a *ApiRequestService) ProcessJRZQ0A03Request(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ0A03, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ0A03", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ0A03", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -907,7 +981,7 @@ func (a *ApiRequestService) ProcessJRZQ0A03Request(params []byte) ([]byte, error
}
// ProcessJRZQ8203Request 借贷行为
func (a *ApiRequestService) ProcessJRZQ8203Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ8203Request(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -915,7 +989,7 @@ func (a *ApiRequestService) ProcessJRZQ8203Request(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ8203, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ8203", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ8203", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -964,7 +1038,7 @@ func (a *ApiRequestService) ProcessJRZQ8203Request(params []byte) ([]byte, error
}
// ProcessJRZQ4AA8Request 还款压力
func (a *ApiRequestService) ProcessJRZQ4AA8Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ4AA8Request(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
mobile := gjson.GetBytes(params, "mobile")
@@ -972,7 +1046,7 @@ func (a *ApiRequestService) ProcessJRZQ4AA8Request(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ4AA8, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ4AA8", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ4AA8", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
"mobile_no": mobile.String(),
@@ -1013,7 +1087,7 @@ func (a *ApiRequestService) ProcessJRZQ4AA8Request(params []byte) ([]byte, error
}
// ProcessQYGL8271Request 企业涉诉
func (a *ApiRequestService) ProcessQYGL8271Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessQYGL8271Request(ctx context.Context, params []byte) ([]byte, error) {
entName := gjson.GetBytes(params, "ent_name")
entCode := gjson.GetBytes(params, "ent_code")
@@ -1021,7 +1095,7 @@ func (a *ApiRequestService) ProcessQYGL8271Request(params []byte) ([]byte, error
return nil, errors.New("api请求, QYGL8271, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("QYGL8271", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "QYGL8271", map[string]interface{}{
"ent_name": entName.String(),
"ent_code": entCode.String(),
})
@@ -1073,13 +1147,13 @@ func (a *ApiRequestService) ProcessQYGL8271Request(params []byte) ([]byte, error
}
// ProcessQYGL6F2DRequest 人企关联
func (a *ApiRequestService) ProcessQYGL6F2DRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessQYGL6F2DRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
if !idCard.Exists() {
return nil, errors.New("api请求, QYGL6F2D, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("QYGL6F2D", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "QYGL6F2D", map[string]interface{}{
"id_card": idCard.String(),
})
@@ -1114,13 +1188,13 @@ func (a *ApiRequestService) ProcessQYGL6F2DRequest(params []byte) ([]byte, error
}
// ProcessQCXG7A2BRequest 名下车辆
func (a *ApiRequestService) ProcessQCXG7A2BRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessQCXG7A2BRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
if !idCard.Exists() {
return nil, errors.New("api请求, QCXG7A2B, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("QCXG7A2B", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "QCXG7A2B", map[string]interface{}{
"id_card": idCard.String(),
})
@@ -1132,7 +1206,7 @@ func (a *ApiRequestService) ProcessQCXG7A2BRequest(params []byte) ([]byte, error
}
// ProcessYYSY09CDRequest 三要素
func (a *ApiRequestService) ProcessYYSY09CDRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessYYSY09CDRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1140,7 +1214,7 @@ func (a *ApiRequestService) ProcessYYSY09CDRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, YYSY09CD, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("YYSY09CD", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "YYSY09CD", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1175,7 +1249,7 @@ func (a *ApiRequestService) ProcessYYSY09CDRequest(params []byte) ([]byte, error
}
// ProcessBehaviorRiskScanRequest 行为风险扫描
func (a *ApiRequestService) ProcessBehaviorRiskScanRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessBehaviorRiskScanRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
@@ -1197,7 +1271,7 @@ func (a *ApiRequestService) ProcessBehaviorRiskScanRequest(params []byte) ([]byt
// 反赌反诈
go func() {
defer wg.Done()
respBytes, err := a.ProcessFLXG0687Request(params)
respBytes, err := a.ProcessFLXG0687Request(ctx, params)
results <- apiResult{name: "anti_fraud_gaming", data: respBytes, err: err}
}()
@@ -1242,7 +1316,7 @@ func (a *ApiRequestService) ProcessBehaviorRiskScanRequest(params []byte) ([]byt
}
// ProcessDWBG8B4DRequest 谛听多维报告
func (a *ApiRequestService) ProcessDWBG8B4DRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessDWBG8B4DRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1251,7 +1325,7 @@ func (a *ApiRequestService) ProcessDWBG8B4DRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, DWBG8B4D, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("DWBG8B4D", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "DWBG8B4D", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1267,7 +1341,7 @@ func (a *ApiRequestService) ProcessDWBG8B4DRequest(params []byte) ([]byte, error
}
// ProcessDWBG6A2CRequest 司南报告服务
func (a *ApiRequestService) ProcessDWBG6A2CRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessDWBG6A2CRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1276,7 +1350,7 @@ func (a *ApiRequestService) ProcessDWBG6A2CRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, DWBG6A2C, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("DWBG6A2C", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "DWBG6A2C", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1292,7 +1366,7 @@ func (a *ApiRequestService) ProcessDWBG6A2CRequest(params []byte) ([]byte, error
}
// ProcessJRZQ4B6CRequest 探针C风险评估
func (a *ApiRequestService) ProcessJRZQ4B6CRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ4B6CRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1300,7 +1374,7 @@ func (a *ApiRequestService) ProcessJRZQ4B6CRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ4B6C, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ4B6C", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ4B6C", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1316,7 +1390,7 @@ func (a *ApiRequestService) ProcessJRZQ4B6CRequest(params []byte) ([]byte, error
}
// ProcessJRZQ09J8Request 收入评估
func (a *ApiRequestService) ProcessJRZQ09J8Request(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ09J8Request(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1324,7 +1398,7 @@ func (a *ApiRequestService) ProcessJRZQ09J8Request(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ09J8, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ09J8", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ09J8", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1340,7 +1414,7 @@ func (a *ApiRequestService) ProcessJRZQ09J8Request(params []byte) ([]byte, error
}
// ProcessJRZQ5E9FRequest 借选指数
func (a *ApiRequestService) ProcessJRZQ5E9FRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ5E9FRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1348,7 +1422,7 @@ func (a *ApiRequestService) ProcessJRZQ5E9FRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ5E9F, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ5E9F", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ5E9F", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1364,13 +1438,13 @@ func (a *ApiRequestService) ProcessJRZQ5E9FRequest(params []byte) ([]byte, error
}
// ProcessQYGL3F8ERequest 人企关系加强版2
func (a *ApiRequestService) ProcessQYGL3F8ERequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessQYGL3F8ERequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
if !idCard.Exists() {
return nil, errors.New("api请求, QYGL3F8E, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("QYGL3F8E", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "QYGL3F8E", map[string]interface{}{
"id_card": idCard.String(),
})
@@ -1383,14 +1457,14 @@ func (a *ApiRequestService) ProcessQYGL3F8ERequest(params []byte) ([]byte, error
}
// ProcessIVYZ81NCRequest 婚姻,登记时间版
func (a *ApiRequestService) ProcessIVYZ81NCRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessIVYZ81NCRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
if !name.Exists() || !idCard.Exists() {
return nil, errors.New("api请求, IVYZ81NC, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("IVYZ81NC", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "IVYZ81NC", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
})
@@ -1404,14 +1478,14 @@ func (a *ApiRequestService) ProcessIVYZ81NCRequest(params []byte) ([]byte, error
}
// ProcessIVYZ7F3ARequest 学历查询版B
func (a *ApiRequestService) ProcessIVYZ7F3ARequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessIVYZ7F3ARequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
if !idCard.Exists() || !name.Exists() {
return nil, errors.New("api请求, IVYZ7F3A, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("IVYZ7F3A", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "IVYZ7F3A", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
"authorized": "1",
@@ -1426,7 +1500,7 @@ func (a *ApiRequestService) ProcessIVYZ7F3ARequest(params []byte) ([]byte, error
}
// ProcessDWBG7F3ARequest 多头借贷行业风险版
func (a *ApiRequestService) ProcessDWBG7F3ARequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessDWBG7F3ARequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1434,7 +1508,7 @@ func (a *ApiRequestService) ProcessDWBG7F3ARequest(params []byte) ([]byte, error
return nil, errors.New("api请求, DWBG7F3A, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("DWBG7F3A", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "DWBG7F3A", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1449,7 +1523,7 @@ func (a *ApiRequestService) ProcessDWBG7F3ARequest(params []byte) ([]byte, error
}
// ProcessJRZQ8A2DRequest 特殊名单验证B
func (a *ApiRequestService) ProcessJRZQ8A2DRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ8A2DRequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1457,7 +1531,7 @@ func (a *ApiRequestService) ProcessJRZQ8A2DRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ8A2D, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ8A2D", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ8A2D", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1473,13 +1547,13 @@ func (a *ApiRequestService) ProcessJRZQ8A2DRequest(params []byte) ([]byte, error
}
// ProcessYYSY8B1CRequest 手机在网时长B
func (a *ApiRequestService) ProcessYYSY8B1CRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessYYSY8B1CRequest(ctx context.Context, params []byte) ([]byte, error) {
mobile := gjson.GetBytes(params, "mobile")
if !mobile.Exists() {
return nil, errors.New("api请求, YYSY8B1C, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("YYSY8B1C", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "YYSY8B1C", map[string]interface{}{
"mobile_no": mobile.String(),
})
@@ -1492,13 +1566,13 @@ func (a *ApiRequestService) ProcessYYSY8B1CRequest(params []byte) ([]byte, error
}
// ProcessYYSY7D3ERequest 携号转网查询
func (a *ApiRequestService) ProcessYYSY7D3ERequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessYYSY7D3ERequest(ctx context.Context, params []byte) ([]byte, error) {
mobile := gjson.GetBytes(params, "mobile")
if !mobile.Exists() {
return nil, errors.New("api请求, YYSY7D3E, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("YYSY7D3E", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "YYSY7D3E", map[string]interface{}{
"mobile_no": mobile.String(),
})
@@ -1511,7 +1585,7 @@ func (a *ApiRequestService) ProcessYYSY7D3ERequest(params []byte) ([]byte, error
}
// ProcessFLXG7E8FRequest 个人司法涉诉查询
func (a *ApiRequestService) ProcessFLXG7E8FRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessFLXG7E8FRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
mobile := gjson.GetBytes(params, "mobile")
@@ -1519,7 +1593,7 @@ func (a *ApiRequestService) ProcessFLXG7E8FRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, FLXG7E8F, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("FLXG7E8F", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "FLXG7E8F", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
"mobile_no": mobile.String(),
@@ -1534,7 +1608,7 @@ func (a *ApiRequestService) ProcessFLXG7E8FRequest(params []byte) ([]byte, error
}
// ProcessIVYZ8I9JRequest 互联网行为推测
func (a *ApiRequestService) ProcessIVYZ8I9JRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessIVYZ8I9JRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
mobile := gjson.GetBytes(params, "mobile")
@@ -1542,7 +1616,7 @@ func (a *ApiRequestService) ProcessIVYZ8I9JRequest(params []byte) ([]byte, error
return nil, errors.New("api请求, IVYZ8I9J, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("IVYZ8I9J", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "IVYZ8I9J", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
"mobile_no": mobile.String(),
@@ -1557,7 +1631,7 @@ func (a *ApiRequestService) ProcessIVYZ8I9JRequest(params []byte) ([]byte, error
}
// ProcessJRZQ7F1ARequest 全景雷达
func (a *ApiRequestService) ProcessJRZQ7F1ARequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ7F1ARequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1565,7 +1639,7 @@ func (a *ApiRequestService) ProcessJRZQ7F1ARequest(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ7F1A, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ7F1A", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ7F1A", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),
@@ -1581,14 +1655,14 @@ func (a *ApiRequestService) ProcessJRZQ7F1ARequest(params []byte) ([]byte, error
}
// ProcessIVYZ3P9MRequest 学历实时查询
func (a *ApiRequestService) ProcessIVYZ3P9MRequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessIVYZ3P9MRequest(ctx context.Context, params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
name := gjson.GetBytes(params, "name")
if !idCard.Exists() || !name.Exists() {
return nil, errors.New("api请求, IVYZ3P9M, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("IVYZ3P9M", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "IVYZ3P9M", map[string]interface{}{
"id_card": idCard.String(),
"name": name.String(),
})
@@ -1601,7 +1675,7 @@ func (a *ApiRequestService) ProcessIVYZ3P9MRequest(params []byte) ([]byte, error
}
// ProcessJRZQ6F2ARequest 借贷申请
func (a *ApiRequestService) ProcessJRZQ6F2ARequest(params []byte) ([]byte, error) {
func (a *ApiRequestService) ProcessJRZQ6F2ARequest(ctx context.Context, params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
@@ -1609,7 +1683,7 @@ func (a *ApiRequestService) ProcessJRZQ6F2ARequest(params []byte) ([]byte, error
return nil, errors.New("api请求, JRZQ6F2A, 获取相关参数失败")
}
resp, err := a.tianyuanapi.CallInterface("JRZQ6F2A", map[string]interface{}{
resp, err := a.callTianyuanApiWithLog(ctx, "", "JRZQ6F2A", map[string]interface{}{
"name": name.String(),
"id_card": idCard.String(),
"mobile_no": mobile.String(),

View File

@@ -0,0 +1,201 @@
package service
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"time"
"ycc-server/app/main/model"
"github.com/Masterminds/squirrel"
"github.com/zeromicro/go-zero/core/logx"
)
// TianyuanapiCallLogService 天元API调用记录服务
type TianyuanapiCallLogService struct {
tianyuanapiCallLogModel model.TianyuanapiCallLogModel
featureModel model.FeatureModel
}
// NewTianyuanapiCallLogService 创建天元API调用记录服务
func NewTianyuanapiCallLogService(
tianyuanapiCallLogModel model.TianyuanapiCallLogModel,
featureModel model.FeatureModel,
) *TianyuanapiCallLogService {
return &TianyuanapiCallLogService{
tianyuanapiCallLogModel: tianyuanapiCallLogModel,
featureModel: featureModel,
}
}
// CallLogOptions 调用记录选项
type CallLogOptions struct {
FeatureID string // 功能ID
ApiID string // API标识YYSYBE08
OrderID string // 订单ID可选
QueryID string // 查询ID可选
CallStatus int64 // 调用状态0=失败1=成功
ResponseTime int64 // 响应耗时(毫秒)
ErrorCode string // 错误码(失败时)
ErrorMessage string // 错误信息(失败时)
RequestParams interface{} // 请求参数(可选)
ResponseData interface{} // 响应数据(可选)
TransactionID string // 天元API流水号
}
// RecordCall 记录天元API调用
func (s *TianyuanapiCallLogService) RecordCall(ctx context.Context, opts CallLogOptions) error {
// 1. 获取feature的成本价
costPrice := 0.00
if opts.CallStatus == 1 { // 只有成功才计算成本
feature, err := s.featureModel.FindOne(ctx, opts.FeatureID)
if err == nil {
costPrice = feature.CostPrice
} else {
logx.Errorf("查询feature成本价失败feature_id=%s, err=%v", opts.FeatureID, err)
}
}
// 2. 转换参数和响应为JSON字符串
var requestParamsStr, responseDataStr *string
if opts.RequestParams != nil {
if bytes, err := json.Marshal(opts.RequestParams); err == nil {
jsonStr := string(bytes)
requestParamsStr = &jsonStr
}
}
if opts.ResponseData != nil {
// 只记录响应数据的前1000个字符避免存储过大
if bytes, err := json.Marshal(opts.ResponseData); err == nil {
jsonStr := string(bytes)
if len(jsonStr) > 1000 {
jsonStr = jsonStr[:1000] + "...[truncated]"
}
responseDataStr = &jsonStr
}
}
// 3. 构建调用记录
callTime := time.Now()
deleteTime := sql.NullTime{}
callLog := &model.TianyuanapiCallLog{
FeatureId: opts.FeatureID,
ApiId: opts.ApiID,
OrderId: sql.NullString{},
QueryId: sql.NullString{},
CallStatus: opts.CallStatus,
CallTime: callTime,
ResponseTime: sql.NullInt64{},
CostPrice: costPrice,
ErrorCode: sql.NullString{},
ErrorMessage: sql.NullString{},
RequestParams: sql.NullString{},
ResponseData: sql.NullString{},
TransactionId: sql.NullString{},
CreateTime: callTime,
UpdateTime: callTime,
DeleteTime: deleteTime,
DelState: 0,
Version: 0,
}
// 4. 填充可选字段
if opts.OrderID != "" {
callLog.OrderId = sql.NullString{String: opts.OrderID, Valid: true}
}
if opts.QueryID != "" {
callLog.QueryId = sql.NullString{String: opts.QueryID, Valid: true}
}
if opts.ResponseTime > 0 {
callLog.ResponseTime = sql.NullInt64{Int64: opts.ResponseTime, Valid: true}
}
if opts.ErrorCode != "" {
callLog.ErrorCode = sql.NullString{String: opts.ErrorCode, Valid: true}
}
if opts.ErrorMessage != "" {
callLog.ErrorMessage = sql.NullString{String: opts.ErrorMessage, Valid: true}
}
if requestParamsStr != nil {
callLog.RequestParams = sql.NullString{String: *requestParamsStr, Valid: true}
}
if responseDataStr != nil {
callLog.ResponseData = sql.NullString{String: *responseDataStr, Valid: true}
}
if opts.TransactionID != "" {
callLog.TransactionId = sql.NullString{String: opts.TransactionID, Valid: true}
}
// 5. 插入记录
_, err := s.tianyuanapiCallLogModel.Insert(ctx, nil, callLog)
if err != nil {
logx.Errorf("插入天元API调用记录失败feature_id=%s, api_id=%s, err=%v", opts.FeatureID, opts.ApiID, err)
return fmt.Errorf("插入调用记录失败: %w", err)
}
return nil
}
// GetStatistics 获取统计信息
type StatisticsFilter struct {
FeatureID string // 功能ID
ApiID string // API标识
StartDate time.Time // 开始日期
EndDate time.Time // 结束日期
}
// Statistics 统计信息
type Statistics struct {
TotalCalls int64 // 总调用次数
SuccessCalls int64 // 成功次数
FailedCalls int64 // 失败次数
TotalCost float64 // 总成本(成功调用的成本之和)
}
// GetStatistics 获取统计信息
func (s *TianyuanapiCallLogService) GetStatistics(ctx context.Context, filter StatisticsFilter) (*Statistics, error) {
builder := s.tianyuanapiCallLogModel.SelectBuilder()
// 添加过滤条件
if filter.FeatureID != "" {
builder = builder.Where(squirrel.Eq{"feature_id": filter.FeatureID})
}
if filter.ApiID != "" {
builder = builder.Where(squirrel.Eq{"api_id": filter.ApiID})
}
if !filter.StartDate.IsZero() {
builder = builder.Where(squirrel.GtOrEq{"call_time": filter.StartDate})
}
if !filter.EndDate.IsZero() {
builder = builder.Where(squirrel.Lt{"call_time": filter.EndDate})
}
// 统计总调用次数
totalCalls, err := s.tianyuanapiCallLogModel.FindCount(ctx, builder, "id")
if err != nil {
return nil, fmt.Errorf("统计总调用次数失败: %w", err)
}
// 统计成功次数
successBuilder := builder.Where(squirrel.Eq{"call_status": 1})
successCalls, err := s.tianyuanapiCallLogModel.FindCount(ctx, successBuilder, "id")
if err != nil {
return nil, fmt.Errorf("统计成功次数失败: %w", err)
}
// 统计失败次数
failedCalls := totalCalls - successCalls
// 统计总成本(仅成功调用)
totalCost, err := s.tianyuanapiCallLogModel.FindSum(ctx, successBuilder, "cost_price")
if err != nil {
return nil, fmt.Errorf("统计总成本失败: %w", err)
}
return &Statistics{
TotalCalls: totalCalls,
SuccessCalls: successCalls,
FailedCalls: failedCalls,
TotalCost: totalCost,
}, nil
}

View File

@@ -134,6 +134,9 @@ func (s *WhitelistService) GetWhitelistedFeatureApisByIdCard(
idCard string,
) (map[string]bool, error) {
result := make(map[string]bool)
if s == nil {
return result, nil
}
if idCard == "" {
return result, nil
}