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
package com.yeshi.buwan.service.imp.recommend;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
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 com.yeshi.buwan.dao.recommend.CategoryRecommendCacheVideoNumberDao;
import com.yeshi.buwan.dao.recommend.CategoryRecommendVideoDao;
import com.yeshi.buwan.dao.recommend.SuperCategoryRecommendVideoDao;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.recommend.CategoryRecommendCacheVideoNumber;
import com.yeshi.buwan.domain.recommend.CategoryRecommendVideo;
import com.yeshi.buwan.domain.recommend.SuperCategoryRecommendVideo;
import com.yeshi.buwan.domain.web.CategoryRecommendVideoAdmin;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.util.Constant;
 
@Service
public class CategoryRecommendVideoService {
    @Resource
    private CategoryRecommendVideoDao categoryRecommendVideoDao;
    @Resource
    private SuperCategoryRecommendVideoDao superCategoryRecommendVideoDao;
    @Resource
    private CategoryRecommendCacheVideoNumberDao categoryRecommendCacheVideoNumberDao;
 
 
    // 获取大区推荐的Banner
    @Cacheable(value = "classCache", key = "'getCategoryRecommendVideoBannerList'+'-'+#detailSystemId+'-'+#videoTypeId")
    public List<CategoryRecommendVideo> getCategoryRecommendVideoBannerList(String detailSystemId, long videoTypeId) {
        return categoryRecommendVideoDao.list(
                "select sc.categoryRecommendVideo from SuperCategoryRecommendVideo sc where  sc.categoryRecommendVideo is not null and sc.categoryRecommendVideo.videoInfo is not null and sc.detailSystem.id=? and sc.categoryRecommendVideo.videoType.id=?  order by sc.categoryRecommendVideo.orderby desc",
                new Serializable[]{detailSystemId, videoTypeId});
    }
 
    public void addSuperCategoryRecommendVideoBanner(SuperCategoryRecommendVideo sc) {
 
        superCategoryRecommendVideoDao.create(sc);
 
    }
 
    public void deleteSuperCategoryRecommendVideo(String id) {
 
        superCategoryRecommendVideoDao.delete(new SuperCategoryRecommendVideo(id));
 
    }
 
    public void addCategoryRecommendVideo(CategoryRecommendVideo crv) {
        categoryRecommendVideoDao.create(crv);
    }
 
    public CategoryRecommendVideo getCategoryRecommendVideoById(String id) {
        return categoryRecommendVideoDao.find(CategoryRecommendVideo.class, id);
    }
 
    public void updateCategoryRecommendVideo(CategoryRecommendVideo crv) {
        categoryRecommendVideoDao.update(crv);
    }
 
