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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.yeshi.fanli.service.impl.goods;
 
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
 
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.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dao.taobao.TaoBaoCouponDao;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TaoBaoCoupon;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.service.inter.goods.TaoBaoCouponService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoCouponUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class TaoBaoCouponServiceImpl implements TaoBaoCouponService {
 
    @Resource
    private TaoBaoCouponDao taoBaoCouponDao;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private RedisManager redisManager;
 
    @Transactional
    public void updateTaoBaoCoupon() {
        List<TaoBaoUnionConfig> config = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID);
 
        Set<TaoBaoCoupon> taoBaoCouponList = TaoBaoCouponUtil.getTaoBaoCouponList(config.get(0));
        if (taoBaoCouponList == null) {
            return;
        }
 
        List<TaoBaoCoupon> list = new ArrayList<>();
        for (TaoBaoCoupon taoBaoCoupon : taoBaoCouponList) {
            list.add(taoBaoCoupon);
        }
 
        // 随机排序
        if (list != null)
            Collections.shuffle(list);
        long minId = 1;
        int ii = 0;
        for (TaoBaoCoupon taoBaoCoupon : list) {
            if (ii == 0) {
                minId = (Long) taoBaoCouponDao.save(taoBaoCoupon);
            } else {
                taoBaoCouponDao.save(taoBaoCoupon);
            }
            ii++;
        }
 
        final long endId = minId;
        taoBaoCouponDao.excute(new HibernateCallback() {
 
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createSQLQuery("delete from yeshi_ec_taobao_coupon where id < ? ");
                query.setParameter(0, endId);
                return query.executeUpdate();
            }
        });
    }
 
    public List<TaoBaoCoupon> getTaoBaoCouponList(String key, int page) {
        int start = (page - 1) * Constant.PAGE_SIZE;
        List<TaoBaoCoupon> list = taoBaoCouponDao.list(
                "from TaoBaoCoupon tbc where tbc.title like ? order by tbc.orderby,tbc.id ", start, Constant.PAGE_SIZE,
                new Serializable[] { "%" + key + "%" });
        return list;
    }
 
    public int getCount(String key) {
        Long count = taoBaoCouponDao.getCount("select count(*) from TaoBaoCoupon tbc where tbc.title like ? ",
                new Serializable[] { "%" + key + "%" });
        return count.intValue();
    }
 
    public void deleteTaoBaoCoupons(long[] ids) {
        for (long id : ids) {
            taoBaoCouponDao.delete(new TaoBaoCoupon(id));
        }
    }
 
    public TaoBaoCoupon getaoBaoCoupon(long id) {
        TaoBaoCoupon find = taoBaoCouponDao.find(TaoBaoCoupon.class, id);
        return find;
    }
 
    @Transactional
    public void updatetaoBaoCoupon(TaoBaoCoupon find) {
        taoBaoCouponDao.update(find);
    }
 
    public List<TaoBaoCoupon> getTaoBaoCouponListByClassKeys(String[] keys, int page) {
        int start = (page - 1) * Constant.PAGE_SIZE;
        StringBuffer sb = new StringBuffer("from TaoBaoCoupon tbc where tbc.classNames like ? ");
        int keyCount = keys.length;
        for (int i = 0; i < keyCount; i++) {
            if (i != 0) {
                sb.append(" or tbc.classNames like ? ");
            }
            keys[i] = "%" + keys[i] + "%";
        }
        sb.append(" order by tbc.biz30day desc ");
        List<TaoBaoCoupon> list = taoBaoCouponDao.list(sb.toString(), start, Constant.PAGE_SIZE, keys);
        // if(list.size() > 0){
        // String proportion =
        // hongBaoManageService.get(Constant.HONGBAO_GOODS_PROPORTION);
        // double pd=Double.parseDouble(proportion)/100;
        // for (TaoBaoCoupon taoBaoCoupon : list) {
        // double tk = (taoBaoCoupon.getTkRate()/100) * pd;
        // BigDecimal zkPrice = taoBaoCoupon.getZkPrice();
        // BigDecimal hb = BigDecimalUtil.mul2(zkPrice, new BigDecimal(tk));
        // taoBaoCoupon.setHongbao(hb);
        // }
        // }
        return list;
 
    }
 
    public List<TaoBaoCoupon> getTaoBaoCouponListBykeys(List<String> searchKeys, int page) {
 
        if (searchKeys == null || searchKeys.size() == 0) {
            searchKeys = new ArrayList<String>();
            searchKeys.add("");
        }
 
        StringBuffer sb = new StringBuffer("from TaoBaoCoupon tbc where ");
        int ii = 0;
        for (String key : searchKeys) {
            key = key.replaceAll(" ", "%");
            if (ii == 0) {
                sb.append("( tbc.title like '%" + key + "%' ");
            } else {
                sb.append(" or tbc.title like '%" + key + "%' ");
            }
            ii++;
        }
        sb.append(") and tbc.showType = 3 ");
        // sb.append(" order by tbc.biz30day desc ");
        int start = (page - 1) * Constant.PAGE_SIZE;
        return taoBaoCouponDao.list(sb.toString(), start, Constant.PAGE_SIZE, new Serializable[] {});
    }
 
    public TaoBaoCoupon getTaoBaoCouponByActionId(String id) {
        List<TaoBaoCoupon> list = taoBaoCouponDao.list("from TaoBaoCoupon tbc where tbc.auctionId = ? ",
                new Serializable[] { id });
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    public int getCount(List<String> searchKeys) {
 
        if (searchKeys == null || searchKeys.size() == 0) {
            return 0;
        }
 
        StringBuffer sb = new StringBuffer();
        sb.append("select count(tbc.id) from TaoBaoCoupon tbc where ");
        int ii = 0;
        for (String key : searchKeys) {
            key = key.replaceAll(" ", "%");
            if (ii == 0) {
                sb.append("( tbc.title like '%" + key + "%' ");
            } else {
                sb.append(" or tbc.title like '%" + key + "%' ");
            }
            ii++;
        }
        sb.append(") and tbc.showType = 3 ");
        Long count = taoBaoCouponDao.getCount(sb.toString());
        return count.intValue();
    }
 
    @Override
    public TaoBaoCoupon getTaoBaoCouponByAuctionId(long id) {
        List<TaoBaoCoupon> findList = taoBaoCouponDao.list("from TaoBaoCoupon coupon where coupon.auctionId=" + id);
        if (findList != null && findList.size() > 0)
            return findList.get(0);
        return null;
    }
 
    /**
     * 采用redis做缓存
     */
    @Override
    public List<TaoBaoGoodsBrief> getTaoBaoCouponList(int page) {
        System.out.println("进入了方法");
        String key = "cachecoupon-" + page;
        String resultStr = "";
        try {
            resultStr = redisManager.getCommonString(key);
        } catch (Exception e) {
        }
 
        if (!StringUtil.isNullOrEmpty(resultStr)) {
            Type type = new TypeToken<List<TaoBaoGoodsBrief>>() {
            }.getType();
            List<TaoBaoGoodsBrief> list = new Gson().fromJson(resultStr, type);
            return list;
        }
 
        TaoBaoSearchResult result = TaoKeApiUtil.getMaterialByMaterialId(3756, page, 20);// searchCouple(null,
                                                                                            // null,
                                                                                            // page,
                                                                                            // 20);
        if (result != null && result.getTaoBaoGoodsBriefs() != null && result.getTaoBaoGoodsBriefs().size() > 0) {
            // 20分钟缓存
            try {
                redisManager.cacheCommonString(key, new Gson().toJson(result.getTaoBaoGoodsBriefs()), 60 * 20);
            } catch (Exception e) {
            }
 
            return result.getTaoBaoGoodsBriefs();
        }
 
        return null;
    }
 
    @Cacheable(value = "taoBaoCouponCache", key = "'getTaoBaoCouponListByMeterialId'+#materialId+'-'+#page")
    @Override
    public List<TaoBaoGoodsBrief> getTaoBaoCouponListByMeterialId(int materialId, int page) {
        TaoBaoSearchResult result = TaoKeApiUtil.getMaterialByMaterialId(materialId, page, 20);
        if (result != null)
            return result.getTaoBaoGoodsBriefs();
        return null;
    }
 
}