admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
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
package com.yeshi.buwan.service.imp;
 
import com.yeshi.buwan.dao.CategoryVideoDao;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.VideoResourceDao;
import com.yeshi.buwan.domain.CategoryVideo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.util.Constant;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class VideoService {
    @Resource
    private VideoInfoDao videoInfoDao;
 
    @Resource
    private VideoResourceDao videoResourceDao;
 
    @Resource
    private ClassService classServcie;
 
    @Resource
    private CategoryVideoDao categoryVideoDao;
 
    public VideoService() {
 
    }
 
    public VideoInfo getVideoInfo(String id) {
        return videoInfoDao.find(VideoInfo.class, id);
    }
 
 
    public List<VideoInfo> getVideoInfoList(List<String> ids) {
        if (ids == null || ids.size() == 0)
            return null;
        List<VideoInfo> list = videoInfoDao.listByVideoIds(ids);
        Map<String, VideoInfo> map = new HashMap<>();
        for (VideoInfo vi : list) {
            map.put(vi.getId(), vi);
        }
        list.clear();
        for (String id : ids) {
            list.add(map.get(id));
        }
        return list;
    }
 
    public List<VideoInfo> getVideoListByClass(int type, int page) {
        String types[] = classServcie.getChildrenType(type);
        String wheres = "";
        String as[];
        int j = (as = types).length;
        for (int i = 0; i < j; i++) {
            String st = as[i];
            wheres = (new StringBuilder(String.valueOf(wheres))).append("or v.videoType.id=").append(st).append(" ")
                    .toString();
        }
 
        if (wheres.startsWith("or"))
            wheres = wheres.substring(2, wheres.length());
        List<VideoInfo> list = videoInfoDao.list(
                (new StringBuilder("from VideoInfo v where v.show='1' and (")).append(wheres).append(")").toString(),
                (page - 1) * Constant.pageCount, 20, new String[]{});
        return list;
    }
 
    public List<VideoInfo> searchVideoUsedByAdmin(String name) {
        return videoInfoDao.list("from VideoInfo v where v.name like ? order by REPLACE(v.name,?,'')", 0, 20,
                new String[]{"%" + name + "%", name});
    }
 
    public long getVideoCountByClass(int type) {
        String types[] = classServcie.getChildrenType(type);
        String wheres = "";
        String as[];
        int j = (as = types).length;
        for (int i = 0; i < j; i++) {
            String st = as[i];
            wheres = (new StringBuilder(String.valueOf(wheres))).append("or v.videoType.id=").append(st).append(" ")
                    .toString();
        }
 
        if (wheres.startsWith("or"))
            wheres = wheres.substring(2, wheres.length());
        return videoInfoDao
                .getCount((new StringBuilder("select count(*)  from VideoInfo v where ")).append(wheres).toString());
    }
 
    public void addVideoWatch(final String vid) {
        videoInfoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    session.createSQLQuery("update wk_video_video v set v.watchcount=v.watchcount+1 where v.id=?")
                            .setParameter(0, vid).executeUpdate();
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return null;
            }
        });
 
    }
 
    public void addCategoryVideo(CategoryVideo cv) {
        categoryVideoDao.save(cv);
    }
 
    /**
     * 将没有添加进分裂视频的视频加入杂类
     */
    public void addNOAddToCategoryVideo() {
        videoInfoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    List list = session
                            .createSQLQuery(
                                    "SELECT  DISTINCT(v.`id`) FROM wk_video_video v LEFT JOIN wk_category_video cv ON cv.`videoid`=v.`id` WHERE cv.`id` IS NULL")
                            .setFirstResult(0).setMaxResults(10000).list();
                    for (int i = 0; i < list.size(); i++) {
                        String videoId = list.get(i) + "";
                        session.createSQLQuery(
                                "INSERT INTO wk_category_video(videoid,videotypeid) VALUES(" + videoId + ",299)")
                                .executeUpdate();
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return null;
            }
        });
 
    }
 
    @Cacheable(value = "guessLikeCache", key = "'guessLike'+'-'+#page")
    public List<VideoInfo> guessLike(int page) {
        List<VideoInfo> list = videoInfoDao.list(
                "select  cv.video from CategoryVideo cv where (cv.videoType= 216) and cv.video.show=1 order by cv.id desc",
                (page - 1) * Constant.pageCount, Constant.pageCount, null);
        String where = "";
        for (VideoInfo vi : list) {
            where += String.format(" rv.video.id=%s or", vi.getId());
        }
        if (where.endsWith("or"))
            where = where.substring(0, where.length() - 2);
 
        // 查询来源
        List<VideoResource> resourceList = videoResourceDao.list(String.format("from VideoResource"));
        Map<String, VideoResource> map = new HashMap<>();
        for (VideoResource rv : resourceList)
            map.put(rv.getName(), rv);
 
        // 来源赋值
 
        for (VideoInfo video : list) {
            List<VideoResource> rsList = new ArrayList<>();
            if (video.getPicture().contains("juhe")) {
                rsList.add(map.get("爱奇艺"));
            } else if (video.getPicture().contains("aixifan")) {
                rsList.add(map.get("AcFun"));
            } else if (video.getPicture().contains("funshion")) {
                rsList.add(map.get("风行"));
            } else {
                VideoResource vr = new VideoResource();
                vr.setPicture("");
                vr.setName("");
                rsList.add(vr);
            }
            video.setResourceList(rsList);
        }
        return list;
    }
}