admin
2020-11-28 dc5be7d38446f70e6ff86df311119c32b41fe7f8
fanli/src/main/java/com/yeshi/fanli/job/order/vipshop/UpdateVipShopOrderJob.java
@@ -5,6 +5,7 @@
import javax.annotation.Resource;
import com.yeshi.fanli.util.Constant;
import org.springframework.stereotype.Component;
import com.xxl.job.core.biz.model.ReturnT;
@@ -18,119 +19,140 @@
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.cmq.order.VipShopOrderCMQManager;
import com.yeshi.fanli.util.mq.cmq.order.VipShopOrderCMQManager;
import com.yeshi.fanli.util.vipshop.VipShopApiUtil;
//从淘宝爬去订单更新
@Component
public class UpdateVipShopOrderJob {
   @Resource
   private VipShopOrderService vipShopOrderService;
    @Resource
    private VipShopOrderService vipShopOrderService;
   @Resource
   private RedisManager redisManager;
    @Resource
    private RedisManager redisManager;
   /**
    * 保存订单
    *
    * @param VipShopOrderList
    */
   public void saveVipShopOrders(List<VipShopOrder> vipShopOrderList) {
      for (VipShopOrder order : vipShopOrderList) {
         try {
            vipShopOrderService.addOrder(order);
            /**
             * 做频率限制
             */
            String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.VIPShopOrder, order.getOrderSn() + "");
            String result = redisManager.getCommonString(key);
            // 判断
    /**
     * 保存订单
     *
     * @param vipShopOrderList
     */
    public void saveVipShopOrders(List<VipShopOrder> vipShopOrderList) {
        //5分钟不更新就报警
        try {
            redisManager.cacheCommonString(RedisKeyEnum.monitor.getKey() + Constant.SOURCE_TYPE_VIP, "1", 60 * 5);
        } catch (Exception e) {
        }
        for (VipShopOrder order : vipShopOrderList) {
            try {
                vipShopOrderService.addOrder(order);
                /**
                 * 做频率限制
                 */
                String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.VIPShopOrder, order.getOrderSn() + "");
                String result = redisManager.getCommonString(key);
                // 判断
//            if (StringUtil.isNullOrEmpty(result)) {
               VipShopOrderCMQManager.getInstance().addVipShopOrder(order.getOrderSn() + "");
               redisManager.cacheCommonString(key, "1", 60 * 60 * 2);// 2小时内不再更新
               LogHelper.test("唯品会订单消息发送成功");
                VipShopOrderCMQManager.getInstance().addVipShopOrder(order.getOrderSn() + "");
                redisManager.cacheCommonString(key, "1", 60 * 60 * 2);// 2小时内不再更新
                LogHelper.test("唯品会订单消息发送成功");
//            }else{
//               LogHelper.test("唯品会订单频率限制");
//            }
         } catch (VipShopOrderException e) {
            e.printStackTrace();
            LogHelper.errorDetailInfo(e);
         }
      }
   }
            } catch (VipShopOrderException e) {
                e.printStackTrace();
                LogHelper.errorDetailInfo(e);
            }
        }
    }
   /**
    * 快速订单更新(爬取本小时内的单,每分钟一次)
    */
   @XxlJob("updateVipShopOrderHandler")
   public ReturnT<String> updateVipShopSoonOrder(String param) throws Exception {
      LogHelper.test("updateVipShopOrderHandler:"+param);
      long endTime = System.currentTimeMillis();
      if ("1hour".equalsIgnoreCase(param)) {// 更新1小时内的订单
         updateByOrderTime(endTime - 1000 * 60 * 60 * 1L, endTime);
         updateByUpdateTime(endTime - 1000 * 60 * 60 * 1L, endTime);
      } else if ("1day".equalsIgnoreCase(param)) {// 更新最近1天更新的数据
         updateByOrderTime(endTime - 1000 * 60 * 60 * 24L, endTime);
         updateByUpdateTime(endTime - 1000 * 60 * 60 * 24L, endTime);
      }
      /*
       * else if ("3day".equalsIgnoreCase(param)) {// 更新最近3天更新的数据
       * updateByOrderTime(endTime - 1000 * 60 * 60 * 24 * 3L, endTime);
       * updateByUpdateTime(endTime - 1000 * 60 * 60 * 24 * 3L, endTime); }
       */
      return ReturnT.SUCCESS;
   }
    /**
     * 快速订单更新(爬取本小时内的单,每分钟一次)
     */
    @XxlJob("updateVipShopOrderHandler")
    public ReturnT<String> updateVipShopSoonOrder(String param) throws Exception {
        LogHelper.test("updateVipShopOrderHandler:" + param);
        long endTime = System.currentTimeMillis();
        if ("1hour".equalsIgnoreCase(param)) {// 更新1小时内的订单
            updateByOrderTime(endTime - 1000 * 60 * 60 * 1L, endTime);
            updateByUpdateTime(endTime - 1000 * 60 * 60 * 1L, endTime);
        } else if ("1day".equalsIgnoreCase(param)) {// 更新最近1天更新的数据
            updateByOrderTime(endTime - 1000 * 60 * 60 * 24L, endTime);
            updateByUpdateTime(endTime - 1000 * 60 * 60 * 24L, endTime);
        }
        /*
         * else if ("3day".equalsIgnoreCase(param)) {// 更新最近3天更新的数据
         * updateByOrderTime(endTime - 1000 * 60 * 60 * 24 * 3L, endTime);
         * updateByUpdateTime(endTime - 1000 * 60 * 60 * 24 * 3L, endTime); }
         */
        return ReturnT.SUCCESS;
    }
   /**
    * 按下单时间更新
    * @Title: updateByOrderTime
    * @Description:
    * @param startTime
    * @param endTime
    * void 返回类型
    * @throws
    */
   public void updateByOrderTime(long startTime, long endTime) {
      List<VipShopOrder> vipShopOrderList = new ArrayList<>();
      int page = 1;
      VipShopQueryOrderResultDTO result = VipShopApiUtil
            .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
      while (result != null && result.getOrderList().size() > 0) {
         page++;
         vipShopOrderList.addAll(result.getOrderList());
         result = VipShopApiUtil
               .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
      }
    //根据订单号更新
    @XxlJob("order-vip-updateByOrderSn")
    public ReturnT<String> updateByOrderSn(String param) throws Exception {
        List<VipShopOrder> orders = vipShopOrderService.listByOrderSn(param);
        if (orders == null || orders.size() == 0)
            throw new Exception("订单不存在");
      saveVipShopOrders(vipShopOrderList);
   }
        VipShopQueryOrderResultDTO result = VipShopApiUtil
                .getOrderList(VipShopOrderQueryModel.createOrderTime(orders.get(0).getOrderTime(), orders.get(0).getOrderTime() + 1000, null, 1));
        saveVipShopOrders(result.getOrderList());
        return ReturnT.SUCCESS;
    }
   /**
    * 按更新时间更新
    * @Title: updateByOrderTime
    * @Description:
    * @param startTime
    * @param endTime
    * void 返回类型
    * @throws
    */
    /**
     * 按下单时间更新
     *
     * @param startTime
     * @param endTime   void 返回类型
     * @throws
     * @Title: updateByOrderTime
     * @Description:
     */
   public void updateByUpdateTime(long startTime, long endTime) {
      List<VipShopOrder> vipShopOrderList = new ArrayList<>();
      int page = 1;
      VipShopQueryOrderResultDTO result = VipShopApiUtil
            .getOrderList(VipShopOrderQueryModel.createUpdateTime(startTime, endTime, null, page));
    public void updateByOrderTime(long startTime, long endTime) {
        List<VipShopOrder> vipShopOrderList = new ArrayList<>();
        int page = 1;
        VipShopQueryOrderResultDTO result = VipShopApiUtil
                .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
      while (result != null && result.getOrderList().size() > 0) {
         page++;
         vipShopOrderList.addAll(result.getOrderList());
         result = VipShopApiUtil
               .getOrderList(VipShopOrderQueryModel.createUpdateTime(startTime, endTime, null, page));
      }
      saveVipShopOrders(vipShopOrderList);
   }
        while (result != null && result.getOrderList().size() > 0) {
            page++;
            vipShopOrderList.addAll(result.getOrderList());
            result = VipShopApiUtil
                    .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
        }
        saveVipShopOrders(vipShopOrderList);
    }
    /**
     * 按更新时间更新
     *
     * @param startTime
     * @param endTime   void 返回类型
     * @throws
     * @Title: updateByOrderTime
     * @Description:
     */
    public void updateByUpdateTime(long startTime, long endTime) {
        List<VipShopOrder> vipShopOrderList = new ArrayList<>();
        int page = 1;
        VipShopQueryOrderResultDTO result = VipShopApiUtil
                .getOrderList(VipShopOrderQueryModel.createUpdateTime(startTime, endTime, null, page));
        while (result != null && result.getOrderList().size() > 0) {
            page++;
            vipShopOrderList.addAll(result.getOrderList());
            result = VipShopApiUtil
                    .getOrderList(VipShopOrderQueryModel.createUpdateTime(startTime, endTime, null, page));
        }
        saveVipShopOrders(vipShopOrderList);
    }
}