admin
2021-03-13 7becc97c5bfdd827b9a999c26746bb8e8bc3e25c
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
package com.yeshi.buwan.service.imp.juhe;
 
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.VideoResourceMapExtraInfoDao;
import com.yeshi.buwan.dao.VideoTypeDao;
import com.yeshi.buwan.dao.juhe.pptv.PPTVProgramDao;
import com.yeshi.buwan.dao.juhe.pptv.PPTVSeriesDao;
import com.yeshi.buwan.dao.juhe.pptv.PPTVSeriesProgramMapDao;
import com.yeshi.buwan.dao.juhe.pptv.VideoPPTVMapDao;
import com.yeshi.buwan.dao.video.AlbumVideoMapDao;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResourceMapExtraInfo;
import com.yeshi.buwan.domain.VideoType;
import com.yeshi.buwan.domain.video.AlbumVideoMap;
import com.yeshi.buwan.dto.mq.PPTVMQMsg;
import com.yeshi.buwan.pptv.PPTVQuery;
import com.yeshi.buwan.pptv.PPTVUtil;
import com.yeshi.buwan.pptv.entity.PPTVProgram;
import com.yeshi.buwan.pptv.entity.PPTVSeries;
import com.yeshi.buwan.pptv.entity.PPTVSeriesProgramMap;
import com.yeshi.buwan.pptv.entity.VideoPPTVMap;
import com.yeshi.buwan.service.imp.CategoryVideoService;
import com.yeshi.buwan.service.imp.ResourceVideoService;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.imp.VideoTypeService;
import com.yeshi.buwan.service.inter.juhe.PPTVService;
import com.yeshi.buwan.util.mq.CMQManager;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
 
@Service
public class PPTVServiceImpl implements PPTVService {
    @Resource
    private PPTVSeriesDao pptvSeriesDao;
    @Resource
    private PPTVProgramDao pptvProgramDao;
    @Resource
    private PPTVSeriesProgramMapDao pptvSeriesProgramMapDao;
    @Resource
    private VideoPPTVMapDao videoPPTVMapDao;
    @Resource
    private VideoInfoService videoInfoService;
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private AlbumVideoMapDao albumVideoMapDao;
    @Resource
    private ResourceVideoService resourceVideoService;
    @Resource
    private CategoryVideoService categoryVideoService;
 
    @Resource
    private VideoTypeService videoTypeService;
 
