a5409926a3509daf79582536c1d6ae7409effad9
[usecase-ui.git] /
1 import {Component, Input, OnInit, ViewChild,SimpleChanges} from '@angular/core';
2 import { SlicingTaskServices } from '.././../../../../../core/services/slicingTaskServices';
3 import { BUSINESS_STATUS } from '../../../../../../../constants/constants';
4 import { NzModalService, NzMessageService } from 'ng-zorro-antd';
5 import { SlicingBusinessModelComponent } from '../slicing-business-model/slicing-business-model.component';
6 @Component({
7     selector: 'app-slicing-business-table',
8     templateUrl: './slicing-business-table.component.html',
9     styleUrls: ['./slicing-business-table.component.less']
10 })
11 export class SlicingBusinessTableComponent implements OnInit {
12
13     constructor(
14         private myhttp: SlicingTaskServices,
15         private modalService: NzModalService,
16         private message: NzMessageService
17     ) {
18     }
19
20     @Input() currentTabName;
21
22     ngOnChanges(changes: SimpleChanges) {
23         if(changes.currentTabName.currentValue === 'Slicing Business Management'){
24             this.getBusinessList()
25         }else {
26             this.progressingTimer.forEach((item) => {
27                 clearInterval(item.timer);
28             });
29             this.progressingTimer = [];
30         }
31     }
32     ngOnInit() {
33
34     }
35     ngOnDestroy() {
36         this.progressingTimer.forEach((item) => {
37             clearInterval(item.timer)
38         })
39     }
40     selectedValue: string = BUSINESS_STATUS[0];
41     listOfData: any[] = [];
42     pageIndex: number = 1;
43     pageSize: number = 10;
44     total: number = 0;
45     loading = false;
46     isSelect: boolean = false;
47     statusOptions: any[] = BUSINESS_STATUS;
48     progressingTimer: any[] = [];
49     terminateStart: any[] = [];
50     @ViewChild('notification') notification1: any;
51
52     getBusinessList(): void {
53         this.loading = true;
54         this.isSelect = false;
55         this.listOfData = [];
56         let paramsObj = {
57             pageNo: this.pageIndex,
58             pageSize: this.pageSize
59         };
60         if (this.selectedValue !== BUSINESS_STATUS[0]) {
61             paramsObj["businessStatus"] = this.selectedValue.toLocaleLowerCase();
62             this.isSelect = true;
63         }
64         let getSlicingBusinessListFailedCallback =  () => {
65             this.loading = false;
66         }
67         this.myhttp.getSlicingBusinessList(paramsObj, this.isSelect, getSlicingBusinessListFailedCallback).then(res => {
68             const { result_body: { slicing_business_list, record_number } } = res;
69             this.loading = false;
70             this.total = record_number;
71                 if(slicing_business_list !==null && slicing_business_list.length >0){
72                     this.listOfData = slicing_business_list.map((item, index) => {
73                         if (item.last_operation_progress && item.last_operation_type && Number(item.last_operation_progress) < 100) {
74                             let updata = (prodata: { operation_progress: string })   => {
75                                 item.last_operation_progress = prodata.operation_progress || item.last_operation_progress;
76                             };
77                             let obj = {
78                                 serviceId: item.service_instance_id
79                             };
80                             if (item.last_operation_type.toUpperCase() === 'DELETE') this.terminateStart[index] = true
81                             else this.terminateStart[index] = false;
82                             this.queryProgress(obj, item.orchestration_status, index, updata).then((res) => {
83                                 item.last_operation_progress = '100';
84                                 this.getBusinessList();
85                             })
86                         }
87                         return item
88                     });
89                 }
90         })
91     }
92     getListOfProcessingStatus() {
93         this.pageIndex = 1;
94         this.pageSize = 10;
95         this.progressingTimer.forEach((item) => {
96             clearInterval(item.timer);
97         });
98         this.progressingTimer = [];
99         this.getBusinessList();
100     }
101     searchData(reset: boolean = false) {
102         this.progressingTimer.forEach((item) => {
103             clearInterval(item.timer);
104         });
105         this.progressingTimer = [];
106         this.getBusinessList();
107     }
108     switchChange(slicing, i) {
109         this.modalService.confirm({
110             nzTitle: '<i>Do you Want to ' + (slicing.orchestration_status === 'activated' ? 'deactivate' : 'activate') + ' slicing business?</i>',
111             nzContent: '<b>Name:' + slicing.service_instance_name + '</b>',
112             nzOnOk: () => {
113                 this.notification1.notificationStart('slicing business', slicing.orchestration_status === 'activated' ? 'deactivate' : 'activate', slicing.service_instance_id);
114                 let paramsObj = {
115                     serviceId: slicing.service_instance_id
116                 };
117                 if (slicing.orchestration_status === 'activated') {
118                     this.changeActivate(paramsObj, false, slicing, "deactivate", "deactivated", i)
119                 } else {
120                     this.changeActivate(paramsObj, true, slicing, "activate", "activated", i);
121                 }
122             },
123             nzCancelText: 'No',
124             nzOnCancel: () => {
125                 let singleSlicing = Object.assign({}, this.listOfData[i]);
126                 this.listOfData[i] = singleSlicing;
127                 this.listOfData = [...this.listOfData];
128             }
129         });
130     }
131     changeActivate(paramsObj, isActivate, slicing, activateValue, finished, index) {
132         this.loading = true;
133         let changeActivateFailedCallback = () => {
134             this.loading = false;
135             let singleSlicing = Object.assign({}, this.listOfData[index]);
136             this.listOfData[index] = singleSlicing;
137             this.listOfData = [...this.listOfData];
138             this.notification1.notificationFailed('slicing business', finished, slicing.service_instance_id);
139             this.getBusinessList();
140         }
141         this.myhttp.changeActivateSlicingService(paramsObj, isActivate, changeActivateFailedCallback).then(res => {
142             this.loading = false;
143             this.notification1.notificationSuccess('slicing business', finished, slicing.service_instance_id);
144             this.getBusinessList();
145         })
146     }
147     terminate(slicing,index) {
148         this.modalService.confirm({
149             nzTitle: 'Do you Want to terminate slicing business?',
150             nzContent: '<b>Name:&nbsp;</b>' + slicing.service_instance_name,
151             nzOnOk: () => {
152                 this.notification1.notificationStart('slicing business', 'terminate', slicing.service_instance_id);
153                 let paramsObj = { serviceId: slicing.service_instance_id };
154                 this.terminateStart[index] = true;
155                 this.loading = true;
156                 let terminateFailedCallback  = () => {
157                     this.loading = false;
158                     this.notification1.notificationFailed('slicing business', 'terminate', slicing.service_instance_id);
159                     this.terminateStart[index] = false;
160                 }
161                 this.myhttp.terminateSlicingService(paramsObj, terminateFailedCallback).then(res => {
162                     this.loading = false;
163                     this.notification1.notificationSuccess('slicing business', 'terminate', slicing.service_instance_id);
164                     this.getBusinessList();
165                 })
166             },
167             nzCancelText: 'No',
168             nzOnCancel: () => {
169                 console.info('Cancel termination')
170             }
171         });
172     }
173     showdetail(data) {
174         const BusinessModal = this.modalService.create({
175             nzTitle: "Detail",
176             nzContent: SlicingBusinessModelComponent,
177             nzWidth: "70%",
178             nzOkText: null,
179             nzCancelText: null,
180             nzComponentParams: {
181                 businessId: data.service_instance_id,
182                 outerData:data
183             }
184         })
185     }
186     queryProgress(obj, action, index, callback) {
187         return new Promise(res => {
188             let requery = () => {
189                 let queryProgressFailedCallback  = () => {
190                     this.progressingTimer.forEach((item) => {
191                         if (item.serviceId === obj.serviceId) {
192                             clearInterval(item.timer);
193                         }
194                     });
195                     this.getBusinessList();
196                 }
197                 this.myhttp.getSlicingBusinessProgress(obj, queryProgressFailedCallback)
198                     .then((data) => {
199                         if (data.result_body.operation_progress && Number(data.result_body.operation_progress) < 100) {
200                             callback(data.result_body);
201                             let progressSetTimeOut = setTimeout(() => {
202                                 requery();
203                             }, 5000);
204                             this.progressingTimer.push({
205                                 id: obj.serviceId,
206                                 timer: progressSetTimeOut
207                             })
208                         } else {
209                             this.progressingTimer.forEach((item) => {
210                                 if (item.serviceId === obj.serviceId) {
211                                     clearInterval(item.timer);
212                                 }
213                             });
214                             res(data.result_body);
215                         }
216                     })
217             };
218             requery();
219         })
220     }
221 }