alert.service.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { HttpClient, HttpParams } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { Observable } from 'rxjs';
  4. import { Alert } from '../pojo/Alert';
  5. import { Message } from '../pojo/Message';
  6. import { Page } from '../pojo/Page';
  7. const alerts_uri = '/alerts';
  8. const alerts_summary_uri = '/alerts/summary';
  9. const alerts_status_uri = '/alerts/status';
  10. @Injectable({
  11. providedIn: 'root'
  12. })
  13. export class AlertService {
  14. constructor(private http: HttpClient) {}
  15. public getAlerts(pageIndex: number, pageSize: number): Observable<Message<Page<Alert>>> {
  16. pageIndex = pageIndex ? pageIndex : 0;
  17. pageSize = pageSize ? pageSize : 8;
  18. // 注意HttpParams是不可变对象 需要保存set后返回的对象为最新对象
  19. let httpParams = new HttpParams();
  20. httpParams = httpParams.appendAll({
  21. sort: 'id',
  22. order: 'desc',
  23. status: '0',
  24. pageIndex: pageIndex,
  25. pageSize: pageSize
  26. });
  27. const options = { params: httpParams };
  28. return this.http.get<Message<Page<Alert>>>(alerts_uri, options);
  29. }
  30. public searchAlerts(
  31. status: number | undefined,
  32. priority: number | undefined,
  33. content: string | undefined,
  34. pageIndex: number,
  35. pageSize: number
  36. ): Observable<Message<Page<Alert>>> {
  37. pageIndex = pageIndex ? pageIndex : 0;
  38. pageSize = pageSize ? pageSize : 8;
  39. // 注意HttpParams是不可变对象 需要保存set后返回的对象为最新对象
  40. let httpParams = new HttpParams();
  41. httpParams = httpParams.appendAll({
  42. sort: 'id',
  43. order: 'desc',
  44. pageIndex: pageIndex,
  45. pageSize: pageSize
  46. });
  47. if (status != undefined && status != 9) {
  48. httpParams = httpParams.append('status', status);
  49. }
  50. if (priority != undefined && priority != 9) {
  51. httpParams = httpParams.append('priority', priority);
  52. }
  53. if (content != undefined && content != '' && content.trim() != '') {
  54. httpParams = httpParams.append('content', content.trim());
  55. }
  56. const options = { params: httpParams };
  57. return this.http.get<Message<Page<Alert>>>(alerts_uri, options);
  58. }
  59. public deleteAlerts(alertIds: Set<number>): Observable<Message<any>> {
  60. let httpParams = new HttpParams();
  61. alertIds.forEach(alertId => {
  62. // 注意HttpParams是不可变对象 需要保存append后返回的对象为最新对象
  63. // append方法可以叠加同一key, set方法会把key之前的值覆盖只留一个key-value
  64. httpParams = httpParams.append('ids', alertId);
  65. });
  66. const options = { params: httpParams };
  67. return this.http.delete<Message<any>>(alerts_uri, options);
  68. }
  69. public applyAlertsStatus(alertIds: Set<number>, status: number): Observable<Message<any>> {
  70. let httpParams = new HttpParams();
  71. alertIds.forEach(alertId => {
  72. // 注意HttpParams是不可变对象 需要保存append后返回的对象为最新对象
  73. // append方法可以叠加同一key, set方法会把key之前的值覆盖只留一个key-value
  74. httpParams = httpParams.append('ids', alertId);
  75. });
  76. const options = { params: httpParams };
  77. return this.http.put<Message<any>>(`${alerts_status_uri}/${status}`, null, options);
  78. }
  79. public getAlertsSummary(): Observable<Message<any>> {
  80. return this.http.get<Message<any>>(alerts_summary_uri);
  81. }
  82. }