71624423a4c11f34e0c088d028baec8fe6c06cd1
[usecase-ui.git] /
1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2 import { IntentManagementService } from '../../../core/services/intentManagement.service';
3 import { Util } from '../../../shared/utils/utils';
4 import {NzMessageService} from "ng-zorro-antd";
5
6 @Component({
7   selector: 'app-input-intent-management',
8   templateUrl: './input-intent-management.component.html',
9   styleUrls: ['../intent-management.component.less']
10 })
11 export class InputIntentManagementComponent implements OnInit {
12
13   constructor(
14     private myhttp: IntentManagementService,
15     private Util: Util,
16     private message: NzMessageService,
17   ) { }
18   defaultParams:Object={
19     intentId:'',
20     intentName:'',
21     intentExpectations:[
22       {
23         expectationId:'',
24         expectationName:'',
25         expectationType:'',
26         expectationObject:{
27           objectType:'',
28           objectInstance:''
29         },
30         expectationTargets:[
31           {
32             targetId:'',
33             targetName:'',
34             targetCondition:{
35               conditionId:'',
36               conditionName:'',
37               operator:'',
38               conditionValue:'',
39               conditionList:null
40             }
41           }
42         ]
43       }
44     ],
45   };
46   currentIndex:number = -1;
47   intentExpectationShow: boolean = false;
48   editExpectationTableList: Object={};
49
50   listOfData: any[] = [];
51
52   @Input() showModel: boolean;
53   @Input() editIntentTableData;
54   @Output() modalOpreation = new EventEmitter();
55
56   ngOnInit() {}
57   ngOnChanges() {
58     if (this.showModel) {
59       if (JSON.stringify(this.editIntentTableData)!=='{}') {
60         this.defaultParams=this.editIntentTableData
61         this.listOfData=this.editIntentTableData['intentExpectations']
62       }
63     }
64         }
65
66   handleCancel(): void {
67     this.showModel = false;
68     this.clearIntentData()
69     this.modalOpreation.emit({ "cancel": true });
70   }
71   handleOk(): void {
72     if(JSON.stringify(this.editIntentTableData)==='{}'){
73       this.defaultParams['intentId']=this.Util.getUuid()
74       this.createIntentInstance()
75     }else{
76       this.editIntentInstanceData()
77     }
78     this.clearIntentData()
79   }
80   clearIntentData(): void{
81     this.defaultParams = {
82       intentId:'',
83       intentName:'',
84       intentExpectations:[
85         {
86           expectationId:'',
87           expectationName:'',
88           expectationType:'',
89           expectationObject:{
90             objectType:'',
91             objectInstance:''
92           },
93           expectationTargets:[
94             {
95               targetId:'',
96               targetName:'',
97               targetCondition:{
98                 conditionId:'',
99                 conditionName:'',
100                 operator:'',
101                 conditionValue:'',
102                 conditionList:null
103               }
104             }
105           ]
106         }
107       ]
108     };
109     this.listOfData=[]
110   }
111
112   inputIntentExpectationShow(): void {
113     this.intentExpectationShow = true;
114   }
115   inputIntentExpectationClose($event: any): void {
116     this.intentExpectationShow = false;
117     this.editExpectationTableList={}
118     if ($event.cancel) {
119         return;
120     }
121     if(this.currentIndex>-1){
122       this.listOfData[this.currentIndex]=$event.param
123       this.currentIndex=-1
124     }else{
125       this.listOfData.push($event.param)
126     }
127     this.defaultParams['intentExpectations']=this.listOfData
128   }
129   editExpectationList(data,i): void {
130     this.editExpectationTableList=JSON.parse(JSON.stringify(data))
131     this.currentIndex=i
132     this.intentExpectationShow = true
133   }
134   deleteExpectationList(i): void{
135     this.listOfData.splice(i,1)
136   }
137
138   createIntentInstance(): void {
139     this.myhttp.createIntentManagement({
140       ...this.defaultParams
141     }).subscribe(
142       (response) => {
143         this.showModel = false;
144         this.modalOpreation.emit({ "cancel": false });
145         if(response.result_header.result_code===200){
146           this.message.success('Created successfully');
147         }else{
148           this.message.error(response.result_header.result_message);
149         }
150       },
151       (err) => {
152         this.showModel = false;
153         this.message.error('Created failed');
154       }
155     )
156   }
157   editIntentInstanceData(): void {
158     let id = this.defaultParams['intentId'];
159     let obj = this.defaultParams;
160     this.myhttp.updateIntentManagementData(id,obj).subscribe(
161       (response) => {
162         this.showModel = false;
163         this.modalOpreation.emit({ "cancel": false});
164         if(response.result_header.result_code===200){
165           this.message.success('Modification succeeded');
166         }else{
167           this.message.error(response.result_header.result_message);
168         }
169       },
170       (err) => {
171         this.showModel = false;
172         this.message.error('upload fail');
173       }
174     )
175   }
176 }