sessions.component.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit, Inject } from '@angular/core';
  17. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  18. import { I18NService } from '@core';
  19. import { _HttpClient, ALAIN_I18N_TOKEN, SettingsService } from '@delon/theme';
  20. import { format, addDays } from 'date-fns';
  21. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  22. import { NzMessageService } from 'ng-zorro-antd/message';
  23. import { NzTableQueryParams } from 'ng-zorro-antd/table';
  24. import { SessionsService } from '../../../service/sessions.service';
  25. import { set2String } from '../../../shared/index';
  26. @Component({
  27. selector: 'app-sessions',
  28. templateUrl: './sessions.component.html',
  29. styleUrls: ['./sessions.component.less']
  30. })
  31. export class SessionsComponent implements OnInit {
  32. query: {
  33. params: {
  34. username: String;
  35. displayName: String;
  36. employeeNumber: String;
  37. startDate: String;
  38. endDate: String;
  39. startDatePicker: Date;
  40. endDatePicker: Date;
  41. pageSize: number;
  42. pageNumber: number;
  43. pageSizeOptions: number[];
  44. };
  45. results: {
  46. records: number;
  47. rows: NzSafeAny[];
  48. };
  49. expandForm: Boolean;
  50. submitLoading: boolean;
  51. tableLoading: boolean;
  52. tableCheckedId: Set<String>;
  53. indeterminate: boolean;
  54. checked: boolean;
  55. } = {
  56. params: {
  57. username: '',
  58. displayName: '',
  59. employeeNumber: '',
  60. startDate: '',
  61. endDate: '',
  62. startDatePicker: addDays(new Date(), -30),
  63. endDatePicker: new Date(),
  64. pageSize: 10,
  65. pageNumber: 1,
  66. pageSizeOptions: [10, 20, 50]
  67. },
  68. results: {
  69. records: 0,
  70. rows: []
  71. },
  72. expandForm: false,
  73. submitLoading: false,
  74. tableLoading: false,
  75. tableCheckedId: new Set<String>(),
  76. indeterminate: false,
  77. checked: false
  78. };
  79. constructor(
  80. private fb: FormBuilder,
  81. private sessionsService: SessionsService,
  82. private msg: NzMessageService,
  83. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  84. private cdr: ChangeDetectorRef
  85. ) { }
  86. ngOnInit(): void {
  87. this.fetch();
  88. }
  89. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  90. this.query.params.pageNumber = tableQueryParams.pageIndex;
  91. this.query.params.pageSize = tableQueryParams.pageSize;
  92. this.fetch();
  93. }
  94. onSearch(): void {
  95. this.fetch();
  96. }
  97. onReset(): void { }
  98. onTerminate(e: MouseEvent): void {
  99. e.preventDefault();
  100. this.sessionsService.delete(set2String(this.query.tableCheckedId)).subscribe(res => {
  101. if (res.code == 0) {
  102. this.msg.success(this.i18n.fanyi('mxk.alert.operate.success'));
  103. this.fetch();
  104. } else {
  105. this.msg.error(this.i18n.fanyi('mxk.alert.operate.error'));
  106. }
  107. this.cdr.detectChanges();
  108. });
  109. }
  110. fetch(): void {
  111. this.query.submitLoading = true;
  112. this.query.tableLoading = true;
  113. this.query.indeterminate = false;
  114. this.query.checked = false;
  115. this.query.tableCheckedId.clear();
  116. if (this.query.expandForm) {
  117. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  118. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  119. } else {
  120. this.query.params.endDate = '';
  121. this.query.params.startDate = '';
  122. }
  123. this.sessionsService.fetch(this.query.params).subscribe(res => {
  124. this.query.results = res.data;
  125. this.query.submitLoading = false;
  126. this.query.tableLoading = false;
  127. this.cdr.detectChanges();
  128. });
  129. }
  130. updateTableCheckedSet(id: String, checked: boolean): void {
  131. if (checked) {
  132. this.query.tableCheckedId.add(id);
  133. } else {
  134. this.query.tableCheckedId.delete(id);
  135. }
  136. }
  137. refreshTableCheckedStatus(): void {
  138. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  139. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  140. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  141. }
  142. onTableItemChecked(id: String, checked: boolean): void {
  143. this.updateTableCheckedSet(id, checked);
  144. this.refreshTableCheckedStatus();
  145. }
  146. onTableAllChecked(checked: boolean): void {
  147. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  148. this.refreshTableCheckedStatus();
  149. }
  150. }