    @Resource
    private VideoResourceMapExtraInfoDao videoResourceMapExtraInfoDao;
 
 
    @Override
    public void addToVideoInfo(PPTVSeries series) {
 
        if (series.getSeries() == null || series.getSeries().size() <= 0)
            return;
 
        VideoInfo newVideoInfo = PPTVUtil.convertToVideoInfo(series);
        if (newVideoInfo == null)
            return;
//        if (newVideoInfo.getVideoType() != null) {
//            newVideoInfo.setVideoType(videoTypeService.getVideoType(newVideoInfo.getVideoType().getId()));
//        }
        VideoPPTVMap videoPPTV = videoPPTVMapDao.selectByInfoId(series.getInfoID());
        if (videoPPTV != null) {//渠道视频已经存在
            VideoInfo videoInfo = videoInfoService.getVideoInfo(videoPPTV.getVideoId() + "");
            if (videoInfo == null)
                return;
            newVideoInfo.setId(videoInfo.getId());
            if (!newVideoInfo.getTag().equalsIgnoreCase(videoInfo.getTag())) {
                newVideoInfo.setCreatetime(videoInfo.getCreatetime());
                //更新信息tag信息
                videoInfo.setVideocount(newVideoInfo.getVideocount());
                videoInfo.setTag(newVideoInfo.getTag());
                videoInfo.setPicture(newVideoInfo.getPicture());
                videoInfo.setHpicture(newVideoInfo.getHpicture());
                videoInfo.setVpicture(newVideoInfo.getVpicture());
                videoInfo.setShow(newVideoInfo.getShow());
                videoInfo.setVideoType(newVideoInfo.getVideoType());
                videoInfoDao.update(videoInfo);
            }
        } else {//视频不存在
            //判断2个视频实体是否为同一视频
            VideoInfo oldVideo = videoInfoService.getExistSameVideoWithDirector(newVideoInfo);
            if (oldVideo == null) {
                //添加视频
                Serializable id = videoInfoDao.save(newVideoInfo);
                newVideoInfo.setId(id + "");
            } else {//为同一视频
                //更新tag
                newVideoInfo.setId(oldVideo.getId());
                oldVideo.setVideocount(newVideoInfo.getVideocount());
                oldVideo.setTag(newVideoInfo.getTag());
                oldVideo.setPicture(newVideoInfo.getPicture());
                oldVideo.setHpicture(newVideoInfo.getHpicture());
                oldVideo.setVpicture(newVideoInfo.getVpicture());
                oldVideo.setShow(newVideoInfo.getShow());
                oldVideo.setVideoType(newVideoInfo.getVideoType());
                videoInfoDao.update(oldVideo);
            }
 
            //加入专辑视频映射
            AlbumVideoMap map = new AlbumVideoMap();
            map.setId(newVideoInfo.getId());
            map.setCreateTime(new Date());
            map.setVideoId(newVideoInfo.getId());
            map.setRootVideoType(newVideoInfo.getVideoType().getId());
            map.setVideoCount(newVideoInfo.getVideocount());
            albumVideoMapDao.save(map);
 
            //加入映射
            VideoPPTVMap vi = new VideoPPTVMap();
            vi.setInfoId(series.getInfoID());
            vi.setVideoId(newVideoInfo.getId());
            vi.setCreateTime(new Date());
            videoPPTVMapDao.save(vi);
        }
 
        //添加视频资源附加信息
        VideoResourceMapExtraInfo vrm = new VideoResourceMapExtraInfo(newVideoInfo.getId(), (long) PPTVUtil.RESOURCE_ID);
        VideoResourceMapExtraInfo extra = videoResourceMapExtraInfoDao.find(VideoResourceMapExtraInfo.class, vrm.getId());
        if (extra == null) {
            vrm.setFree(Integer.parseInt(series.getFree()));
            vrm.setCreateTime(new Date());
            videoResourceMapExtraInfoDao.save(vrm);
        } else {//更新
            extra.setFree(Integer.parseInt(series.getFree()));
            extra.setUpdateTime(new Date());
            videoResourceMapExtraInfoDao.update(extra);
        }
 
        //添加视频来源映射
        resourceVideoService.addVideoResource(newVideoInfo.getId(), PPTVUtil.RESOURCE_ID + "");
        //添加视频分类映射
        categoryVideoService.addCategoryVideo(newVideoInfo.getId(), newVideoInfo.getVideoType().getId());
        CMQManager.getInstance().addSolrMsg(newVideoInfo.getId());
    }
 
    @Override
    public void offLineSeries(String infoId) {
        //查询出对应的视频ID
        VideoPPTVMap map = videoPPTVMapDao.selectByInfoId(infoId);
        if (map == null)
            return;
        videoPPTVMapDao.deleteByPrimaryKey(map.getVideoId());
        resourceVideoService.delete(map.getVideoId() + "", PPTVUtil.RESOURCE_ID + "");
    }
 
    @Override
    public PPTVSeries getSeriesDetail(String infoId) {
        PPTVSeries series = pptvSeriesDao.get(infoId);
        if (series == null) {
            return null;
        }
        PPTVQuery query = new PPTVQuery();
        query.infoId = infoId;
        query.count = 100;
        long count = pptvSeriesProgramMapDao.count(query);
        int page = (int) (count % query.count == 0 ? count / query.count : count / query.count + 1);
        List<PPTVProgram> programList = new ArrayList<>();
        for (int i = 0; i < page; i++) {
            query.start = i * query.count;
            List<PPTVSeriesProgramMap> mapList = pptvSeriesProgramMapDao.list(query);
            List<String> codeList = new ArrayList<>();
            for (PPTVSeriesProgramMap map : mapList) {
                codeList.add(map.getProgramCode());
            }
            List<PPTVProgram> tempList = pptvProgramDao.list(codeList);
            if (tempList != null && tempList.size() > 0) {
                programList.addAll(tempList);
            }
        }
        Comparator<PPTVProgram> comparator = new Comparator<PPTVProgram>() {
            @Override
            public int compare(PPTVProgram o1, PPTVProgram o2) {
                return o1.getRank() - o2.getRank();
            }
        };
        Collections.sort(programList, comparator);
 
        series.setSeries(programList);
        return series;
    }
 
    @Override
    public void save(List<PPTVSeries> seriesList) {
        for (PPTVSeries series : seriesList) {
            save(series);
        }
    }
 
