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;
|
}
|
}
|