admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/job/order/vipshop/UpdateVipShopOrderJob.java
@@ -1,158 +1,246 @@
package com.yeshi.fanli.job.order.vipshop;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.yeshi.fanli.util.Constant;
import org.springframework.stereotype.Component;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.fanli.dto.vipshop.VipShopOrderQueryModel;
import com.yeshi.fanli.dto.vipshop.VipShopQueryOrderResultDTO;
import com.yeshi.fanli.entity.vipshop.VipShopOrder;
import com.yeshi.fanli.exception.vipshop.VipShopOrderException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.vipshop.VipShopOrderService;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
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 RedisManager redisManager;
    /**
     * 保存订单
     *
     * @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("唯品会订单消息发送成功");
//            }else{
//               LogHelper.test("唯品会订单频率限制");
//            }
            } 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("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("订单不存在");
        VipShopQueryOrderResultDTO result = VipShopApiUtil
                .getOrderList(VipShopOrderQueryModel.createOrderTime(orders.get(0).getOrderTime(), orders.get(0).getOrderTime() + 1000, null, 1));
        saveVipShopOrders(result.getOrderList());
        return ReturnT.SUCCESS;
    }
    /**
     * 按下单时间更新
     *
     * @param startTime
     * @param endTime   void 返回类型
     * @throws
     * @Title: updateByOrderTime
     * @Description:
     */
    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));
        }
        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);
    }
}
package com.yeshi.fanli.job.order.vipshop;
import java.util.*;
import javax.annotation.Resource;
import com.yeshi.fanli.dao.mybatis.vipshop.VipShopOrderMapper;
import com.yeshi.fanli.dto.mq.order.body.CommonOrderMQMsg;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.mq.cmq.order.OrdersCMQManager;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqManager;
import com.yeshi.fanli.util.vipshop.DingDanXiaApiUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.fanli.dto.vipshop.VipShopOrderQueryModel;
import com.yeshi.fanli.dto.vipshop.VipShopQueryOrderResultDTO;
import com.yeshi.fanli.entity.vipshop.VipShopOrder;
import com.yeshi.fanli.exception.vipshop.VipShopOrderException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.vipshop.VipShopOrderService;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
//从淘宝爬去订单更新
@Component
public class UpdateVipShopOrderJob {
    private  Logger logger = LoggerFactory.getLogger("debugLog");
    @Resource
    private VipShopOrderService vipShopOrderService;
    @Resource
    private RedisManager redisManager;
    @Resource
    private VipShopOrderMapper vipShopOrderMapper;
    @Resource
    private OrdersCMQManager ordersCMQManager;
    /**
     * 保存订单
     *
     * @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)) {
                ordersCMQManager.addOrder(new CommonOrderMQMsg(order.getOrderSn() + "", Constant.SOURCE_TYPE_VIP));
                redisManager.cacheCommonString(key, "1", 60 * 60 * 2);// 2小时内不再更新
                LogHelper.test("唯品会订单消息发送成功");
//            }else{
//               LogHelper.test("唯品会订单频率限制");
//            }
            } catch (VipShopOrderException e) {
                e.printStackTrace();
                LogHelper.errorDetailInfo(e);
            }
        }
    }
    /**
     * 快速订单更新(爬取本小时内的单,每分钟一次)
     */
    @XxlJob("updateVipShopOrderHandler")
    public ReturnT<String> updateVipShopSoonOrder(String param) throws Exception {
        LogHelper.test("updateVipShopOrderHandler:" + param);
        logger.info("唯品会订单更新开始:updateVipShopSoonOrder 参数:"+param);
        try {
            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天更新的数据
                Long startT = endTime - 1000 * 60 * 60 * 24L;
                for (long start = startT; start < startT + 1000 * 60 * 60 * 24L; start += 1000 * 60 * 60) {
                    updateByOrderTime(start, start + 1000 * 60 * 60);
                    updateByUpdateTime(start, start + 1000 * 60 * 60);
                }
            } else if ("1month".equalsIgnoreCase(param)) {// 更新最近1个月的数据
                for (int i = 0; i < 30; i++) {
                    Long startT = endTime - 1000 * 60 * 60 * 24L * (i + 1);
                    for (long start = startT; start < startT + 1000 * 60 * 60 * 24L; start += 1000 * 60 * 60) {
                        updateByOrderTime(start, start + 1000 * 60 * 60);
                        updateByUpdateTime(start, start + 1000 * 60 * 60);
                    }
                }
            }
            /*
             * else if ("3day".equalsIgnoreCase(param)) {// 更新最近3天更新的数据
             * updateByOrderTime(endTime - 1000 * 60 * 60 * 24 * 3L, endTime);
             * updateByUpdateTime(endTime - 1000 * 60 * 60 * 24 * 3L, endTime); }
             */
        }catch(Exception e){
            logger.error("唯品会订单更新异常:",e);
        }finally {
            logger.info("唯品会订单更新结束:updateVipShopSoonOrder 参数:"+ param);
        }
        return ReturnT.SUCCESS;
    }
    //根据订单号更新
    @XxlJob("order-vip-updateByOrderSn")
    public ReturnT<String> updateByOrderSn(String param) throws Exception {
        logger.info(String.format("唯品会订单更新:%s", param));
        try {
            String[] ps = param.split(",");
            for (String p : ps) {
                List<VipShopOrder> orders = vipShopOrderService.listByOrderSn(p);
                if (orders == null || orders.size() == 0) {
                    // 请求订单详情
                    saveVipShopOrders(Arrays.asList(new VipShopOrder[]{DingDanXiaApiUtil.getOrderDetail(p)}));
                } else {
                    Thread.sleep(1000);
                    VipShopOrder vipShopOrder = DingDanXiaApiUtil.getOrderDetail(p);
                    if (vipShopOrder != null) {
                        List<VipShopOrder> vipShopOrderList = new ArrayList<>();
                        vipShopOrderList.add(vipShopOrder);
                        saveVipShopOrders(vipShopOrderList);
                    }
                }
            }
            logger.info("唯品会订单更新成功");
        }catch(Exception e){
            logger.info(String.format("唯品会订单更新失败:%s", e.getMessage()));
        }
        return ReturnT.SUCCESS;
    }
    /**
     * 更新长时间未更新的订单号
     *
     * @param param
     * @return
     * @throws Exception
     */
    @Resource
    private CommonOrderService commonOrderService;
    @XxlJob("order-vip-updateByLongTimeNoUpdate")
    public ReturnT<String> updateByLongTimeNoUpdate(String param) throws Exception {
        List<CommonOrder> commonOrders = commonOrderService.listPayStateOrder(Constant.SOURCE_TYPE_VIP, new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L * 60), new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L * 10), 1, 200);
        if (commonOrders == null || commonOrders.size() == 0) {
            throw new Exception("没有需要更新的订单");
        }
        Set<String> orderIds = new HashSet<>();
        for (CommonOrder commonOrder : commonOrders) {
            orderIds.add(commonOrder.getOrderNo());
        }
        for (String orderId : orderIds) {
            updateByOrderSn(orderId);
        }
        return ReturnT.SUCCESS;
    }
    /**
     * 按下单时间更新
     *
     * @param startTime
     * @param endTime   void 返回类型
     * @throws
     * @Title: updateByOrderTime
     * @Description:
     */
    public void updateByOrderTime(long startTime, long endTime) {
        List<VipShopOrder> vipShopOrderList = new ArrayList<>();
        int page = 1;
        VipShopQueryOrderResultDTO result = DingDanXiaApiUtil
                .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
        while (result != null && result.getOrderList().size() > 0) {
            page++;
            vipShopOrderList.addAll(result.getOrderList());
            result = DingDanXiaApiUtil
                    .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
        }
        saveVipShopOrders(vipShopOrderList);
        //订单侠的接口
        page = 1;
        result = DingDanXiaApiUtil
                .getOrderList(VipShopOrderQueryModel.createOrderTime(startTime, endTime, null, page));
        while (result != null && result.getOrderList().size() > 0) {
            page++;
            vipShopOrderList.addAll(result.getOrderList());
            result = DingDanXiaApiUtil
                    .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 = DingDanXiaApiUtil
                .getOrderList(VipShopOrderQueryModel.createUpdateTime(startTime, endTime, null, page));
        while (result != null && result.getOrderList().size() > 0) {
            page++;
            vipShopOrderList.addAll(result.getOrderList());
            result = DingDanXiaApiUtil
                    .getOrderList(VipShopOrderQueryModel.createUpdateTime(startTime, endTime, null, page));
        }
        saveVipShopOrders(vipShopOrderList);
    }
}