admin
2021-02-19 58577bae968f2a10232bc8b3c04910b93ea3c69a
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
package com.yeshi.buwan.util;
 
import com.yeshi.buwan.domain.ResourceVideo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.dto.mq.*;
import com.yeshi.buwan.funtv.entity.FunTVAlbum2;
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum2;
import com.yeshi.buwan.pptv.entity.PPTVSeries;
import com.yeshi.buwan.service.imp.JobThreadExecutorServiceImpl;
import com.yeshi.buwan.service.imp.ResourceVideoService;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.inter.juhe.FunTV2Service;
import com.yeshi.buwan.service.inter.juhe.Iqiyi2Service;
import com.yeshi.buwan.service.inter.juhe.PPTVService;
import com.yeshi.buwan.service.manager.SolrAlbumDataManager;
import com.yeshi.buwan.util.mq.CMQManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 系统初始化
 *
 * @author Administrator
 */
@Component
public class SpringContext implements ApplicationListener<ContextRefreshedEvent> {
 
    @Resource
    private VideoInfoService videoInfoService;
 
    @Resource
    private SolrAlbumDataManager solrDataManager;
 
    @Resource
    private Iqiyi2Service iqiyi2Service;
 
    @Resource
    private FunTV2Service funTV2Service;
 
    @Resource
    private ResourceVideoService resourceVideoService;
 
    @Resource
    private PPTVService pptvService;
 
    private static boolean isInited = false;
 
