diff --git a/internal/domains/api/services/processors/qygl/qygl3f8e_processor.go b/internal/domains/api/services/processors/qygl/qygl3f8e_processor.go index 5cf1b8b..e2a39ee 100644 --- a/internal/domains/api/services/processors/qygl/qygl3f8e_processor.go +++ b/internal/domains/api/services/processors/qygl/qygl3f8e_processor.go @@ -29,22 +29,23 @@ func ProcessQYGL3F8ERequest(ctx context.Context, params []byte, deps *processors // 设置最大处理企业数量 maxProcessCount := 3 - // 1. 首先调用QYGLB4C0获取个人关联的企业信息 - b4c0Params := dto.QYGLB4C0Req{ - IDCard: paramsDto.IDCard, + // 1. 首先调用QYGL6S1B获取个人关联的企业信息 + b4c0Params := dto.QYGL6S1BReq{ + IDCard: paramsDto.IDCard, + Authorized: "1", } b4c0ParamsBytes, err := json.Marshal(b4c0Params) if err != nil { return nil, errors.Join(processors.ErrSystem, err) } - b4c0Response, err := ProcessQYGLB4C0Request(ctx, b4c0ParamsBytes, deps) + b4c0Response, err := ProcessQYGL6S1BRequest(ctx, b4c0ParamsBytes, deps) if err != nil { return nil, err // 错误已经是处理器标准错误,直接返回 } - // 2. 解析QYGLB4C0的响应,获取企业列表 - companies, err := parseCompaniesFromB4C0Response(b4c0Response) + // 2. 解析QYGL6S1B的响应,获取企业列表 + companies, err := parseCompaniesFrom6S1BResponse(b4c0Response) if err != nil { return nil, errors.Join(processors.ErrSystem, err) } @@ -74,7 +75,7 @@ func ProcessQYGL3F8ERequest(ctx context.Context, params []byte, deps *processors } // 6. 构建最终响应 - finalResponse, err := buildFinalResponse(b4c0Response, enrichedCompanies, sortedCompanies[:processCount]) + finalResponse, err := buildFinalResponse(enrichedCompanies, sortedCompanies) if err != nil { return nil, errors.Join(processors.ErrSystem, err) } @@ -95,54 +96,121 @@ type CompanyInfo struct { Dishonest int // 失信被执行人数量 } -// parseCompaniesFromB4C0Response 从QYGLB4C0响应中解析企业列表 -func parseCompaniesFromB4C0Response(response []byte) ([]CompanyInfo, error) { - // 解析响应数据 - dataResult := gjson.GetBytes(response, "data") - if !dataResult.Exists() { - return nil, fmt.Errorf("响应中缺少data字段") +// parseCompaniesFrom6S1BResponse 从QYGL6S1B响应中解析企业列表 +func parseCompaniesFrom6S1BResponse(response []byte) ([]CompanyInfo, error) { + // 解析响应数据 - 根对象下的各个数组 + //担任法人信息 + legRepInfoList := gjson.GetBytes(response, "legRepInfoList") + //shareholderList 股东 + shareholderList := gjson.GetBytes(response, "shareholderList") + //ryPosPerList 高管 + ryPosPerList := gjson.GetBytes(response, "ryPosPerList") + //caseInfoList 行政处罚(可能为空) + caseInfoList := gjson.GetBytes(response, "caseInfoList") + //performerList 被执行人(可能为空) + performerList := gjson.GetBytes(response, "performerList") + //lossPromiseList 失信被执行人(可能为空) + lossPromiseList := gjson.GetBytes(response, "lossPromiseList") + + // 分别从三个列表获取企业信息,然后合并成一个扁平列表 + // 将三个数组格式 [{},{}],[{},{}],[{},{}] 合并成单个扁平数组 [{},{},{},{},{},{},{},{}] + // 构建企业数据列表,包含来源信息 + type CompanyWithSource struct { + json gjson.Result + source string // 标识来源:legRepInfoList, shareholderList, ryPosPerList + } + companiesWithSource := make([]CompanyWithSource, 0) + + // 从legRepInfoList获取企业并添加到合并列表 + if legRepInfoList.Exists() && legRepInfoList.IsArray() { + for _, item := range legRepInfoList.Array() { + companiesWithSource = append(companiesWithSource, CompanyWithSource{ + json: item, + source: "legRepInfoList", + }) + } } - datalistResult := gjson.Get(dataResult.Raw, "datalist") - if !datalistResult.Exists() { - return nil, fmt.Errorf("datalist字段不存在") + // 从shareholderList获取企业并添加到合并列表 + if shareholderList.Exists() && shareholderList.IsArray() { + for _, item := range shareholderList.Array() { + companiesWithSource = append(companiesWithSource, CompanyWithSource{ + json: item, + source: "shareholderList", + }) + } } - companiesArray := datalistResult.Array() - companies := make([]CompanyInfo, 0, len(companiesArray)) + // 从ryPosPerList获取企业并添加到合并列表 + if ryPosPerList.Exists() && ryPosPerList.IsArray() { + for _, item := range ryPosPerList.Array() { + companiesWithSource = append(companiesWithSource, CompanyWithSource{ + json: item, + source: "ryPosPerList", + }) + } + } - for i, companyJson := range companiesArray { - // 获取企业基本信息 - name := companyJson.Get("basicInfo.name").String() - creditCode := companyJson.Get("basicInfo.creditCode").String() + // 如果没有找到任何企业信息 + if len(companiesWithSource) == 0 { + return nil, fmt.Errorf("响应中缺少企业信息数组") + } + + // 在循环外统一统计各种数据 + adminPenalty := 0 + executed := 0 + dishonest := 0 + + // 统计行政处罚 + if caseInfoList.Exists() && caseInfoList.IsArray() { + adminPenalty = len(caseInfoList.Array()) + } + + // 统计被执行人 + if performerList.Exists() && performerList.IsArray() { + executed = len(performerList.Array()) + } + + // 统计失信被执行人 + if lossPromiseList.Exists() && lossPromiseList.IsArray() { + dishonest = len(lossPromiseList.Array()) + } + + // 计算全局关系数量 - 统计有多少种关系类型存在 + relationCount := 0 + if legRepInfoList.Exists() && legRepInfoList.IsArray() && len(legRepInfoList.Array()) > 0 { + relationCount++ + } + if shareholderList.Exists() && shareholderList.IsArray() && len(shareholderList.Array()) > 0 { + relationCount++ + } + if ryPosPerList.Exists() && ryPosPerList.IsArray() && len(ryPosPerList.Array()) > 0 { + relationCount++ + } + + companies := make([]CompanyInfo, 0, len(companiesWithSource)) + + for i, companyWithSource := range companiesWithSource { + companyJson := companyWithSource.json + name := companyJson.Get("orgName").String() + creditCode := companyJson.Get("creditNo").String() if name == "" || creditCode == "" { continue // 跳过无效企业 } - // 计算各种统计数据 - adminPenalty := 0 - executed := 0 - dishonest := 0 - - // 统计行政处罚 - if adminPenaltyResult := companyJson.Get("adminPenalty"); adminPenaltyResult.Exists() && adminPenaltyResult.IsArray() { - adminPenalty = len(adminPenaltyResult.Array()) + // 根据企业来源计算关系权重 + // legRepInfoList(法人) > shareholderList(股东) > ryPosPerList(高管) + relationshipVal := 0 + switch companyWithSource.source { + case "legRepInfoList": + relationshipVal = 6 // 法人关系 - 权重最高 + case "shareholderList": + relationshipVal = 5 // 股东关系 - 权重次高 + case "ryPosPerList": + relationshipVal = 4 // 高管关系 - 权重较低 } - // 统计被执行人 - if executedPersonResult := companyJson.Get("executedPerson"); executedPersonResult.Exists() && executedPersonResult.IsArray() { - executed = len(executedPersonResult.Array()) - } - - // 统计失信被执行人 - if dishonestResult := companyJson.Get("dishonestExecutedPerson"); dishonestResult.Exists() && dishonestResult.IsArray() { - dishonest = len(dishonestResult.Array()) - } - - // 计算关系权重 - relationshipVal, relationCount := calculateRelationshipPriority(companyJson) - companies = append(companies, CompanyInfo{ Index: i, Data: companyJson, @@ -159,53 +227,6 @@ func parseCompaniesFromB4C0Response(response []byte) ([]CompanyInfo, error) { return companies, nil } -// calculateRelationshipPriority 计算关系优先级 -func calculateRelationshipPriority(companyJson gjson.Result) (int, int) { - relationshipVal := 0 - relationCount := 0 - - relationshipResult := companyJson.Get("relationship") - if !relationshipResult.Exists() || !relationshipResult.IsArray() { - return relationshipVal, relationCount - } - - relationships := relationshipResult.Array() - for _, rel := range relationships { - relationCount++ - relStr := rel.String() - - // 根据关系类型设置权重:股东(6) > 历史股东(5) > 法人(4) > 历史法人(3) > 高管(2) > 历史高管(1) - switch relStr { - case "sh": // 股东 - if relationshipVal < 6 { - relationshipVal = 6 - } - case "his_sh": // 历史股东 - if relationshipVal < 5 { - relationshipVal = 5 - } - case "lp": // 法人 - if relationshipVal < 4 { - relationshipVal = 4 - } - case "his_lp": // 历史法人 - if relationshipVal < 3 { - relationshipVal = 3 - } - case "tm": // 高管 - if relationshipVal < 2 { - relationshipVal = 2 - } - case "his_tm": // 历史高管 - if relationshipVal < 1 { - relationshipVal = 1 - } - } - } - - return relationshipVal, relationCount -} - // sortCompaniesByPriority 按优先级对企业进行排序 func sortCompaniesByPriority(companies []CompanyInfo) []CompanyInfo { // 创建副本进行排序 @@ -442,42 +463,11 @@ func generateAuthDateRange() string { } // buildFinalResponse 构建最终响应 -func buildFinalResponse(originalResponse []byte, enrichedCompanies []EnrichedCompanyInfo, processedCompanies []CompanyInfo) ([]byte, error) { - // 解析原始响应 - var originalData map[string]interface{} - if err := json.Unmarshal(originalResponse, &originalData); err != nil { - return nil, fmt.Errorf("解析原始响应失败: %v", err) - } - - // 获取总数,默认为0 - total := 0 - var originalDatalist []interface{} - - // 安全地获取数据,防止字段不存在的情况 - if dataField, ok := originalData["data"].(map[string]interface{}); ok { - // 获取total字段 - if totalField, exists := dataField["total"]; exists { - switch t := totalField.(type) { - case float64: - total = int(t) - case int: - total = t - case string: - // 如果total是字符串,尝试转换为整数 - if parsedTotal, err := json.Number(t).Int64(); err == nil { - total = int(parsedTotal) - } - } - } - - // 获取datalist字段 - if datalist, exists := dataField["datalist"].([]interface{}); exists { - originalDatalist = datalist - } - } - +// enrichedCompanies: 已增强的企业信息(前3个处理过的) +// allCompanies: 所有企业信息(从legRepInfoList, shareholderList, ryPosPerList合并的扁平列表,格式为 [{},{},{},{},{},{},{},{}]) +func buildFinalResponse(enrichedCompanies []EnrichedCompanyInfo, allCompanies []CompanyInfo) ([]byte, error) { // 如果没有企业数据,返回空列表 - if len(originalDatalist) == 0 { + if len(allCompanies) == 0 { finalResponse := map[string]interface{}{ "items": []interface{}{}, "total": 0, @@ -485,52 +475,61 @@ func buildFinalResponse(originalResponse []byte, enrichedCompanies []EnrichedCom return json.Marshal(finalResponse) } - // 创建增强后的企业列表 - enhancedDatalist := make([]interface{}, len(originalDatalist)) - - // 创建已处理企业的映射 + // 创建已处理企业的映射(使用Index作为key,方便查找) + // enrichedCompanies 中的企业是按处理顺序传入的,对应 allCompanies 中已排序的前几个 + // 注意:allCompanies 已经排序过了,enrichedCompanies 的顺序对应 allCompanies 的前几个 processedMap := make(map[int]EnrichedCompanyInfo) for i, enriched := range enrichedCompanies { - if i < len(processedCompanies) { - processedMap[processedCompanies[i].Index] = enriched + if i < len(allCompanies) { + // enrichedCompanies[i] 对应 allCompanies[i],使用 allCompanies[i].Index 作为key + // 这样后续遍历 allCompanies 时,可以通过 company.Index 查找对应的 enriched + processedMap[allCompanies[i].Index] = enriched } } - // 更新企业列表 - for i, company := range originalDatalist { - if enriched, exists := processedMap[i]; exists { - // 已处理的企业,添加详细信息 - companyMap, ok := company.(map[string]interface{}) - if ok { - companyMap["invest_history"] = enriched.InvestHistory - companyMap["financing_history"] = enriched.FinancingHistory - companyMap["punishment_info"] = enriched.PunishmentInfo - companyMap["abnormal_info"] = enriched.AbnormalInfo - companyMap["lawsuit_info"] = enriched.LawsuitInfo - companyMap["own_tax"] = enriched.OwnTax - companyMap["tax_contravention"] = enriched.TaxContravention - enhancedDatalist[i] = companyMap - } else { - enhancedDatalist[i] = company - } - } else { - // 未处理的企业,添加空的详细信息 - companyMap, ok := company.(map[string]interface{}) - if ok { - companyMap["invest_history"] = map[string]interface{}{} - companyMap["financing_history"] = map[string]interface{}{} - companyMap["punishment_info"] = map[string]interface{}{} - companyMap["abnormal_info"] = map[string]interface{}{} - companyMap["lawsuit_info"] = map[string]interface{}{} - companyMap["own_tax"] = map[string]interface{}{} - companyMap["tax_contravention"] = map[string]interface{}{} - enhancedDatalist[i] = companyMap - } else { - enhancedDatalist[i] = company + // 构建增强后的企业列表 + enhancedDatalist := make([]interface{}, 0, len(allCompanies)) + + for _, company := range allCompanies { + // 将gjson.Result(CompanyInfo.Data)转换为map[string]interface{} + // CompanyInfo.Data 包含来自legRepInfoList/shareholderList/ryPosPerList的完整原始对象 + // 例如:{"regNo": "...", "orgName": "...", "creditNo": "...", "industry": "...", ...} + var companyMap map[string]interface{} + if err := json.Unmarshal([]byte(company.Data.Raw), &companyMap); err != nil { + // 如果转换失败,创建一个基本对象(包含orgName和creditNo) + companyMap = map[string]interface{}{ + "orgName": company.Name, + "creditNo": company.CreditCode, } } + + // 检查是否是已处理的企业(前3个) + if enriched, exists := processedMap[company.Index]; exists { + // 已处理的企业,添加详细信息 + companyMap["invest_history"] = enriched.InvestHistory + companyMap["financing_history"] = enriched.FinancingHistory + companyMap["punishment_info"] = enriched.PunishmentInfo + companyMap["abnormal_info"] = enriched.AbnormalInfo + companyMap["lawsuit_info"] = enriched.LawsuitInfo + companyMap["own_tax"] = enriched.OwnTax + companyMap["tax_contravention"] = enriched.TaxContravention + } else { + // 未处理的企业,添加空的详细信息 + companyMap["invest_history"] = map[string]interface{}{} + companyMap["financing_history"] = map[string]interface{}{} + companyMap["punishment_info"] = map[string]interface{}{} + companyMap["abnormal_info"] = map[string]interface{}{} + companyMap["lawsuit_info"] = map[string]interface{}{} + companyMap["own_tax"] = map[string]interface{}{} + companyMap["tax_contravention"] = map[string]interface{}{} + } + + enhancedDatalist = append(enhancedDatalist, companyMap) } + // 总数就是所有企业的数量(从三个列表合并后的总数) + total := len(allCompanies) + // 构建最终的简化响应格式 finalResponse := map[string]interface{}{ "items": enhancedDatalist,