buli
2023-04-21 43fdbff49764d55c7b3a19a1d6e7d8aeb62072ef
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
import { LightningElement,api, track, wire } from 'lwc';
import getEquipmentSetId  from '@salesforce/apex/SelectRentalButtonController.getEquipmentSetId';
 
import getEquipmentSet  from '@salesforce/apex/SelectRentalButtonController.getEquipmentSet';
 
import updateEquipment  from '@salesforce/apex/SelectRentalButtonController.updateEquipment';
 
import getSessionId  from '@salesforce/apex/SelectRentalButtonController.getSessionId';
 
import getRecords  from '@salesforce/apex/SelectRentalButtonController.getRecords';
 
 
import findIdArea   from '@salesforce/apex/SelectRentalButtonController.findIdArea';
import {CurrentPageReference} from 'lightning/navigation';
import { CloseActionScreenEvent } from 'lightning/actions';
export default class lexSelectRental extends LightningElement {
 
    @api recordId;
    IsLoading;
    Equipment_Set_Detail__c;
 
    @wire(CurrentPageReference)
    getStateParameters(currentPageReference){
        if(currentPageReference){
            console.log(currentPageReference);
            console.log("进入获取page参数")
            const IdValue=currentPageReference.state.recordId;
            if(IdValue){
                let str=`${IdValue}`;
                this.recordId=str;
                console.log("recordId="+str);
            }
        }
    }
 
    connectedCallback(){
        getEquipmentSetId({recordId:this.recordId}).then(res=>{
            if(res!=null){
            this.IsLoading=false;
            console.log("res");
            console.log(res);
            console.log("进入初始化对象其他属性");
            this.Equipment_Set_Detail__c=res;
            console.log(this.Equipment_Set_Detail__c);
            console.log(this.Equipment_Set_Detail__c.Select_rental__c);
            
            this.SelectLending();
            }
            else{
                 let a="没进方法";
                 console.log(a);
            }
        }).catch(error =>{
            console.log("error");
            console.log(error.message);
        }).finally(()=>{
 
        });
    }
 
 
 
    async SelectLending(){
 
 
        try{
            this.dispatchEvent(new CloseActionScreenEvent());
            window.sforce = window.sforce || {};
            window.sforce.connection = window.sforce.connection || {};
            let a =await getSessionId();
            window.sforce.connection.sessionId =await getSessionId();
            console.log(a);
            let esid = this.Equipment_Set_Detail__c.Equipment_Set__c;
            console.log(esid);
            //选择借出是一个复选框,这里就是找复选框勾上的数据Id,不知道这个怎么接受从前端穿参数。
            //所以我直接查数据库
            let esdids =new Array();
            esdids=await getRecords();
            console.log(esdids);
            let hasError = false;
            //更新数据
            if (esdids.length > 0) {
                console.log('进入第一个if方法');
                let sqlResult = await getEquipmentSet({esid:esid});
                console.log(sqlResult);
                console.log('返回对象数组第一个'+sqlResult[0]);
                for (let i = 0; i < sqlResult.length; i++) {
                    let esd = sqlResult[i];
                    console.log(i);
                    esd.Select_rental__c = false;
                }
                let result = await updateEquipment({sqlResult : sqlResult});
                console.log(result);
                for (let i=0; i<result.length; i++) {
                    if (result[i]!="success") { 
                        alert(result[i].errors.message);
                        hasError = true;
                        return;
                    }
 
                  }
                let EqObject = await findIdArea({id : esdids});
                console.log(EqObject);
                for (let i = 0; i < EqObject.length; i++) {
                    let esd = EqObject[i];
                    esd.Select_rental__c = true;
                }
                result = await updateEquipment({sqlResult : EqObject});
                console.log(result);
                for (let i=0; i<result.length; i++) {
                    if (result[i]!="success") { 
                        alert(result[i].errors.message);
                        hasError = true;
                        return;
                    }
                }
        
            if (!hasError) {
                //window.top.location.reload;
                //这里好像top不存在
                window.location.reload();
 
            }
        }
 
        else {
            alert("请至少选择一个备品set明细。");
        }
    }
        catch(e){
            console.log(e);
            alert(e.message);
    }
 
    }
}