admin
2021-08-02 e89de9319d8f771f8e53cb5f876d06465a580c57
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
package com.yeshi.buwan.service.imp;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.service.inter.video.VideoInfoExtraService;
import com.yeshi.buwan.util.JuHe.VideoResourceUtil;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.buwan.dao.CategoryRecommendCacheVideoDao;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoInfoRank;
import com.yeshi.buwan.domain.recommend.CategoryRecommendCacheVideo;
import com.yeshi.buwan.service.imp.recommend.CategoryRecommendVideoService;
 
import javax.annotation.Resource;
 
@Service
public class CategoryRecommendCacheVideoService {
 
    @Autowired
    private CategoryRecommendCacheVideoDao dao;
 
    @Autowired
    private CategoryRecommendVideoService categoryRecommendVideoService;
 
    @Resource
    private VideoInfoExtraService videoInfoExtraService;
 
    @Resource
    private VideoResourceUtil videoResourceUtil;
 
    public List<CategoryRecommendCacheVideo> getList(long videotype, String key, int page, int pageSize) {
 
        List<CategoryRecommendCacheVideo> list = null;
 
        int start = (page - 1) * pageSize;
 
        if (videotype <= 0) { // 全部
            String hql = "";
            if (key != null && !"".equals(key.trim())) {
                hql = "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc";
                list = dao.list(hql, start, pageSize, new Serializable[]{"%" + key + "%"});
            } else {
                hql = "from CategoryRecommendCacheVideo crcv order by crcv.rank desc , crcv.orderby desc";
                list = dao.list(hql, start, pageSize, new Serializable[]{});
            }
        } else {
            if (key != null && !"".equals(key.trim())) {
                // list = dao.sqlList("select * from wk_recommend_category_cache
                // cc left join wk_video_video vv on vv.id=cc.videoid where
                // cc.videotypeid = ? and vv.name like ? order by cc.rank =
                // 0,cc.rank,cc.orderby desc", start, pageSize, params);
                list = dao.list(
                        "from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc",
                        start, pageSize, new Serializable[]{videotype, "%" + key + "%"});
            } else {
                list = dao.list(
                        "from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? order by crcv.rank desc , crcv.orderby desc",
                        start, pageSize, new Serializable[]{videotype});
            }
        }
 
        // if(list != null && list.size() > 0){
        // Collections.sort(list, new Comparator<CategoryRecommendCacheVideo>()
        // {
        // @Override
        // public int compare(CategoryRecommendCacheVideo o1,
        // CategoryRecommendCacheVideo o2) {
        // int rank1= o1.getRank();
        // int rank2 = o2.getRank();
        // int b1 =o1.getOrderby();
        // int b2=o2.getOrderby();
        // if(rank2 == 0 && rank1 == 0){
        // return b2-b1;
        // }else if(rank2 == 0 && rank1 !=0){
        // return -1;
        // }else if(rank2 != 0 && rank1 == 0){
        // return 1;
        // }else if(rank2 != 0 && rank1 != 0){
        // int i = rank1 - rank2;
        // if(i==0){
        // return b2-b1;
        // }
        // return i;
        // }
        // return 0;
        // }
        // });
        // }
        return list;
 
    }
 
    public int getCount(long videotype, String key) {
        if (videotype <= 0) {
            if (key != null && !"".equals(key.trim())) {
                return (int) dao.getCount(
                        "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? ",
                        new Serializable[]{"%" + key + "%"});
            } else {
                return (int) dao.getCount("select count(*) from CategoryRecommendCacheVideo crcv",
                        new Serializable[]{});
            }
        } else {
            if (key != null && !"".equals(key.trim())) {
                return (int) dao.getCount(
                        "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? ",
                        new Serializable[]{videotype, "%" + key + "%"});
            }
            return (int) dao.getCount(
                    "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? ",
                    new Serializable[]{videotype});
        }
 
    }
 
    public CategoryRecommendCacheVideo getTop(long id) {
        return dao.find(CategoryRecommendCacheVideo.class, id + "");
    }
 
    @Transactional
    @CacheEvict(value = "topCache", allEntries = true)
    public void update(CategoryRecommendCacheVideo crcv) {
        dao.update(crcv);
    }
 
    @Transactional
    public void save(CategoryRecommendCacheVideo crcv) {
        dao.save(crcv);
    }
 
    @Transactional
    @CacheEvict(value = "topCache", allEntries = true)
    public void delete(long id) {
        CategoryRecommendCacheVideo crcv = new CategoryRecommendCacheVideo();
        crcv.setId(id + "");
        dao.delete(crcv);
    }
 