    @Override
    public void save(PPTVSeries series) {
        //删除的InfoId
        Set<String> infoIds = new HashSet<>();
        //获取是否有更新
        PPTVSeries oldSeries = pptvSeriesDao.get(series.getInfoID());
        switch (series.getStatus()) {
            //保存
            case "add":
                //更新
            case "update":
                if (oldSeries == null || !oldSeries.getUpdateTime().equalsIgnoreCase(series.getUpdateTime())) {
                    series.setCreateTime(new Date());
                    pptvSeriesDao.save(series);
                    //处理新增/改变的剧集信息
                    CMQManager.getInstance().addPPTVSeriesUpdateMsg(new PPTVMQMsg(series.getInfoID(), PPTVMQMsg.TYPE_ADD_OR_UPDATE));
                }
 
                break;
            //删除
            case "del":
                deleteSeries(series);
                infoIds.add(series.getInfoID());
                break;
        }
 
        //删除的InfoId
        for (PPTVProgram pptvProgram : series.getSeries()) {
            Set<String> deleteInfoIds = processProgram(series, pptvProgram);
            if (deleteInfoIds != null) {
                infoIds.addAll(deleteInfoIds);
            }
        }
 
        for (String id : infoIds) {
            //处理删除的剧集信息
            CMQManager.getInstance().addPPTVSeriesUpdateMsg(new PPTVMQMsg(id, PPTVMQMsg.TYPE_DELETE));
        }
 
 
    }
 
    @Override
    public PPTVSeries getSeriesDetailByVideoId(String videoId) {
        VideoPPTVMap map = videoPPTVMapDao.get(videoId);
        if (map == null) {
            return null;
        }
        return getSeriesDetail(map.getInfoId());
    }
 
    @Override
    public PPTVProgram selectProgramById(String id) {
        return pptvProgramDao.get(id);
    }
 
    @Override
    public PPTVSeriesProgramMap selectMapByCode(String seriesCode, String programCode) {
        PPTVQuery query = new PPTVQuery();
        query.programCode = programCode;
        query.seriesCode = seriesCode;
        query.count = 1;
        List<PPTVSeriesProgramMap> list = pptvSeriesProgramMapDao.list(query);
        if (list == null || list.size() == 0)
            return null;
        return list.get(0);
    }
 
    @Override
    public VideoPPTVMap selectVideoPPTVMapByInfoId(String infoId) {
        return videoPPTVMapDao.selectByInfoId(infoId);
    }
 
    /**
     * 删除剧集
     *
     * @param series
     */
    private void deleteSeries(PPTVSeries series) {
        PPTVQuery query = new PPTVQuery();
        query.seriesCode = series.getSeriesCode();
        Set<String> seriesCodeSet = deleteMap(query);
        pptvSeriesDao.delete(series.getInfoID());
    }
 
 
    private Set<String> processProgram(PPTVSeries series, PPTVProgram program) {
        program.setCreateTime(new Date());
        //获取是否有更新
        PPTVProgram oldProgram = pptvProgramDao.get(program.getProgramCode());
        switch (program.getStatus()) {
            //保存
            case "add":
                //更新
            case "update":
                if (oldProgram == null || !oldProgram.getContentOnlineTime().equalsIgnoreCase(oldProgram.getContentOnlineTime())) {
                    program.setCreateTime(new Date());
                    pptvProgramDao.save(program);
 
                    PPTVSeriesProgramMap map = new PPTVSeriesProgramMap(series.getInfoID(), series.getSeriesCode(), program.getProgramCode(), program.getRank());
                    pptvSeriesProgramMapDao.save(map);
                }
                break;
            //删除
            case "del":
                return deleteProgram(program);
        }
 
        return null;
    }
 
    /**
     * 删除节目
     *
     * @param program
     */
    private Set<String> deleteProgram(PPTVProgram program) {
        //删除
        PPTVQuery query = new PPTVQuery();
        query.programCode = program.getProgramCode();
        Set<String> infoIdSet = deleteMap(query);
        pptvProgramDao.delete(program.getProgramCode());
        return infoIdSet;
    }
 
    private Set<String> deleteMap(PPTVQuery query) {
        long count = pptvSeriesProgramMapDao.count(query);
        if (count <= 0) {
            return null;
        }
        Set<String> infoIdSet = new HashSet<>();
        query.count = (int) count;
        List<PPTVSeriesProgramMap> mapList = pptvSeriesProgramMapDao.list(query);
        //删除map
        for (PPTVSeriesProgramMap map : mapList) {
            infoIdSet.add(map.getInfoId());
            pptvSeriesProgramMapDao.delete(map.getId());
        }
 
        return infoIdSet;
    }
 
 
}