import { Component, OnInit } from '@angular/core'; import { NzModalService } from 'ng-zorro-antd/modal'; import { NzNotificationService } from 'ng-zorro-antd/notification'; import { finalize } from 'rxjs/operators'; import { NoticeReceiver } from '../../../pojo/NoticeReceiver'; import { NoticeRule } from '../../../pojo/NoticeRule'; import { NoticeReceiverService } from '../../../service/notice-receiver.service'; import { NoticeRuleService } from '../../../service/notice-rule.service'; @Component({ selector: 'app-alert-notice', templateUrl: './alert-notice.component.html', styles: [] }) export class AlertNoticeComponent implements OnInit { constructor( private notifySvc: NzNotificationService, private noticeReceiverSvc: NoticeReceiverService, private modal: NzModalService, private noticeRuleSvc: NoticeRuleService ) {} receivers!: NoticeReceiver[]; receiverTableLoading: boolean = true; rules!: NoticeRule[]; ruleTableLoading: boolean = true; ngOnInit(): void { this.loadReceiversTable(); this.loadRulesTable(); } syncReceiver() { this.loadReceiversTable(); } syncRule() { this.loadRulesTable(); } loadReceiversTable() { this.receiverTableLoading = true; let receiverInit$ = this.noticeReceiverSvc.getReceivers().subscribe( message => { this.receiverTableLoading = false; if (message.code === 0) { this.receivers = message.data; } else { console.warn(message.msg); } receiverInit$.unsubscribe(); }, error => { console.error(error.msg); this.receiverTableLoading = false; receiverInit$.unsubscribe(); } ); } loadRulesTable() { this.ruleTableLoading = true; let rulesInit$ = this.noticeRuleSvc.getNoticeRules().subscribe( message => { this.ruleTableLoading = false; if (message.code === 0) { this.rules = message.data; } else { console.warn(message.msg); } rulesInit$.unsubscribe(); }, error => { console.error(error.msg); this.ruleTableLoading = false; rulesInit$.unsubscribe(); } ); } onDeleteOneNoticeReceiver(receiveId: number) { this.modal.confirm({ nzTitle: '请确认是否删除!', nzOkText: '确定', nzCancelText: '取消', nzOkDanger: true, nzOkType: 'primary', nzOnOk: () => this.deleteOneNoticeReceiver(receiveId) }); } deleteOneNoticeReceiver(receiveId: number) { const deleteReceiver$ = this.noticeReceiverSvc .deleteReceiver(receiveId) .pipe( finalize(() => { deleteReceiver$.unsubscribe(); }) ) .subscribe( message => { if (message.code === 0) { this.notifySvc.success('删除成功!', ''); this.loadReceiversTable(); } else { this.notifySvc.error('删除失败!', message.msg); } }, error => { this.notifySvc.error('删除失败!', error.msg); } ); } onDeleteOneNoticeRule(ruleId: number) { this.modal.confirm({ nzTitle: '请确认是否删除!', nzOkText: '确定', nzCancelText: '取消', nzOkDanger: true, nzOkType: 'primary', nzOnOk: () => this.deleteOneNoticeRule(ruleId) }); } deleteOneNoticeRule(ruleId: number) { const deleteRule$ = this.noticeRuleSvc .deleteNoticeRule(ruleId) .pipe( finalize(() => { deleteRule$.unsubscribe(); }) ) .subscribe( message => { if (message.code === 0) { this.notifySvc.success('删除成功!', ''); this.loadRulesTable(); } else { this.notifySvc.error('删除失败!', message.msg); } }, error => { this.notifySvc.error('删除失败!', error.msg); } ); } // start 新增或修改通知接收人弹出框 isManageReceiverModalVisible: boolean = false; isManageReceiverModalAdd: boolean = true; isManageReceiverModalOkLoading: boolean = false; receiver!: NoticeReceiver; onNewNoticeReceiver() { this.receiver = new NoticeReceiver(); this.isManageReceiverModalVisible = true; this.isManageReceiverModalAdd = true; } onEditOneNoticeReceiver(receiver: NoticeReceiver) { this.receiver = receiver; this.isManageReceiverModalVisible = true; this.isManageReceiverModalAdd = false; } onManageReceiverModalCancel() { this.isManageReceiverModalVisible = false; } onManageReceiverModalOk() { this.isManageReceiverModalOkLoading = true; if (this.isManageReceiverModalAdd) { const modalOk$ = this.noticeReceiverSvc .newReceiver(this.receiver) .pipe( finalize(() => { modalOk$.unsubscribe(); this.isManageReceiverModalOkLoading = false; }) ) .subscribe( message => { if (message.code === 0) { this.isManageReceiverModalVisible = false; this.notifySvc.success('新增成功!', ''); this.loadReceiversTable(); } else { this.notifySvc.error('新增失败!', message.msg); } }, error => { this.notifySvc.error('新增失败!', error.msg); } ); } else { const modalOk$ = this.noticeReceiverSvc .editReceiver(this.receiver) .pipe( finalize(() => { modalOk$.unsubscribe(); this.isManageReceiverModalOkLoading = false; }) ) .subscribe( message => { if (message.code === 0) { this.isManageReceiverModalVisible = false; this.notifySvc.success('修改成功!', ''); this.loadReceiversTable(); } else { this.notifySvc.error('修改失败!', message.msg); } }, error => { this.notifySvc.error('修改失败!', error.msg); } ); } } // start 新增或修改通知策略弹出框 isManageRuleModalVisible: boolean = false; isManageRuleModalAdd: boolean = true; isManageRuleModalOkLoading: boolean = false; rule!: NoticeRule; receiversOption: any[] = []; onNewNoticeRule() { this.rule = new NoticeRule(); this.isManageRuleModalVisible = true; this.isManageRuleModalAdd = true; } onEditOneNoticeRule(rule: NoticeRule) { this.rule = rule; this.isManageRuleModalVisible = true; this.isManageRuleModalAdd = false; this.receiversOption.push({ value: rule.receiverId, label: rule.receiverName }); } loadReceiversOption() { let receiverOption$ = this.noticeReceiverSvc.getReceivers().subscribe( message => { if (message.code === 0) { let data = message.data; this.receiversOption = []; data.forEach(item => { let label = `${item.name}-`; switch (item.type) { case 0: label = `${label}Phone`; break; case 1: label = `${label}Email`; break; case 2: label = `${label}WebHook`; break; case 3: label = `${label}WeChat`; break; case 4: label = `${label}WeWorkRobot`; } this.receiversOption.push({ value: item.id, label: label }); }); } else { console.warn(message.msg); } receiverOption$.unsubscribe(); }, error => { console.error(error.msg); receiverOption$.unsubscribe(); } ); } onManageRuleModalCancel() { this.isManageRuleModalVisible = false; } onManageRuleModalOk() { this.receiversOption.forEach(option => { if (option.value == this.rule.receiverId) { this.rule.receiverName = option.label; } }); this.isManageRuleModalOkLoading = true; if (this.isManageRuleModalAdd) { const modalOk$ = this.noticeRuleSvc .newNoticeRule(this.rule) .pipe( finalize(() => { modalOk$.unsubscribe(); this.isManageRuleModalOkLoading = false; }) ) .subscribe( message => { if (message.code === 0) { this.isManageRuleModalVisible = false; this.notifySvc.success('新增成功!', ''); this.loadRulesTable(); } else { this.notifySvc.error('新增失败!', message.msg); } }, error => { this.notifySvc.error('新增失败!', error.msg); } ); } else { const modalOk$ = this.noticeRuleSvc .editNoticeRule(this.rule) .pipe( finalize(() => { modalOk$.unsubscribe(); this.isManageRuleModalOkLoading = false; }) ) .subscribe( message => { if (message.code === 0) { this.isManageRuleModalVisible = false; this.notifySvc.success('修改成功!', ''); this.loadRulesTable(); } else { this.notifySvc.error('修改失败!', message.msg); } }, error => { this.notifySvc.error('修改失败!', error.msg); } ); } } }