yujian
2019-03-14 4aadf484e193995c23ee1d5bb1971a497d2f9a0d
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
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 org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.goods.RecommendSectionDao;
import com.yeshi.fanli.dao.mybatis.share.ShareMapper;
import com.yeshi.fanli.entity.admin.RecommendSectionAdmin;
import com.yeshi.fanli.entity.bus.recommend.RecommendSection;
import com.yeshi.fanli.entity.bus.recommend.RecommendSectionDetail;
import com.yeshi.fanli.entity.bus.su.recommend.SuperRecommendSection;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.goods.RecommendSectionDetailService;
import com.yeshi.fanli.service.inter.goods.RecommendSectionGoodsService;
import com.yeshi.fanli.service.inter.goods.RecommendSectionService;
import com.yeshi.fanli.service.inter.goods.SuperRecommendSectionService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.Utils;
 
@Service
public class RecommendSectionServiceImpl implements RecommendSectionService {
 
    @Resource
    private ShareMapper shareMapper;
    
    @Resource
    private RecommendSectionDao recommendSectionDao;
 
    @Resource
    private SuperRecommendSectionService superRecommendSectionService;
    
    @Resource
    private RecommendSectionGoodsService recommendSectionGoodsService;
 
    @Resource
    private BusinessSystemService businessSystemService;
    
    @Resource
    private RecommendSectionDetailService recommendSectionDetailService;
    
    @Transactional(rollbackFor=Exception.class)
    public boolean addRecommendSection(RecommendSection section,RecommendSectionDetail detail) {
        section.setCreatetime(java.lang.System.currentTimeMillis());
        long rsid = (Long) recommendSectionDao.save(section);
        if(rsid <= 0){
            return false;
        }
        if(detail==null){
            detail=new RecommendSectionDetail();
        }
        detail.setRecommendSection(section);
        recommendSectionDetailService.saveSectionDetail(detail);
        return true;
    }
 
    public long getCount() {
        return recommendSectionDao
                .getCount("select count(*) from RecommendSection");
    }
 
    @SuppressWarnings({ "unchecked" })
    public List<RecommendSectionAdmin> getRecommendSections(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.BusinessSystem system = businessSystemService.getBusinessSystem(platform, packages);
        List<RecommendSection> recommendSectionList=null;
        List<SuperRecommendSection> superRecommendSectionList=null;
        Map<Long,RecommendSectionAdmin> recommendSectionAdminMap=new HashMap<Long, RecommendSectionAdmin>();
        List<RecommendSectionAdmin> recommendSectionAdmin=new ArrayList<RecommendSectionAdmin>();
        if(system == null){
            if("".equals(key.trim())){
                recommendSectionList = (List<RecommendSection>) recommendSectionDao.excute(new HibernateCallback<List<RecommendSection>>(){
                        public List<RecommendSection> doInHibernate(Session session)
                                throws HibernateException {
                            Query query = session.createQuery(" from RecommendSection rs order by rs.orderby");
                            query.setFirstResult(strat);
                            query.setMaxResults(Constant.PAGE_SIZE);
                            return query.list();
                        }
                });
            }else{
                final String likekey = "%"+key+"%";
                recommendSectionList= (List<RecommendSection>) recommendSectionDao.excute(new HibernateCallback<List<RecommendSection>>(){
                            public List<RecommendSection> doInHibernate(Session session)
                                    throws HibernateException {
                                Query query = session.createQuery(" from RecommendSection rs where rs.name like ? order by rs.orderby");
                                query.setFirstResult(strat);
                                query.setMaxResults(Constant.PAGE_SIZE);
                                query.setParameter(0, likekey);
                                return query.list();
                            }
                });
            }
            List<Long> rsIdList = new ArrayList<Long>();
            for (RecommendSection rs : recommendSectionList) {
                rsIdList.add(rs.getId());
            }
            superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionsBySections(rsIdList);
            Map<Long,List<BusinessSystem>> superMap = new HashMap<Long, List<BusinessSystem>>();
            Long rsId=null;
            List<BusinessSystem> systemList=null;
            for (SuperRecommendSection srs : superRecommendSectionList) {
                rsId = srs.getRecommendSection().getId();
                systemList = superMap.get(rsId);
                if(systemList==null){
                    systemList=new ArrayList<BusinessSystem>();
                }
                systemList.add(srs.getSystem());
                superMap.put(rsId,systemList);
            }
            RecommendSectionAdmin rsa=null;
            for (RecommendSection recommendSection : recommendSectionList) {
                long id = recommendSection.getId();
                rsa = recommendSectionAdminMap.get(id);
                if(rsa==null){
                    rsa=new RecommendSectionAdmin();
                }
                List<BusinessSystem> list = superMap.get(id);
                if(list != null){
                    rsa.getSystemList().addAll(superMap.get(id));
                }
                rsa.setRecommendSection(recommendSection);
                recommendSectionAdminMap.put(id, rsa);
            }    
        }else if(system != null){
            if("".equals(key.trim())){
                superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionBySystemId(system.getId(),strat, Constant.PAGE_SIZE);
                List<Long> rsIdList =new ArrayList<Long>();
                for (SuperRecommendSection superRecommendSection : superRecommendSectionList) {
                    RecommendSection recommendSection = superRecommendSection.getRecommendSection();
                    rsIdList.add(recommendSection.getId());
                }
                superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionsBySections(rsIdList);
            }else{
                String likekey = "%"+key+"%";
                superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionBySystemId(system.getId(),strat, Constant.PAGE_SIZE,likekey);
                List<Long> rsIdList =new ArrayList<Long>();
                for (SuperRecommendSection superRecommendSection : superRecommendSectionList) {
                    RecommendSection recommendSection = superRecommendSection.getRecommendSection();
                    rsIdList.add(recommendSection.getId());
                }
                superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionsBySections(rsIdList);
            }
            RecommendSectionAdmin rsa=null;
            for (SuperRecommendSection superRecommendSection : superRecommendSectionList) {
                RecommendSection recommendSection = superRecommendSection.getRecommendSection();
                rsa = recommendSectionAdminMap.get(recommendSection.getId());
                if(rsa==null){
                    rsa=new RecommendSectionAdmin(recommendSection);
                }
                rsa.getSystemList().add(superRecommendSection.getSystem());
                recommendSectionAdminMap.put(recommendSection.getId(), rsa);
            }
        }
        Map map = Utils.orderBy(recommendSectionAdminMap);
        Collection<RecommendSectionAdmin> values = map.values();
        recommendSectionAdmin.addAll(values);
        return recommendSectionAdmin;
        
    }
 
