yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package com.yeshi.fanli.service.impl.homemodule;
 
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
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.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.homemodule.SwiperPictureMapper;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class SwiperPictureServiceImpl implements SwiperPictureService {
 
    @Resource
    private SwiperPictureMapper swiperPictureMapper;
 
    @Resource
    private JumpDetailV2Service jumpDetailV2Service;
 
    @Resource
    private AdActivityVersionControlService adActivityVersionControlService;
 
    @Resource
    private AppVersionService appVersionService;
 
    @Override
    public int insertSelective(SwiperPicture record) {
        return swiperPictureMapper.insertSelective(record);
    }
 
    @Override
    public SwiperPicture selectByPrimaryKey(Long id) {
        return swiperPictureMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(SwiperPicture record) {
        return swiperPictureMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(SwiperPicture record) {
        return swiperPictureMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public List<SwiperPicture> queryByBannerID(long start, int count, Long bannerId) throws SwiperPictureException {
        return swiperPictureMapper.queryByBannerID(start, count, bannerId);
    }
 
    @Override
    public long countQueryByBannerID(Long bannerId) throws SwiperPictureException {
        return swiperPictureMapper.countQueryByBannerID(bannerId);
    }
 
    @Override
    public void saveObject(MultipartFile file, SwiperPicture record, String jumpType)
            throws SwiperPictureException, Exception {
 
        if (record == null) {
            throw new SwiperPictureException(1, "参数不能为空");
        }
 
        Long bannerId = record.getBannerId();
        if (bannerId == null) {
            throw new SwiperPictureException(1, "标识管理ID不能为空");
        }
 
        String params = record.getParams();
        if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
            record.setParams(null);
        } else if (!StringUtil.isJson(params)) {
            throw new SwiperPictureException(1, "跳转参数非JSON格式");
        } else {
            record.setParams(params.trim());
        }
 
        if (!StringUtil.isNullOrEmpty(jumpType)) {
            List<JumpDetailV2> listByType = jumpDetailV2Service.listByType(jumpType);
            if (listByType != null && listByType.size() > 0) {
                record.setJumpDetail(listByType.get(0));
            }
        }
 
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String startTime_str = record.getStartTime_str();
            if (startTime_str != null && startTime_str.trim().length() > 0) {
                startTime_str = startTime_str.replaceAll("T", " ");
                record.setStartTime(format.parse(startTime_str));
            }
 
            String endTime_str = record.getEndTime_str();
            if (endTime_str != null && endTime_str.trim().length() > 0) {
                endTime_str = endTime_str.replaceAll("T", " ");
                record.setEndTime(format.parse(endTime_str));
            }
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        String remark = record.getRemark();
        if (remark != null && (remark.trim().length() == 0 || remark.equalsIgnoreCase("null"))) {
            record.setRemark(null);
        }
 
        String picture = null;
        if (file != null) {
            picture = uploadPicture(file);
        }
 
        Long id = record.getId();
 
        if (id == null) {
            int maxOrder = swiperPictureMapper.getMaxOrderByBannerID(bannerId);
            record.setOrder(maxOrder + 1);
 
            Integer state = record.getState();
            // 默认停用
            if (state == null) {
                record.setState(1);
            }
 
            Integer autoControl = record.getAutoControl();
            if (autoControl == null) {
                // 默认非系统控制
                record.setAutoControl(1);
            }
            record.setCreatetime(new Date());
            record.setUpdatetime(new Date());
            record.setSrc(picture);
 
            swiperPictureMapper.insert(record);
        } else {
            // 修改
            SwiperPicture resultObj = swiperPictureMapper.selectByPrimaryKey(id);
            if (resultObj == null) {
                throw new SwiperPictureException(1, "参数不能为空");
            }
 
            if (picture != null && picture.trim().length() > 0) {
                // 删除已存在图片
                removePicture(resultObj);
 
                record.setSrc(picture);
            } else {
                record.setSrc(resultObj.getSrc());
            }
 
            record.setOrder(resultObj.getOrder());
            record.setCreatetime(resultObj.getCreatetime());
            record.setUpdatetime(new Date());
 
            swiperPictureMapper.updateByPrimaryKey(record);
        }
    }
 
    /**
     * 上传图片
     * 
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadPicture(MultipartFile file) throws Exception {
 
        // 文件解析
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
 
        // 文件路径
        String filePath = FilePathEnum.swiper.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
 
        return fileLink;
    }
 
    /**
     * 删除图片-不更新数据库
     * 
     * @param record
     * @throws Exception
     */
    public void removePicture(SwiperPicture record) throws Exception {
        String picture = record.getSrc();
        if (picture != null && picture.trim().length() > 0) {
            COSManager.getInstance().deleteFile(picture);
        }
    }
    
    
    @Override
    public void switchState(Long id) throws SwiperPictureException {
        if (id == null) {
            throw new SwiperPictureException(1, "请传递正确参数");
        }
        
        SwiperPicture resultObj = swiperPictureMapper.selectByPrimaryKey(id);
        if (resultObj == null) {
            throw new SwiperPictureException(1, "此内容已不存在");
        }
        
        Integer state = resultObj.getState();
        if (state == null || state == 0) {
            state = 1;
        } else {
            state = 0;
        }
        
        SwiperPicture updateObj = new SwiperPicture();
        updateObj.setId(id);
        updateObj.setState(state);
        updateObj.setJumpNeedLogin(resultObj.isJumpNeedLogin());
        swiperPictureMapper.updateByPrimaryKeySelective(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBatchByPrimaryKey(List<Long> list) throws SwiperPictureException {
        List<SwiperPicture> listSwiper = swiperPictureMapper.queryByListPrimaryKey(list);
        for (SwiperPicture swiperPicture : listSwiper) {
            String src = swiperPicture.getSrc();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
        }
 
        if (listSwiper != null)
            for (SwiperPicture picture : listSwiper) {
                List<AdActivityVersionControl> versionList = adActivityVersionControlService
                        .listByTypeAndSourceId(AdActivityType.banner, picture.getId());
                if (versionList != null)
                    for (AdActivityVersionControl control : versionList)
                        adActivityVersionControlService.deleteByPrimaryKey(control.getId());
            }
        return swiperPictureMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public int deleteBatchByBannerID(List<Long> list) throws SwiperPictureException {
        return swiperPictureMapper.deleteBatchByBannerID(list);
    }
 
    @Override
    public List<SwiperPicture> queryByListBannerID(List<Long> list) throws Exception {
        return swiperPictureMapper.queryByListBannerID(list);
    }
 
    @Override
    public List<SwiperPicture> getOrderByBannerID(Long bannerId, Integer type, Integer order) {
        return swiperPictureMapper.getOrderByBannerID(bannerId, type, order);
    }
 
    @Override
    public int getMaxOrderByBannerID(Long bannerId) throws SwiperPictureException {
        return swiperPictureMapper.getMaxOrderByBannerID(bannerId);
    }
 
    @Override
    @Cacheable(value = "bannerCache", key = "'getByBannerCard-'+#card")
    public List<SwiperPicture> getByBannerCard(String card) {
        List<SwiperPicture> list = swiperPictureMapper.getByBannerCard(card);
        if (list != null && list.size() > 0) {
            for (SwiperPicture swiperPicture : list) {
                boolean needLogin = swiperPicture.isJumpNeedLogin();
                JumpDetailV2 jumpDetail = swiperPicture.getJumpDetail();
                if (jumpDetail != null) {
                    jumpDetail.setNeedLogin(needLogin);
                    swiperPicture.setJumpDetail(jumpDetail);
                }
            }
        }
        return list;
    }
 
    /**
     * 专题版本过滤
     * 
     * @param list
     * @param platform
     * @param versionCode
     */
    private void filterSwipePicture(List<SwiperPicture> list, String platform, int versionCode) {
        if (list == null || list.size() == 0)
            return;
        AppVersionInfo app = appVersionService.getClientVersion(platform, versionCode);
        if (app == null) {
            list.clear();
            return;
        }
        List<Long> versionIdList = new ArrayList<>();
        versionIdList.add(app.getId());
        List<Long> sourceIdList = new ArrayList<>();
        for (SwiperPicture picture : list) {
            sourceIdList.add(picture.getId());
        }
 
        Set<Long> sourceIds = adActivityVersionControlService.filterSourceIdByVersion(sourceIdList,
                AdActivityType.banner, versionIdList);
        for (int i = 0; i < list.size(); i++) {
            if (!sourceIds.contains(list.get(i).getId())) {
                list.remove(i--);
            }
        }
    }
 
    @Cacheable(value = "bannerCache", key = "'getByBannerCardAndVersion-'+#card+'-'+#platform+'-'+#version")
    @Override
    public List<SwiperPicture> getByBannerCardAndVersion(String card, String platform, int version) {
        List<SwiperPicture> list = swiperPictureMapper.getByBannerCard(card);
        filterSwipePicture(list, platform, version);
        for (SwiperPicture picture : list) {
            if (picture.isJumpNeedLogin() && picture.getJumpDetail() != null) {
                picture.getJumpDetail().setNeedLogin(true);
            }
        }
 
        return list;
    }
 
    @Override
    @Cacheable(value = "bannerCache", key = "'getByBannerId-'+#bannerId")
    public List<SwiperPicture> getByBannerId(Long bannerId) {
        return swiperPictureMapper.getByBannerId(bannerId);
    }
 
    @Cacheable(value = "bannerCache", key = "'getByBannerId-'+#bannerId+'-'+#platform+'-'+#version")
    @Override
    public List<SwiperPicture> getByBannerId(Long bannerId, String platform, int version)
            throws SwiperPictureException {
        List<SwiperPicture> pictureList = swiperPictureMapper.getByBannerId(bannerId);
        filterSwipePicture(pictureList, platform, version);
        return pictureList;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setVersions(Long id, List<Long> versions) throws SwiperPictureException {
        SwiperPicture swiperPicture = selectByPrimaryKey(id);
        if (swiperPicture == null) {
            throw new SwiperPictureException(1, "Banner不存在");
        }
 
        Set<Long> oldSet = new HashSet<>();
 
        List<AdActivityVersionControl> versionList = adActivityVersionControlService
                .listByTypeAndSourceId(AdActivityType.banner, swiperPicture.getId());
        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.banner, 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(swiperPicture.getId());
            control.setType(AdActivityType.banner);
            control.setVersion(new AppVersionInfo(versionId));
            try {
                adActivityVersionControlService.addVersionControl(control);
            } catch (Exception e) {
                throw new SwiperPictureException(2, e.getMessage());
            }
        }
 
    }
 
}