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);
|
}
|
|
}
|