admin
2021-01-25 d182390205a9828bd1091b06fa712e028004c687
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
package com.newvideo.service.imp;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.newvideo.dao.HomeNoticeDao;
import com.newvideo.dao.SuperHomeNoticeDao;
import com.newvideo.dao.VideoInfoDao;
import com.newvideo.domain.DetailSystem;
import com.newvideo.domain.HomeNotice;
import com.newvideo.domain.HomeType;
import com.newvideo.domain.SuperHomeNotice;
import com.newvideo.domain.SystemInfo;
import com.newvideo.domain.web.DetailSystemSelect;
import com.newvideo.domain.web.HomeNoticeAdmin;
import com.newvideo.util.Constant;
 
@Service
public class HomeNoticeService {
    @Resource
    private HomeNoticeDao homeNoticeDao;
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private SuperHomeNoticeDao superHomeNoticeDao;
 
    // 首页公告
    public List<HomeNotice> getHomeNoticeList(SystemInfo system) {
        List<HomeNotice> list = homeNoticeDao
                .list("from HomeNotice h where h.show='1' and h.system.id=" + system.getId());
        return list;
    }
 
    // 首页公告
    public List<HomeNotice> getHomeNoticeList() {
        List<HomeNotice> list = homeNoticeDao.list("from HomeNotice");
        return list;
    }
 
    public List<HomeNotice> getVisiableHomeNoticeList() {
        List<HomeNotice> list = homeNoticeDao.list("from HomeNotice  h where h.show=1");
        return list;
    }
 
    public void addHomeNotice(HomeNotice notice) {
        homeNoticeDao.create(notice);
    }
 
    public void updateHomeNotice(HomeNotice notice) {
        homeNoticeDao.update(notice);
    }
 
    public HomeNotice getHomeNotice(Serializable s) {
        return homeNoticeDao.find(HomeNotice.class, s);
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHomeNotice(final HomeNotice notice) {
        homeNoticeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeNotice> list = session.createQuery("from SuperHomeNotice sv where sv.homeNotice.id=?")
                            .setParameter(0, notice.getId()).list();
                    session.getTransaction().begin();
                    for (SuperHomeNotice sv : list)
                        session.delete(sv);
                    session.delete(new HomeNotice(notice.getId()));
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public List<HomeType> getIOSTestHomeType() {
        List<HomeType> list = new ArrayList<HomeType>();
        HomeType type = new HomeType();
        type.setId("130");
        type.setName("萌宠奇趣");
        type.setVideoList(
                videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=130 order by v.createtime desc",
                        0, 3, new String[] {}));
        list.add(type);
 
        type = new HomeType();
        type.setId("129");
        type.setName("相声小品");
        type.setVideoList(
                videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=129 order by v.createtime desc",
                        0, 3, new String[] {}));
        list.add(type);
 
        type = new HomeType();
        type.setId("19");
        type.setName("纪录片");
        type.setVideoList(
                videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=19 order by v.createtime desc", 0,
                        3, new String[] {}));
        list.add(type);
 
        type = new HomeType();
        type.setId("14");
        type.setName("游戏");
        type.setVideoList(videoInfoDao.list(
                "from VideoInfo v where v.show=1 and v.videoType.id=91 OR v.videoType.id=92 OR v.videoType.id=93 OR v.videoType.id=94 order by v.createtime desc",
                0, 3, new String[] {}));
        list.add(type);
 
