admin
2021-03-01 d73687bc6115007145b4aab050e4e29ff87fd8ae
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
package com.yeshi.buwan.service.imp.juhe;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.buwan.dao.CategoryVideoDao;
import com.yeshi.buwan.dao.juhe.sohu.SoHuAlbumDao;
import com.yeshi.buwan.dao.juhe.sohu.SoHuVideoDao;
import com.yeshi.buwan.domain.CategoryVideo;
import com.yeshi.buwan.domain.ResourceVideo;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.domain.VideoType;
import com.yeshi.buwan.domain.push.VideoPushHistory;
import com.yeshi.buwan.service.imp.StatisticsService;
import com.yeshi.buwan.service.imp.push.PushService;
import com.yeshi.buwan.sohu.SoHuUtil;
import com.yeshi.buwan.sohu.entity.SoHuAlbum;
import com.yeshi.buwan.sohu.entity.SoHuVideo;
import com.yeshi.buwan.sohu.entity.VideoSoHu;
import com.yeshi.buwan.util.StringUtil;
 
@Service
public class SoHuService {
    @Resource
    private SoHuAlbumDao soHuAlbumDao;
    @Resource
    private SoHuVideoDao soHuVideoDao;
    @Resource
    private StatisticsService statisticsService;
    @Resource
    private PushService pushService;
    @Resource
    private CategoryVideoDao categoryVideoDao;
 
