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
package com.yeshi.buwan.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.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.buwan.dao.DetailSystemDao;
import com.yeshi.buwan.dao.SpecialDao;
import com.yeshi.buwan.dao.SpecialVideoDao;
import com.yeshi.buwan.dao.SuperSpecialDao;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.domain.DetailSystem;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.special.Special;
import com.yeshi.buwan.domain.special.SpecialVideo;
import com.yeshi.buwan.domain.special.SuperSpecial;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.domain.web.SpecialAdmin;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.web.tag.PageEntity;
 
@Service
public class SpecialService {
    @Resource
    private SpecialDao specialDao;
    @Resource
    private SpecialVideoDao specialVideoDao;
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private SuperSpecialDao superSpecialDao;
    @Resource
    private DetailSystemDao detailSystemDao;
 
 
    @Cacheable(value = "classCache", key = "'getSpecialList'+'-'+#detailSystemId+'-'+#page")
    public List<Special> getSpecialList(String detailSystemId, int page) {
        return specialDao.list(
                "select ss.special from SuperSpecial ss where ss.detailSystem.id=? and ss.special.show=1 order by ss.special.orderby desc",
                (page - 1) * Constant.pageCount, Constant.pageCount, new String[]{detailSystemId});
    }
 
    public List<Special> getSpecialList(String detailSystemId) {
        return specialDao.list(
                "select ss.special from SuperSpecial ss where ss.detailSystem.id=? order by ss.special.orderby desc",
                new String[]{detailSystemId});
    }
 
    public List<Special> getSpecialList() {
        return specialDao.list("from Special");
    }
 
    @Cacheable(value = "classCache", key = "'getSpecialCount'+'-'+#detailSystemId")
    public long getSpecialCount(String detailSystemId) {
        return specialDao.getCount("select count(*)  from SuperSpecial ss where ss.detailSystem.id=?",
                new String[]{detailSystemId});
    }
 
    @Cacheable(value = "classCache", key = "'getSpecial'+'-'+#id")
    public Special getSpecial(String id) {
        return specialDao.find(Special.class, id);
    }
 
    public void addSpecial(Special special) {
        specialDao.save(special);
    }
 
