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
package com.yeshi.fanli.service.impl.goods;
 
import java.io.Serializable;
import java.util.List;
 
import javax.annotation.Resource;
import javax.transaction.Transactional;
 
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 com.yeshi.fanli.dao.goods.ClassRecommendGoodsDao;
import com.yeshi.fanli.entity.bus.clazz.ClassRecommendGoods;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.ExistObjectException;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.service.inter.goods.ClassRecommendGoodsService;
import com.yeshi.fanli.service.inter.goods.GoodsClassService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
 
@Service
public class ClassRecommendGoodsServiceImpl implements
        ClassRecommendGoodsService {
 
    @Resource
    private ClassRecommendGoodsDao classRecommendGoodsDao;
    
    @Resource
    private GoodsClassService goodsClassService;
    
    @Resource
    private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
    
    @Cacheable(value="crgCache",key="'gcrc-'+#id")
    public List<ClassRecommendGoods> getClassRecommendGoodsByGoodsClassId(
            long id) {
            return classRecommendGoodsDao.list("from ClassRecommendGoods crg "
                    + "where crg.goodsClass.id=?",new Serializable[]{id});
    
    }
    @Transactional
    public void addRecommendGoods(String tbid, long gcid) throws ExistObjectException{
        TaoBaoGoodsBrief taoBaoGoodsBrief=null;
        ClassRecommendGoods crg=null;
        taoBaoGoodsBrief = taoBaoGoodsBriefService.getTaoBaoByAuctionId(tbid);
        if(taoBaoGoodsBrief == null){
            taoBaoGoodsBrief = TaoBaoUtil.getTaoBaoGoodsBrief(tbid);
            taoBaoGoodsBriefService.save(taoBaoGoodsBrief);
        }
        List<ClassRecommendGoods> list = classRecommendGoodsDao.list("from ClassRecommendGoods crg where crg.taoBaoGoodsBrief.id=? and crg.goodsClass.id=?",new Serializable[]{taoBaoGoodsBrief.getId(),gcid});
        if(list.size()>0){
            throw new ExistObjectException("同一商品不能重复关联");
        }
        crg=new ClassRecommendGoods(new GoodsClass(gcid), taoBaoGoodsBrief, 1, System.currentTimeMillis());
        classRecommendGoodsDao.create(crg);
    }
    public List<ClassRecommendGoods> getClassRecommendGoods(int index, String key,
            long cgid) {
        int start = index * Constant.PAGE_SIZE;
        return classRecommendGoodsDao.list("from ClassRecommendGoods crg where crg.goodsClass.id=? and crg.taoBaoGoodsBrief.title like ?",start,Constant.PAGE_SIZE,new Serializable[]{cgid,"%"+key+"%"});
        
    }
    public int getCount(long cgid, String key) {
        Long lcount = classRecommendGoodsDao.getCount("select count(*) from ClassRecommendGoods crg where crg.goodsClass.id=? and crg.taoBaoGoodsBrief.title like ?", new Serializable[]{cgid,"%"+key+"%"});
        return lcount.intValue();
    }
    
    public void deleteRecommendGoods(long id) {
        classRecommendGoodsDao.delete(new ClassRecommendGoods(id));
    }
    
    public ClassRecommendGoods getRecommendGoods(long cgid) {
        return classRecommendGoodsDao.find(ClassRecommendGoods.class, cgid);
    } 
    
    public void updateRecommendGoods(long cgid, int orderby) throws NotExistObjectException {
        ClassRecommendGoods find = classRecommendGoodsDao.find(ClassRecommendGoods.class, cgid);
        if(find==null){
            throw new NotExistObjectException("不存在该对象");
        }
        find.setOrderby(orderby);
        classRecommendGoodsDao.update(find);
    }
    public void deleteRecommendGoodsByTB(final long id) {
        classRecommendGoodsDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
                Query query = session.createQuery("delete from ClassRecommendGoods crg where crg.taoBaoGoodsBrief.id = ? ");
                query.setParameter(0, id);
                query.executeUpdate();
                return null;
            }
        });
    }
    public void addRecommendGoods(GoodsClass goodsClass, TaoBaoGoodsBrief taoBaoGoodsBrief) throws ExistObjectException {
        List<ClassRecommendGoods> list = classRecommendGoodsDao.list("from ClassRecommendGoods crg where crg.goodsClass.id = ? and crg.taoBaoGoodsBrief.id = ?",new Serializable[]{goodsClass.getId(),taoBaoGoodsBrief.getId()});
        if(list.size()==0){
            classRecommendGoodsDao.create(new ClassRecommendGoods(goodsClass, taoBaoGoodsBrief, 1, System.currentTimeMillis()));
        }else{
            throw new ExistObjectException("商品不可重复关联");
        }
    }
    public void deleteClassGoodsByGC(final long id) {
        classRecommendGoodsDao.excute(new HibernateCallback() {
                public Object doInHibernate(Session session)
                        throws HibernateException {
                    Query query = session.createQuery("delete from ClassRecommendGoods crg where crg.goodsClass.id = ? ");
                    query.setParameter(0, id);
                    query.executeUpdate();
                    return null;
                }
            });
    }
}