Administrator
2018-10-30 c0c91fbda1ba601c4c8cb6d12fd43dfbe02eea5d
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
package com.yeshi.fanli.service.impl.goods;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.transaction.Transactional;
 
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.goods.RecommendSpecialDao;
import com.yeshi.fanli.entity.admin.RecommendSpecialAdmin;
import com.yeshi.fanli.entity.bus.recommend.RecommendSpecial;
import com.yeshi.fanli.entity.bus.su.recommend.SuperRecommendSpecial;
import com.yeshi.fanli.entity.system.System;
import com.yeshi.fanli.service.inter.config.SystemService;
import com.yeshi.fanli.service.inter.goods.RecommendSpecialService;
import com.yeshi.fanli.service.inter.goods.SuperRecommendSpecialService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.Utils;
 
@Service
public class RecommendSpecialServiceImpl implements RecommendSpecialService {
 
    @Resource
    private RecommendSpecialDao recommendSpecialDao;
 
    @Resource
    private SuperRecommendSpecialService superRecommendSpecialService;
    
    @Resource
    private SystemService systemService;
 
    public void addRecommendSpecial(RecommendSpecial special) {
        special.setCreatetime(java.lang.System.currentTimeMillis());
        recommendSpecialDao.create(special);
    }
 
    public long getCount() {
        long count = recommendSpecialDao
                .getCount("select count(*) from RecommendSpecial");
        return count;
    }
 
    @SuppressWarnings("unchecked")
    public List<RecommendSpecialAdmin> getRecommendSpecials(int index,
            String platform, String packages, String key) {
        final int strat = index * Constant.PAGE_SIZE;
        platform = Utils.getMap().get(platform);
        com.yeshi.fanli.entity.system.System system = systemService.getSystem(platform, packages);
        List<RecommendSpecial> recommendSpecialList=null;
        List<SuperRecommendSpecial> superRecommendSpecialList=null;
        Map<Long,RecommendSpecialAdmin> recommendSpecialAdminMap=new HashMap<Long, RecommendSpecialAdmin>();
        List<RecommendSpecialAdmin> recommendSpecialAdmin=new ArrayList<RecommendSpecialAdmin>();
        if(system == null){
            if("".equals(key.trim())){
                recommendSpecialList = (List<RecommendSpecial>) recommendSpecialDao.excute(new HibernateCallback<List<RecommendSpecial>>(){
                        public List<RecommendSpecial> doInHibernate(Session session)
                                throws HibernateException {
                            Query query = session.createQuery(" from RecommendSpecial rs");
                            query.setFirstResult(strat);
                            query.setMaxResults(Constant.PAGE_SIZE);
                            return query.list();
                        }
                });
                List<Long> rsIdList = new ArrayList<Long>();
                for (RecommendSpecial rs : recommendSpecialList) {
                    rsIdList.add(rs.getId());
                }
                superRecommendSpecialList = superRecommendSpecialService.getSuperRecommendSpecials(rsIdList);
            }else{
                final String likekey = "%"+key+"%";
                recommendSpecialList= (List<RecommendSpecial>) recommendSpecialDao.excute(new HibernateCallback<List<RecommendSpecial>>(){
                            public List<RecommendSpecial> doInHibernate(Session session)
                                    throws HibernateException {
                                Query query = session.createQuery(" from RecommendSpecial rs where rs.name like ? ");
                                query.setFirstResult(strat);
                                query.setMaxResults(Constant.PAGE_SIZE);
                                query.setParameter(0, likekey);
                                return query.list();
                            }
                });
                List<Long> rsIdList = new ArrayList<Long>();
                for (RecommendSpecial rs : recommendSpecialList) {
                    rsIdList.add(rs.getId());
                }
                superRecommendSpecialList = superRecommendSpecialService.getSuperRecommendSpecials(rsIdList);
            }
            Map<Long,List<System>> superMap = new HashMap<Long, List<System>>();
            Long rsId=null;
            List<System> systemList=null;
            for (SuperRecommendSpecial srs : superRecommendSpecialList) {
                rsId = srs.getRecommendSpecial().getId();
                systemList = superMap.get(rsId);
                if(systemList==null){
                    systemList=new ArrayList<System>();
                }
                systemList.add(srs.getSystem());
                superMap.put(rsId,systemList);
            }
            RecommendSpecialAdmin rsa=null;
            for (RecommendSpecial recommendSpecial : recommendSpecialList) {
                long id = recommendSpecial.getId();
                rsa = recommendSpecialAdminMap.get(id);
                if(rsa==null){
                    rsa=new RecommendSpecialAdmin();
                }
                List<System> list = superMap.get(id);
                if(list != null){
                    rsa.getSystemList().addAll(superMap.get(id));
                }
                rsa.setRecommendSpecial(recommendSpecial);
                recommendSpecialAdminMap.put(id, rsa);
            }    
        }else if(system != null){
            if("".equals(key.trim())){
                superRecommendSpecialList = superRecommendSpecialService.getSuperRecommendSpecialBySystemId(system.getId(),strat, Constant.PAGE_SIZE);
                List<Long> rsIdList =new ArrayList<Long>();
                for (SuperRecommendSpecial superRecommendSpecial : superRecommendSpecialList) {
                    RecommendSpecial recommendSpecial = superRecommendSpecial.getRecommendSpecial();
                    rsIdList.add(recommendSpecial.getId());
                }
                superRecommendSpecialList = superRecommendSpecialService.getSuperRecommendSpecials(rsIdList);
            }else{
                String likekey = "%"+key+"%";
                superRecommendSpecialList = superRecommendSpecialService.getSuperRecommendSpecialBySystemId(system.getId(),strat, Constant.PAGE_SIZE,likekey);
                List<Long> rsIdList =new ArrayList<Long>();
                for (SuperRecommendSpecial superRecommendSpecial : superRecommendSpecialList) {
                    RecommendSpecial recommendSpecial = superRecommendSpecial.getRecommendSpecial();
                    rsIdList.add(recommendSpecial.getId());
                }
                superRecommendSpecialList = superRecommendSpecialService.getSuperRecommendSpecials(rsIdList);
            }
            RecommendSpecialAdmin rsa=null;
            for (SuperRecommendSpecial superRecommendSpecial : superRecommendSpecialList) {
                RecommendSpecial recommendSpecial = superRecommendSpecial.getRecommendSpecial();
                rsa = recommendSpecialAdminMap.get(recommendSpecial.getId());
                if(rsa==null){
                    rsa=new RecommendSpecialAdmin(recommendSpecial);
                }
                rsa.getSystemList().add(superRecommendSpecial.getSystem());
                recommendSpecialAdminMap.put(recommendSpecial.getId(), rsa);
            }
        }
        Map map = Utils.orderBy(recommendSpecialAdminMap);
        Collection<RecommendSpecialAdmin> values = map.values();
        recommendSpecialAdmin.addAll(values);
        return recommendSpecialAdmin;
        
    }
 
