admin
2021-02-06 cad915058c3c53bf328a8ae9ca9bc7de099caba7
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
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.HomeAdDao;
import com.yeshi.buwan.dao.SuperHomeAdDao;
import com.yeshi.buwan.domain.DetailSystem;
import com.yeshi.buwan.domain.HomeAd;
import com.yeshi.buwan.domain.SuperHomeAd;
import com.yeshi.buwan.domain.SystemInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.domain.web.HomeAdAdmin;
import com.yeshi.buwan.util.Constant;
 
@Service
public class HomeAdService {
    @Resource
    private HomeAdDao homeAdDao;
    @Resource
    private SuperHomeAdDao superHomeAdDao;
 
    public SuperHomeAdDao getSuperHomeAdDao() {
        return superHomeAdDao;
    }
 
    public void setSuperHomeAdDao(SuperHomeAdDao superHomeAdDao) {
        this.superHomeAdDao = superHomeAdDao;
    }
 
    public HomeAdDao getHomeAdDao() {
        return homeAdDao;
    }
 
    public void setHomeAdDao(HomeAdDao homeAdDao) {
        this.homeAdDao = homeAdDao;
    }
 
    public List<HomeAd> getHomeAdList(SystemInfo system) {
        List<HomeAd> list = homeAdDao
                .list("from HomeAd h where h.system.id=" + system.getId() + " order by  h.orderby desc");
        return list;
    }
 
    @Cacheable(value = "homeCache", key = "'getHomeAdList'+'-'+#detailSystem.id")
    public List<HomeAd> getHomeAdList(DetailSystem detailSystem) {
        List<HomeAd> list = homeAdDao.list("select h.homeAd from SuperHomeAd h where h.detailSystem.id="
                + detailSystem.getId() + " order by h.homeAd.orderby desc");
        return list;
    }
 
    public static List<HomeAd> getIOSTestHomeAdList() {
        List<HomeAd> list = new ArrayList<>();
        HomeAd ad = new HomeAd();
        ad.setPicture("http://img.zcool.cn/community/01a5a7562499216ac7254878995802.jpg");
        VideoInfo info = new VideoInfo();
        info.setId("161850");
        info.setName("【摔杯为号】赵家班父子摔杯演闹剧");
        info.setPicture("http://img2.funshion.com/pictures/poseidon01/418/985/4189850_v_still.jpg");
        info.setThirdType("0");
        info.setShare("0");
        ad.setVideo(info);
        list.add(ad);
 
        ad = new HomeAd();
        ad.setPicture("http://img.zcool.cn/community/01ac60563acca86ac7259e0fe92cae.jpg");
        info = new VideoInfo();
        info.setId("161334");
        info.setName("【冯巩】冯巩,牛莉《夫妻日记》");
        info.setPicture("http://img.zcool.cn/community/01ac60563acca86ac7259e0fe92cae.jpg");
        info.setThirdType("0");
        info.setShare("0");
        ad.setVideo(info);
        list.add(ad);
 
        ad = new HomeAd();
        ad.setPicture("http://img.zcool.cn/community/01ae2b563accb032f87512f63f63ad.jpg");
        info = new VideoInfo();
        info.setId("163707");
        info.setName("【郭德纲于谦】《谦哥带儿子见世面》");
        info.setPicture("http://img.zcool.cn/community/01cdfb563acca06ac7259e0fb69676.jpg");
        info.setThirdType("0");
        info.setShare("0");
        ad.setVideo(info);
        list.add(ad);
 
        ad = new HomeAd();
        ad.setPicture("http://img.zcool.cn/community/01ae2b563accb032f87512f63f63ad.jpg");
        info = new VideoInfo();
        info.setId("161339");
        info.setName("【欢乐喜剧人】开心麻花《感染者》");
        info.setPicture("http://img2.funshion.com/pictures/poseidon01/418/985/4189850_v_still.jpg");
        info.setThirdType("0");
        info.setShare("0");
        ad.setVideo(info);
        list.add(ad);
 
        return list;
    }
 
