Browse Source

♻️ refactor(contracts-renew): 优化关联关系查询逻辑

- 移除contracts-renew接口中不必要的contract关联关系
- 修改通用CRUD服务的关联查询别名生成逻辑,使用relationIndex替代part作为别名后缀,避免关联关系名称冲突问题

♻️ refactor(utils): 改进通用CRUD服务的关联查询实现

- 更新关联关系处理注释,明确支持的嵌套关联格式
- 优化关联查询别名生成算法,提高查询稳定性和可靠性
yourname 8 months ago
parent
commit
4792e54129

+ 1 - 1
src/server/api/contracts-renew/index.ts

@@ -10,7 +10,7 @@ const hetongRenewRoutes = createCrudRoutes({
   getSchema: HetongRenewSchema,
   listSchema: HetongRenewSchema,
   searchFields: ['contractId', 'state', 'auditStatus'],
-  relations: ['contract', 'contract.client'],
+  relations: ['contract.client'],
   middleware: [authMiddleware]
 });
 

+ 3 - 3
src/server/utils/generic-crud.service.ts

@@ -26,14 +26,14 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
     const skip = (page - 1) * pageSize;
     const query = this.repository.createQueryBuilder('entity');
 
-    // 添加关联关系(支持嵌套关联,如 ['contract', 'contract.client'])
+    // 添加关联关系(支持嵌套关联,如 ['contract.client'])
     if (relations.length > 0) {
-      relations.forEach(relation => {
+      relations.forEach((relation, relationIndex) => {
         const parts = relation.split('.');
         let currentAlias = 'entity';
         
         parts.forEach((part, index) => {
-          const newAlias = index === 0 ? part : `${currentAlias}_${part}`;
+          const newAlias = index === 0 ? part : `${currentAlias}_${relationIndex}`;
           query.leftJoinAndSelect(`${currentAlias}.${part}`, newAlias);
           currentAlias = newAlias;
         });