group-members-editer.component.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, ViewContainerRef, Input, 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 { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
  24. import { NzTableQueryParams } from 'ng-zorro-antd/table';
  25. import { Groups } from '../../../../entity/Groups';
  26. import { GroupMembersService } from '../../../../service/group-members.service';
  27. @Component({
  28. selector: 'app-group-members-editer',
  29. templateUrl: './group-members-editer.component.html',
  30. styleUrls: ['./group-members-editer.component.less']
  31. })
  32. export class GroupMembersEditerComponent implements OnInit {
  33. @Input() groupId?: String;
  34. @Input() isEdit?: boolean;
  35. query: {
  36. params: {
  37. name: String;
  38. displayName: String;
  39. username: String;
  40. groupId: String;
  41. startDate: String;
  42. endDate: String;
  43. startDatePicker: Date;
  44. endDatePicker: Date;
  45. pageSize: number;
  46. pageNumber: number;
  47. pageSizeOptions: number[];
  48. };
  49. results: {
  50. records: number;
  51. rows: NzSafeAny[];
  52. };
  53. expandForm: Boolean;
  54. submitLoading: boolean;
  55. tableLoading: boolean;
  56. tableCheckedId: Set<String>;
  57. indeterminate: boolean;
  58. checked: boolean;
  59. } = {
  60. params: {
  61. name: '',
  62. displayName: '',
  63. username: '',
  64. groupId: '',
  65. startDate: '',
  66. endDate: '',
  67. startDatePicker: addDays(new Date(), -30),
  68. endDatePicker: new Date(),
  69. pageSize: 5,
  70. pageNumber: 1,
  71. pageSizeOptions: [5, 15, 50]
  72. },
  73. results: {
  74. records: 0,
  75. rows: []
  76. },
  77. expandForm: false,
  78. submitLoading: false,
  79. tableLoading: false,
  80. tableCheckedId: new Set<String>(),
  81. indeterminate: false,
  82. checked: false
  83. };
  84. constructor(
  85. private modalRef: NzModalRef,
  86. private groupMembersService: GroupMembersService,
  87. private viewContainerRef: ViewContainerRef,
  88. private fb: FormBuilder,
  89. private msg: NzMessageService,
  90. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  91. private cdr: ChangeDetectorRef
  92. ) { }
  93. ngOnInit(): void {
  94. if (this.groupId) {
  95. this.query.params.groupId = this.groupId;
  96. this.fetch();
  97. }
  98. }
  99. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  100. this.query.params.pageNumber = tableQueryParams.pageIndex;
  101. this.query.params.pageSize = tableQueryParams.pageSize;
  102. this.fetch();
  103. }
  104. onSearch(): void {
  105. this.fetch();
  106. }
  107. onReset(): void { }
  108. fetch(): void {
  109. this.query.submitLoading = true;
  110. this.query.tableLoading = true;
  111. this.query.indeterminate = false;
  112. this.query.checked = false;
  113. this.query.tableCheckedId.clear();
  114. if (this.query.expandForm) {
  115. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  116. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  117. } else {
  118. this.query.params.endDate = '';
  119. this.query.params.startDate = '';
  120. }
  121. this.groupMembersService.memberOut(this.query.params).subscribe(res => {
  122. this.query.results = res.data;
  123. this.query.submitLoading = false;
  124. this.query.tableLoading = false;
  125. this.cdr.detectChanges();
  126. });
  127. }
  128. updateTableCheckedSet(id: String, checked: boolean): void {
  129. if (checked) {
  130. this.query.tableCheckedId.add(id);
  131. } else {
  132. this.query.tableCheckedId.delete(id);
  133. }
  134. }
  135. refreshTableCheckedStatus(): void {
  136. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  137. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  138. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  139. }
  140. onTableItemChecked(id: String, checked: boolean): void {
  141. //this.onTableAllChecked(false);
  142. this.updateTableCheckedSet(id, checked);
  143. this.refreshTableCheckedStatus();
  144. }
  145. onTableAllChecked(checked: boolean): void {
  146. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  147. this.refreshTableCheckedStatus();
  148. }
  149. onSubmit(e: MouseEvent): void {
  150. e.preventDefault();
  151. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  152. let selectedData = listOfEnabledData.filter(({ id, name }) => {
  153. return this.query.tableCheckedId.has(id);
  154. });
  155. let memberIds = '';
  156. let memberNames = '';
  157. for (let i = 0; i < selectedData.length; i++) {
  158. memberIds = `${memberIds},${selectedData[i].id}`;
  159. memberNames = `${memberNames},${selectedData[i].username}`;
  160. }
  161. this.groupMembersService.add({ groupId: this.groupId, memberId: memberIds, memberName: memberNames }).subscribe(res => {
  162. this.query.results = res.data;
  163. this.query.submitLoading = false;
  164. this.query.tableLoading = false;
  165. if (res.code == 0) {
  166. this.msg.success(this.i18n.fanyi('mxk.alert.operate.success'));
  167. this.fetch();
  168. } else {
  169. this.msg.error(this.i18n.fanyi('mxk.alert.operate.error'));
  170. }
  171. this.cdr.detectChanges();
  172. });
  173. }
  174. onClose(e: MouseEvent): void {
  175. e.preventDefault();
  176. }
  177. }