From a97d3b4a19c0fc7586bd02f5b51f609904d5cf15 Mon Sep 17 00:00:00 2001
From: wangrong <wangrong@shsening.com>
Date: 星期六, 25 七月 2026 12:12:00 +0800
Subject: [PATCH] 萤石云设备对接

---
 src/modules/basicdata/service/iot.ts |  105 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 104 insertions(+), 1 deletions(-)

diff --git a/src/modules/basicdata/service/iot.ts b/src/modules/basicdata/service/iot.ts
index c97c924..a39f305 100644
--- a/src/modules/basicdata/service/iot.ts
+++ b/src/modules/basicdata/service/iot.ts
@@ -1,8 +1,12 @@
-import { Provide } from '@midwayjs/core';
+import { Inject, Provide } from '@midwayjs/core';
 import { BaseService } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
+import { BaseSysDepartmentEntity } from '../../base/entity/sys/department';
 import { BasicdataIotEntity } from '../entity/iot';
+import { BaseSysPermsService } from '../../base/service/sys/perms';
+import { BaseSysParamService } from '../../base/service/sys/param';
+import * as _ from 'lodash';
 
 /**
  * 鏁版嵁婧愬畾涔変俊鎭湇鍔�
@@ -11,4 +15,103 @@
 export class BasicdataIotService extends BaseService {
   @InjectEntityModel(BasicdataIotEntity)
   basicdataIotEntity: Repository<BasicdataIotEntity>;
+
+  @InjectEntityModel(BaseSysDepartmentEntity)
+  baseSysDepartmentEntity: Repository<BaseSysDepartmentEntity>;
+
+  @Inject()
+  baseSysParamService: BaseSysParamService;
+
+  @Inject()
+  baseSysPermsService: BaseSysPermsService;
+
+  @Inject()
+  ctx;
+
+
+  /**
+   * 鑾峰緱閮ㄩ棬鑿滃崟
+   */
+  async list() {
+    const permsDepartmentArr = await this.baseSysPermsService.departmentIds(
+      this.ctx.admin.userId
+    );
+    // 杩囨护閮ㄩ棬鏉冮檺
+    const sql = `
+        (
+            SELECT 
+                a.id AS a_id,
+                a.tenantId AS a_tenantId,
+                a.name AS a_name,
+                a.parentId AS a_parentId,
+                a.orderNum AS a_orderNum
+            FROM base_sys_department a 
+            WHERE 1=1 
+                ${this.setSql(
+                  this.ctx.admin.username !== 'admin',
+                  `and (a.departmentId in (?) `,
+                  [!_.isEmpty(permsDepartmentArr) ? permsDepartmentArr : [null]]
+                )}             
+        )
+        UNION ALL
+        (
+            SELECT 
+                b.businessId AS a_id,
+                a.tenantId AS a_tenantId,
+                b.businessName AS a_name,
+                b.departmentId AS a_parentId,
+                a.orderNum AS a_orderNum
+            FROM t_basicdata_site b
+            LEFT JOIN base_sys_department a ON b.departmentId = a.id
+        )
+        ORDER BY a_orderNum ASC`;
+    const result = await this.nativeQuery(sql);
+    return result;    
+  }
+
+  /**
+   * 鐩戞帶璁惧鏌ヨ
+   */
+  async getYsDevices(businessId: string) {
+    // 淇鍘熷厛key銆乻ecret鍙栧�奸鍊抌ug + 澧炲姞await
+    const ys7_appKey: string = await this.baseSysParamService.dataByKey('ys7.appKey');
+    // departmentId 鏄暟瀛楋紝灏濊瘯杞崲锛涜浆鎹㈠け璐ヨ祴鍊� null
+    const deptId = /^\d+$/.test(businessId) ? Number(businessId) : null;
+    const qb = this.basicdataIotEntity.createQueryBuilder('a')
+      .select([
+        'a.id',
+        'a.iotCode',
+        'a.iotName',
+        'a.iotTypeCode',
+        'a.businessType',
+        'a.businessId',
+        'a.departmentId',
+        'a.iotKey',
+        'a.iotSecret',
+        'a.iotChanel',
+        'a.installLocation',
+        'a.protocol'
+      ])
+      .where('a.iotTypeCode IN (:...typeIds)', {
+        typeIds: ['钀ょ煶浜戝浐瀹氱洃鎺�', '钀ょ煶浜戣溅杞界洃鎺�'],
+      });
+    if (deptId !== null) {
+      qb.andWhere('(a.businessId = :businessId OR a.departmentId = :deptId)', {
+        businessId,
+        deptId
+      });
+    } else {
+      qb.andWhere('a.businessId = :businessId', { businessId });
+    }
+    const result = await qb.orderBy('a.iotName', 'ASC').getMany();
+    const deviceList = result.map(item => {
+      return {
+        ...item,
+        iotKey: ys7_appKey
+      };
+    });
+    return deviceList;
+  }
+
+
 }
\ No newline at end of file

--
Gitblit v1.9.1