高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
public with sharing class CampaignBTPDFController {
    //
    private Id btId;
    public campaignInfo campaignRecords { get; set; }
    // SWAG-BHW3Y9 update by vivek 20200108 start
    public List<BusinessTwo> businessPurposeTwoList { get; set; }
    public List<BusinessTwo> businessResultTwoList { get; set; }
    public List<BusinessTwo> businessHoldTwoList { get; set; }
    // SWAG-BHW3Y9 update by vivek 20200108 end
    private Integer nameMax = 200;   //名称最大长度 全是中文的话 最多18 但因为不会全是中文 所以设为20
    public CampaignBTPDFController() {
        btId = ApexPages.currentPage().getParameters().get('id');
    }
 
    public void init() {
        //campaignRecords = new campaignInfo();
        Campaign cam = [select Id,Name,createddate,Status,report_Status__c,Owner.Category5__c,Owner.Category6__c,OwnerName__c,
                                Name2__c,StartDate,EndDate,State_Master__r.Name,City_Master__r.Name,Town__c,
                                WorkshopPlace__c,Meeting_Type__c,
                                // SWAG-BF39A8 start total_participants__c
                                Convening_Participants_Num__c
                                // SWAG-BF39A8 end
                                ,BusinessPurpose__c,
                                BusinessPurpose_det__c,BusinessResult_det__c,Society_Hold_Place_det__c,
                                Society_Hold_Place__c,BusinessResult__c from Campaign where id = : btId];
 
        // SWAG-BHW3Y9 update by vivek 20200108 start
        
        // List<String> businessPurposeList = pictureFormat(cam.BusinessPurpose_det__c,'','255');
        // cam.BusinessPurpose_det__c = businessPurposeList[0];
        // cam.BusinessPurpose_detRight__c = businessPurposeList[1].replace('00N10000008qovu', '00Np0000004IaHj');
        businessPurposeTwoList = new List<BusinessTwo>();
        businessResultTwoList = new List<BusinessTwo>();
        businessHoldTwoList = new List<BusinessTwo>();
        businessPurposeTwoList = pictureFormat(cam.BusinessPurpose_det__c,'','510');
        businessResultTwoList = pictureFormat(cam.BusinessResult_det__c,'','510');
        businessHoldTwoList = pictureFormat(cam.Society_Hold_Place_det__c,'','510');
        System.debug('businessPurposeTwoList======'+businessPurposeTwoList);
        // SWAG-BHW3Y9 update by vivek 20200108 end
        campaignRecords = new campaignInfo(cam,nameMax);
    }
    // SWAG-BHW3Y9 update by vivek 20200108 start
    // 用来格式化图片大小
    public List<BusinessTwo> pictureFormat(String pstr,String pht,String pwt){
        if(pstr == null || pstr == ''){
            return null;
        }
        List<BusinessTwo> businessTwoList = new List<BusinessTwo>();
        // 添加了表格start
        pstr = '>' + pstr + '<';
        String imgStrT = pstr.replaceAll('<table.*</table>','').replaceAll('>[^>]*<','><');
        // System.debug(imgStrT.substring(0,imgStrT.length()-4));
        String imgStr = imgStrT.substring(1,imgStrT.length()-1);
        // String tableStrT = pstr.replaceAll('<img.*</img>','').replaceAll('>[^>]*<','><');
        pstr = '</table' + pstr;
        String tableStrT = pstr.replaceAll('<img.*</img>','').replaceAll('</table[^<]*<', '</table><');
        // System.debug(tableStrT.substring(0,tableStrT.length()-4));
        String tableStr = tableStrT.substring(8,tableStrT.length()-1);
        // System.debug(tableStr.replaceAll('<table[^>]*>','<table align="left" border="1" cellpadding="0" cellspacing="0" style="width: '+pwt+'px;">'));
 
        // 表格格式处理
        BusinessTwo bpttable = new BusinessTwo();
        // bpttable.busImgSrcL = tableStr.replaceAll('<table[^>]*>','<table align="left" border="1" cellpadding="0" cellspacing="0" style="width: '+pwt+'px;">');
        bpttable.busImgStyleL = '';
        // Double pwtl = Double.valueOf(pwt)-50;
        Campaign campaignL = new Campaign();
        campaignL.BusinessPurpose_det__c = tableStr.replaceAll('<table[^>]*>','<table align="center" border="1" cellpadding="0" cellspacing="0" style="width: '+pwt+'px;">');
        bpttable.cn = campaignL;
        businessTwoList.add(bpttable);
 
 
        // 图片格式处理
        // String str = pstr;
        String str = imgStr;
        System.debug('pstr'+pstr);
        String pHeight = pht;
        String pWidth = pwt;
        Double pWht = 0;
        if(pHeight != ''){
            pWht = Double.valueOf(pHeight);
        }else{
            pWht = Double.valueOf(pWidth);
        }
        
        List<String> strlist = str.split('</img>');
        if(!strlist[strlist.size()-1].contains('<img')){
            strlist.remove(strlist.size()-1);
        }
        System.debug('strlist======'+strlist);
        // Map<Integer,Double> strMap = new Map<Integer,Double>();
        for(Integer s = 0 ; s < strlist.size() ; s++ ){
            List<String> strlist1 =  strlist[s].split('px;');
            System.debug('strlist1======'+strlist1+'');
            System.debug('strlist1.size()======'+strlist1.size());
            if(strlist1.size() == 1){
                if(pHeight != ''){
                    strlist1[0] = strlist1[0].substring(0,strlist1[0].length()-1)+'style="height: '+pHeight+'px; width: auto;">';
                }else{
                    strlist1[0] = strlist1[0].substring(0,strlist1[0].length()-1)+'style="height: auto; width: '+pWidth+'px;">';
                }
                
                strlist[s] = strlist1[0]; 
            }
            if(strlist1.size() == 3){
                List<String> strlist2 = strlist1[0].split(' ');
                System.debug('strlist2======'+strlist2);
                List<String> strlist3 = strlist1[1].split(' ');
                System.debug('strlist3======'+strlist3);
                double imgheight = Double.valueOf(strlist2[strlist2.size()-1]);
                double imgwidth = Double.valueOf(strlist3[strlist3.size()-1]);
                double imgwht = 0;
                if(pHeight != ''){
                    imgwht = imgheight;
                }else{
                    imgwht = imgwidth;
                }
                // double bilv = pHeight/imgheight;
                if(imgwht > pWht ){
                    // imgheight = imgheight*bilv;
                    // imgwidth = imgwidth*bilv;
                    // imgheight = pHeight;
                    System.debug('strlist:'+s+'imgheight======'+imgheight+'imgwidth======'+imgwidth);
                    if(pHeight != ''){
                        strlist2[strlist2.size()-1] = pHeight+'px;';
                        strlist3[strlist3.size()-1] = 'auto;';
                    }else{
                        strlist2[strlist2.size()-1] = 'auto;';
                        strlist3[strlist3.size()-1] = pWidth+'px;';
                    }
                    
                    String strlist2str = '';
                    String strlist3str = '';
                    for(String j : strlist2){
                        strlist2str += ' '+j;
                    }
                    for(String j : strlist3){
                        strlist3str += ' '+j;
                    }
                    strlist1[0] = strlist2str;
                    strlist1[1] = strlist3str;
                    String strliststr = '';
                    for(Integer i = 0 ; i< strlist1.size() ; i++){
                        strliststr += strlist1[i];
                    }
                    System.debug('strliststr======'+strliststr);
                    strlist[s] = strliststr;
                }
            }
        }
        String str1 = '';
        String str2 = '';
        System.debug('strlist=========='+strlist);
        if(strlist.size() == 1){
            BusinessTwo bpt = new BusinessTwo();
            bpt.busImgSrcL = strlist[0].split('src=')[1].split('style=')[0].replace('"','').replace('amp;','');
            bpt.busImgStyleL = strlist[0].split('src=')[1].split('style=')[1].replace('"','');
            businessTwoList.add(bpt);
        }else{
            // for(Integer i = 0, j = 1 ; j <= strlist.size() ; j++){
            for(Integer i = 0 ; i < strlist.size() ; i++){
                // if(isDivisibility(i,2)){
                    BusinessTwo bpt = new BusinessTwo();
 
                    bpt.busImgSrcL = strlist[i].split('src=')[1].split('style=')[0].replace('"','').replace('amp;','');
                    bpt.busImgStyleL = strlist[i].split('src=')[1].split('style=')[1].replace('"','');
 
                    // if(j >= strlist.size()){
                    //     break;
                    // }
                    // bpt.busImgSrc = strlist[j].split('src=')[1].split('style=')[0].replace('"','').replace('amp;','');
                    // bpt.busImgStyle = strlist[j].split('src=')[1].split('style=')[1].replace('"','');
                    businessTwoList.add(bpt);
                // }
                // i++;
                
            }
        }
        // if(str.substring(str.length()-6) == '</img>'){
        //     str1 += '</img>';
        // }
        // List<String> returnList = new List<String>();
        // returnList.add(str1);
        // returnList.add(str2);
        // System.debug('str1======'+str1.replace('amp;', ''));
        // System.debug('str2======'+str2.replace('amp;', ''));
        // System.debug('returnList======'+returnList);
 
        return businessTwoList;
    }
 
    public Boolean isDivisibility(Integer dividend,Integer divider) {
        //先将被除数转换成Decimal,否则先计算整数除法结果为Integer类型,然后将整数转换成Decimal
        Decimal resultDecimal = Decimal.valueOf(dividend) / divider;
        //获取结果上线
        Integer resultSeiling = Integer.valueOf(resultDecimal.round(System.RoundingMode.CEILING));
        Integer resultDown = Integer.valueOf(resultDecimal.round(System.RoundingMode.DOWN));
        
        return resultSeiling == resultDown;
    }
 
    class BusinessTwo{
        public String busImgSrcL { get; set; }
        public String busImgStyleL { get; set; }
        public String busImgSrc { get; set; }
        public String busImgStyle { get; set; }
 
        public Campaign cn { get; set; }
 
        public BusinessTwo(){
            this.busImgStyleL = '';
            this.busImgSrcL = '';
            this.busImgSrc = '';
            this.busImgStyle = '';
            this.cn = null;
        }
    }
    // SWAG-BHW3Y9 update by vivek 20200108 end
    
 
    // Data Bean
    class campaignInfo implements Comparable {
        public Campaign cn { get; set; }
        // 从富文本中拆出来的文本
        public String businessPurpose { get; set; }
        public String businessResult { get; set; }
        public String society_Hold_Place { get; set; }
 
        // SWAG-BHW3Y9 update by vivek 20200108 start
        public String BusinessPurpose_det { get; set; }
        // SWAG-BHW3Y9 update by vivek 20200108 end
        // 
        public campaignInfo(Campaign e) {
            cn = e;
        }
 
        //private String Substr(Integer length, String parameter){
        //    String params = '';
        //    Integer size = (parameter.length() / length) + (Math.mod(parameter.length(), length) > 0 ? 1 :0);
        //    if(size == 0){
        //        size = 1;
        //    }
        //    for(Integer I =0; I<size;I++){
        //        String s;
        //        if(I == (size-1)){
        //            s = parameter.substring(I*length,parameter.length());
        //        }else{
        //            s = parameter.substring(I*length,(I+1)*length)+'<br/>';
        //        }
                
        //        params += s;
        //    }
        //    return params;
        //}
 
        public campaignInfo(Campaign mcae, Integer nameMax) {
            this.cn = mcae;
 
         //   public String businessPurpose { get; set; }
            //public String businessResult { get; set; }
            //public String society_Hold_Place { get; set; }
 
            //this.NeedHide = HideOrNot;
                // Assetある
            this.businessPurpose = mcae.BusinessPurpose__c;
            this.businessResult = mcae.BusinessResult__c;
            this.society_Hold_Place = mcae.Society_Hold_Place__c;
 
            // SWAG-BHW3Y9 update by vivek 20200108 start
            // this.BusinessPurpose_det = 'https://ocsm--StageFull--c.cs31.content.force.com/servlet/rtaImage?eid=701p0000000IX0b&feoid=00N10000008qovu&refid=0EMp00000005Foh';
            // SWAG-BHW3Y9 update by vivek 20200108 end
 
            this.businessPurpose = businessPurpose == null? '' : businessPurpose.replaceAll('<[^>]*>','');
            this.businessResult = businessResult == null? '' : businessResult.replaceAll('<[^>]*>','');
            this.society_Hold_Place = society_Hold_Place == null? '' : society_Hold_Place.replaceAll('<[^>]*>','');
            //this.businessPurpose = Substr(nameMax,businessPurpose);
            //this.businessResult = Substr(nameMax,businessResult);
            //this.society_Hold_Place = Substr(nameMax,society_Hold_Place);
        }
        // 排序
        public Integer compareTo(Object compareTo) {
            return null;
        }
    }
}