doctor-mail/apps/server/src/models/department/department.row.service.ts

92 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-12-30 08:26:40 +08:00
/**
* RowCacheService
* SQL
*/
import { Injectable } from '@nestjs/common';
import {
db,
DepartmentMethodSchema,
ObjectType,
UserProfile,
2025-01-06 08:45:23 +08:00
} from '@nice/common';
2024-12-30 08:26:40 +08:00
import { date, z } from 'zod';
import { RowCacheService } from '../base/row-cache.service';
import { isFieldCondition } from '../base/sql-builder';
@Injectable()
export class DepartmentRowService extends RowCacheService {
/**
* DepartmentRowService
*
*/
constructor() {
super(ObjectType.DEPARTMENT, false);
}
/**
* SQL
* @param requset - DepartmentMethodSchema.getRows schema
* @returns SQL
*/
createUnGroupingRowSelect(
requset: z.infer<typeof DepartmentMethodSchema.getRows>,
): string[] {
// 调用父类方法生成基础查询字段,并拼接部门特定的字段
const result = super.createUnGroupingRowSelect(requset).concat([
`${this.tableName}.name AS name`, // 部门名称
`${this.tableName}.is_domain AS is_domain`, // 是否为域
`${this.tableName}.order AS order`, // 排序
`${this.tableName}.has_children AS has_children`, // 是否有子部门
`${this.tableName}.parent_id AS parent_id` // 父部门 ID
]);
return result;
}
/**
* getRows
* @param request - DepartmentMethodSchema.getRows schema
* @param staff -
* @returns
*/
protected createGetRowsFilters(
request: z.infer<typeof DepartmentMethodSchema.getRows>,
staff: UserProfile,
) {
// 调用父类方法生成基础过滤条件
const condition = super.createGetRowsFilters(request);
const { parentId, includeDeleted = false } = request;
// 如果条件已经是字段条件,则跳过后续处理
if (isFieldCondition(condition)) {
return;
}
// 如果请求中没有分组键,则添加父部门 ID 过滤条件
if (request.groupKeys.length === 0) {
if (parentId) {
condition.AND.push({
field: `${this.tableName}.parent_id`,
value: parentId,
op: 'equals', // 等于操作符
});
} else if (parentId === null) {
condition.AND.push({
field: `${this.tableName}.parent_id`,
op: "blank", // 空白操作符
});
}
}
// 如果 includeDeleted 为 false则排除已删除的行
if (!includeDeleted) {
condition.AND.push({
field: `${this.tableName}.deleted_at`,
type: 'date',
op: 'blank', // 空白操作符
});
}
return condition;
}
}