我的jHipster项目中有4个实体:用户,部门,userAssignmentToDepartments(用户可能属于多个部门),订单。订单实体的字段为“ department_id”。问题是:
2。如何获取具有哪个用户权限的部门ID?
order.routes.ts
export const orderRoute: Routes = [
{
path: 'order',
component: OrderComponent,
resolve: {
pagingParams: JhiResolvePagingParams
},
data: {
authorities: ['ROLE_USER'],
defaultSort: 'id,asc',
pageTitle: 'Orders'
},
canActivate: [UserRouteAccessService]
}...
order.service.ts
export class OrderService {
query(req?: any): Observable<EntityArrayResponseType> {
const options = createRequestOption(req);
return this.http
.get<IOrder[]>(this.resourceUrl, { params: options, observe: 'response' })
.pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
}.....
order.component.ts
export class OrderComponent implements OnInit, OnDestroy {
loadAll() {
this.orderService
.query({
page: this.page - 1,
size: this.itemsPerPage,
sort: this.sort()
})
.subscribe(
(res: HttpResponse<IOrder[]>) => this.paginateOrders(res.body, res.headers),
(res: HttpErrorResponse) => this.onError(res.message)
);
}
department.model.ts
export interface IDepartment {
id?: number;
name?: string;
}
export class Departmetn implements IDepartment {
constructor(public id?: number, public name?: string) {}
}
order.model.ts
import { Moment } from 'moment';
export interface IOrder {
id?: number;
date?: Moment;
departmetnId?: number
}
export class Order implements IOrder {
constructor(public id?: number, public date?: Moment, public departmentId?: number) {}
}
user.model.ts
export interface IUser {
id?: number;
}
export class User implements IUser {
constructor(public id?: number) {}
}
user-assignment.ts
export interface IUserAssignment {
id?: number;
userLogin?: string;
userId?: number;
centerName?: string;
centerId?: number;
}
export class UserAssignment implements IUserAssignment {
constructor(
public id?: number,
public userLogin?: string,
public userId?: number,
public departmentName?: string,
public departmentId?: number
) {}
}
答案 0 :(得分:0)
假设这些是您的任务和订单列表
userAssignments : IUserAssignment[]
orders : IOrder[]
用于获取用户的userAssignmentToDepartments中包含department_id的订单
getOrders(userID : number){
var deptList : number[] = []
for(let assignment of this.userAssignments){
if(assignment.userId==userID){
deptList.push(assignment.departmentId);
}
}
var orderList : IOrder[]
for(let order of this.orders){
if(deptList.find(dept => dept==order.departmentId)){
orderList.push(order);
}
}
console.log(orderList);
}
要获得部门授权以特定用户使用,
getAuthorizedDepts(userID : number){
var deptList : number[] = []
for(let assignment of this.userAssignments){
if(assignment.userId==userID){
deptList.push(assignment.departmentId);
}
}
console.log(deptList)
}