| | |
| | | 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 { BaseSysParamEntity } from '../../base/entity/sys/param'; |
| | | import * as _ from 'lodash'; |
| | | |
| | | /** |
| | |
| | | @InjectEntityModel(BaseSysDepartmentEntity) |
| | | baseSysDepartmentEntity: Repository<BaseSysDepartmentEntity>; |
| | | |
| | | @Inject() |
| | | baseSysParamService: BaseSysParamService; |
| | | @InjectEntityModel(BaseSysParamEntity) |
| | | baseSysParamEntity: Repository<BaseSysParamEntity>; |
| | | |
| | | @Inject() |
| | | baseSysPermsService: BaseSysPermsService; |
| | |
| | | @Inject() |
| | | ctx; |
| | | |
| | | |
| | | /** |
| | | * 获得部门菜单 |
| | | * 获得部门+站点合并树(遵循 Cool 数据权限规范) |
| | | */ |
| | | async list() { |
| | | const permsDepartmentArr = await this.baseSysPermsService.departmentIds( |
| | | this.ctx.admin.userId |
| | | ); |
| | | // 过滤部门权限 |
| | | 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 |
| | | 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; |
| | | ( |
| | | 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 ys7_appKey: string = await this.baseSysParamService.dataByKey('ys7.appKey'); |
| | | // 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') |
| | |
| | | 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; |
| | | return result; |
| | | } |
| | | |
| | | |