yujian
2019-12-19 bb07147aaa96d1ef4c99e358dbbe7dad160dc62a
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.yeshi.fanli.service.impl.help;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.help.HelpCenterMapper;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.bus.help.HelpCenter;
import com.yeshi.fanli.entity.bus.help.HelpInfo;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
import com.yeshi.fanli.exception.config.HelpCenterException;
import com.yeshi.fanli.service.inter.help.HelpCenterService;
import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.db.MongoDBManager;
 
@Service
public class HelpCenterServiceImpl implements HelpCenterService {
 
    @Resource
    private HelpCenterMapper helpCenterMapper;
    
    @Resource
    private MongoDBManager mongoDBManager;
    
    @Resource
    private AdActivityVersionControlService adActivityVersionControlService;
    
 
    @Override
    public int insertSelective(HelpCenter record) throws HelpCenterException {
        return helpCenterMapper.insertSelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(HelpCenter record) throws HelpCenterException {
        return helpCenterMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(HelpCenter record) throws HelpCenterException {
        return helpCenterMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public HelpCenter selectByPrimaryKey(Long id) throws HelpCenterException {
        return helpCenterMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public HelpCenter selectByPrimaryKeyCache(Long id) throws HelpCenterException {
        return selectByPrimaryKey(id);
    }
 
    @Override
    public String save(HelpCenter helpCenter, String content, String html) throws HelpCenterException,Exception {
        if (StringUtil.isNullOrEmpty(helpCenter.getTitle()))
            throw new HelpCenterException(1,"标题不能为空");
        
        if (helpCenter.getHelpClass() == null || helpCenter.getHelpClass().getId() <= 0)
            throw new HelpCenterException(1,"分类题不能为空");
        
        helpCenter.setUpdatetime(new Date());
        Long id = helpCenter.getId();
        if (id == null) {
            helpCenter.setCreatetime(new Date());
            helpCenterMapper.insert(helpCenter);
        } else {
            HelpCenter result = helpCenterMapper.selectByPrimaryKey(id);
            if (result == null) 
                throw new HelpCenterException(1,"此类内容已不存在,请刷新");
            
            helpCenter.setCreatetime(result.getCreatetime());
            helpCenter.setWeight(result.getWeight());
            helpCenterMapper.updateByPrimaryKey(helpCenter);
        }
        
        List<Map<String, Object>> positions = getPosition(html);
        if (positions != null && positions.size() > 0) {
            @SuppressWarnings("restriction")
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            
            for (int i = 0; i < positions.size(); i++) {
                Map<String, Object> map = positions.get(i);
                String base64Img = (String) map.get("base64Img");
                byte[] b = decoder.decodeBuffer(base64Img);
                for (int j = 0; j < b.length; ++j) {
                    if (b[j] < 0) {
                        b[j] += 256;
                    }
                }
 
                String type = (String) map.get("type");
                // 上传文件相对位置
                String fileUrl = "helpInfo/" + UUID.randomUUID().toString().replace("-", "") + "." +type;
                /* 上传新图片 */
                String uploadFilePath = COSManager.getInstance().uploadFileByte(b, fileUrl).getUrl();
 
                if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
                    String header = (String) map.get("header");
                    html = html.replace(header, uploadFilePath);
                }
            }
        }
    
        
        String replaceHtml = replaceIframe(html);
        Long backId = helpCenter.getId();
        HelpInfo helpInfo = new HelpInfo(backId, content, replaceHtml);
        mongoDBManager.saveHelpInfo(helpInfo);
        return html;
    }
 
    public List<Map<String, Object>> getPosition(String content) {
 
        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
 
        // 需要对比的字符串
        String compareStr = "base64";
        String compareStrStart = "data:image/";
        // 字符串查找初始从0开始查找
        int indexStart = 0;
        int indexTypeStart = 0;
        // 获取查找字符串的长度,这里有个规律:第二次查找出字符串的起始位置等于 第一次ab字符串出现的位置+ab的长度
        int compareStrLength = compareStr.length();
 
        while (true) {
            Map<String, Object> place = new HashMap<String, Object>();
 
            int tm = content.indexOf(compareStr, indexStart);
            int typetm = content.indexOf(compareStrStart, indexTypeStart);
 
            if (tm <= 0) {
                // 直到没有匹配结果为止
                break;
            } else {
                // 没查找一次就从新计算下次开始查找的位置
                indexStart = tm + compareStrLength;
                indexTypeStart = typetm + compareStrLength;
 
                //System.out.println(indexStart);
                //System.out.println(indexTypeStart);
                
                place.put("start", tm + 7);
 
                String suffix = content.substring(typetm + 11, tm - 1);
                place.put("type", suffix);
                
                String newContent = content.substring(tm + 7, content.length());
                
                
                char qmark = '"';
                int end = newContent.indexOf(qmark);
                place.put("end", end);
 
                String base64Img = newContent.substring(0, end);
                place.put("base64Img", base64Img);
                
                
                String header = compareStrStart + suffix+ ";" + compareStr +"," +base64Img;
                place.put("header", header);
                
                
                listMap.add(place);
            }
        }
 
        return listMap;
    }
 
    public String replaceIframe(String html) {
    
        String result = html;
        
        // 字符串查找初始从0开始查找
        int indexStart = 0;
        // 需要对比的字符串
        String compareStr = "<iframe";
        
        int compareStrLength = compareStr.length();
    
        while (true) {
    
            int tm = html.indexOf(compareStr, indexStart);
    
            if (tm <= 0) {
                // 直到没有匹配结果为止
                break;
            } else {
                // 起始位置
                indexStart = tm + compareStrLength;
    
                // 结束位置
                String newContent = html.substring(tm, html.length());
                int end = newContent.indexOf("</iframe>");
    
                // 替换指定字段  <iframe src=""></iframe>
                String replaceStr = html.substring(tm,  tm +  end + 9);
                
                char qmark = '"';
                // src 起始位置
                int srcindex = replaceStr.indexOf("src=", 0);
                
                String replaceSrc = replaceStr.substring(srcindex + 5, replaceStr.length());
                // src "结束位置
                int endsrc = replaceSrc.indexOf(qmark, 0);
                
                String src = replaceSrc.substring(0, endsrc +1);
                
                String voide = "<video src=\"" + src + " controls=\"controls\"> 您的浏览器不支持video标签,请更新浏览器</video>";
                
                result = html.replace(replaceStr, voide);
            }
        }
    
        return result;
    }
 
    @Override
    public List<HelpCenter> query(int pageIndex, int pageSize, String key, Long cid, Integer orderMode) throws HelpCenterException {
        return helpCenterMapper.query(pageIndex, pageSize, key, cid, orderMode);
    }
 
    @Override
    public long countQuery(String key, Long cid) throws HelpCenterException {
        return helpCenterMapper.countQuery(key, cid);
    }
 
    
    @Override
    @Cacheable(value = "helpCenterCache",key="'queryIdAndTitle-'+#pageIndex+'-'+#key+'-'+#cid")
    public List<HelpCenter> queryIdAndTitle(int pageIndex, int pageSize, String key, Long cid) 
            throws HelpCenterException {
        return helpCenterMapper.queryIdAndTitle(pageIndex, pageSize, key, cid);
    }
    
    @Override
    public int deleteBatchById(List<String> idList) throws HelpCenterException {
        return helpCenterMapper.deleteBatchById(idList);
    }
 
    @Override
    public HelpInfo getHelpInfo(Long id) throws HelpCenterException {
        return mongoDBManager.getHelpInfo(id);
    }
    
    @Override
    @Cacheable(value = "helpCenterCache",key="'getHelpInfoCache-'+#id")
    public HelpInfo getHelpInfoCache(Long id) throws HelpCenterException {
        return getHelpInfo(id);
    }
    
    
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteInfoBatchById(List<String> idList) throws HelpCenterException {
        if (idList != null) {
            for (String id : idList) {
                mongoDBManager.removeHelpInfo(Long.parseLong(id));
            }
        }
    }
    
    
    
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setVersions(Long id, List<Long> versions) throws HelpCenterException {
        HelpCenter helpCenter = helpCenterMapper.selectByPrimaryKey(id);
        if (helpCenter == null) {
            throw new HelpCenterException(1, "此信息不存在,请刷新重试");
        }
 
        Set<Long> oldSet = new HashSet<>();
        List<AdActivityVersionControl> versionList = adActivityVersionControlService
                .listByTypeAndSourceId(AdActivityType.helpCenter, id);
        if (versionList != null) {
            for (AdActivityVersionControl control : versionList)
                oldSet.add(control.getVersion().getId());
        }
 
        Set<Long> newSet = new HashSet<>();
        for (Long version : versions) {
            newSet.add(version);
        }
 
        Set<Long> delSet = new HashSet<>();
        delSet.addAll(oldSet);
        delSet.removeAll(newSet);
        for (Long versionId : delSet) {
            adActivityVersionControlService.deleteBySourceAndVersion(id, AdActivityType.helpCenter, versionId);
        }
 
        Set<Long> addSet = new HashSet<>();
        addSet.addAll(newSet);
        addSet.removeAll(oldSet);
 
        // 添加映射
        for (Long versionId : addSet) {
            AdActivityVersionControl control = new AdActivityVersionControl();
            control.setCreateTime(new Date());
            control.setSourceId(id);
            control.setType(AdActivityType.helpCenter);
            control.setVersion(new AppVersionInfo(versionId));
            try {
                adActivityVersionControlService.addVersionControl(control);
            } catch (Exception e) {
                throw new HelpCenterException(2, e.getMessage());
            }
        }
    }
}