accounts.component.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 } from '@angular/core';
  17. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  18. import { _HttpClient } from '@delon/theme';
  19. import { format, addDays } from 'date-fns';
  20. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  21. import { NzMessageService } from 'ng-zorro-antd/message';
  22. import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
  23. import { NzTableQueryParams } from 'ng-zorro-antd/table';
  24. import { AccountsService } from '../../service/accounts.service';
  25. import { set2String } from '../../shared/index';
  26. import { SelectAppsComponent } from '../apps/select-apps/select-apps.component';
  27. import { AccountEditerComponent } from './account-editer/account-editer.component';
  28. @Component({
  29. selector: 'app-accounts',
  30. templateUrl: './accounts.component.html',
  31. styleUrls: ['./accounts.component.less']
  32. })
  33. export class AccountsComponent implements OnInit {
  34. query: {
  35. params: {
  36. username: String;
  37. displayName: String;
  38. employeeNumber: String;
  39. appName: String;
  40. startDate: String;
  41. endDate: String;
  42. startDatePicker: Date;
  43. endDatePicker: Date;
  44. pageSize: number;
  45. pageNumber: number;
  46. pageSizeOptions: number[];
  47. };
  48. results: {
  49. records: number;
  50. rows: NzSafeAny[];
  51. };
  52. expandForm: Boolean;
  53. submitLoading: boolean;
  54. tableLoading: boolean;
  55. tableCheckedId: Set<String>;
  56. indeterminate: boolean;
  57. checked: boolean;
  58. } = {
  59. params: {
  60. username: '',
  61. displayName: '',
  62. employeeNumber: '',
  63. appName: '',
  64. startDate: '',
  65. endDate: '',
  66. startDatePicker: addDays(new Date(), -30),
  67. endDatePicker: new Date(),
  68. pageSize: 10,
  69. pageNumber: 1,
  70. pageSizeOptions: [10, 20, 50]
  71. },
  72. results: {
  73. records: 0,
  74. rows: []
  75. },
  76. expandForm: false,
  77. submitLoading: false,
  78. tableLoading: false,
  79. tableCheckedId: new Set<String>(),
  80. indeterminate: false,
  81. checked: false
  82. };
  83. constructor(
  84. private modalService: NzModalService,
  85. private viewContainerRef: ViewContainerRef,
  86. private accountsService: AccountsService,
  87. private fb: FormBuilder,
  88. private msg: NzMessageService,
  89. private cdr: ChangeDetectorRef
  90. ) { }
  91. ngOnInit(): void {
  92. this.fetch();
  93. }
  94. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  95. this.query.params.pageNumber = tableQueryParams.pageIndex;
  96. this.query.params.pageSize = tableQueryParams.pageSize;
  97. this.fetch();
  98. }
  99. onSearch(): void {
  100. this.fetch();
  101. }
  102. onReset(): void { }
  103. onSelect(e: MouseEvent): void {
  104. e.preventDefault();
  105. const modal = this.modalService.create({
  106. nzContent: SelectAppsComponent,
  107. nzViewContainerRef: this.viewContainerRef,
  108. nzComponentParams: {},
  109. nzWidth: 700,
  110. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  111. });
  112. // Return a result when closed
  113. modal.afterClose.subscribe(result => {
  114. if (result.refresh) {
  115. this.query.params.appName = result.data.name;
  116. //this.query.params.appId = result.data.id;
  117. console.log(result);
  118. this.fetch();
  119. }
  120. });
  121. }
  122. onBatchDelete(e: MouseEvent): void {
  123. e.preventDefault();
  124. this.accountsService.delete(set2String(this.query.tableCheckedId)).subscribe(res => {
  125. if (res.code == 0) {
  126. this.msg.success(`提交成功`);
  127. this.fetch();
  128. } else {
  129. this.msg.success(`提交失败`);
  130. }
  131. this.cdr.detectChanges();
  132. });
  133. }
  134. onAdd(e: MouseEvent): void {
  135. e.preventDefault();
  136. const modal = this.modalService.create({
  137. nzContent: AccountEditerComponent,
  138. nzViewContainerRef: this.viewContainerRef,
  139. nzComponentParams: {
  140. isEdit: false,
  141. id: ''
  142. },
  143. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  144. });
  145. // Return a result when closed
  146. modal.afterClose.subscribe(result => {
  147. if (result.refresh) {
  148. this.fetch();
  149. }
  150. });
  151. }
  152. onEdit(e: MouseEvent, editiId: String): void {
  153. e.preventDefault();
  154. const modal = this.modalService.create({
  155. nzContent: AccountEditerComponent,
  156. nzViewContainerRef: this.viewContainerRef,
  157. nzComponentParams: {
  158. isEdit: true,
  159. id: editiId
  160. },
  161. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  162. });
  163. // Return a result when closed
  164. modal.afterClose.subscribe(result => {
  165. if (result.refresh) {
  166. this.fetch();
  167. }
  168. });
  169. }
  170. onDelete(e: MouseEvent, deleteId: String): void {
  171. e.preventDefault();
  172. this.accountsService.delete(deleteId).subscribe(res => {
  173. if (res.code == 0) {
  174. this.msg.success(`提交成功`);
  175. this.fetch();
  176. } else {
  177. this.msg.success(`提交失败`);
  178. }
  179. this.cdr.detectChanges();
  180. });
  181. }
  182. onUpdateStatus(e: MouseEvent, accountId: String, status: number): void {
  183. e.preventDefault();
  184. this.accountsService.updateStatus({ id: accountId, status: status }).subscribe(res => {
  185. if (res.code == 0) {
  186. this.msg.success(`提交成功`);
  187. this.fetch();
  188. } else {
  189. this.msg.success(`提交失败`);
  190. }
  191. this.cdr.detectChanges();
  192. });
  193. }
  194. fetch(): void {
  195. this.query.submitLoading = true;
  196. this.query.tableLoading = true;
  197. this.query.indeterminate = false;
  198. this.query.checked = false;
  199. this.query.tableCheckedId.clear();
  200. if (this.query.expandForm) {
  201. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  202. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  203. } else {
  204. this.query.params.endDate = '';
  205. this.query.params.startDate = '';
  206. }
  207. this.accountsService.fetch(this.query.params).subscribe(res => {
  208. this.query.results = res.data;
  209. this.query.submitLoading = false;
  210. this.query.tableLoading = false;
  211. this.cdr.detectChanges();
  212. });
  213. }
  214. updateTableCheckedSet(id: String, checked: boolean): void {
  215. if (checked) {
  216. this.query.tableCheckedId.add(id);
  217. } else {
  218. this.query.tableCheckedId.delete(id);
  219. }
  220. }
  221. refreshTableCheckedStatus(): void {
  222. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  223. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  224. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  225. }
  226. onTableItemChecked(id: String, checked: boolean): void {
  227. this.updateTableCheckedSet(id, checked);
  228. this.refreshTableCheckedStatus();
  229. }
  230. onTableAllChecked(checked: boolean): void {
  231. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  232. this.refreshTableCheckedStatus();
  233. }
  234. }