synchronizers.component.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, 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 { SynchronizersService } from '../../../service/synchronizers.service';
  26. import { set2String } from '../../../shared/index';
  27. import { SynchronizerEditerComponent } from './synchronizer-editer/synchronizer-editer.component';
  28. @Component({
  29. selector: 'app-synchronizers',
  30. templateUrl: './synchronizers.component.html',
  31. styleUrls: ['./synchronizers.component.less']
  32. })
  33. export class SynchronizersComponent implements OnInit {
  34. query: {
  35. params: {
  36. name: String;
  37. displayName: String;
  38. protocol: String;
  39. startDate: String;
  40. endDate: String;
  41. startDatePicker: Date;
  42. endDatePicker: Date;
  43. pageSize: number;
  44. pageNumber: number;
  45. pageSizeOptions: number[];
  46. };
  47. results: {
  48. records: number;
  49. rows: NzSafeAny[];
  50. };
  51. expandForm: Boolean;
  52. submitLoading: boolean;
  53. tableLoading: boolean;
  54. tableCheckedId: Set<String>;
  55. indeterminate: boolean;
  56. checked: boolean;
  57. } = {
  58. params: {
  59. name: '',
  60. displayName: '',
  61. protocol: '',
  62. startDate: '',
  63. endDate: '',
  64. startDatePicker: addDays(new Date(), -30),
  65. endDatePicker: new Date(),
  66. pageSize: 10,
  67. pageNumber: 1,
  68. pageSizeOptions: [10, 20, 50]
  69. },
  70. results: {
  71. records: 0,
  72. rows: []
  73. },
  74. expandForm: false,
  75. submitLoading: false,
  76. tableLoading: false,
  77. tableCheckedId: new Set<String>(),
  78. indeterminate: false,
  79. checked: false
  80. };
  81. constructor(
  82. private modalService: NzModalService,
  83. private synchronizersService: SynchronizersService,
  84. private viewContainerRef: ViewContainerRef,
  85. private fb: FormBuilder,
  86. private msg: NzMessageService,
  87. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  88. private cdr: ChangeDetectorRef
  89. ) { }
  90. ngOnInit(): void {
  91. this.fetch();
  92. }
  93. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  94. this.query.params.pageNumber = tableQueryParams.pageIndex;
  95. this.query.params.pageSize = tableQueryParams.pageSize;
  96. this.fetch();
  97. }
  98. onSearch(): void {
  99. this.fetch();
  100. }
  101. onReset(): void { }
  102. onBatchDelete(e: MouseEvent): void {
  103. e.preventDefault();
  104. this.synchronizersService.delete(set2String(this.query.tableCheckedId)).subscribe(res => {
  105. if (res.code == 0) {
  106. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  107. this.fetch();
  108. } else {
  109. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  110. }
  111. this.cdr.detectChanges();
  112. });
  113. }
  114. onSynchr(e: MouseEvent, synchrId: String): void {
  115. this.synchronizersService.synchr(synchrId).subscribe(res => {
  116. if (res.code == 0) {
  117. this.msg.success(this.i18n.fanyi('mxk.alert.operate.success'));
  118. } else {
  119. this.msg.error(this.i18n.fanyi('mxk.alert.operate.error'));
  120. }
  121. this.cdr.detectChanges();
  122. });
  123. }
  124. onAdd(e: MouseEvent): void {
  125. e.preventDefault();
  126. const modal = this.modalService.create({
  127. nzContent: SynchronizerEditerComponent,
  128. nzViewContainerRef: this.viewContainerRef,
  129. nzComponentParams: {
  130. isEdit: false,
  131. id: ''
  132. },
  133. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  134. });
  135. // Return a result when closed
  136. modal.afterClose.subscribe(result => {
  137. if (result.refresh) {
  138. this.fetch();
  139. }
  140. });
  141. }
  142. onEdit(e: MouseEvent, editId: String): void {
  143. e.preventDefault();
  144. const modal = this.modalService.create({
  145. nzContent: SynchronizerEditerComponent,
  146. nzViewContainerRef: this.viewContainerRef,
  147. nzComponentParams: {
  148. isEdit: true,
  149. id: editId
  150. },
  151. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  152. });
  153. // Return a result when closed
  154. modal.afterClose.subscribe(result => {
  155. if (result.refresh) {
  156. this.fetch();
  157. }
  158. });
  159. }
  160. onDelete(e: MouseEvent, deleteId: String): void {
  161. e.preventDefault();
  162. this.synchronizersService.delete(deleteId).subscribe(res => {
  163. if (res.code == 0) {
  164. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  165. this.fetch();
  166. } else {
  167. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  168. }
  169. this.cdr.detectChanges();
  170. });
  171. }
  172. fetch(): void {
  173. this.query.submitLoading = true;
  174. this.query.tableLoading = true;
  175. this.query.indeterminate = false;
  176. this.query.checked = false;
  177. this.query.tableCheckedId.clear();
  178. if (this.query.expandForm) {
  179. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  180. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  181. } else {
  182. this.query.params.endDate = '';
  183. this.query.params.startDate = '';
  184. }
  185. this.synchronizersService.fetch(this.query.params).subscribe(res => {
  186. this.query.results = res.data;
  187. this.query.submitLoading = false;
  188. this.query.tableLoading = false;
  189. this.cdr.detectChanges();
  190. });
  191. }
  192. updateTableCheckedSet(id: String, checked: boolean): void {
  193. if (checked) {
  194. this.query.tableCheckedId.add(id);
  195. } else {
  196. this.query.tableCheckedId.delete(id);
  197. }
  198. }
  199. refreshTableCheckedStatus(): void {
  200. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  201. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  202. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  203. }
  204. onTableItemChecked(id: String, checked: boolean): void {
  205. this.updateTableCheckedSet(id, checked);
  206. this.refreshTableCheckedStatus();
  207. }
  208. onTableAllChecked(checked: boolean): void {
  209. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  210. this.refreshTableCheckedStatus();
  211. }
  212. }