yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
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
package com.yeshi.fanli.service.impl.goods;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
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.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.goods.RecommendSectionGoodsDao;
import com.yeshi.fanli.dao.taobao.TaoBaoGoodsBriefDao;
import com.yeshi.fanli.entity.bus.recommend.RecommendSection;
import com.yeshi.fanli.entity.bus.recommend.RecommendSectionGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.ExistObjectException;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.service.inter.goods.RecommendSectionGoodsService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class RecommendSectionGoodsServiceImpl implements RecommendSectionGoodsService {
 
    @Resource
    private RecommendSectionGoodsDao recommendSectionGoodsDao;
 
    @Resource
    private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
 
    @Resource
    private TaoBaoGoodsBriefDao taoBaoGoodsBriefDao;
 
    @Cacheable(value = "sectionCache")
    public List<RecommendSectionGoods> getRecommendSectionGoods(long id, int counts) {
        List<RecommendSectionGoods> list = recommendSectionGoodsDao.list(
                "from RecommendSectionGoods rsg where rsg.recommendSection.id=? order by rsg.orderby ", 0, counts,
                new Serializable[] { id });
 
        return list;
    }
 
    public List<RecommendSectionGoods> getRecommendSectionGoods(int index, long rsid, String key) {
 
        int start = index * Constant.PAGE_SIZE;
        return recommendSectionGoodsDao.list(
                "from RecommendSectionGoods rsg where rsg.recommendSection.id=? and rsg.taoBaoGoodsBrief.title like ? order by rsg.orderby",
                start, Constant.PAGE_SIZE, new Serializable[] { rsid, "%" + key + "%" });
 
    }
 
    public int getCount(final long rsid, final String key) {
 
        return (Integer) recommendSectionGoodsDao.excute(new HibernateCallback<Integer>() {
 
            public Integer doInHibernate(Session session) throws HibernateException {
                // SQLQuery query = session.createSQLQuery("select count(a.id)
                // from (SELECT ssg.id FROM yeshi_ec_recommend_section_goods ssg
                // LEFT JOIN yeshi_ec_taobao_goods tg ON ssg.goodsid=tg.id WHERE
                // ssg.recommend_section_id=? AND tg.title LIKE ?) a ");
                Query query = session.createQuery(
                        "select count(rsg.id) from RecommendSectionGoods rsg where rsg.recommendSection.id=? and rsg.taoBaoGoodsBrief.title like ? ");
                query.setParameter(0, rsid);
                query.setParameter(1, "%" + key + "%");
                Long result = (Long) query.uniqueResult();
                return result.intValue();
            }
        });
 
    }
 
    public void deleteSectionGoods(long[] rsgids) {
        for (long id : rsgids) {
            recommendSectionGoodsDao.delete(new RecommendSectionGoods(id));
        }
 
    }
 
    public void addSectionGoods(long rsid, String tbgid) throws ExistObjectException {
        RecommendSectionGoods rsg = null;
        TaoBaoGoodsBrief taoBaoGoodsBrief = null;
        taoBaoGoodsBrief = taoBaoGoodsBriefService.getTaoBaoByAuctionId(tbgid);
        if (taoBaoGoodsBrief == null) {
            try {
                taoBaoGoodsBrief = TaoKeApiUtil.searchGoodsDetail(Long.parseLong(tbgid));
            } catch (TaobaoGoodsDownException e) {
                e.printStackTrace();
            }
            if (taoBaoGoodsBrief == null)
                return;
 
            taoBaoGoodsBrief.setId(null);
            taoBaoGoodsBrief.setCreatetime(new Date());
            taoBaoGoodsBrief.setUpdatetime(new Date());
            taoBaoGoodsBrief.setHasRecommended(0);
            taoBaoGoodsBrief.setHasSame(0);
            taoBaoGoodsBriefDao.create(taoBaoGoodsBrief);
        }
 
        List<RecommendSectionGoods> list = recommendSectionGoodsDao.list(
                "from RecommendSectionGoods rsg where rsg.taoBaoGoodsBrief.id=? and rsg.recommendSection.id=?",
                new Serializable[] { taoBaoGoodsBrief.getId(), rsid });
        if (list.size() > 0) {
            throw new ExistObjectException("同一商品不能重复关联");
        }
        rsg = new RecommendSectionGoods(taoBaoGoodsBrief, new RecommendSection(rsid), System.currentTimeMillis(), 1);
        recommendSectionGoodsDao.create(rsg);
 
    }
 
    public RecommendSectionGoods getRecommendSectionGoods(long id) {
        return recommendSectionGoodsDao.find(RecommendSectionGoods.class, id);
    }
 
    public void updateRecommendSectionGoods(RecommendSectionGoods rsg) throws NotExistObjectException {
        RecommendSectionGoods find = recommendSectionGoodsDao.find(RecommendSectionGoods.class, rsg.getId());
        if (find == null) {
            throw new NotExistObjectException("不存在该对象");
        }
        find.setOrderby(rsg.getOrderby());
        recommendSectionGoodsDao.update(find);
 
    }
 
    public void deleteRecommendSectionGoodsByTB(final long id) {
        recommendSectionGoodsDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session
                        .createQuery("delete from RecommendSectionGoods rsg where rsg.taoBaoGoodsBrief.id = ? ");
                query.setParameter(0, id);
                query.executeUpdate();
                return null;
            }
        });
    }
 
    @Transactional
    public void deleteRecommendSectionGoodsByTbAuctionId(final long auctionId) {
        recommendSectionGoodsDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Query query2 = session
                        .createQuery("from RecommendSectionGoods tb where tb.taoBaoGoodsBrief.auctionId = ? ");
                query2.setParameter(0, auctionId);
                query2.setFirstResult(0);
                query2.setMaxResults(1);
                List<RecommendSectionGoods> list = query2.list();
                if (list.size() == 0) {
                    return null;
                }
                RecommendSectionGoods recommendSectionGoods = list.get(0);
                session.delete(recommendSectionGoods);
                return null;
            }
        });
    }
 
    public void addRecommendSectionGoods(RecommendSection recommendSection, TaoBaoGoodsBrief taoBaoGoodsBrief)
            throws ExistObjectException {
        List<RecommendSectionGoods> list = recommendSectionGoodsDao.list(
                "from RecommendSectionGoods rsg where rsg.recommendSection.id=? and rsg.taoBaoGoodsBrief.id=?",
                new Serializable[] { recommendSection.getId(), taoBaoGoodsBrief.getId() });
        if (list.size() == 0) {
            recommendSectionGoodsDao.create(
                    new RecommendSectionGoods(taoBaoGoodsBrief, recommendSection, System.currentTimeMillis(), 1));
        } else {
            throw new ExistObjectException("同一商品不能重复关联");
        }
    }
 
    public void deleteSectionGoodsBySections(final long[] rsids) {
        recommendSectionGoodsDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                StringBuffer sb = new StringBuffer("delete from RecommendSectionGoods rsg ");
                int ii = 0;
                for (long id : rsids) {
                    if (ii == 0) {
                        sb.append(" where rsg.recommendSection.id = ? ");
                    } else {
                        sb.append(" or rsg.recommendSection.id = ? ");
                    }
                    ii++;
                }
                Query query = session.createQuery(sb.toString());
                ii = 0;
                for (long id : rsids) {
                    query.setParameter(ii, id);
                    ii++;
                }
                query.executeUpdate();
                return null;
            }
        });
    }
 
    @Cacheable(value = "sectionCache")
    public Map<Long, List<RecommendSectionGoods>> getAllSectionGoodsMap() {
 
        List<RecommendSectionGoods> list = recommendSectionGoodsDao
                .list("from RecommendSectionGoods rsg order by orderby desc");
        if (list.size() == 0) {
            return new HashMap<Long, List<RecommendSectionGoods>>();
        }
        Map<Long, List<RecommendSectionGoods>> map = new HashMap<Long, List<RecommendSectionGoods>>();
        for (RecommendSectionGoods recommendSectionGoods : list) {
            long id = recommendSectionGoods.getRecommendSection().getId();
            List<RecommendSectionGoods> rsglist = map.get(id);
            if (rsglist == null) {
                rsglist = new ArrayList<RecommendSectionGoods>();
                rsglist.add(recommendSectionGoods);
                map.put(id, rsglist);
            } else {
                rsglist.add(recommendSectionGoods);
            }
        }
        return map;
    }
 
    @Override
    public List<RecommendSectionGoods> getSectionGoods(int count) {
        return recommendSectionGoodsDao.list("from RecommendSectionGoods goods order by goods.orderby desc", 0, count,
                null);
    }
 
}