wangrong
2026-07-25 c2c8841967d9ab95e1bda8c65c2e37c8f0511005
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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 { BaseSysParamEntity } from '../../base/entity/sys/param';
import * as _ from 'lodash';
 
/**
 * 数据源定义信息服务
 */
@Provide()
export class BasicdataIotService extends BaseService {
  @InjectEntityModel(BasicdataIotEntity)
  basicdataIotEntity: Repository<BasicdataIotEntity>;
 
  @InjectEntityModel(BaseSysDepartmentEntity)
  baseSysDepartmentEntity: Repository<BaseSysDepartmentEntity>;
 
  @InjectEntityModel(BaseSysParamEntity)
  baseSysParamEntity: Repository<BaseSysParamEntity>;
 
  @Inject()
  baseSysPermsService: BaseSysPermsService;
 
  @Inject()
  ctx;
 
  /**
   * 获得部门+站点合并树(遵循 Cool 数据权限规范)
   */
  async sitelist() {
    const { userId, username } = this.ctx.admin;
 
    // 超管不受限
    const isAdmin = username === 'admin';
    let deptIds: any[] = [];
 
    if (!isAdmin) {
      deptIds = await this.baseSysPermsService.departmentIds(userId);
      // 没配部门权限时给个必定为空的值,防止查出全库
      if (_.isEmpty(deptIds)) deptIds = [null];
    }
 
    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
      ${isAdmin ? 'WHERE 1=1' : 'WHERE a.id IN (?)'}
    )
    UNION ALL
    (
      SELECT
        b.businessId   AS a_id,
        a.tenantId     AS a_tenantId,
        b.businessName AS a_name,
        b.departmentId AS a_parentId,
        COALESCE(a.orderNum, 999) AS a_orderNum
      FROM t_basicdata_site b
      LEFT JOIN base_sys_department a ON b.departmentId = a.id
      ${!isAdmin ? 'WHERE b.departmentId IN (?)' : ''}
    )
    ORDER BY a_orderNum ASC
  `;
 
    // nativeQuery 第二个参数必须显式传参数组
    const params = isAdmin ? [] : [deptIds, deptIds];
 
    return this.nativeQuery(sql, params);
  }
 
  /**
   * 监控设备查询
   */
  async getYsDevices(businessId: string) {
    // 修复原先key、secret取值颠倒bug + 增加await
    // const appKey_result = await this.baseSysParamEntity.findOneBy({ keyName: '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();
    return result;
  }
 
 
}