    private final static Logger logger = LoggerFactory.getLogger(SpringContext.class);
 
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        if (arg0.getApplicationContext().getParent() != null) {
            System.out.println(System.currentTimeMillis());
            onApplication(arg0);
        }
    }
 
    private synchronized void onApplication(ContextRefreshedEvent context) {
        if (!isInited) {
            isInited = true;
            System.out.println("系统初始化成功");
            init();
        }
    }
 
    private void init() {
        logger.error("初始化");
        if (!Constant.JobTasker) {
            doSolrJob();
            doAddIqiyi2Video();
            doAddFunTV2Video();
            doDeleteVideoResource();
            doUpdateVideoExtraInfo();
        }
    }
 
    private void doSolrJob() {
        for (int i = 0; i < 5; i++) {
            new JobThreadExecutorServiceImpl().run(new Runnable() {
                @Override
                public void run() {
                    logger.info("doSolrJob");
                    List<SolrVideoMQMsg> solrMsgList = CMQManager.getInstance().consumeSolrMsg(16);
                    if (solrMsgList != null)
                        for (SolrVideoMQMsg solrVideo : solrMsgList) {
                            try {
                                VideoInfo videoInfo = videoInfoService.getVideoInfo(solrVideo.getId());
                                if (videoInfo != null) {
                                    if ("1".equalsIgnoreCase(videoInfo.getShow())) {
                                        List<VideoResource> resourceList = new ArrayList<>();
                                        List<ResourceVideo> rvList = resourceVideoService.getResourceList(videoInfo.getId());
                                        if (rvList != null)
                                            for (ResourceVideo rv : rvList)
                                                resourceList.add(rv.getResource());
                                        videoInfo.setResourceList(resourceList);
                                        solrDataManager.saveOrUpdate(videoInfo);
                                    } else
                                        solrDataManager.deleteById(videoInfo.getId());
                                }
                                CMQManager.getInstance().deleteSolrMsg(solrVideo.getHandler());
                            } catch (Exception e) {
                                logger.error("添加到搜索引擎出错", e);
                            }
                        }
                }
            });
        }
    }
 
 
    private void doAddIqiyi2Video() {
        new JobThreadExecutorServiceImpl().run(new Runnable() {
            @Override
            public void run() {
                logger.info("doAddIqiyi2Video");
                List<IqiyiAlbum2MQMsg> iqiyiAlbumMsgList = CMQManager.getInstance().consumeIqiyiAlbumUpdateMsg(16);
                if (iqiyiAlbumMsgList != null)
                    for (IqiyiAlbum2MQMsg iqiyiAlbum2MQMsg : iqiyiAlbumMsgList) {
                        try {
                            Long qikuID = iqiyiAlbum2MQMsg.getId();
                            IqiyiAlbum2 album2 = iqiyi2Service.selectAlbumById(qikuID);
                            logger.info("爱奇艺专辑:" + album2.getName());
                            if (album2 != null) {
                                iqiyi2Service.addToVideoInfo(album2);
                            }
                            CMQManager.getInstance().deleteIqiyiAlbumUpdateMsg(iqiyiAlbum2MQMsg.getHandler());
                        } catch (Exception e) {
                            logger.error("爱奇艺专辑添加到视频出错:" + e.getMessage());
                            logger.error("ID:" + iqiyiAlbum2MQMsg.getId());
                        }
                    }
            }
        });
 
    }
 
 
    private void doAddFunTV2Video() {
        new JobThreadExecutorServiceImpl().run(new Runnable() {
            @Override
            public void run() {
                List<FunTVAlbum2MQMsg> funTVAlbum2MsgList = CMQManager.getInstance().consumeFunTVAlbumUpdateMsg(16);
                if (funTVAlbum2MsgList != null)
                    for (FunTVAlbum2MQMsg funTVAlbum2MQMsg : funTVAlbum2MsgList) {
                        try {
                            String mediaId = funTVAlbum2MQMsg.getId();
                            FunTVAlbum2 album2 = funTV2Service.getAlbumDetail(mediaId);
                            if (album2 != null) {
                                funTV2Service.processAlbum(album2);
                            }
                            CMQManager.getInstance().deleteFunTVAlbumUpdateMsg(funTVAlbum2MQMsg.getHandler());
                        } catch (Exception e) {
                            logger.error("风行专辑添加到视频出错:" + e.getMessage());
                            logger.error("ID:" + funTVAlbum2MQMsg.getId());
                        }
                    }
            }
        });
 
    }
 
 
    private void doAddPPTVVideo() {
        new JobThreadExecutorServiceImpl().run(new Runnable() {
            @Override
            public void run() {
                List<PPTVMQMsg> pptvMsgList = CMQManager.getInstance().consumePPTVSeriesUpdateMsg(16);
                if (pptvMsgList != null)
                    for (PPTVMQMsg pptvmqMsg : pptvMsgList) {
                        try {
                            switch (pptvmqMsg.getType()) {
                                case PPTVMQMsg.TYPE_ADD_OR_UPDATE:
                                    PPTVSeries pptvSeries = pptvService.getSeriesDetail(pptvmqMsg.getInfoId());
                                    if (pptvSeries != null) {
                                        pptvService.addToVideoInfo(pptvSeries);
                                    }
                                    break;
 
                                case PPTVMQMsg.TYPE_DELETE:
                                    pptvService.offLineSeries(pptvmqMsg.getInfoId());
                                    break;
                            }
                            CMQManager.getInstance().deletePPTVSeriesUpdateMsg(pptvmqMsg.getHandler());
                        } catch (Exception e) {
                            logger.error("PPTV添加到视频出错:" + e.getMessage());
                            logger.error("infoId:" + pptvmqMsg.getInfoId());
                        }
                    }
            }
        });
 
    }
 
    private void doDeleteVideoResource() {
        new JobThreadExecutorServiceImpl().run(new Runnable() {
            @Override
            public void run() {
                List<CMQResult> cmqMsgList = CMQManager.getInstance().consumeVideoResourceDeleteMsg(16);
                if (cmqMsgList != null)
                    for (CMQResult msg : cmqMsgList) {
                        try {
                            String videoId = msg.getData() + "";
                            //查询资源列表
                            List<ResourceVideo> resourceVideoList = resourceVideoService.getResourceList(videoId);
                            //隐藏视频
                            if (resourceVideoList == null || resourceVideoList.size() == 0)
                                videoInfoService.hiddenVideo(videoId);
                            //更新搜索引擎
                            CMQManager.getInstance().addSolrMsg(videoId);
                            CMQManager.getInstance().deleteVideoResourceDeleteMsg(msg.getHandler());
                        } catch (Exception e) {
                            logger.error("视频资源删除处理出错:" + e.getMessage());
                            logger.error("ID:" + msg.getData());
                        }
                    }
            }
        });
 
    }
 
 
    private void doUpdateVideoExtraInfo() {
        new JobThreadExecutorServiceImpl().run(new Runnable() {
            @Override
            public void run() {
                List<CMQResult> cmqMsgList = CMQManager.getInstance().consumeUpdateVideoExtraInfoMsg(16);
                if (cmqMsgList != null)
                    for (CMQResult msg : cmqMsgList) {
                        try {
                            VideoExtraInfoChangeMQMsg videoExtraInfoChangeMQMsg = (VideoExtraInfoChangeMQMsg) msg.getData();
 
                            if (videoExtraInfoChangeMQMsg != null) {
                                switch (videoExtraInfoChangeMQMsg.getType()) {
                                    case VideoExtraInfoChangeMQMsg.TYPE_RESOURCE:
                                        if (VideoExtraInfoChangeMQMsg.ACTION_DELETE.equalsIgnoreCase(videoExtraInfoChangeMQMsg.getAction())) {//删除视频源
                                            CMQManager.getInstance().addVideoResourceDeleteMsg(videoExtraInfoChangeMQMsg.getVideoId());
                                        }
                                        break;
                                    case VideoExtraInfoChangeMQMsg.TYPE_CATEGORY:
                                        break;
                                }
 
                                videoInfoService.statisticVideoExtraInfo(videoExtraInfoChangeMQMsg.getVideoId());
                            }
                            CMQManager.getInstance().deleteUpdateVideoExtraInfoMsg(msg.getHandler());
                        } catch (Exception e) {
                            logger.error("视频资源删除处理出错:" + e.getMessage());
                            logger.error("ID:" + msg.getData());
                        }
                    }
            }
        });
 
    }
 
 
}