    public RecommendSection getRecommendSection(long id) {
        return recommendSectionDao.find(RecommendSection.class, id);
    }
 
    @Transactional
    public void deleteSections(final long[] rsids) {
 
        superRecommendSectionService.deleteSuperRecommendSections(rsids);
        recommendSectionGoodsService.deleteSectionGoodsBySections(rsids);
        recommendSectionDetailService.deleteSectionDetailBySections(rsids);
        recommendSectionDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
 
                StringBuffer sb = new StringBuffer(
                        "delete from RecommendSection s ");
                for (int i = 0; i < rsids.length; i++) {
                    if (i == 0) {
                        sb.append(" where s.id=? ");
                    } else {
                        sb.append(" or s.id=?");
                    }
                }
                Query query = session.createQuery(sb.toString());
                for (int i = 0; i < rsids.length; i++) {
                    query.setLong(i, rsids[i]);
                }
                query.executeUpdate();
                return null;
            }
        });
 
    }
 
    public void updateSection(RecommendSection section) {
        section.setCreatetime(java.lang.System.currentTimeMillis());
        recommendSectionDao.update(section);
    }
 
    public int getCount(String platform, String packages, final String key) {
 
        platform = Utils.getMap().get(platform);
        final BusinessSystem system = businessSystemService.getBusinessSystem(platform, packages);
        return (Integer) recommendSectionDao.excute(new HibernateCallback<Integer>() {
            
            public Integer doInHibernate(Session session)
                    throws HibernateException {
                String k = "%"+key+"%";
                Query query;
                if(system==null){
//                    Query query = session.createSQLQuery("select count(a.name) from (select name from yeshi_ec_recommend_section where `name` like ?) a");
                    query = session.createQuery("select count(rs.id) from RecommendSection 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_recommendsection shs left  join yeshi_ec_recommend_section hs on shs.recommendsection_id=hs.id where shs.system_id=? and hs.name like ?) a");
                    query=session.createQuery("select count(srs.id) from SuperRecommendSection srs where srs.system.id=? and srs.name like ?");
                    query.setParameter(0, system.getId());
                    query.setParameter(1, k);
                }
                    Long result = (Long) query.uniqueResult();
                    int intValue = result.intValue();
                    return intValue;
            }
        });
    
    }
 
    public List<RecommendSection> getRecommendSectionAll() {
        return recommendSectionDao.list("from RecommendSection rs order by rs.orderby");
    }
 
    @Override
    public int getCounts(String key) {
        return shareMapper.getCounts(key);
    }
 
}