yujian
2019-09-03 3d50552f1910a5d30cc5bb322f0f7984bce51624
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package com.yeshi.fanli.service.impl.homemodule;
 
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
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.JsonUtil;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.homemodule.SpecialMapper;
import com.yeshi.fanli.dto.common.CommonContentTypeEnum;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.homemodule.Special;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.exception.homemodule.SpecialException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.homemodule.SpecialCardService;
import com.yeshi.fanli.service.inter.homemodule.SpecialPlaceService;
import com.yeshi.fanli.service.inter.homemodule.SpecialService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.VersionUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class SpecialServiceImpl implements SpecialService {
 
    @Resource
    private SpecialMapper specialMapper;
 
    @Resource
    private SpecialPlaceService specialPlaceService;
 
    @Resource
    private SpecialCardService specialCardService;
 
    @Resource
    private JumpDetailV2Service jumpDetailV2Service;
 
    @Resource
    private ConfigService configService;
    
    @Resource
    private AppVersionService appVersionService;
    
 
    @Override
    public Special selectByPrimaryKey(Long id) {
        return specialMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void saveObject(MultipartFile file, MultipartFile file2, MultipartFile file3, Special record,
            String jumpType) throws SpecialException, Exception {
 
        Long cardId = record.getCardId();
        if (cardId == null) {
            throw new SpecialException(1, "管理ID不能为空");
        }
 
        String name = record.getName();
        if (name == null || name.trim().length() == 0) {
            throw new SpecialException(1, "专题名称不能为空");
        }
 
        Integer sex = record.getSex();
        if (sex == null) {
            throw new SpecialException(1, "性别不能为空");
        }
 
        String version = record.getVersion();
        Integer platformCode = record.getPlatform();
        if (!StringUtil.isNullOrEmpty(version) && platformCode != null) {
            String platform = "android";
            if (platformCode == 2)
                platform = "ios";
            AppVersionInfo versionInfo = appVersionService.getByPlatformAndVersion(platform, version);
            if (versionInfo == null)
                throw new SpecialException(1, "版本号信息缺失");
            
            record.setVersionCode(versionInfo.getVersionCode());
            record.setPlatform(platformCode);
        } else {
            if (!StringUtil.isNullOrEmpty(version))
                throw new SpecialException(1, "平台类型不能为空");
            
            if (platformCode != null) 
                throw new SpecialException(1, "版本号不能为空");
        }
 
        String params = record.getParams();
        if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
            record.setParams(null);
        } else if (!StringUtil.isJson(params)) {
            throw new SpecialException(1, "跳转参数非JSON格式");
        }
 
        if (!StringUtil.isNullOrEmpty(jumpType)) {
            List<JumpDetailV2> listByType = jumpDetailV2Service.listByType(jumpType);
            if (listByType != null && listByType.size() > 0) {
                record.setJumpDetail(listByType.get(0));
            }
        }
 
        Long state = record.getState();
        if (state == null) {
            record.setState(0L);
        }
 
        // 图片上传
        String picture = null;
        if (file != null) {
            picture = uploadPicture(file);
        }
 
        // 图片上传
        String subPicture = null;
        if (file2 != null) {
            subPicture = uploadPicture(file2);
        }
 
        // 图片上传
        String icon = null;
        if (file3 != null) {
            icon = uploadPicture(file3);
        }
 
        Long id = record.getId();
        if (id == null) {
            record.setIcon(icon);
            record.setPicture(picture);
            record.setSubPicture(subPicture);
 
            // 排序
            record.setOrderby(specialMapper.getDefaultMaxOrder(cardId) + 1);
            record.setOrderMan(specialMapper.getManMaxOrder(cardId) + 1);
            record.setOrderWoman(specialMapper.getWomanMaxOrder(cardId) + 1);
 
            record.setCreatetime(new Date());
            record.setUpdatetime(new Date());
            specialMapper.insert(record);
 
            Special s = new Special();
            s.setId(record.getId());
            s.setOrderby(Integer.parseInt(s.getId() + ""));
            s.setOrderMan(Integer.parseInt(s.getId() + ""));
            s.setOrderWoman(Integer.parseInt(s.getId() + ""));
            specialMapper.updateByPrimaryKeySelective(s);
        } else {
            // 修改
            Special resultObj = specialMapper.selectByPrimaryKey(id);
            if (resultObj == null) {
                throw new SpecialException(1, "修改内容已不存在");
            }
 
            // 删除图片
            Boolean delIcon = record.getDelIcon();
            if (delIcon != null && delIcon) {
                removePicture(resultObj.getIcon());
                resultObj.setIcon(null);
            }
            Boolean delPicture = record.getDelPicture();
            if (delPicture != null && delPicture) {
                removePicture(resultObj.getPicture());
                resultObj.setPicture(null);
            }
            Boolean delSubPicture = record.getDelSubPicture();
            if (delSubPicture != null && delSubPicture) {
                removePicture(resultObj.getSubPicture());
                resultObj.setSubPicture(null);
            }
 
            if (picture != null && picture.trim().length() > 0) {
                // 删除老图
                removePicture(resultObj.getPicture());
                // 存储新图
                record.setPicture(picture);
            } else {
                record.setPicture(resultObj.getPicture());
            }
 
            if (subPicture != null && subPicture.trim().length() > 0) {
                // 删除老图
                removePicture(resultObj.getSubPicture());
                // 存储新图
                record.setSubPicture(subPicture);
            } else {
                record.setSubPicture(resultObj.getSubPicture());
            }
 
            if (icon != null && icon.trim().length() > 0) {
                // 删除老图
                removePicture(resultObj.getIcon());
                // 存储新图
                record.setIcon(icon);
            } else {
                record.setIcon(resultObj.getIcon());
            }
 
            record.setOrderby(resultObj.getOrderby());
            record.setOrderMan(resultObj.getOrderMan());
            record.setOrderWoman(resultObj.getOrderWoman());
            record.setCreatetime(resultObj.getCreatetime());
 
            record.setUpdatetime(new Date());
            specialMapper.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 = "/img/special/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
 
        return fileLink;
    }
 
    /**
     * 删除图片-不更新数据库
     * 
     * @param record
     * @throws Exception
     */
    public void removePicture(String picture) throws Exception {
        if (picture != null && picture.trim().length() > 0) {
            COSManager.getInstance().deleteFile(picture);
        }
    }
 
    @Override
    @Transactional
    public void updateOrder(Long id, Integer moveType, Integer sex) throws SpecialException {
 
        if (id == null || moveType == null || (!moveType.equals(1) && !moveType.equals(-1)) || sex == null) {
            throw new SpecialException(1, "请传递正确参数");
        }
 
        Special resultObj = specialMapper.selectByPrimaryKey(id);
        if (resultObj == null) {
            throw new SpecialException(1, "此内容已不存在");
        }
 
        Long cardId = resultObj.getCardId();
        // 获取交换对象
        Special exchangeObject = null;
        if (Special.SEX_ALL == sex || Special.SEX_DEFAULT == sex) {
            Integer orderby = resultObj.getOrderby();
            exchangeObject = specialMapper.getOrderByCardID(cardId, moveType, orderby, sex);
            if (exchangeObject == null) {
                throw new SpecialException(1, "没有找到可交换的位置");
            }
 
            resultObj.setOrderby(exchangeObject.getOrderby());
            exchangeObject.setOrderby(orderby);
        } else if (Special.SEX_MAN == sex) {
            Integer orderMan = resultObj.getOrderMan();
            exchangeObject = specialMapper.getOrderByCardID(cardId, moveType, orderMan, sex);
            if (exchangeObject == null) {
                throw new SpecialException(1, "没有找到可交换的位置");
            }
 
            resultObj.setOrderMan(exchangeObject.getOrderMan());
            exchangeObject.setOrderMan(orderMan);
        } else if (Special.SEX_WOMAN == sex) {
            Integer orderWoman = resultObj.getOrderWoman();
            exchangeObject = specialMapper.getOrderByCardID(cardId, moveType, orderWoman, sex);
 
            if (exchangeObject == null) {
                throw new SpecialException(1, "没有找到可交换的位置");
            }
 
            resultObj.setOrderWoman(exchangeObject.getOrderWoman());
            exchangeObject.setOrderWoman(orderWoman);
        } else {
            throw new SpecialException(1, "sex参数不正确");
        }
 
        specialMapper.updateByPrimaryKey(resultObj);
        specialMapper.updateByPrimaryKey(exchangeObject);
    }
 
    @Override
    @Transactional
    public int deleteBatchByPrimaryKey(List<Long> list) throws Exception {
        List<Special> listSpecial = specialMapper.queryByListPrimaryKey(list);
        for (Special special : listSpecial) {
            String src = special.getPicture();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
 
            String subPicture = special.getSubPicture();
            if (!StringUtil.isNullOrEmpty(subPicture)) {
                COSManager.getInstance().deleteFile(subPicture);
            }
        }
        return specialMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public int deleteBatchByCardID(List<Long> list) throws Exception {
        List<Special> listSpecial = specialMapper.queryByListCardID(list);
        for (Special special : listSpecial) {
            String src = special.getPicture();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
 
            String subPicture = special.getSubPicture();
            if (!StringUtil.isNullOrEmpty(subPicture)) {
                COSManager.getInstance().deleteFile(subPicture);
            }
        }
 
        return specialMapper.deleteBatchByCardID(list);
    }
 
    @Override
    public List<Special> listQueryByCard(long start, int count, Long card, String key, Integer sex) {
 
        List<Special> list = specialMapper.listQueryByCard(start, count, card, key, sex);
        if (list == null || list.size() == 0) {
            return list;
        }
 
        // 跳转链接
        for (Special special : list) {
            String params = special.getParams();
            if (StringUtil.isNullOrEmpty(params)) {
                special.setParams("");
            }
 
            String remark = special.getRemark();
            if (StringUtil.isNullOrEmpty(remark)) {
                special.setRemark("");
            }
 
            JumpDetailV2 jumpDetail = special.getJumpDetail();
            if (jumpDetail == null) {
                // 默认未选择
                JumpDetailV2 jumpDetailV2 = new JumpDetailV2();
                jumpDetailV2.setName("-未选择-");
                jumpDetailV2.setType("default");
                special.setJumpDetail(jumpDetailV2);
            }
        }
        return list;
    }
 
    @Override
    public long countlistQueryByCard(Long card, String key, Integer sex) {
        return specialMapper.countlistQueryByCard(card, key, sex);
    }
 
    @Override
    @Cacheable(value = "specialCache", key = "'listBySystemAndCard-'+#card+'-'+#systemId")
    public List<Special> listBySystemAndCard(String card, Long systemId) {
        return specialMapper.listBySystemAndCard(card, systemId);
    }
 
    @Override
    @Cacheable(value = "specialCache", key = "'listPageBySystemAndCard-'+#start+'-'+#count+'-'+#card+'-'+#systemId")
    public List<Special> listPageBySystemAndCard(long start, int count, String card, Long systemId) {
        return specialMapper.listPageBySystemAndCard(start, count, card, systemId);
    }
 
    @Override
    @Cacheable(value = "specialCache", key = "'listByVersion-'+#start+'-'+#count+'-'+#card+'-'+#platform+'-'+#versionCode")
    public List<Special> listByVersion(long start, int count, String card, Integer platform, Integer versionCode) {
        return specialMapper.listByVersion(start, count, card, platform, versionCode);
    }
    
    
    @Override
    @Cacheable(value = "specialCache", key = "'listCacheSpecialToIndex'+#acceptData.platform+'-'+#acceptData.version+'-'+#sex")
    public JSONObject listCacheSpecialToIndex(AcceptData acceptData, Integer sex) throws Exception {
 
        // 首页-5圆形2排区域
        JSONObject arcMap = new JSONObject();
 
        String indexArc = "index_arc";
        if (VersionUtil.greaterThan_1_6_0(acceptData.getPlatform(), acceptData.getVersion()))
            indexArc = "index_arc_1.6.0";
        else if (VersionUtil.greaterThan_1_5_70(acceptData.getPlatform(), acceptData.getVersion()))
            indexArc = "index_arc_1.5.7";
        else if (VersionUtil.greaterThan_1_5_60(acceptData.getPlatform(), acceptData.getVersion()))
            indexArc = "index_arc_1.5.6";
 
        List<Special> listArc = specialMapper.listByPlaceKey(indexArc, sex);
        if (listArc == null) {
            listArc = new ArrayList<Special>();
        } else if (listArc.size() > 0) {
            Special special = listArc.get(0);
            if (special != null) {
                // 背景图片
                arcMap.put("bottomPicture", special.getBottomPicture());
            }
        }
 
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
 
        // 处理9.9包邮
        if (listArc != null && VersionUtil.greaterThan_1_5_50(acceptData.getPlatform(), acceptData.getVersion()))
            for (Special s : listArc) {
                if (s.getName().contains("9.9")) {// 9块9
                    s.setJumpDetail(jumpDetailV2Service.getByTypeCache("common_template"));
                    JSONObject params = new JSONObject();
                    params.put("key", CommonContentTypeEnum._9k9.name());
                    params.put("title", CommonContentTypeEnum._9k9.getDesc());
                    s.setParams(params.toString());
                } else if (s.getName().contains("自购立减")) {
                    if (hour < 6) {// 0-6点之间不进入自购立减的页面
                        s.setJumpDetail(jumpDetailV2Service.getByTypeCache("web"));
                        JSONObject params = new JSONObject();
                        params.put("url", configService.get("tlj_own_buy_nogoods"));
                        s.setParams(params.toString());
                    } else {
                        s.setJumpDetail(jumpDetailV2Service.getByTypeCache("tlj_own_buy"));
                        s.setParams(null);
                    }
                }
            }
 
        // 数据处理
        handlelist(listArc);
        arcMap.put("list", JsonUtil.getApiCommonGson().toJson(listArc));
 
        // 首页-活动区域(女王节)
        JSONObject activityMap = new JSONObject();
        List<Special> listActivity = null;
        if ("ios".equalsIgnoreCase(acceptData.getPlatform())
                && configService.iosOnLining(Integer.parseInt(acceptData.getVersion()))) {
            // 如果IOS当前版本处于审核状态就不返回
        } else {
            listActivity = specialMapper.listByPlaceKey("index_activity", null);
        }
 
        if (listActivity == null) {
            listActivity = new ArrayList<Special>();
        } else if (listActivity.size() > 0) {
            Special special = listActivity.get(0);
            if (special != null) {
                // 背景图片
                activityMap.put("bottomPicture", special.getBottomPicture());
            }
        }
 
        handlelist(listActivity);
 
        activityMap.put("list", JsonUtil.getApiCommonGson().toJson(listActivity));
 
        // 首页-方形专题(品牌券、母婴...)
        String indexBlock = "index_block";
        if (VersionUtil.greaterThan_1_5_60(acceptData.getPlatform(), acceptData.getVersion()))
            indexBlock = "index_block_1.5.6";
 
        JSONObject blockJsonMap = new JSONObject();
        List<Special> listBlock = specialMapper.listByPlaceKey(indexBlock, sex);
        if (listBlock == null) {
            listBlock = new ArrayList<Special>();
        } else if (listBlock.size() > 0) {
            Special special = listBlock.get(0);
            if (special != null) {
                // 背景图片
                blockJsonMap.put("bottomPicture", special.getBottomPicture());
            }
        }
 
        // 品牌券
        // >=1.5.40后的版本可以用
        if (("android".equalsIgnoreCase(acceptData.getPlatform()) && Integer.parseInt(acceptData.getVersion()) > 39)
                || "ios".equalsIgnoreCase(acceptData.getPlatform()) && Integer.parseInt(acceptData.getVersion()) > 48)
            if (listBlock != null)
                for (int i = 0; i < listBlock.size(); i++) {
                    if ("品牌券".equalsIgnoreCase(listBlock.get(i).getName())) {
                        listBlock.get(i).setJumpDetail(jumpDetailV2Service.getByTypeCache("hot_cakes"));
                    }
                }
 
        // 数据处理
        handlelist(listBlock);
        blockJsonMap.put("list", JsonUtil.getApiCommonGson().toJson(listBlock));
 
        JSONObject root = new JSONObject();
        root.put("arcArea", arcMap);
        root.put("activityArea", activityMap);
        root.put("blockArea", blockJsonMap);
        return root;
    }
 
    @Override
    public List<Special> listByPlaceKey(String placeKey) {
        return specialMapper.listByPlaceKey(placeKey, null);
    }
 
    /**
     * 处理 数据
     * 
     * @param list
     */
    public void handlelist(List<Special> list) {
        if (list != null && list.size() > 0) {
            for (Special special : list) {
                boolean needLogin = special.isJumpLogin();
                JumpDetailV2 jumpDetail = special.getJumpDetail();
                if (jumpDetail != null) {
                    jumpDetail.setNeedLogin(needLogin);
                    special.setJumpDetail(jumpDetail);
                }
            }
        }
    }
}