高章伟
2022-02-18 650847118307a1c9ae0ada15b7c69bbf5792c54c
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
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;}
    //
    /*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; 
        }     
    }
    
 
 
    //初始化方法
    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>();
        luList = [select id,Contact__c,ContactNumber__c,FromThePeriod__c,EndThePeriod__c,Remarks__c,Customer__c,Follow_UP_Opp__c from loaner_user__c where  loaner_application__c =:laId];
        if(luList.size() >0){
            for(loaner_user__c lu : luList){
                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() {
 
        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>();
        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);
    }
 
}