package com.yeshi.fanli.service.impl.goods;
|
|
import java.io.Serializable;
|
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.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.taobao.TaoBaoCouponUtil;
|
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
|
|
@Service
|
public class TaoBaoCouponServiceImpl implements TaoBaoCouponService {
|
|
@Resource
|
private TaoBaoCouponDao taoBaoCouponDao;
|
|
@Resource
|
private TaoBaoUnionConfigService taoBaoUnionConfigService;
|
|
@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;
|
}
|
@Cacheable(value = "taoBaoCouponCache", key = "'getTaoBaoCouponList'+#page")
|
@Override
|
public List<TaoBaoGoodsBrief> getTaoBaoCouponList(int page) {
|
System.out.println("进入了方法");
|
TaoBaoSearchResult result = TaoKeApiUtil.getMaterialByMaterialId(3756, page, 20);// searchCouple(null, null, page, 20);
|
if (result != null)
|
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;
|
}
|
|
}
|