buli
2022-04-15 900c50a247705d6fe8833e84d31b5d51616b7c26
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
public with sharing class NewLoanerUserController {
    //基础url
    public String baseUrl { get; private set; }
 
    public List<loaner_user__c> luList{get; private set; }
    //数据List
    public List<loaner_user__c> dataLines {get; private set;}
    //订单Id
    public String laId {get; private set;}
    // Update 20220317 By Yang Kaiyu Start
    public String staticResource {get; set;}
    public String staticResourceContact {get; set;}
    //PI contact
    public List<ContactClass> conList {get;set;}
 
    public String contactId {get;set;}
    public string conListJson {get{
        if(conList==null)return null;
        return JSON.serialize(conList);
    }}
    public String dataLinesJson{get{
        if(dataLines==null)return null;
        return JSON.serialize(dataLines);
    }}
    class ContactClass{
        public String contactIdValue{set;get;}
        public String contactNameValue{set;get;}
        public String contactPhoneValue{set;get;}
    }    
    // Update 20220317 By Yang Kaiyu End
    /*public NewLoanerUserController() {
         laId = System.currentPageReference().getParameters().get('headId');
         //ApexPages.currentPage().getParameters().get('headId');
 
    }*/
 
    public NewLoanerUserController(ApexPages.StandardController stdController) {
        String luId = stdController.getId();
        if(luId == null || luId == ''){
             laId = System.currentPageReference().getParameters().get('headId');
        }else{
            loaner_user__c lu = [select id,loaner_application__c from loaner_user__c where id = :luId];
            laId = lu.loaner_application__c; 
        }  
        // Update 20220317 By Yang Kaiyu Start  
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('loaner_user__c')); 
        staticResourceContact = JSON.serialize(PIHelper.getPIIntegrationInfo('contact')); 
        conList = new List<ContactClass>();
        // Update 20220317 By Yang Kaiyu End
    }
    
 
 
    //初始化方法
    public PageReference init() {
         // 基础Url
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        dataLines = new List<loaner_user__c>();
        // Update 20220317 By Yang Kaiyu Start
        luList = [select id,Contact__r.AWS_Data_Id__c,Contact__c,Contact__r.LastName,Contact__r.Phone,Contact__r.LastName_Encrypted__c,Contact__r.Phone_Encrypted__c,ContactNumber__c,
                        FromThePeriod__c,EndThePeriod__c,Remarks__c,Customer__c,Follow_UP_Opp__c 
                    from loaner_user__c 
                    where  loaner_application__c =:laId];
        // Update 20220317 By Yang Kaiyu End
        if(luList.size() >0){
            for(loaner_user__c lu : luList){
                // Update 20220317 By Yang Kaiyu Start
                ContactClass con = new ContactClass();
                con.contactIdValue   = lu.Contact__r.AWS_Data_Id__c;
                con.contactNameValue = lu.Contact__r.LastName_Encrypted__c;
                con.contactPhoneValue= lu.Contact__r.Phone_Encrypted__c;
                conList.add(con);
                // Update 20220317 By Yang Kaiyu End
                dataLines.add(lu);
            }
        }
        if(dataLines.size()<50){
            integer num = dataLines.size();
            for(integer i = 0 ;i<50-num;i++){
                loaner_user__c luc = new loaner_user__c();
                dataLines.add(luc);
            }
        }
        return null;
    }
 
    public PageReference updateUser() {
 
        System.debug('进入updateUser');
 
        List<loaner_user__c> updateList = new List<loaner_user__c>();
        loaner_application__c la =[select id,RecordType.DeveloperName from loaner_application__c where id=:laId];
        List<String> contactIdList = new List<String>();
 
        String firstLuId = '';
        String accountName = '';
        List<String> idList = new List<String>();
        System.debug('dataLines = ' + dataLines);
        for(loaner_user__c luc : dataLines){
            if(!(String.isBlank(luc.Contact__c) || luc.Customer__c == null)){
                loaner_user__c lu = new loaner_user__c();
                lu.Contact__c = luc.Contact__c;
                contactIdList.add(luc.Contact__c);
                
                if(firstLuId == '') firstLuId = luc.Contact__c;
                
                lu.FromThePeriod__c = luc.FromThePeriod__c;
                lu.EndThePeriod__c = luc.EndThePeriod__c;
                lu.Remarks__c = luc.Remarks__c;
                lu.Customer__c = luc.Customer__c;
                lu.Follow_UP_Opp__c = luc.Follow_UP_Opp__c;
                lu.loaner_application__c = laId;
                updateList.add(lu);
                //
                idList.add(luc.Customer__c);
            }
        }
 
        List<Contact> contactList = [select id,Phone from Contact where id = :contactIdList];
        for(Contact con : contactList){
            for(loaner_user__c lu1 : updateList){
                if(lu1.Contact__c == con.id){
                    lu1.ContactNumber__c = con.Phone;
                }
            }
        }
        if(firstLuId == '' || firstLuId == null){
            delete luList;
            String url = baseUrl;
            url += '\\' + laId;
            return new Pagereference(url);
        }
        if(la.RecordType.DeveloperName == 'BS'){
            Contact contact = [select id,Name,Phone,Address1__c,Postcode__c from Contact where id = : firstLuId]; 
            la.id = laId;
            la.Loaner_receive_staff__c = contact.Name;
            la.Loaner_receive_staff_phone__c = contact.Phone;
            la.direct_shippment_address__c = contact.Address1__c;
            la.Post_Code__c = contact.Postcode__c;
        }
 
        Savepoint sp = Database.setSavepoint();
       
        try {
            
            if (updateList.size() > 0) {
                delete luList;
                insert updateList;
            }
            System.debug(accountName);
            if(accountName == ''){
                System.debug('1111');
                List<Account> luList = new List<Account>();
                luList = [select id,Name from Account where id in :idList];
                System.debug('luList'+ luList);
                for(Account lu : luList){
                    if(accountName == ''){
                        accountName = lu.Name;
                    }else{
                        accountName += (',' + lu.Name);
                    }
                }
            }
            System.debug(accountName);
            la.Loaner_Account__c = accountName;
            update la;
        }catch (Exception ex) {
                Database.rollback(sp);
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage() + ' | Line:' + ex.getLineNumber()));
                return null;
        }
        // 返回样机借出申请画面
        String url = baseUrl;
        url += '\\' + laId;
        return new Pagereference(url);
    }
 
 
    public PageReference cancelBtn() {
        // 返回样机借出申请画面
        String url = baseUrl;
        url += '\\' + laId;
        return new Pagereference(url);
    }
 
}