高章伟
2022-02-15 3871fb42959175bf233b673c0800a905f7274d59
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
public with sharing class TydelikeAccController {
    public Account acc {get;set;}
    public Contact con {get;set;}
    public TydelikeAccount__c tyac {get;set;}
    public TydelikeContact__c tyco {get;set;}
    public String tycoId { get; set; }
    public String tyacId { get; set; }
 
    public String baseUrl { get; set; }
    public String rtUrl { get; set; }
 
    public TydelikeAccController() {
        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';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
        if (rtUrl == null || rtUrl == 'null') {
            rtUrl = '';
        }
    }
    //页面初始化
    public PageReference init() {
        tyacId = System.currentPageReference().getParameters().get('tyacId');
        tycoId = System.currentPageReference().getParameters().get('tycoId');
        System.debug('tyacId='+tyacId);
        System.debug('tycoId+'+tycoId);
 
        //当前登陆人ID。传了产品分类就用传的,没传就用取到的。
        /*String now_userId = UserInfo.getUserId();
        now_userId.substring(0,15);
        List<Id> uidList = new List<Id>();
        List<user> userList = new List<user>();
 
        userList = [select ContactId from User where Id =:now_userId]; 
        if (userList.size() == 0) {
            return null;
        }
 
        uidList.add(userList.get(0).ContactId);
        String productSegment = '';
        List<Contact> cprList = new List<Contact>();
        cprList = [select ProductSegment__c from Contact where Id =:uidList];
        if (cprList.size() == 0) {
            return null;
        }
        productSegment = cprList.get(0).ProductSegment__c;
        System.debug('productSegment='+productSegment);*/
 
        //显示客户
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        tyacList = [select id,Name,AssetBS3D__c,AssetBS2D__c,OCNServiceStaff__c,AssetBS1D__c,AssetBS5D__c,ManagementCode_Ext__c,
        Sub_UseD__c,AssetBS4D__c,EnglishNameD__c,DepartmentNameD__c,FacilityNameD__c,IndustryCD__c,MarketVerticalsD__c,CancelReasonD__c,
        ExportRegulatedCustomer__c,Province__c,Whether_OEM__c,OfSkrap__c,GlobalAccountName__c,DivisionName_D__c,UseD__c,UserType__c,
        WebsiteD__c,KeyAccount__c,TyCity__c,TyAddress__c,TyPostCode__c,ProductSegment__c,FaxD__c,Remark__c,DealerSelectOwner__c,
        PhoneD__c,NationalEnterpriseUser__c 
        from TydelikeAccount__c where id =: tyacId];
        if (tyacList.size() > 0) {
            tyac = tyacList[0];
        }
        System.debug('tyac='+tyac);
 
        //初始化客户赋值
        acc = new Account();
        acc.Name = tyac.Name;
        acc.AssetBS3D__c = tyac.AssetBS3D__c;
        acc.AssetBS2D__c = tyac.AssetBS2D__c;
        acc.OCNServiceStaff__c = tyac.OCNServiceStaff__c;
        acc.AssetBS1D__c = tyac.AssetBS1D__c;
        acc.AssetBS5D__c = tyac.AssetBS5D__c;
        acc.ManagementCode_Ext__c = tyac.ManagementCode_Ext__c;
        acc.Sub_UseD__c = tyac.Sub_UseD__c;
        acc.EnglishNameD__c = tyac.EnglishNameD__c;
        acc.DepartmentNameD__c = tyac.DepartmentNameD__c;
        acc.FacilityNameD__c = tyac.FacilityNameD__c;
        acc.MarketVerticalsD__c = tyac.MarketVerticalsD__c;
        acc.CancelReasonD__c = tyac.CancelReasonD__c;
        acc.ExportRegulatedCustomer__c = tyac.ExportRegulatedCustomer__c;
        acc.Province__c = tyac.Province__c;
        acc.Whether_OEM__c = tyac.Whether_OEM__c;
        acc.GlobalAccountName__c = tyac.GlobalAccountName__c;
        acc.DivisionName_D__c = tyac.DivisionName_D__c;
        acc.UseD__c = tyac.UseD__c;
        acc.UserType__c = tyac.UserType__c;
        acc.WebsiteD__c = tyac.WebsiteD__c;
        acc.CityD__c = tyac.TyCity__c;
        acc.Address1D__c = tyac.TyAddress__c;
        acc.PostCodeD__c = tyac.TyPostCode__c;
        acc.ProductSegment__c = tyac.ProductSegment__c;
        /*acc.ProductSegment__c = tyac.ProductSegment__c == null?productSegment:tyac.ProductSegment__c;*/
        acc.FaxD__c = tyac.FaxD__c;
        acc.Remark__c = tyac.Remark__c;
        acc.DealerSelectOwner__c = tyac.DealerSelectOwner__c;
        acc.PhoneD__c = tyac.PhoneD__c;
        acc.NationalEnterpriseUser__c = tyac.NationalEnterpriseUser__c;
        acc.AssetBS4D__c = tyac.AssetBS4D__c;
        
        if (String.isNotBlank(tycoId)) {
            List<TydelikeContact__c> tycoList = [select 
            Id,Name,TyAddress__c,TyFaxD__c,TyPhoneD__c,TyPostcode__c,EnglishAddress__c,ContactEnglishName__c 
            from TydelikeContact__c 
            where Id =: tycoId];
            if (tycoList.size() > 0) {
                tyco = tycoList[0];
            }
 
            //初始化联系人赋值
            con = new Contact();
            con.LastName = tyco.Name;
            con.Address1D__c = tyco.TyAddress__c;
            con.FaxD__c = tyco.TyFaxD__c;
            con.MobilePhoneD__c = tyco.TyPhoneD__c;
            con.PostcodeD__c = tyco.TyPostcode__c;
            con.EnglishAddress__c = tyco.EnglishAddress__c;
            con.ContactEnglishName__c = tyco.ContactEnglishName__c;
        }
        
 
        return null;
    }
 
    public PageReference save() {
        //创建,肯定有客户。若无联系人,显示客户创建页面。
        //通过url传进来的联系人id,tycoId。通过url传进来的客户id,tyacId
        tycoId = System.currentPageReference().getParameters().get('tycoId');
        tyacId = System.currentPageReference().getParameters().get('tyacId');
        List<TydelikeContact__c> tyconList = new List<TydelikeContact__c>();
        if (String.isNotBlank(tycoId)) {
            TydelikeContact__c tycontact = new TydelikeContact__c();
            tycontact.Id = tycoId;
            tycontact.OfSkrap__c = true;
            tycontact.Ofomtecon__c = true;
            tyconList.add(tycontact);
            System.debug('tyconList='+tyconList);
        }
        //OfSkrap__c,Ofomtecon__c,Ofomteacc__c,帮助调查。
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        TydelikeAccount__c tyaccount = new TydelikeAccount__c();
        tyaccount.Id = tyacId;
        tyaccount.Ofomteacc__c = true;
        tyacList.add(tyaccount);       
        System.debug('tyacList='+tyacList);
 
        Savepoint sp = Database.setSavepoint();
        try {
            if (String.isNotBlank(tycoId)) {
                update tyconList;  
            }
            update tyacList;
            insert acc;
            if (String.isNotBlank(tycoId)) {
                con.AccountId = acc.Id;
                if (acc.ProductSegment__c == 'BS') {
                    con.ProductSegmentBS__c = true;
                }else if(acc.ProductSegment__c == 'IE'){
                    con.ProductSegmentIE__c = true;
                }else if(acc.ProductSegment__c == 'NDT'){
                    con.ProductSegmentNDT__c = true;
                }else if(acc.ProductSegment__c == 'ANI'){
                    con.ProductSegmentANI__c = true;
                }else if(acc.ProductSegment__c == 'RVI'){
                    con.ProductSegmentRVI__c = true;
                }
                insert con;
            }
            
            String url = baseUrl + '/apex/TydelikeAC';
            return new Pagereference(url);
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
        }
        return null;
    }
}