cf9b221c78250ab653798cab6d4e945fbbf73efe
[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     selectedValue: string = BUSINESS_STATUS[0];
36     listOfData: any[] = [];
37     pageIndex: number = 1;
38     pageSize: number = 10;
39     total: number = 0;
40     loading = false;
41     isSelect: boolean = false;
42     statusOptions: any[] = BUSINESS_STATUS;
43     progressingTimer: any[] = [];
44     terminateStart: any[] = [];
45     @ViewChild('notification') notification1: any;
46
47     getBusinessList(): void {
48         this.loading = true;
49         this.isSelect = false;
50         this.listOfData = [];
51         let paramsObj = {
52             pageNo: this.pageIndex,
53             pageSize: this.pageSize
54         };
55         if (this.selectedValue !== BUSINESS_STATUS[0]) {
56             paramsObj["businessStatus"] = this.selectedValue.toLocaleLowerCase();
57             this.isSelect = true;
58         }
59         let getSlicingBusinessListFailedCallback =  () => {
60             this.loading = false;
61         }
62         this.myhttp.getSlicingBusinessList(paramsObj, this.isSelect, getSlicingBusinessListFailedCallback).then(res => {
63             const { result_body: { slicing_business_list, record_number } } = res;
64             this.loading = false;
65             this.total = record_number;
66                 if(slicing_business_list !==null && slicing_business_list.length >0){
67                     this.listOfData = slicing_business_list.map((item, index) => {
68                         if (item.last_operation_progress && item.last_operation_type && Number(item.last_operation_progress) < 100) {
69                             let updata = (prodata: { operation_progress: string })   => {
70                                 item.last_operation_progress = prodata.operation_progress || item.last_operation_progress;
71                             };
72                             let obj = {
73                                 serviceId: item.service_instance_id
74                             };
75                             if (item.last_operation_type.toUpperCase() === 'DELETE') this.terminateStart[index] = true
76                             else this.terminateStart[index] = false;
77                             this.queryProgress(obj, item.orchestration_status, index, updata).then((res) => {
78                                 item.last_operation_progress = '100';
79                                 this.getBusinessList();
80                             })
81                         }
82                         return item
83                     });
84                 }
85         })
86     }
87     getListOfProcessingStatus() {
88         this.pageIndex = 1;
89         this.pageSize = 10;
90         this.progressingTimer.forEach((item) => {
91             clearInterval(item.timer);
92         });
93         this.progressingTimer = [];
94         this.getBusinessList();
95     }
96     searchData(reset: boolean = false) {
97         this.progressingTimer.forEach((item) => {
98             clearInterval(item.timer);
99         });
100         this.progressingTimer = [];
101         this.getBusinessList();
102     }
103     switchChange(slicing, i) {
104         this.modalService.confirm({
105             nzTitle: '<i>Do you Want to ' + (slicing.orchestration_status === 'activated' ? 'deactivate' : 'activate') + ' slicing business?</i>',
106             nzContent: '<b>Name:' + slicing.service_instance_name + '</b>',
107             nzOnOk: () => {
108                 this.notification1.notificationStart('slicing business', slicing.orchestration_status === 'activated' ? 'deactivate' : 'activate', slicing.service_instance_id);
109                 let paramsObj = {
110                     serviceId: slicing.service_instance_id
111                 };
112                 if (slicing.orchestration_status === 'activated') {
113                     this.changeActivate(paramsObj, false, slicing, "deactivate", "deactivated", i)
114                 } else {
115                     this.changeActivate(paramsObj, true, slicing, "activate", "activated", i);
116                 }
117             },
118             nzCancelText: 'No',
119             nzOnCancel: () => {
120                 let singleSlicing = Object.assign({}, this.listOfData[i]);
121                 this.listOfData[i] = singleSlicing;
122                 this.listOfData = [...this.listOfData];
123             }
124         });
125     }
126     changeActivate(paramsObj, isActivate, slicing, activateValue, finished, index) {
127         this.loading = true;
128         let changeActivateFailedCallback = () => {
129             this.loading = false;
130             let singleSlicing = Object.assign({}, this.listOfData[index]);
131             this.listOfData[index] = singleSlicing;
132             this.listOfData = [...this.listOfData];
133             this.notification1.notificationFailed('slicing business', finished, slicing.service_instance_id);
134             this.getBusinessList();
135         }
136         this.myhttp.changeActivateSlicingService(paramsObj, isActivate, changeActivateFailedCallback).then(res => {
137             this.loading = false;
138             this.notification1.notificationSuccess('slicing business', finished, slicing.service_instance_id);
139             this.getBusinessList();
140         })
141     }
142     terminate(slicing,index) {
143         this.modalService.confirm({
144             nzTitle: 'Do you Want to terminate slicing business?',
145             nzContent: '<b>Name:&nbsp;</b>' + slicing.service_instance_name,
146             nzOnOk: () => {
147                 this.notification1.notificationStart('slicing business', 'terminate', slicing.service_instance_id);
148                 let paramsObj = { serviceId: slicing.service_instance_id };
149                 this.terminateStart[index] = true;
150                 this.loading = true;
151                 let terminateFailedCallback  = () => {
152                     this.loading = false;
153                     this.notification1.notificationFailed('slicing business', 'terminate', slicing.service_instance_id);
154                     this.terminateStart[index] = false;
155                 }
156                 this.myhttp.terminateSlicingService(paramsObj, terminateFailedCallback).then(res => {
157                     this.loading = false;
158                     this.notification1.notificationSuccess('slicing business', 'terminate', slicing.service_instance_id);
159                     this.getBusinessList();
160                 })
161             },
162             nzCancelText: 'No',
163             nzOnCancel: () => {
164                 console.info('Cancel termination')
165             }
166         });
167     }
168     showdetail(data) {
169         const BusinessModal = this.modalService.create({
170             nzTitle: "Detail",
171             nzContent: SlicingBusinessModelComponent,
172             nzWidth: "70%",
173             nzOkText: null,
174             nzCancelText: null,
175             nzComponentParams: {
176                 businessId: data.service_instance_id
177             }
178         })
179     }
180     queryProgress(obj, action, index, callback) {
181         return new Promise(res => {
182             let requery = () => {
183                 let queryProgressFailedCallback  = () => {
184                     this.progressingTimer.forEach((item) => {
185                         if (item.serviceId === obj.serviceId) {
186                             clearInterval(item.timer);
187                         }
188                     });
189                     this.getBusinessList();
190                 }
191                 this.myhttp.getSlicingBusinessProgress(obj, queryProgressFailedCallback)
192                     .then((data) => {
193                         if (data.result_body.operation_progress && Number(data.result_body.operation_progress) < 100) {
194                             callback(data.result_body);
195                             let progressSetTimeOut = setTimeout(() => {
196                                 requery();
197                             }, 5000);
198                             this.progressingTimer.push({
199                                 id: obj.serviceId,
200                                 timer: progressSetTimeOut
201                             })
202                         } else {
203                             this.progressingTimer.forEach((item) => {
204                                 if (item.serviceId === obj.serviceId) {
205                                     clearInterval(item.timer);
206                                 }
207                             });
208                             res(data.result_body);
209                         }
210                     })
211             };
212             requery();
213         })
214     }
215 }