From a9a1c709f56bb7c4361aa04b7cc647baaed74fcd Mon Sep 17 00:00:00 2001
From: wangrong <wangrong@shsening.com>
Date: 星期四, 13 八月 2026 11:05:34 +0800
Subject: [PATCH] 修复部分bug和修改校区监控摄像头合集显示

---
 src/modules/basicdata/service/site.ts |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 103 insertions(+), 5 deletions(-)

diff --git a/src/modules/basicdata/service/site.ts b/src/modules/basicdata/service/site.ts
index c41130b..7bd89ce 100644
--- a/src/modules/basicdata/service/site.ts
+++ b/src/modules/basicdata/service/site.ts
@@ -1,14 +1,112 @@
-import { Provide } from '@midwayjs/core';
+import { Provide, Inject, Context } from '@midwayjs/core';
 import { BaseService } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
 import { BasicdataSiteEntity } from '../entity/site';
+import { BaseSysDepartmentEntity } from '../../base/entity/sys/department';
+import { WithDeptFilterWhere } from '../../base/middleware/with-dept-filter-where';
+import { Context as KoaContext } from '@midwayjs/koa';
 
-/**
- * 绔欑偣鏈嶅姟
- */
 @Provide()
 export class BasicdataSiteService extends BaseService {
   @InjectEntityModel(BasicdataSiteEntity)
   basicdataSiteEntity: Repository<BasicdataSiteEntity>;
-}
+
+  @InjectEntityModel(BaseSysDepartmentEntity)
+  baseSysDepartmentEntity: Repository<BaseSysDepartmentEntity>;
+
+  @Inject()
+  ctx: KoaContext;
+
+  /**
+   * 閲嶅啓 page 鈥斺�� 鏄惧紡鍒楀嚭瀛楁锛岄伩鍏� a.* 鐨勫埆鍚嶉棶棰�
+   */
+  async page(query: any) {
+    const {
+      page = 1,
+      size = 20,
+      departmentId,
+      siteType,
+      status,
+      keyWord,
+      sort,
+      order,
+    } = query;
+    const qb = this.basicdataSiteEntity
+      .createQueryBuilder('a')
+      .leftJoin(BaseSysDepartmentEntity, 'b', 'a.departmentId = b.id')
+      .select([
+        'a.id',
+        'a.businessId',
+        'a.businessName',
+        'a.departmentId',
+        'a.siteType',
+        'a.address',
+        'a.leader',
+        'a.longitude',
+        'a.latitude',
+        'a.photo',
+        'a.status',
+        'a.remark',
+        'a.createTime',
+        'a.updateTime',
+        'b.name AS departmentName',
+      ]);
+    const authConds = await WithDeptFilterWhere(this.ctx, {
+      alias: 'a',
+      field: 'departmentId',
+    });
+    for (const [sql, params] of authConds) {
+      qb.andWhere(sql, params);
+    }
+    if (departmentId) {
+      const deptIds = await this.getDepartmentAndChildren(Number(departmentId));
+      qb.andWhere('a.departmentId IN (:deptIds)', { deptIds });
+    }
+    if (siteType !== undefined && siteType !== '') {
+      qb.andWhere('a.siteType = :siteType', { siteType });
+    }
+    if (status !== undefined && status !== '') {
+      qb.andWhere('a.status = :status', { status: Number(status) });
+    }
+    if (keyWord) {
+      qb.andWhere(
+        '(a.businessId LIKE :kw OR a.businessName LIKE :kw OR a.address LIKE :kw OR a.leader LIKE :kw)',
+        { kw: `%${keyWord}%` }
+      );
+    }
+    const sortField = order || 'createTime';
+    const sortDir = sort === 'asc' ? 'ASC' : 'DESC';
+    qb.orderBy(`a.${sortField}`, sortDir);
+    const skip = (Number(page) - 1) * Number(size);
+    const [list, total] = await qb
+      .offset(skip)
+      .limit(Number(size))
+      .getManyAndCount();
+
+    return {
+      list,
+      pagination: { page: Number(page), size: Number(size), total },
+    };
+  }
+
+  /**
+   * 閫掑綊鑾峰彇閮ㄩ棬鍙婂叾鎵�鏈夊瓙閮ㄩ棬 ID
+   */
+  async getDepartmentAndChildren(deptId: number): Promise<number[]> {
+    const result: number[] = [deptId];
+    const findChildren = async (pid: number) => {
+      const children = await this.baseSysDepartmentEntity
+        .createQueryBuilder('d')
+        .select('d.id')
+        .where('d.parentId = :pid', { pid })
+        .getMany();
+      for (const c of children) {
+        result.push(c.id);
+        await findChildren(c.id);
+      }
+    };
+    await findChildren(deptId);
+    return result;
+  }
+}
\ No newline at end of file

--
Gitblit v1.9.1