import { Context } from '@midwayjs/koa';


export interface DeptFilterOptions {
  alias?: string;
  field?: string;
}

export async function WithDeptFilterWhere(
  ctx: Context,
  options: DeptFilterOptions = {}
): Promise<any[][]> {
  const { alias = 'a', field = 'departmentId' } = options;
  const admin = ctx.admin;
  if (!admin || admin.username == 'admin') return [];

  const deptIds = admin.departmentIds;
  if (!Array.isArray(deptIds) || deptIds.length === 0) {
    return [['1 = 0']];
  }

  return [
    [
      `${alias}.${field} IN (:departmentIds)`,
      { departmentIds: deptIds },
    ],
  ];
}