wangrong
2026-07-25 a97d3b4a19c0fc7586bd02f5b51f609904d5cf15
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
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';
 
/**
 * 数据源定义信息服务
 */
@Provide()
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、secret取值颠倒bug + 增加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;
  }
 
 
}