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