diff --git a/.claude/skills/fangjia-openapi-dev/SKILL.md b/.claude/skills/fangjia-openapi-dev/SKILL.md index d2b8010e1..5a572bec4 100644 --- a/.claude/skills/fangjia-openapi-dev/SKILL.md +++ b/.claude/skills/fangjia-openapi-dev/SKILL.md @@ -479,7 +479,7 @@ mav.setModel(BasicModel.buildOKModel().setResult(result)); ```bash cd deliverables/fangjiaopen mvn tomcat7:run -# 访问: http://localhost:8032/{controller}/{action}?param=value&token=xxx +# 访问: http://localhost:9180/{controller}/{action}?param=value&token=xxx ``` ### Q5: 如何构建部署? diff --git a/.claude/skills/fangjia-openapi-dev/references/api-testing-guide.md b/.claude/skills/fangjia-openapi-dev/references/api-testing-guide.md index f25c7f9b4..f30a154d0 100644 --- a/.claude/skills/fangjia-openapi-dev/references/api-testing-guide.md +++ b/.claude/skills/fangjia-openapi-dev/references/api-testing-guide.md @@ -476,6 +476,16 @@ groovy -cp "target/classes;target/classes/lib/*" \ 5. ❌ 异常时终止整个测试 6. ❌ 手动管理线程池(除非有特殊需求,否则用工具类) 7. ❌ 忘记处理线程安全问题(使用工具类可避免) +8. ❌ 脚本顶层使用 `static` 修饰符 — **Groovy 脚本不支持顶层 `static`**,会报编译错误。应改为 `final`: + ```groovy + // ❌ 错误 + static final String TOKEN = "xxx" + static final String HOST = "http://..." + + // ✅ 正确 + final String TOKEN = "xxx" + final String HOST = "http://..." + ``` ## 测试完成后的分析 diff --git a/.claude/skills/fangjia-openapi-dev/references/database-tables.md b/.claude/skills/fangjia-openapi-dev/references/database-tables.md index 2d43eb8b9..959f8deca 100644 --- a/.claude/skills/fangjia-openapi-dev/references/database-tables.md +++ b/.claude/skills/fangjia-openapi-dev/references/database-tables.md @@ -308,9 +308,14 @@ MongoCollection coll = Mongo3Utils.getDbCollection( | `city` | String | 城市 | | `region` | String | 区域 | | `name` | String | 小区/建筑名称 | -| `cat` | String | 分类(district=小区, office=写字楼) | +| `cat` | String | 分类(district=小区, office=写字楼)⚠️ 注意:字段名是 `cat`,不是 `category` | +| `status` | Integer | 状态(0=正常,其他=已删除/无效) | | `estate_type2` | String | 物业类型 | +**⚠️ 注意事项**: +- 分类字段名是 `cat`,**不是** `category` +- 查询时必须加 `status: 0` 过滤无效数据,否则会包含已删除的记录 + **访问方式**: **业务项目** - 通过 Service 层查询: @@ -335,11 +340,24 @@ MongoCollection coll = Mongo3Utils.getDbCollection( "fangjia", "seaweed" ) + +// ⚠️ 典型查询:注意用 cat 不是 category,必须加 status=0 +Document query = new Document("city", "福州") + .append("cat", "district") // ⚠️ 字段名是 cat,不是 category + .append("status", 0) // ⚠️ 必须加,过滤已删除/无效数据 +Document projection = new Document("_id", 1).append("city", 1) + .append("region", 1).append("name", 1) + +MongoCursor cursor = coll.find(query) + .projection(projection) + .limit(1000) + .iterator() ``` **使用场景**: - 根据ID查询小区权威信息 - 获取小区的城市、区域、名称用于后续查询 +- 提取测试数据(测试脚本中批量获取小区列表) - 验证POI信息的准确性 --- diff --git a/CLAUDE.md b/CLAUDE.md index c84e5c34b..5baa6a065 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -31,7 +31,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co - **fangjia-open**: 开放API Web层(WAR包) **打包产物**: fangjia-open.war -**本地开发端口**: 8032 (通过tomcat7-maven-plugin) +**本地开发端口**: 9180 (通过tomcat7-maven-plugin) ### 3. deliverables/fangjiastatic - 静态资源服务 静态文件托管服务,包含以下模块: @@ -384,8 +384,8 @@ pandoc -f <源格式> -t <目标格式> input -o output ### 如何修改开放API? 1. 找到对应的Controller: `projects/fangjia-open/src/main/java/` -2. 修改业务逻辑: `projects/fangjia-service/` 或 `projects/fangjia-loan-service/` -3. 修改数据层: `projects/fangjia-dal/` 或 `projects/fangjia-loan-dal/` +2. 修改业务逻辑: `projects/fangjia-service/` +3. 修改数据层: `projects/fangjia-dal/` 4. 在`deliverables/fangjiaopen`构建测试 ### 如何运行测试? diff --git a/projects/fangjia-dal/src/main/java/com/fangjia/dal/mongo/model/AverageStatistics.java b/projects/fangjia-dal/src/main/java/com/fangjia/dal/mongo/model/AverageStatistics.java index 78a3844ff..f7f738dc3 100644 --- a/projects/fangjia-dal/src/main/java/com/fangjia/dal/mongo/model/AverageStatistics.java +++ b/projects/fangjia-dal/src/main/java/com/fangjia/dal/mongo/model/AverageStatistics.java @@ -1166,4 +1166,28 @@ public class AverageStatistics implements MongoEntity { public void setGxdEsfPrice(Integer gxdEsfPrice) { this.gxdEsfPrice = gxdEsfPrice; } + + /**幸福里均价数据*/ + private Integer xingfuliEsfPrice; + + @Column(name = "xingfuli_esf_price") + public Integer getXingfuliEsfPrice() { + return xingfuliEsfPrice; + } + + public void setXingfuliEsfPrice(Integer xingfuliEsfPrice) { + this.xingfuliEsfPrice = xingfuliEsfPrice; + } + + /**麦田均价数据*/ + private Integer maitianEsfPrice; + + @Column(name = "maitian_esf_price") + public Integer getMaitianEsfPrice() { + return maitianEsfPrice; + } + + public void setMaitianEsfPrice(Integer maitianEsfPrice) { + this.maitianEsfPrice = maitianEsfPrice; + } } diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/PropertyController.java b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/PropertyController.java index cf3a827b7..5b690b48b 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/PropertyController.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/PropertyController.java @@ -6859,6 +6859,108 @@ public class PropertyController { return mav; } + /** + * 小区第三方均价接口 + * ApiCode: 3004 + * 根据小区id获取最新月份的第三方均价 + */ + public Object thirdPartyPriceAction(@Param(name = "id") String id) { + RestfulModelAndView mav = new RestfulModelAndView(); + mav.setViewType("rest"); + + if (StringUtils.isBlank(id)) { + mav.setModel(BasicModel.buildBadRequestModel("参数缺失:id(小区ID)是必须参数")); + return mav; + } + + District district = districtService.findById(id); + if (district == null) { + mav.setModel(BasicModel.buildBadRequestModel("该小区不存在")); + return mav; + } + + List statsList = averageStatisticsService.findRecentMonths( + district.getCity(), district.getRegion(), district.getName(), district.getCategory(), 3); + + UniversalDto result = UniversalDto.newDto(); + result.append("id", id); + result.append("city", district.getCity()); + result.append("district", district.getRegion()); + result.append("name", district.getName()); + + if (statsList != null && !statsList.isEmpty()) { + result.append("date", statsList.get(0).getShortDate()); + + Integer xinfuliAvgPrice = null; + Integer anjukeAvgPrice = null; + Integer fangAvgPrice = null; + Integer lianjiaAvgPrice = null; + Integer cityhouseAvgPrice = null; + Integer jiwuAvgPrice = null; + Integer taofangAvgPrice = null; + Integer maitianAvgPrice = null; + + for (AverageStatistics stats : statsList) { + if (xinfuliAvgPrice == null && stats.getXingfuliEsfPrice() != null && stats.getXingfuliEsfPrice() > 0) { + xinfuliAvgPrice = stats.getXingfuliEsfPrice(); + } + if (anjukeAvgPrice == null) { + anjukeAvgPrice = getFirstValidThirdPrice(stats.getAnjukePrice(), stats.getAnjukeEsfPrice()); + } + if (fangAvgPrice == null && stats.getSoufangEsfPrice() != null && stats.getSoufangEsfPrice() > 0) { + fangAvgPrice = stats.getSoufangEsfPrice(); + } + if (lianjiaAvgPrice == null && stats.getLianjiaEsfPrice() != null && stats.getLianjiaEsfPrice() > 0) { + lianjiaAvgPrice = stats.getLianjiaEsfPrice(); + } + if (cityhouseAvgPrice == null && stats.getCityhouseEsfPrice() != null && stats.getCityhouseEsfPrice() > 0) { + cityhouseAvgPrice = stats.getCityhouseEsfPrice(); + } + if (jiwuAvgPrice == null && stats.getJiwuEsfPrice() != null && stats.getJiwuEsfPrice() > 0) { + jiwuAvgPrice = stats.getJiwuEsfPrice(); + } + if (taofangAvgPrice == null && stats.getTsfhouseEsfPrice() != null && stats.getTsfhouseEsfPrice() > 0) { + taofangAvgPrice = stats.getTsfhouseEsfPrice(); + } + if (maitianAvgPrice == null && stats.getMaitianEsfPrice() != null && stats.getMaitianEsfPrice() > 0) { + maitianAvgPrice = stats.getMaitianEsfPrice(); + } + } + + result.append("xinfuliAvgPrice", xinfuliAvgPrice); + result.append("anjukeAvgPrice", anjukeAvgPrice); + result.append("fangAvgPrice", fangAvgPrice); + result.append("lianjiaAvgPrice", lianjiaAvgPrice); + result.append("cityhouseAvgPrice", cityhouseAvgPrice); + result.append("jiwuAvgPrice", jiwuAvgPrice); + result.append("taofangAvgPrice", taofangAvgPrice); + result.append("maitianAvgPrice", maitianAvgPrice); + } else { + result.append("date", null); + result.append("xinfuliAvgPrice", null); + result.append("anjukeAvgPrice", null); + result.append("fangAvgPrice", null); + result.append("lianjiaAvgPrice", null); + result.append("cityhouseAvgPrice", null); + result.append("jiwuAvgPrice", null); + result.append("taofangAvgPrice", null); + result.append("maitianAvgPrice", null); + } + + List resultList = new ArrayList(); + resultList.add(result); + mav.setModel(BasicModel.buildOKModel().setResult(resultList)); + return mav; + } + + private Integer getFirstValidThirdPrice(Integer... prices) { + for (Integer p : prices) { + if (p != null && p > 0) { + return p; + } + } + return null; + } private List createDealHouseCase(List caseDtos, List dealPrices,int distance, Integer showSource){ diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/opentest/ThirdPartyPriceInterfaceTest.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/opentest/ThirdPartyPriceInterfaceTest.groovy new file mode 100644 index 000000000..2f95152d7 --- /dev/null +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/opentest/ThirdPartyPriceInterfaceTest.groovy @@ -0,0 +1,157 @@ +package com.fangjia.script.opentest + +import com.fangjia.core.util.HttpRequest +import com.fangjia.script.util.ExcelUtils +import net.sf.json.JSONArray +import net.sf.json.JSONObject +import org.apache.commons.lang.SystemUtils + +/** + * 小区第三方均价接口 - 接口测试脚本 + * 接口: /property/thirdPartyPrice + * + * 依赖:先运行 ThirdPartyPriceTestDataExtractor.groovy 生成测试数据 + * 输出: opentest/data/third_party_price_test_result.xlsx + * - Sheet1: 测试结果(详细数据) + * - Sheet2: 统计数据(汇总信息) + */ + +final String TOKEN = "F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB" +final String HOST = SystemUtils.IS_OS_WINDOWS ? "http://localhost:9180" : "http://open.fangjia.com" + +String scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent +String dataDir = new File(scriptDir, "data").absolutePath +String inputFile = "${dataDir}${File.separator}third_party_price_test_data.xlsx" +String outputFile = "${dataDir}${File.separator}third_party_price_test_result.xlsx" + +// 1. 读取测试数据(含表头) +List> allData = ExcelUtils.read(inputFile, 0, 0) + +// 2. 修改表头,添加结果列 +allData[0] = ["序号", "id", "city", "region", "name", + "date", "xinfuliAvgPrice", "anjukeAvgPrice", "fangAvgPrice", + "lianjiaAvgPrice", "cityhouseAvgPrice", "jiwuAvgPrice", + "taofangAvgPrice", "maitianAvgPrice", + "耗时(ms)", "异常信息"] + +// 3. 为每行添加空的结果列 +for (int i = 1; i < allData.size(); i++) { + List row = allData[i] + (5..15).each { row << "" } +} + +// 4. 写入初始结果文件 +ExcelUtils.write(outputFile, allData, "测试结果") + +// 5. 多线程调用接口 +ExcelUtils.multiThreadingModify(outputFile, { List row -> + String id = row[1]?.toString() ?: "" + if (!id) return + + HttpRequest request = new HttpRequest() + long startTime = System.currentTimeMillis() + + try { + String url = "${HOST}/property/thirdPartyPrice?id=${id}&token=${TOKEN}" + String response = request.get(url, "utf-8") + long duration = System.currentTimeMillis() - startTime + + JSONObject json = JSONObject.fromObject(response) + int code = json.optInt("code") + JSONArray resultArr = json.optJSONArray("result") + + if (code == 200 && resultArr != null && resultArr.size() > 0) { + JSONObject result = resultArr.getJSONObject(0) + row[5] = result.opt("date") + row[6] = result.opt("xinfuliAvgPrice") + row[7] = result.opt("anjukeAvgPrice") + row[8] = result.opt("fangAvgPrice") + row[9] = result.opt("lianjiaAvgPrice") + row[10] = result.opt("cityhouseAvgPrice") + row[11] = result.opt("jiwuAvgPrice") + row[12] = result.opt("taofangAvgPrice") + row[13] = result.opt("maitianAvgPrice") + row[14] = duration + row[15] = "" + } else { + row[14] = duration + row[15] = "接口返回错误(code=${code}): ${json.optString('msg', '')}" + } + + } catch (Exception e) { + long duration = System.currentTimeMillis() - startTime + row[14] = duration + row[15] = "异常: ${e.class.simpleName} - ${e.message}" + } +}, 0, "测试结果") + +// 6. 读取结果并统计 +List> finalData = ExcelUtils.read(outputFile, 0, 0) +int totalCount = finalData.size() - 1 +int successCount = 0 +int failCount = 0 +long totalTime = 0 + +Map timeDist = [ + "<100ms" : 0, + "100-200ms" : 0, + "200-500ms" : 0, + "500-1000ms": 0, + ">1000ms" : 0 +] + +for (int i = 1; i < finalData.size(); i++) { + List row = finalData[i] + String errorMsg = row[15]?.toString() ?: "" + + if (errorMsg == "") { + successCount++ + if (row[14] instanceof Number) { + int duration = ((Number) row[14]).intValue() + totalTime += duration + if (duration < 100) timeDist["<100ms"]++ + else if (duration < 200) timeDist["100-200ms"]++ + else if (duration < 500) timeDist["200-500ms"]++ + else if (duration < 1000) timeDist["500-1000ms"]++ + else timeDist[">1000ms"]++ + } + } else { + failCount++ + } +} + +// 7. 保存统计 Sheet +List> statsData = [] +statsData << ["统计项", "数值"] +statsData << ["测试时间", new Date().format("yyyy-MM-dd HH:mm:ss")] +statsData << ["", ""] +statsData << ["总测试数", totalCount] +statsData << ["成功数", successCount] +statsData << ["失败数", failCount] +statsData << ["成功率", totalCount > 0 ? String.format('%.2f%%', successCount * 100.0 / totalCount) : '0.00%'] + +if (successCount > 0) { + int avgTime = (totalTime / successCount).intValue() + statsData << ["", ""] + statsData << ["平均耗时(ms)", avgTime] + statsData << ["", ""] + statsData << ["耗时分布", "数量"] + timeDist.each { range, count -> statsData << [range, count] } +} + +ExcelUtils.write(outputFile, statsData, "统计数据") + +// 8. 控制台输出 +println "\n========== 测试完成 ==========" +println "总测试数:${totalCount}" +println "成功数:${successCount}" +println "失败数:${failCount}" +println "成功率:${totalCount > 0 ? String.format('%.2f%%', successCount * 100.0 / totalCount) : '0.00%'}" +if (successCount > 0) { + println "平均耗时:${(totalTime / successCount).intValue()} ms" + println "\n耗时分布:" + timeDist.each { range, count -> if (count > 0) println " ${range}: ${count} 条" } +} +println "\n结果文件:${outputFile}" +println " - Sheet1: 测试结果(详细数据)" +println " - Sheet2: 统计数据(汇总信息)" diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/opentest/ThirdPartyPriceTestDataExtractor.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/opentest/ThirdPartyPriceTestDataExtractor.groovy new file mode 100644 index 000000000..82dc8efe9 --- /dev/null +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/opentest/ThirdPartyPriceTestDataExtractor.groovy @@ -0,0 +1,54 @@ +package com.fangjia.script.opentest + +import com.fangjia.core.Constants +import com.fangjia.core.util.Mongo3Utils +import com.fangjia.script.util.ExcelUtils +import com.mongodb.client.MongoCollection +import com.mongodb.client.MongoCursor +import org.bson.Document + +/** + * 小区第三方均价接口 - 测试数据提取脚本 + * 接口: /property/thirdPartyPrice + * + * 从 fangjia.seaweed 提取福州小区数据作为测试用例 + * 输出: opentest/data/third_party_price_test_data.xlsx + */ + +String scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent +String dataDir = new File(scriptDir, "data").absolutePath +new File(dataDir).mkdirs() +String outputFile = "${dataDir}${File.separator}third_party_price_test_data.xlsx" + +MongoCollection districtColl = Mongo3Utils.getDbCollection( + Constants.FANGJIA_MONGO_URI, "fangjia", "seaweed" +) + +Document query = new Document("city", "福州").append("cat", "district").append("status", 0) +Document projection = new Document("_id", 1).append("city", 1).append("region", 1).append("name", 1) + +MongoCursor cursor = districtColl.find(query) + .projection(projection) + .limit(1000) + .iterator() + +List> excelData = [] +excelData << ["序号", "id", "city", "region", "name"] + +int index = 0 +while (cursor.hasNext()) { + Document doc = cursor.next() + excelData << [ + ++index, + doc.getObjectId("_id").toHexString(), + doc.getString("city") ?: "", + doc.getString("region") ?: "", + doc.getString("name") ?: "" + ] +} +cursor.close() + +ExcelUtils.write(outputFile, excelData, "测试数据") + +println "提取完成,共 ${index} 条数据" +println "输出文件:${outputFile}" diff --git a/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsService.java b/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsService.java index 4fff15570..e35134df3 100644 --- a/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsService.java +++ b/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsService.java @@ -19,4 +19,6 @@ public interface AverageStatisticsService { Map findFirstAverageStatictics(String city,String region,String name,String category, Date sDate); AverageStatistics save(AverageStatistics averageStatistics); + + List findRecentMonths(String city, String region, String name, String category, int months); } diff --git a/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsServiceImpl.java b/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsServiceImpl.java index fef0b66b5..e2290eda1 100644 --- a/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsServiceImpl.java +++ b/projects/fangjia-service/src/main/java/com/fangjia/service/mongo/fangjia/AverageStatisticsServiceImpl.java @@ -205,6 +205,25 @@ public class AverageStatisticsServiceImpl implements AverageStatisticsService { return averageStatistics; } + @Override + public List findRecentMonths(String city, String region, String name, String category, int months) { + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.MONTH, -(months - 1)); + int startDate = Integer.valueOf(DateUtil.formatByPattern(calendar.getTime(), "yyyyMM")); + + List queryParams = new ArrayList(); + queryParams.add(new QueryParam("city", city)); + queryParams.add(new QueryParam("region", region)); + queryParams.add(new QueryParam("name", name)); + queryParams.add(new QueryParam("category", category)); + queryParams.add(new QueryParam("shortDate", QueryParam.GREATER_THAN_EQUAL_OPERATOR, startDate)); + + List sortParams = new ArrayList(); + sortParams.add(new SortParam("shortDate", false)); + + return averageStatisticsDao.findAllByParams(queryParams, sortParams); + } + private boolean isValid(Integer value) { if (value != null && value > 1000) {