    @SuppressWarnings("rawtypes")
    public void deleteSpecial(final Special special) {
        if (special != null) {
            specialDao.excute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    try {
                        @SuppressWarnings("unchecked")
                        List<SuperSpecial> list = session.createQuery("from SuperSpecial sh where sh.special.id=?")
                                .setParameter(0, special.getId()).list();
                        session.getTransaction().begin();
                        for (SuperSpecial sh : list)
                            session.delete(sh);
                        session.delete(special);
                        session.flush();
                        session.getTransaction().commit();
                    } catch (Exception e) {
                        e.printStackTrace();
                        session.getTransaction().rollback();
                    }
                    return null;
                }
            });
 
        }
    }
 
    public void updateSpecial(Special special) {
        specialDao.update(special);
    }
 
    public void addSpecialVideo(SpecialVideo sv) {
        specialVideoDao.create(sv);
    }
 
    @Cacheable(value = "classCache", key = "'getSpecialVideoList'+'-'+#specialId")
    public List<VideoInfo> getSpecialVideoList(String specialId) {
        return videoInfoDao.list(
                "select sv.video from SpecialVideo sv where sv.special.id=? and sv.video.show=1 order by sv.orderby desc",
                new String[]{specialId});
    }
 
    public void deleteSpecialVideo(String specialVideoId) {
        specialVideoDao.delete(new SpecialVideo(specialVideoId));
    }
 
    public long getSpecialVideoListCount(String specialId) {
 
        return videoInfoDao.getCount("select sv.video from SpecialVideo sv where sv.special.id=?",
                new String[]{specialId});
    }
 
    public List<SpecialVideo> getSpecialVideoList(String specialId, String key) {
        return specialVideoDao.list("from SpecialVideo sv where sv.special.id=? and sv.video.name like ?",
                new String[]{specialId, "%" + key + "%"});
    }
 
    public List<SpecialVideo> getSpecialVideoList(String specialId, PageEntity pg, String key) {
        return specialVideoDao.list("from SpecialVideo sv where sv.special.id=? and sv.video.name like ?", ((pg.getPageIndex() - 1) * pg.getPageSize()), pg.getPageSize(),
                new String[]{specialId, "%" + key + "%"});
    }
 
    public long getSpecialVideoListCount(String specialId, String key) {
        return specialVideoDao.getCount(
                "select count(*)  from SpecialVideo sv where sv.special.id=? and sv.video.name like ?",
                new String[]{specialId, "%" + key + "%"});
    }
 
    @SuppressWarnings("unchecked")
    public List<SpecialAdmin> getSpecialAdmin(String key, String systemId, int detailSystem, int page) {
        key = StringUtil.isNullOrEmpty(key) ? "" : key;
        List<SpecialAdmin> zhiBoClassList = new ArrayList<>();
 
        try {
            List<DetailSystem> detailSystemList = detailSystemDao.list("from DetailSystem ds where ds.system.id=" + systemId);
            String sql = "";
            if (detailSystem > 0)
                sql = "select sh.special from SuperSpecial sh where sh.special.name like ? and  sh.detailSystem.id="
                        + detailSystem + " order by  sh.special.orderby desc";
            else
                sql = "from Special zb where zb.name like ? and zb.system.id=" + systemId + " order by zb.orderby desc";
 
            List<Special> list = specialDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount,
                    new Serializable[]{"%" + key + "%"});
            for (Special vb : list) {
                List<DetailSystem> detailSystemS = detailSystemDao
                        .list("select vb.detailSystem from SuperSpecial vb where vb.special.id=" + vb.getId());
 
                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;
                        }
                    }
                }
                SuperSpecial sz = new SuperSpecial();
                sz.setDetailSystem(null);
                sz.setSpecial(vb);
                zhiBoClassList.add(new SpecialAdmin(sz, dssList));
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zhiBoClassList;
    }
 
    public long getSpecialAdminCount(String key, String systemId, int detailSystem) {
        key = StringUtil.isNullOrEmpty(key) ? "" : key;
 
        String sql = "";
        if (detailSystem > 0)
            sql = "select count(*) from  (select count(*) from wk_video_super_special zb left join wk_video_special c on c.id=zb.specialid where zb.detailsystemid="
                    + detailSystem + " and c.name like '%" + key + "%' group by zb.specialid) s";
        else
            sql = "select count(*) from  (select count(*) from wk_video_super_special zb left join wk_video_special c on c.id=zb.specialid where c.system=" + systemId + " and c.name like '%"
                    + key + "%'  group by zb.specialid) s";
 
        return specialVideoDao.getCountSQL(sql);
    }
 
    @SuppressWarnings("unchecked")
    public void deleteSpecialAdmin(final String classId, final String detailSystemId) {
        specialDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
 
                    List<SuperSpecial> list = session
                            .createQuery("from SuperSpecial vb where vb.special.id=? and vb.detailSystem.id=?")
                            .setParameter(0, classId).setParameter(1, detailSystemId).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperSpecial vb : list)
                            session.delete(vb);
                    }
                    session.flush();
                    session.getTransaction().commit();
 
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    /**
     * 专题
     */
 
    public List<SuperSpecial> getSuperSpecialList(String detailSystem) {
        return superSpecialDao.list(
                "FROM SuperSpecial hs where hs.detailSystem.id=" + detailSystem + " order by hs.Special.orderby desc");
    }
 
    public void addSuperSpecial(SuperSpecial sv) {
        List<SuperSpecial> list = superSpecialDao.list("from SuperSpecial sv where sv.special.id="
                + sv.getSpecial().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superSpecialDao.create(sv);
    }
 
    public void updateSuperSpecial(SuperSpecial hotSearch) {
        superSpecialDao.update(hotSearch);
    }
 
    public void deleteSuperSpecial(SuperSpecial hotSearch) {
        superSpecialDao.delete(hotSearch);
    }
 
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void updateSuperSpecialList(final String detailSystemId, final List<SuperSpecial> typeList) {
        specialDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperSpecial> list = session
                            .createQuery("from SuperSpecial sh where sh.detailSystem.id=" + detailSystemId).list();
                    session.getTransaction().begin();
                    for (SuperSpecial ad : list) {
                        session.delete(ad);
                    }
 
                    for (SuperSpecial videoType : typeList) {
                        session.persist(videoType);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    @Cacheable(value = "foundCache", key = "'getSpecialOnMain'+'-'+#detailSystem")
    public List<Special> getSpecialOnMain(String detailSystem) {
        return specialDao.list("SELECT hs.special FROM SuperSpecial hs where hs.detailSystem.id=" + detailSystem
                + " and  hs.special.show=1 and  hs.special.showonmain=1 order by hs.special.orderby desc");
    }
 
}