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
package com.yeshi.fanli.service.impl.help;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.help.AppPageNotificationMapper;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.bus.help.AppPageNotification;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
import com.yeshi.fanli.entity.bus.homemodule.FloatAD;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.help.AppPageNotificationService;
import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class AppPageNotificationServiceImpl implements AppPageNotificationService {
 
    @Resource
    private AppPageNotificationMapper appPageNotificationMapper;
 
    @Resource
    private AppVersionService appVersionService;
 
    @Resource
    private AdActivityVersionControlService adActivityVersionControlService;
 
    @Override
    public AppPageNotification getAppPageNotificationByType(String type) {
 
        return appPageNotificationMapper.selectByType(type);
    }
 
    @Cacheable(value = "configCache", key = "'getValidNotificationByTypeCache-'+#type+'-'+ #platform+'-'+#versionCode")
    @Override
    public AppPageNotification getValidNotificationByTypeCache(String type, String platform, Integer versionCode) {
        List<AppPageNotification> recordList = appPageNotificationMapper.listValidByType(type);
        if (recordList == null || recordList.size() == 0)
            return null;
 
        // 过滤版本
        AppVersionInfo app = appVersionService.getClientVersion(platform, versionCode);
        if (app == null) {
            return null;
        }
        List<Long> versionIdList = new ArrayList<>();
        versionIdList.add(app.getId());
 
        List<Long> sourceIdList = new ArrayList<>();
        for (AppPageNotification an : recordList)
            sourceIdList.add(an.getId());
        Set<Long> sourceIds = adActivityVersionControlService.filterSourceIdByVersion(sourceIdList,
                AdActivityType.notification, versionIdList);
        if (sourceIds == null || sourceIds.size() == 0)
            return null;
        long sourceId = sourceIds.iterator().next();
 
        for (AppPageNotification record : recordList) {
            if (record.getId().longValue() == sourceId)
                return record;
        }
        return null;
    }
 
    @Override
    public void addAppPageNotification(AppPageNotification apn) throws Exception {
        if (apn == null)
            return;
        if (apn.getType() == null)
            throw new Exception("类型为空");
        AppPageNotification old = getAppPageNotificationByType(apn.getType().name());
 
        if (old != null)
            throw new Exception("已存在改类型");
 
        apn.setCreateTime(new Date());
        apn.setUpdateTime(new Date());
        apn.setMd5(StringUtil.Md5(apn.getType().name() + "#" + apn.getContent() + "#" + apn.getContentUrl()));
        appPageNotificationMapper.insertSelective(apn);
    }
 
    @Override
    public void insertSelective(AppPageNotification record) {
        appPageNotificationMapper.insertSelective(record);
    }
 
    @Override
    public void updateByPrimaryKey(AppPageNotification record) {
        appPageNotificationMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public void updateByPrimaryKeySelective(AppPageNotification record) {
        appPageNotificationMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public AppPageNotification selectByPrimaryKey(Long id) {
        return appPageNotificationMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int deleteBatchByPrimaryKey(List<Long> list) {
        return appPageNotificationMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public List<AppPageNotification> listQuery(long start, int count, String key, Integer show, Integer canClose) {
        return appPageNotificationMapper.listQuery(start, count, key, show, canClose);
    }
 
    @Override
    public long countQuery(String key, Integer show, Integer canClose) {
        return appPageNotificationMapper.countQuery(key, show, canClose);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setVersions(Long id, List<Long> versions) throws Exception {
        AppPageNotification record = appPageNotificationMapper.selectByPrimaryKey(id);
        if (record == null) {
            throw new Exception("专题不存在");
        }
 
        Set<Long> oldSet = new HashSet<>();
 
        List<AdActivityVersionControl> versionList = adActivityVersionControlService
                .listByTypeAndSourceId(AdActivityType.notification, 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.notification, 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.notification);
            control.setVersion(new AppVersionInfo(versionId));
            try {
                adActivityVersionControlService.addVersionControl(control);
            } catch (Exception e) {
                throw new SwiperPictureException(2, e.getMessage());
            }
        }
    }
 
}