    @SuppressWarnings("unchecked")
    public String addAlbum(final SoHuAlbum album) {
        // 对VideoList去重
        System.out.println(album.getAid() + "--" + album.getAlbum_name());
        return soHuAlbumDao.excute(new HibernateCallback<String>() {
            public String doInHibernate(Session session) throws HibernateException {
                String id = "";
                try {
                    List<SoHuAlbum> list = session.createQuery("from SoHuAlbum sa where sa.aid=?")
                            .setParameter(0, album.getAid()).list();
 
                    boolean isInsert;
                    if (list == null || list.size() == 0) {
                        id = saveSoHuAlbum(album) + "";
                        isInsert = true;
                    } else {
                        isInsert = album.getLatest_video_count() != list.get(0).getLatest_video_count();
                        if (isInsert) {
                            list.get(0).setModerator(album.getModerator());
                            list.get(0).setLatest_video_count(album.getLatest_video_count());
                            list.get(0).setTotal_video_count(album.getTotal_video_count());
                            updateSoHuAlbum(list.get(0));
                        }
                        id = list.get(0).getId();
                    }
 
                    if (album.getVideoList() != null && isInsert) {
                        Collections.sort(album.getVideoList(), new Comparator<SoHuVideo>() {
                            public int compare(SoHuVideo o1, SoHuVideo o2) {
                                return o2.getVideo_order() - o1.getVideo_order();
                            }
                        });
 
                        List<SoHuVideo> soHuList = session
                                .createQuery("from SoHuVideo sh where sh.aid=? order by sh.video_order desc")
                                .setParameter(0, album.getAid()).setFirstResult(0).setMaxResults(1).list();
 
                        SoHuVideo latestSoHuVideo = soHuList != null && soHuList.size() > 0 ? soHuList.get(0) : null;
                        int startP = 0;
                        if (latestSoHuVideo != null) {
                            for (int i = 0; i < album.getVideoList().size(); i++) {
                                if (latestSoHuVideo.getVid().equalsIgnoreCase(album.getVideoList().get(i).getVid())) {
                                    startP = i + 1;
                                    break;
                                }
                            }
                        }
 
                        if (startP <= album.getVideoList().size())// 有更新内容
                        {
                            startP = startP == 0 ? album.getVideoList().size() : startP;
                            List<SoHuVideo> videoList = album.getVideoList().subList(0, startP);
                            for (SoHuVideo info : videoList)
                                addSoHuVideo(info);
 
                        }
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return id;
            }
        }) + "";
 
    }
 
    public void addSoHuVideo(SoHuVideo info) {
        soHuVideoDao.create(info);
    }
 
    public Serializable saveSoHuAlbum(SoHuAlbum info) {
        return soHuAlbumDao.save(info);
    }
 
    public void updateSoHuAlbum(SoHuAlbum info) {
        soHuAlbumDao.update(info);
    }
 
    public List<SoHuVideo> getLatestVideo(String aid) {
        return soHuVideoDao.list("from SoHuVideo sv where sv.aid=? order by UNIX_TIMESTAMP(sv.create_date) DESC", 0, 1,
                new String[] { aid });
    }
 
    private String saveVideoInfo(final VideoInfo vi, final SoHuAlbum sa) {
        return soHuVideoDao.excute(new HibernateCallback<String>() {
            public String doInHibernate(Session session) throws HibernateException {
                session.getTransaction().begin();
                String vid = session.save(vi) + "";
                VideoSoHu vs = new VideoSoHu();
                vs.setAlbum(sa);
                vs.setVideo(new VideoInfo(vid));
                session.persist(vs);
                ResourceVideo rv = new ResourceVideo();
                rv.setResource(new VideoResource("14"));
                rv.setVideo(new VideoInfo(vid));
                session.persist(rv);
                session.flush();
                session.getTransaction().commit();
                return vid;
            }
        }) + "";
 
    }
 
    // 将专辑添加到视频
    @SuppressWarnings("unchecked")
    public String addToVideoInfo(final SoHuAlbum sa, final boolean isUpdate) {
        String videoid = soHuAlbumDao.excute(new HibernateCallback<String>() {
            public String doInHibernate(Session session) throws HibernateException {
                sa.setVideoList(getLatestVideo(sa.getAid()));
                VideoInfo vi = SoHuUtil.convertAlBumToVideoInfo(sa);
                String vid = "";
                try {
                    session.getTransaction().begin();
                    List<VideoInfo> list = session.createQuery("from VideoInfo vi where vi.name=?")
                            .setParameter(0, vi.getName()).list();
 
                    if (list == null || list.size() == 0)// 没有加入VideoInfo
                    {
                        vid = session.save(vi) + "";
                        VideoSoHu vs = new VideoSoHu();
                        vs.setAlbum(sa);
                        vs.setVideo(new VideoInfo(vid));
                        session.persist(vs);
 
                        ResourceVideo rv = new ResourceVideo();
                        rv.setResource(new VideoResource("14"));
                        rv.setVideo(new VideoInfo(vid));
                        session.persist(rv);
 
                    } else// 加入VideoInfo
                    {
                        vid = list.get(0).getId();
                        // 更新videoInfo
                        list.get(0).setTag(vi.getTag());
                        if (isUpdate)
                            list.get(0).setUpdatetime(System.currentTimeMillis() + "");
                        list.get(0).setVideocount(vi.getVideocount());
                        list.get(0).setLatestHpicture(vi.getLatestHpicture());
                        list.get(0).setLatestVpicture(vi.getLatestVpicture());
                        session.update(list.get(0));
                        // 是否已经加入到了SohuVideo
 
                        Object count = session
                                .createQuery("select count(*) from VideoSoHu vs where vs.video.id=? and vs.album.aid=?")
                                .setParameter(0, list.get(0).getId()).setParameter(1, sa.getAid()).uniqueResult();
                        if (Integer.parseInt(count + "") <= 0)// 加入到VideoSoHu
                        {
                            VideoSoHu vs = new VideoSoHu();
                            vs.setAlbum(sa);
                            vs.setVideo(list.get(0));
                            session.persist(vs);
 
                            ResourceVideo rv = new ResourceVideo();
                            rv.setResource(new VideoResource("14"));
                            rv.setVideo(list.get(0));
                            session.persist(rv);
                        }
 
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    if (session.getTransaction().isActive())
                        session.getTransaction().rollback();
                }
                return vid;
            }
        }) + "";
 
        if (isUpdate) {// 确实是更新
            try {
                VideoPushHistory vph = new VideoPushHistory();
                vph.setCreatetime(System.currentTimeMillis() + "");
                vph.setDetailId(convertSoHuVideoToDETAIL(sa.getVideoList().get(0)).getId() + "");
                vph.setResourceId("13");
                vph.setTag(sa.getTip());
                vph.setType("");
                vph.setVideoInfo(new VideoInfo(videoid));
                pushService.addVideoPushHistory(vph);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        return videoid;
    }
 
    public List<VideoType> getVideoTypeList(SoHuAlbum sa) {
        List<VideoType> list = new ArrayList<>();
        String cateCode = sa.getCate_code();
        String[] ca = cateCode.split(";");
        for (int i = 1; i < ca.length; i++) {
            List<Integer> typeList = SoHuUtil.getSecondCategory(Integer.parseInt(ca[i]));
            for (Integer type : typeList) {
                if (type > 0) {
                    boolean can = true;
                    for (VideoType vtt : list)
                        if (vtt.getId() == type)
                            can = false;
                    if (can)
                        list.add(new VideoType(type));
                }
            }
        }
 
        if (list.size() == 0) {// 找不到主分类
            if (ca[0].equalsIgnoreCase("100")) {
                list.add(new VideoType(151));
            } else if (ca[0].equalsIgnoreCase("101")) {
                list.add(new VideoType(150));
            } else if (ca[0].equalsIgnoreCase("115")) {
                list.add(new VideoType(153));
            } else if (ca[0].equalsIgnoreCase("106")) {
                list.add(new VideoType(152));
            } else if (ca[0].equalsIgnoreCase("133")) {
                list.add(new VideoType(216));
            }
        }
        return list;
    }
 
    public void addToCategoryVideo(String vid, long type) {
 
        long count = soHuAlbumDao.getCount("from CategoryVideo cv where cv.video.id=? and cv.videoType.id=" + type,
                new String[] { vid });
        if (count <= 0) {// 还没添加
            CategoryVideo cv = new CategoryVideo();
            cv.setVideo(new VideoInfo(vid));
            cv.setVideoType(new VideoType(type));
            categoryVideoDao.create(cv);
        }
    }
 
    private VideoDetailInfo convertSoHuVideoToDETAIL(SoHuVideo sv) {
        VideoDetailInfo vd = new VideoDetailInfo(Integer.parseInt(sv.getId()));
        if (!StringUtil.isNullOrEmpty(sv.getPeriod()))
            vd.setTag(sv.getPeriod() + "");
        else
            vd.setTag(sv.getVideo_order() + "");
        vd.setType("1");
        vd.setExtraId(vd.getId() + "");
        return vd;
    }
 
    @SuppressWarnings("unchecked")
    public List<VideoDetailInfo> getVideoDetailList(final String videoid,int page,int pageSize) {
        return (List<VideoDetailInfo>) soHuAlbumDao.excute(new HibernateCallback<List<VideoDetailInfo>>() {
            public List<VideoDetailInfo> doInHibernate(Session session) throws HibernateException {
                List<VideoDetailInfo> detailList = new ArrayList<>();
                try {
                    List<SoHuAlbum> list = session.createQuery("select vs.album from VideoSoHu vs where vs.video.id=?")
                            .setParameter(0, videoid).list();
                    if (list != null && list.size() > 0) {
                        SoHuAlbum sa = list.get(0);
                        String order = " asc";
                        if (sa.getCate_code().contains("106;"))// 综艺
                            order = " desc";
                        List<SoHuVideo> svList = session
                                .createQuery("from SoHuVideo sv where sv.aid=? order by sv.video_order" + order)
                                .setParameter(0, sa.getAid()).list();
                        for (SoHuVideo sv : svList) {
                            detailList.add(convertSoHuVideoToDETAIL(sv));
                        }
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return detailList;
 
            }
        });
 
    }
 
    @SuppressWarnings("unchecked")
    public VideoDetailInfo getLatestVideoDetail(final String videoid) {
        return (VideoDetailInfo) soHuAlbumDao.excute(new HibernateCallback<VideoDetailInfo>() {
            public VideoDetailInfo doInHibernate(Session session) throws HibernateException {
                List<VideoDetailInfo> detailList = new ArrayList<>();
                try {
                    List<SoHuAlbum> list = session.createQuery("select vs.album from VideoSoHu vs where vs.video.id=?")
                            .setParameter(0, videoid).list();
                    if (list != null && list.size() > 0) {
                        SoHuAlbum sa = list.get(0);
                        String order = " asc";
 
                        order = " desc";
                        List<SoHuVideo> svList = session
                                .createQuery("from SoHuVideo sv where sv.aid=? order by sv.video_order" + order)
                                .setParameter(0, sa.getAid()).setFirstResult(0).setMaxResults(1).list();
                        for (SoHuVideo sv : svList) {
                            detailList.add(convertSoHuVideoToDETAIL(sv));
                        }
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return detailList.get(0);
 
            }
        });
 
    }
 
    // 添加播放统计
    public void addPlayStatistics(String detailSystemId, SoHuVideo sv) {
        List list = soHuAlbumDao.sqlList(
                "SELECT vs.`videoid` FROM wk_sohu_album a LEFT JOIN wk_video_sohu vs ON vs.`albumid`=a.`id` WHERE a.`aid`=?",
                new Serializable[] { sv.getAid() });
        if (list != null && list.size() > 0) {
            statisticsService.addStatistics(detailSystemId, list.get(0) + "");
        }
    }
 
    // 获取最近的一条数据
 
    public SoHuVideo getLatestSoHuVideo(String aid) {
        List<SoHuVideo> svList = soHuVideoDao.list("from SoHuVideo sv where sv.aid=? order by sv.video_order desc", 0,
                1, new Serializable[] { aid });
        if (svList != null && svList.size() > 0)
            return svList.get(0);
        return null;
    }
 
}