    public List<HomeAd> getHomeAdList() {
 
        List<HomeAd> list = homeAdDao.list("from HomeAd ha order by ha.orderby desc");
        return list;
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHomeAd(final String id) {
        homeAdDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeAd> list = session.createQuery("from SuperHomeAd sh where sh.homeAd.id=?")
                            .setParameter(0, id).list();
                    session.getTransaction().begin();
                    for (SuperHomeAd shd : list)
                        session.delete(shd);
                    session.createQuery("delete from HomeAd ha where ha.id=?").setParameter(0, id).executeUpdate();
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public Serializable addHomeAd(HomeAd ad) {
        return homeAdDao.save(ad);
    }
 
    public HomeAd getHomeAdById(String id) {
 
        return homeAdDao.find(HomeAd.class, id);
    }
 
    public void updateHomeAd(HomeAd ad) {
        homeAdDao.update(ad);
    }
 
    // 后台操作
    @SuppressWarnings("unchecked")
    public List<HomeAdAdmin> getHomeAdAdmin(final String key, final int detailSystem, final int page) {
        return (List<HomeAdAdmin>) homeAdDao.excute(new HibernateCallback<List<HomeAdAdmin>>() {
            public List<HomeAdAdmin> doInHibernate(Session session) throws HibernateException {
                List<HomeAdAdmin> zhiBoClassList = new ArrayList<>();
                try {
                    List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem").list();
                    String sql = "";
                    if (detailSystem > 0)
                        sql = "select sh.homeAd from SuperHomeAd sh where  sh.detailSystem.id=" + detailSystem
                                + " order by  sh.homeAd.createtime desc";
                    else
                        sql = "from HomeAd zb order by zb.createtime desc";
 
                    List<HomeAd> list = session.createQuery(sql).setFirstResult((page - 1) * Constant.pageCount)
                            .setMaxResults(Constant.pageCount).list();
                    for (HomeAd vb : list) {
                        List<DetailSystem> detailSystemS = session
                                .createQuery("select vb.detailSystem from SuperHomeAd vb where vb.homeAd.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;
                                }
                            }
                        }
                        SuperHomeAd sz = new SuperHomeAd();
                        sz.setDetailSystem(null);
                        sz.setHomeAd(vb);
                        zhiBoClassList.add(new HomeAdAdmin(sz, dssList));
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return zhiBoClassList;
            }
        });
    }
 
    public long getHomeAdAdminCount(String key, int detailSystem) {
        String sql = "";
        if (detailSystem > 0)
            sql = "select count(*) from  (select count(*) from wk_video_super_homead zb left join wk_video_homead c on c.id=zb.homeadid where zb.detailsystemid="
                    + detailSystem + " group by zb.homeadid) s";
        else
            sql = "select count(*) from  wk_video_homead";
        return homeAdDao.getCountSQL(sql);
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHomeAdAdmin(final String classId, final String detailSystemId) {
        homeAdDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeAd> list = session
                            .createQuery("from SuperHomeAd vb where vb.homeAd.id=? and vb.detailSystem.id=?")
                            .setParameter(0, classId).setParameter(1, detailSystemId).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperHomeAd vb : list)
                            session.delete(vb);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public List<HomeAd> getSuperHomeAdList(String detailSystemId) {
        return homeAdDao.list("select sh.homeAd from SuperHomeAd sh where sh.detailSystem.id=" + detailSystemId);
    }
 
    // 更新广告列表
    @SuppressWarnings("unchecked")
    public void updateSuperHomeAdList(final String detailSystemId, final List<HomeAd> adList) {
        homeAdDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeAd> list = session
                            .createQuery("from SuperHomeAd sh where sh.detailSystem.id=" + detailSystemId).list();
                    session.getTransaction().begin();
                    for (SuperHomeAd ad : list) {
                        session.delete(ad);
                    }
 
                    for (HomeAd homeAd : adList) {
                        SuperHomeAd shd = new SuperHomeAd();
                        shd.setCreatetime(System.currentTimeMillis() + "");
                        shd.setDetailSystem(new DetailSystem(detailSystemId));
                        shd.setHomeAd(homeAd);
                        session.persist(shd);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public void addSuperHomeAd(SuperHomeAd sv) {
        List<SuperHomeAd> list = superHomeAdDao.list("from SuperHomeAd sv where sv.homeAd.id=" + sv.getHomeAd().getId()
                + " and sv.detailSystem.id=" + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superHomeAdDao.create(sv);
    }
 
}