    @SuppressWarnings("rawtypes")
    public void deleteCategoryRecommendVideo(final CategoryRecommendVideo crv) {
        categoryRecommendVideoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    session.createQuery("delete from SuperCategoryRecommendVideo s where s.categoryRecommendVideo.id=?")
                            .setParameter(0, crv.getId()).executeUpdate();
                    session.delete(crv);
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    // 分区榜首数量
    @Cacheable(value = "classCache", key = "'getRankVideoNumber'+'-'+#videotype")
    public int getRankVideoNumber(int videotype) {
        List<CategoryRecommendCacheVideoNumber> list = categoryRecommendCacheVideoNumberDao
                .list("from CategoryRecommendCacheVideoNumber cr where  cr.videoType.id=" + videotype);
        if (list != null && list.size() > 0)
            return list.get(0).getNumber();
        else
            return 32;
    }
 
    // 后台操作
    @SuppressWarnings({"unchecked"})
    public List<CategoryRecommendVideoAdmin> getCategoryRecommendVideoAdmin(final String key, final int videoTypeId, final String systemId,
                                                                            final int detailSystem, final int page) {
        return (List<CategoryRecommendVideoAdmin>) categoryRecommendVideoDao
                .excute(new HibernateCallback<List<CategoryRecommendVideoAdmin>>() {
                    public List<CategoryRecommendVideoAdmin> doInHibernate(Session session) throws HibernateException {
                        List<CategoryRecommendVideoAdmin> zhiBoClassList = new ArrayList<>();
                        try {
                            List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem ds where ds.system.id=" + systemId).list();
                            String sql = "";
                            if (detailSystem > 0)
                                // on sh.categoryRecommendVideo.videoInfo= vi.id
                                sql = "select sh.categoryRecommendVideo from SuperCategoryRecommendVideo sh  where sh.categoryRecommendVideo.videoType.id="
                                        + videoTypeId + " and  sh.detailSystem.id=" + detailSystem + " and sh.categoryRecommendVideo.videoInfo.name like '%" + key + "%'"
                                        + " order by  sh.categoryRecommendVideo.createtime desc";
                            else
//                                sql = "from CategoryRecommendVideo zb where zb.videoType.id=" + videoTypeId + " and sh.categoryRecommendVideo.videoInfo.name like '%"+key+"%'"
//                                        + " order by zb.createtime desc";
                                sql = "from CategoryRecommendVideo zb   where zb.videoType.id=" + videoTypeId + " and zb.system.id=" + systemId + " and zb.videoInfo.name like '%" + key + "%'"
                                        + " order by zb.createtime desc";
 
                            List<CategoryRecommendVideo> list = session.createQuery(sql)
                                    .setFirstResult((page - 1) * Constant.pageCount).setMaxResults(Constant.pageCount)
                                    .list();
                            for (CategoryRecommendVideo vb : list) {
                                List<DetailSystem> detailSystemS = session
                                        .createQuery(
                                                "select vb.detailSystem from SuperCategoryRecommendVideo vb where vb.categoryRecommendVideo.id=?")
                                        .setParameter(0, vb.getId()).list();
 
                                List<DetailSystemSelect> dssList = new ArrayList<>();
 
                                for (DetailSystem ds : detailSystemList) {
                                    DetailSystemSelect dss = new DetailSystemSelect();
                                    dss.setDetailSystem(ds);
                                    dss.setSelected(false);
                                    dssList.add(dss);
                                }
 
                                // 设置已经存在的
                                for (DetailSystem ds : detailSystemS) {
                                    for (DetailSystemSelect dss : dssList) {
                                        if (dss.getDetailSystem().getId().equalsIgnoreCase(ds.getId())) {
                                            dss.setSelected(true);
                                            break;
                                        }
                                    }
                                }
                                SuperCategoryRecommendVideo sz = new SuperCategoryRecommendVideo();
                                sz.setDetailSystem(null);
                                sz.setCategoryRecommendVideo(vb);
                                zhiBoClassList.add(new CategoryRecommendVideoAdmin(sz, dssList));
                            }
 
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return zhiBoClassList;
                    }
                });
 
    }
 
    public long getCategoryRecommendVideoAdminCount(String key, int videoTypeId,String systemId, int detailSystem) {
        String sql = "";
        if (detailSystem > 0)
            sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
                    + videoTypeId + " and zb.detailsystemid=" + detailSystem + " group by zb.rcvid) s";
        else
            sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
                    + videoTypeId + " and c.system="+systemId+" group by zb.rcvid) s";
 
        return categoryRecommendVideoDao.getCountSQL(sql);
    }
 
    @SuppressWarnings("unchecked")
    public void deleteCategoryRecommendVideoAdmin(final String crvid, final String detailSystemId) {
        categoryRecommendVideoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
 
                    List<SuperCategoryRecommendVideo> list = session
                            .createQuery(
                                    "from SuperCategoryRecommendVideo vb where vb.categoryRecommendVideo.id=? and vb.detailSystem.id=?")
                            .setParameter(0, crvid).setParameter(1, detailSystemId).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperCategoryRecommendVideo vb : list)
                            session.delete(vb);
                    }
                    session.flush();
                    session.getTransaction().commit();
 
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public List<CategoryRecommendVideo> getSuperCategoryRecommendVideoList(String detailSystemId) {
        return categoryRecommendVideoDao
                .list("select sh.categoryRecommendVideo from SuperCategoryRecommendVideo sh where sh.detailSystem.id="
                        + detailSystemId);
    }
 
    // 更新广告列表
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void updateSuperCategoryRecommendVideoList(final String detailSystemId,
                                                      final List<CategoryRecommendVideo> adList) {
        categoryRecommendVideoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperCategoryRecommendVideo> list = session
                            .createQuery(
                                    "from SuperCategoryRecommendVideo sh where sh.detailSystem.id=" + detailSystemId)
                            .list();
                    session.getTransaction().begin();
                    for (SuperCategoryRecommendVideo ad : list) {
                        session.delete(ad);
                    }
 
                    for (CategoryRecommendVideo categoryRecommendVideo : adList) {
                        SuperCategoryRecommendVideo shd = new SuperCategoryRecommendVideo();
                        shd.setDetailSystem(new DetailSystem(detailSystemId));
                        shd.setCategoryRecommendVideo(categoryRecommendVideo);
                        session.persist(shd);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public void addSuperCategoryRecommendVideo(SuperCategoryRecommendVideo sv) {
        List<SuperCategoryRecommendVideo> list = superCategoryRecommendVideoDao
                .list("from SuperCategoryRecommendVideo sv where sv.categoryRecommendVideo.id="
                        + sv.getCategoryRecommendVideo().getId() + " and sv.detailSystem.id="
                        + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superCategoryRecommendVideoDao.create(sv);
    }
 
 
}