    public CategoryRecommendCacheVideo getTop(long vid, long tid) {
        List<CategoryRecommendCacheVideo> list = dao.list(
                "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? and crcv.videoType.id = ?",
                new Serializable[]{vid + "", tid});
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
 
    @SuppressWarnings("unchecked")
    @Cacheable(value = "topCache", key = "'getVideoListByRank'+'-'+#videotype+'-'+#detailSystem+'-'+#platform+'-'+#version+'-'+#channel")
    public List<VideoInfo> getVideoListByRank(final int videotype, final String detailSystem, String platform, Integer version, String channel) {
 
        List<Long> resourceList = videoResourceUtil.getAvailableResourceIds(new DetailSystem(detailSystem), version, channel);
 
        final int count = categoryRecommendVideoService.getRankVideoNumber(videotype);
        List<VideoInfo> videoInfoList = (List<VideoInfo>) dao.excute(new HibernateCallback<List<VideoInfo>>() {
            @Override
            public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
                SQLQuery query = session.createSQLQuery("SELECT cc.rank,"
                        + "vv.id,vv.picture,vv.name,vv.vpicture,vv.hpicture,vv.latest_vpicture,vv.latest_hpicture,vv.watchcount,vv.commentcount,cc.orderby,vv.tag "
                        + "FROM wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON cc.videoid = vv.id LEFT JOIN wk_resource_video rv ON rv.videoid=vv.id LEFT JOIN wk_video_banquan_video bv on cc.videoid = bv.videoid WHERE bv.id is null and (bv.detailsystemid=? OR bv.detailsystemid IS null ) and rv.resourceid <> 16 AND vv.id IS NOT NULL AND vv.show = 1 AND cc.videotypeid=? AND cc.videoid = vv.id AND cc.rank <= ? AND cc.rank >= 0  GROUP BY cc.id ORDER BY cc.rank !=0 DESC ,cc.rank desc,cc.orderby desc");
                query.setParameter(0, detailSystem);
                query.setParameter(1, videotype);
                query.setParameter(2, count);
                query.setFirstResult(0);
                query.setMaxResults(count);
                List<Object[]> qdata = query.list();
                List<VideoInfoRank> rks = new ArrayList<>();
                List<VideoInfo> vs = new ArrayList<>();
                VideoInfoRank vr;
                for (Object[] objs : qdata) {
                    vr = new VideoInfoRank();
                    vr.setRank(Integer.parseInt(objs[0] + ""));
                    vr.setId(objs[1] + "");
                    vr.setPicture(objs[2] + "");
                    vr.setName(objs[3] + "");
                    vr.setVpicture(objs[4] + "");
                    vr.setHpicture(objs[5] + "");
                    vr.setLatestVpicture(objs[6] + "");
                    vr.setLatestHpicture(objs[7] + "");
                    vr.setWatchCount(objs[8] != null ? objs[8] + "" : "0");
                    vr.setCommentCount(objs[9] != null ? Integer.parseInt(objs[9] + "") : 0);
                    vr.setOrderBy(Integer.parseInt(objs[10] + ""));
                    vr.setTag(objs[11] + "");
                    vr.setVideocount(0);
                    if (vr.getRank() > 0) {
                        rks.add(vr);
                    } else
                        vs.add(vr);
                }
 
                Comparator<VideoInfoRank> c = new Comparator<VideoInfoRank>() {
 
                    @Override
                    public int compare(VideoInfoRank arg0, VideoInfoRank arg1) {
                        return arg0.getRank() - arg1.getRank();
                    }
                };
 
                Collections.sort(rks, c);
 
                // 找出rank=0的视频
 
                for (int i = 1; i <= count; i++) {
                    boolean exist = false;
                    for (VideoInfoRank vir : rks)
                        if (vir.getRank() == i) {
                            exist = true;
                            break;
                        }
                    // 需要加入rks
                    if (!exist) {
                        if (vs.size() > 0) {
                            VideoInfoRank rr = (VideoInfoRank) vs.get(0);
                            rr.setRank(i);
                            rks.add(rr);
                            vs.remove(0);
                        }
                    }
                }
 
                vs.clear();
 
                Collections.sort(rks, c);
 
                for (VideoInfoRank videoInfoRank : rks)
                    vs.add(videoInfoRank);
                return vs;
            }
        });
 
        return videoInfoExtraService.batchExtra(videoInfoList, resourceList);
    }
 
    public CategoryRecommendCacheVideo getByVid(String vid) {
        List<CategoryRecommendCacheVideo> list = dao
                .list("from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? ", new Serializable[]{vid});
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
}