wangrong
7 天以前 a9a1c709f56bb7c4361aa04b7cc647baaed74fcd
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
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;
  }
}