liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { api, wire,LightningElement } from 'lwc';
import { CurrentPageReference } from "lightning/navigation";
import { CloseActionScreenEvent } from 'lightning/actions';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import init  from '@salesforce/apex/OpportunityLightingButtonController.initForNewOpportunityAgencyButton';
import queryForRecords from '@salesforce/apex/OpportunityLightingButtonController.queryForNewOpportunityAgencyButtonToRecords';
import queryForRecTypeList from '@salesforce/apex/OpportunityLightingButtonController.queryForNewOpportunityAgencyButtonToRecTypeList';
import queryForRecords2 from '@salesforce/apex/OpportunityLightingButtonController.queryForNewOpportunityAgencyButtonToRecords2';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
import lwcCSS from '@salesforce/resourceUrl/lwcCSS';
import {loadStyle} from 'lightning/platformResourceLoader';
 
export default class LexNewOpportunityAgency extends NavigationMixin(LightningElement) {
    @api recordId;
    agency1Id;
    agencyOpportunity;
    trade;
    hospitalId;
    name;
    closeForecastedDate;
    bidPlannedDate;
    dealerFinalPrice;
    wholesalePrice;
    agency1;
    agency1Name;
    opportunityStage;
    opportunityCategory;
    IsLoading = true;
    @wire(CurrentPageReference)
    getStateParameters(currentPageReference) {
            console.log(111);
            console.log(currentPageReference);
 
        if (currentPageReference) {
          const urlValue = currentPageReference.state.recordId;
          if (urlValue) {
            let str = `${urlValue}`;
            console.log("str");
            console.log(str);
            this.recordId = str;
          }
        }
    }
    connectedCallback(){
        Promise.all([
            loadStyle(this, lwcCSS)
           ]);
           
        init({
            recordId: this.recordId
        }).then(result=>{
            console.log(result);
            this.agency1Id = result.agency1Id;
            this.agencyOpportunity = result.agencyOpportunity;
            this.trade = result.trade;
            this.hospitalId = result.hospitalId;
            this.name = result.name;
            this.closeForecastedDate = result.closeForecastedDate;
            this.bidPlannedDate = result.bidPlannedDate;
            this.dealerFinalPrice = result.dealerFinalPrice;
            this.wholesalePrice = result.wholesalePrice;
            this.agency1 = result.agency1;
            this.opportunityStage = result.opportunityStage;
            this.opportunityCategory = result.opportunityCategory;
            this.agency1Name = result.agency1Name;
            console.log("1");
            if(this.closeForecastedDate == undefined){
                this.closeForecastedDate = '';
            }else{
                this.closeForecastedDate = new String(this.closeForecastedDate).replaceAll("-","/");
            }
            console.log(this.closeForecastedDate);
            if(this.bidPlannedDate == undefined){
                this.bidPlannedDate = '';
            }else{
                this.bidPlannedDate = new String(this.bidPlannedDate).replaceAll("-","/");
            }
            if(this.dealerFinalPrice == undefined){
                this.dealerFinalPrice = '';
            }
            console.log(this.bidPlannedDate);
            console.log(this.dealerFinalPrice);
            this.newOpportunityAgency();
        }).catch(error=>{
            console.log("error");
            console.log(error);
        })
    }
    newOpportunityAgency(){
        
            console.log("2");
            var newflag = true;
            if (this.agency1Id == undefined) {
                newflag = false;
                this.showToast("经销商不存在,不能转成【经销商目标询价】。","error");
                this.dispatchEvent(new CloseActionScreenEvent());
            }
            else if (this.agencyOpportunity != undefined) {
                newflag = false;
                this.showToast("已经转成过【经销商目标询价】。","error");
                this.dispatchEvent(new CloseActionScreenEvent());
            }
            else if (this.trade != '内貿') {
                newflag = false;
                this.showToast("不是内贸,不能转成【经销商目标询价】。","error");
                this.dispatchEvent(new CloseActionScreenEvent());
            }
            else {
                var records = null;
                console.log("3");
                queryForRecords({
                    agency1Id: this.agency1Id,
                    hospitalId: this.hospitalId
                }).then(result=>{
                    records = result;
                    console.log(records.length);
                    if (records == null || records.length == 0) {
                        newflag = false;
                        this.showToast("没有有效的【经销商医院】。","error");
                        this.dispatchEvent(new CloseActionScreenEvent());
                    }
                    if (newflag) {
                        console.log("5");
                        queryForRecTypeList({
                            sobjectType: 'Agency_Opportunity__c',
                            developerName: 'Opportunity',
                            isActive: true
                        }).then(result=>{
                            console.log("6");
                            var recTypeList = result;
                            if (recTypeList.length == 0) {
                                    this.showToast("没有找到,经销商询价的定义。","error");
                                    this.dispatchEvent(new CloseActionScreenEvent());
                                } else {
                                    console.log("7");
                                    queryForRecords2({
                                        qualifiedApiName: 'Agency_Opportunity__c'
                                    }).then(result=>{
                                        console.log("8");
                                        var records2 = result;
                                        var recTypeId = recTypeList[0].Id;
                                        var prefix, fId_Close_Forecasted_Date__c, fId_Agency_Hospital__c
                                        , fId_OCM_Change_To_Opportunity_Flg__c, fId_Amount__c
                                        , fId_OCMSale_Price__c, fId_Agency__c
                                        , fId_StageName__c, fId_Change_To_Opportunity__c
                                        , fId_Department_Cateogy__c, fId_Bid_Planned_Date__c;
                                        console.log("9");
                                        console.log(records2);
                                        for (var i=0; i< records2.length; i++) {
                                            var record = records2[i];
                                            prefix = record.EntityDefinition.KeyPrefix.toString();
                                            if (record.QualifiedApiName == 'Close_Forecasted_Date__c') {
                                                fId_Close_Forecasted_Date__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'OCM_Change_To_Opportunity_Flg__c') {
                                                fId_OCM_Change_To_Opportunity_Flg__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'Agency_Hospital__c') {
                                                fId_Agency_Hospital__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'Amount__c') {
                                                fId_Amount__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'OCMSale_Price__c') {
                                                fId_OCMSale_Price__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'Agency__c') {
                                                fId_Agency__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'StageName__c') {
                                                fId_StageName__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'Change_To_Opportunity__c') {
                                                fId_Change_To_Opportunity__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'Department_Cateogy__c') {
                                                fId_Department_Cateogy__c = record.DurableId.toString().split('.')[1];
                                            }
                                            if (record.QualifiedApiName == 'Bid_Planned_Date__c') {
                                                fId_Bid_Planned_Date__c = record.DurableId.toString().split('.')[1];
                                            }
                                        }
                                        console.log("10");
                                        console.log(records[0]);
                                        var ahl = records[0];
                                        
                                        try {
                                            // var url = "/a2Y/e?retURL=%2F" 
                                            // + this.recordId 
                                            // + "&Name='(OLYMPUS)'"  
                                            // + encodeURIComponent(this.name)
                                            // + "&RecordType=" + recTypeId  + "&" + fId_Close_Forecasted_Date__c + "=" 
                                            // + encodeURIComponent(this.closeForecastedDate)
                                            // + "&" + fId_Bid_Planned_Date__c + "=" 
                                            // + encodeURIComponent(this.bidPlannedDate)
                                            // + "&" + fId_OCM_Change_To_Opportunity_Flg__c + "=1" + "&CF" + fId_Agency_Hospital__c + "_lkid=" + ahl.Id + "&CF" + fId_Agency_Hospital__c + "=" + encodeURIComponent(ahl.Name) + "&" + fId_Amount__c + "=" 
                                            // + encodeURIComponent(this.dealerFinalPrice)
                                            // + "&" + fId_OCMSale_Price__c + "=" 
                                            // + encodeURIComponent(this.wholesalePrice)
                                            // + "&CF" + fId_Agency__c + "_lkid=" 
                                            // + encodeURIComponent(this.agency1Id) 
                                            // + "&CF" + fId_Agency__c + "=" 
                                            // + encodeURIComponent(this.agency1Name)
                                            // + "&" + fId_StageName__c + "=" 
                                            // + encodeURIComponent(this.opportunityStage) 
                                            // + "&CF" + fId_Change_To_Opportunity__c + "_lkid=" 
                                            // + encodeURIComponent(this.recordId )
                                            // + "&CF" + fId_Change_To_Opportunity__c + "=" 
                                            // + encodeURIComponent(this.name )
                                            // + "&" + fId_Department_Cateogy__c + "=" 
                                            // + encodeURIComponent(this.opportunityCategory);
                                            // console.log(url);
                                            // window.open(url);
                                            if(this.closeForecastedDate){
                                                if(!this.closeForecastedDate.includes(' ')){
                                                    this.closeForecastedDate += ' 00:00:00'
                                                }
                                                this.closeForecastedDate = this.closeForecastedDate.replace(' ','T').replaceAll('/','-') + '.000Z';
                                                console.log(this.closeForecastedDate);
                                            }
                                            if(this.bidPlannedDate){
                                                if(!this.bidPlannedDate.includes(' ')){
                                                    this.bidPlannedDate += ' 00:00:00'
                                                }
                                                this.bidPlannedDate = this.bidPlannedDate.replace(' ','T').replaceAll('/','-') + '.000Z';
                                            }
                                            const defaultValues = encodeDefaultFieldValues({
                                                Name: '(OLYMPUS)' + this.name,
                                                Close_Forecasted_Date__c: this.closeForecastedDate,
                                                Bid_Planned_Date__c: this.bidPlannedDate,
                                                OCM_Change_To_Opportunity_Flg__c: '1',
                                                Agency_Hospital__c: ahl.Id,
                                                Amount__c: this.dealerFinalPrice,
                                                OCMSale_Price__c: this.wholesalePrice,
                                                Agency__c: this.agency1Id,
                                                StageName__c: this.opportunityStage,
                                                Change_To_Opportunity__c: this.recordId,
                                                Department_Cateogy__c: this.opportunityCategory
                                            });
                                            this[NavigationMixin.Navigate]({
                                                type: 'standard__objectPage',
                                                attributes: {
                                                    objectApiName: 'Agency_Opportunity__c',
                                                    actionName: 'new'
                                                },
                                                state: {
                                                    nooverride: '1',
                                                    defaultFieldValues: defaultValues,
                                                    recordTypeId: recTypeId
                                                }
                                            });
                                            this.dispatchEvent(new CloseActionScreenEvent());
                                        } catch (error) {
                                            console.log(error)
                                        }
                                    })
                                }
                        })  
                    }
                });
            }
        this.IsLoading = false;
    }
    showToast(msg,type) {
        if(type == 'success'){
            const event = new ShowToastEvent({
                message: msg,
                variant: type,
            });
            this.dispatchEvent(event);
            this.dispatchEvent(new CloseActionScreenEvent());
        }else{
            const event = new ShowToastEvent({
                message: msg,
                variant: type,
                mode: 'sticky'
            });
            this.dispatchEvent(event);
            this.dispatchEvent(new CloseActionScreenEvent());
        }   
    }
    updateRecordView(recordId) {
        updateRecord({fields: { Id: recordId }});
    }
}