Administrator
2018-11-26 31d6bd3dd8163a5d6c182110dcf21e94b4770dac
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
package com.yeshi.fanli.service.impl.goods;
 
import java.io.Serializable;
import java.util.List;
 
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.CollectionGoodsDao;
import com.yeshi.fanli.entity.bus.user.CollectionGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.service.inter.goods.CollectionGoodsService;
import com.yeshi.fanli.util.Constant;
 
@Service
public class CollectionGoodsServiceImpl implements CollectionGoodsService {
 
    @Resource
    private CollectionGoodsDao dao;
 
    @Override
    public void save(CollectionGoods cg) {
        cg.setCreateTime(System.currentTimeMillis());
        dao.save(cg);
    }
 
    @Override
    public CollectionGoods findByUidAndAuctionId(long uid, long auctionId) {
        List<CollectionGoods> list = dao.list("from CollectionGoods cg where cg.userInfo.id=? and auctionId=?", 0, 1,
                new Serializable[] { uid, auctionId });
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public void delete(CollectionGoods find) {
        dao.delete(find);
    }
 
    @Override
    public List<CollectionGoods> findCollectionGoods(long uid, int page) {
        return dao.list("from CollectionGoods cg where cg.userInfo.id = ? order by id desc",
                (page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE, new Serializable[] { uid });
    }
 
    @Override
    public int getCount(long uid) {
        return (int) dao.getCount("select count(*) from CollectionGoods cg where cg.userInfo.id = ?",
                new Serializable[] { uid });
    }
 
    @Override
    public void delete(long auctionId, long uid) {
        dao.excute(new HibernateCallback() {
 
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session
                        .createQuery("delete from CollectionGoods cg where cg.userInfo.id = ? and auctionId = ?");
                query.setParameter(0, uid);
                query.setParameter(1, auctionId);
                query.executeUpdate();
                return null;
            }
        });
    }
 
    @Override
    public void clear(long uid) {
        dao.excute(new HibernateCallback() {
 
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(" delete from CollectionGoods cg where cg.userInfo.id = ?");
                query.setParameter(0, uid);
                query.executeUpdate();
                return null;
            }
        });
    }
 
    @Override
    public CollectionGoods findByAuctionId(long auctionId) {
        List<CollectionGoods> list = dao.list("from CollectionGoods cg where cg.auctionId=" + auctionId);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return null;
    }
 
    @Transactional
    @Override
    public void updateCollectionGoods(TaoBaoGoodsBrief goods) {
        if (goods != null && goods.getAuctionId() != null) {
            long count = dao.getCount("select count(*) from CollectionGoods cg where cg.auctionId=?",
                    new Serializable[] { goods.getAuctionId() });
            if (count > 0) {
                int pageSize = 50;
                int page = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
                for (int i = 0; i < page; i++) {
                    List<CollectionGoods> list = dao.list("from CollectionGoods cg where cg.auctionId=?", i * pageSize,
                            pageSize, new Serializable[] { goods.getAuctionId() });
                    if (list != null)
                        for (CollectionGoods cg : list) {
                            cg = getCollectionGoods(cg, goods);
                            if (cg != null)
                                dao.update(cg);
                        }
                }
            }
        }
    }
 
    private static CollectionGoods getCollectionGoods(CollectionGoods collectionGoods, TaoBaoGoodsBrief goods) {
        if (collectionGoods == null || goods == null)
            return null;
        // 更新商品信息(标题,销量,图片,佣金比例,价格,券信息)
        collectionGoods.setBiz30day(goods.getBiz30day());
        collectionGoods.setTitle(goods.getTitle());
        collectionGoods.setCouponAmount(goods.getCouponAmount());
        collectionGoods.setCouponEffectiveEndTime(goods.getCouponEffectiveEndTime());
        collectionGoods.setCouponLeftCount(goods.getCouponLeftCount());
        collectionGoods.setCouponInfo(goods.getCouponInfo());
        collectionGoods.setCouponEffectiveStartTime(goods.getCouponEffectiveStartTime());
        collectionGoods.setCouponStartFee(goods.getCouponStartFee());
        collectionGoods.setCouponTotalCount(goods.getCouponTotalCount());
        collectionGoods.setTkRate(goods.getTkRate());
        collectionGoods.setPictUrl(goods.getPictUrl());
        collectionGoods.setZkPrice(goods.getZkPrice());
        collectionGoods.setReservePrice(goods.getReservePrice());
        return collectionGoods;
    }
 
}