    public RecommendSpecial getRecommendSpecial(long id) {
        RecommendSpecial special = recommendSpecialDao.find(
                RecommendSpecial.class, id);
        return special;
    }
 
    @Transactional
    public void deleteSpecials(final long[] rbids) {
 
        superRecommendSpecialService.deleteSuperRecommendSpecials(rbids);
        recommendSpecialDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
 
//                StringBuffer sb = new StringBuffer("delete s,j from yeshi_ec_recommend_special s left join yeshi_ec_action_jumpdetail j on s.jumpid=j.id ");
                StringBuffer sb = new StringBuffer("delete s from yeshi_ec_recommend_special s ");
                for (int i = 0; i < rbids.length; i++) {
                    if (i == 0) {
                        sb.append(" where s.id=? ");
                    } else {
                        sb.append(" or s.id=?");
                    }
                }
                Query query = session.createSQLQuery(sb.toString());
                for (int i = 0; i < rbids.length; i++) {
                    query.setLong(i, rbids[i]);
                }
                query.executeUpdate();
                return null;
            }
        });
 
    }
 
    public void updateSpecial(RecommendSpecial special) {
        recommendSpecialDao.update(special);
    }
 
    public int getCount(String platform, String packages, final String key) {
 
        platform = Utils.getMap().get(platform);
        final System system = systemService.getSystem(platform, packages);
        return (Integer) recommendSpecialDao.excute(new HibernateCallback<Integer>() {
            
            public Integer doInHibernate(Session session)
                    throws HibernateException {
                String k = "%"+key+"%";
                Query query;
                if(system==null){
//                    query = session.createSQLQuery("select count(a.name) from (select name from yeshi_ec_recommend_special where `name` like ?) a");
                    query = session.createQuery("select count(rs.id) from RecommendSpecial rs where rs.name like ?");
                    query.setParameter(0, k);
                }else{
//                    query =session.createSQLQuery("select count(a.name) from (select hs.name from yeshi_ec_super_recommendspecial shs left  join yeshi_ec_recommend_special hs on shs.recommendspecial_id=hs.id where shs.system_id=? and hs.name like ?) a");
                    query = session.createQuery("select count(srs.id) from SuperRecommendSpecial srs where srs.system.id=? and srs.recommendSpecial.name like ?");
                    query.setParameter(0, system.getId());
                    query.setParameter(1, k);
                }
                    Long result = (Long) query.uniqueResult();
                    int intValue = result.intValue();
                    return intValue;
            }
        });
    
    }
 
    public void updateSpecialJumpDetail(final long id) {
        recommendSpecialDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
                Query query = session.createQuery("update RecommendSpecial rs set rs.jumpDetail.id = NULL where  rs.jumpDetail.id = ?");
                query.setParameter(0, id);
                return query.executeUpdate();
            }
        });
    }
 
}