admin
2020-04-13 dd5b15229cb15459fa7c31ccea77dac28cbfafbd
fanli/src/main/java/com/yeshi/fanli/service/impl/goods/TaoBaoCouponServiceImpl.java
@@ -1,6 +1,6 @@
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;
@@ -8,15 +8,13 @@
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.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoCouponMapper;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TaoBaoCoupon;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
@@ -25,6 +23,9 @@
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.RedisKeyEnum;
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;
@@ -32,12 +33,16 @@
public class TaoBaoCouponServiceImpl implements TaoBaoCouponService {
   @Resource
   private TaoBaoCouponDao taoBaoCouponDao;
   private TaoBaoCouponMapper taoBaoCouponMapper;
   @Resource
   private TaoBaoUnionConfigService taoBaoUnionConfigService;
   @Resource
   private RedisManager redisManager;
   @Transactional
   @Override
   public void updateTaoBaoCoupon() {
      List<TaoBaoUnionConfig> config = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID);
@@ -58,151 +63,86 @@
      int ii = 0;
      for (TaoBaoCoupon taoBaoCoupon : list) {
         if (ii == 0) {
            minId = (Long) taoBaoCouponDao.save(taoBaoCoupon);
            taoBaoCouponMapper.insert(taoBaoCoupon);
            minId = taoBaoCoupon.getId();
         } else {
            taoBaoCouponDao.save(taoBaoCoupon);
            taoBaoCouponMapper.insert(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();
      taoBaoCouponMapper.deleteBeforeCouponInfo(minId);
   }
   @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;
   public List<TaoBaoCoupon> getTaoBaoCouponList(String key, int page) {
      return taoBaoCouponMapper.getTaoBaoCouponList((page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE, key);
   }
   @Cacheable(value = "taoBaoCouponCache", key = "'getTaoBaoCouponList'+#page")
   @Override
   public int getCount(String key) {
      return taoBaoCouponMapper.getCount(key);
   }
   @Override
   public List<TaoBaoCoupon> getTaoBaoCouponListBykeys(List<String> searchKeys, int page) {
      if (searchKeys != null && searchKeys.size() == 0) {
         searchKeys = null;
      }
      return taoBaoCouponMapper.getTaoBaoCouponListBykeys((page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE,searchKeys);
   }
   @Override
   public int getCount(List<String> searchKeys) {
      if (searchKeys != null && searchKeys.size() == 0) {
         searchKeys = null;
      }
      return taoBaoCouponMapper.countTaoBaoCouponListBykeys(searchKeys);
   }
   @Override
   public TaoBaoCoupon getTaoBaoCouponByActionId(String auctionId) {
      return taoBaoCouponMapper.getTaoBaoCouponByActionId(auctionId);
   }
   /**
    * 采用redis做缓存
    */
   @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)
      String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoCachecoupon, 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;
   }