        type = new HomeType();
        type.setId("13");
        type.setName("体育");
        type.setVideoList(
                videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=13 order by v.createtime desc", 0,
                        3, new String[] {}));
        list.add(type);
 
        return list;
    }
 
    // 后台操作
    @SuppressWarnings("unchecked")
    public List<HomeNoticeAdmin> getHomeNoticeAdmin(final String key, final int detailSystem, final int page) {
 
        return (List<HomeNoticeAdmin>) homeNoticeDao.excute(new HibernateCallback<List<HomeNoticeAdmin>>() {
            public List<HomeNoticeAdmin> doInHibernate(Session session) throws HibernateException {
                List<HomeNoticeAdmin> zhiBoClassList = new ArrayList<HomeNoticeAdmin>();
                try {
                    List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem").list();
                    String sql = "";
                    if (detailSystem > 0)
                        sql = "select sh.homeNotice from SuperHomeNotice sh where  sh.detailSystem.id=" + detailSystem
                                + " order by  sh.homeNotice.createtime desc";
                    else
                        sql = "from HomeNotice zb order by zb.createtime desc";
 
                    List<HomeNotice> list = session.createQuery(sql).setFirstResult((page - 1) * Constant.pageCount)
                            .setMaxResults(Constant.pageCount).list();
                    for (HomeNotice vb : list) {
                        List<DetailSystem> detailSystemS = session
                                .createQuery("select vb.detailSystem from SuperHomeNotice vb where vb.homeNotice.id=?")
                                .setParameter(0, vb.getId()).list();
 
                        List<DetailSystemSelect> dssList = new ArrayList<DetailSystemSelect>();
 
                        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;
                                }
                            }
                        }
                        SuperHomeNotice sz = new SuperHomeNotice();
                        sz.setDetailSystem(null);
                        sz.setHomeNotice(vb);
                        zhiBoClassList.add(new HomeNoticeAdmin(sz, dssList));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return zhiBoClassList;
            }
        });
 
    }
 
    public long getHomeNoticeAdminCount(String key, int detailSystem) {
        String sql = "";
        if (detailSystem > 0)
            sql = "select count(*) from  (select count(*) from wk_video_super_homenotice zb left join wk_video_homenotice c on c.id=zb.homenoticeid where zb.detailsystem="
                    + detailSystem + " group by zb.homenoticeid) s";
        else
            sql = "select count(*) from  (select count(*) from wk_video_super_homenotice zb left join wk_video_homenotice c on c.id=zb.homenoticeid  group by zb.homenoticeid) s";
 
        return homeNoticeDao.getCountSQL(sql);
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHomeNoticeAdmin(final String classId, final String detailSystemId) {
        homeNoticeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeNotice> list = session
                            .createQuery("from SuperHomeNotice vb where vb.homeNotice.id=? and vb.detailSystem.id=?")
                            .setParameter(0, classId).setParameter(1, detailSystemId).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperHomeNotice vb : list)
                            session.delete(vb);
                    }
                    session.flush();
                    session.getTransaction().commit();
 
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    /**
     * 小黄条
     */
 
    public List<HomeNotice> getHomeNoticeList(String detailSystem) {
 
        return homeNoticeDao
                .list("select hs.homeNotice FROM SuperHomeNotice hs where hs.detailSystem.id=" + detailSystem);
    }
 
    public void addSuperHomeNotice(SuperHomeNotice sv) {
        List<SuperHomeNotice> list = superHomeNoticeDao.list("from SuperHomeNotice sv where sv.homeNotice.id="
                + sv.getHomeNotice().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superHomeNoticeDao.create(sv);
    }
 
    public void updateSuperHomeNotice(SuperHomeNotice hotSearch) {
        superHomeNoticeDao.update(hotSearch);
    }
    
    @Transactional
    public void deleteSuperHomeNotice(final SuperHomeNotice hotSearch) {
        superHomeNoticeDao.excute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session)
                    throws HibernateException {
                Query query = session.createQuery("delete from SuperHomeNotice shn where shn.homeNotice.id = ? and shn.detailSystem.id = ?");
                query.setParameter(0, hotSearch.getHomeNotice().getId());
                query.setParameter(1, hotSearch.getDetailSystem().getId());
                query.executeUpdate();
                return null;
            }
        });
    }
 
    @SuppressWarnings("unchecked")
    public void updateSuperHomeNoticeList(final String detailSystemId, final List<SuperHomeNotice> typeList) {
        homeNoticeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeNotice> list = session
                            .createQuery("from SuperHomeNotice sh where sh.detailSystem.id=" + detailSystemId).list();
                    session.getTransaction().begin();
                    for (SuperHomeNotice ad : list) {
                        session.delete(ad);
                    }
 
                    for (SuperHomeNotice videoType : typeList) {
                        session.persist(videoType);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
}