admin
2023-02-01 856d99bb5adf7f8670206b01750bc0260b8666d2
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
package com.yeshi.fanli.job.order.taobao;
 
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.fanli.dao.taobao.TaoBaoOrderBackUpDao;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoOrderBackUp;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoKeOrderApiUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * @author Administrator
 * @title: TaoBaoOrderUpdateJob
 * @description: 淘宝订单更新机制
 * @date 2021/10/9 16:30
 */
@Component
public class TaoBaoOrderUpdateJob {
    @Resource
    private CommonOrderService commonOrderService;
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
    @Resource
    private UpdateTBRelationAndSpecialOrderJob updateRelationAndSpecialOrderJob;
    @Resource
    private UpdateOrderJob updateOrderJob;
    @Resource
    private TaoBaoOrderBackUpDao taoBaoOrderBackUpDao;
 
    //更新正在处于付款状态的订单
    @XxlJob("order-taobao-updatePayStateOrder")
    public ReturnT<String> updatePayStateOrder(String param) throws Exception {
        //更新30天之前的
        String startTime = "20200201";
        if (!StringUtil.isNullOrEmpty(param)) {
            startTime = param;
        }
 
        List<CommonOrder> commonOrders = commonOrderService.listPayStateOrder(Constant.SOURCE_TYPE_TAOBAO, new Date(TimeUtil.convertToTimeTemp(startTime, "yyyyMMdd")), new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L * 30),1,200);
        if (commonOrders == null || commonOrders.size() == 0) {
            throw new Exception("没有订单需要更新");
        }
 
        Set<String> orderIds = new HashSet<>();
        for (CommonOrder co : commonOrders) {
            orderIds.add(co.getOrderNo());
        }
 
        for (String orderId : orderIds) {
            updateTaoBaoOrder(orderId);
 
        }
        return ReturnT.SUCCESS;
    }
 
    public void updateTaoBaoOrder(String orderId) {
        List<TaoBaoOrder> list = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
        //删除订单成功
        for (int i = 0; i < list.size(); i++) {
            //TODO 需要处理订单成功
            if (list.get(i).getOrderState().equalsIgnoreCase("订单成功")) {
                list.remove(i);
                i--;
            }
        }
 
 
        try {
            //如果创建时间超过了90天的抛出异常
            if (list.size() == 0 || TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss") < System.currentTimeMillis() - 1000 * 60 * 60L * 24 * 90)
                throw new Exception("需要本地更新");
            long startTime = TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss") - 1000L;
            long endTime = TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss") + 1000L;
            updateRelationAndSpecialOrderJob.updateRelationAndSpecialOrder(startTime, endTime);
            updateOrderJob.updateOrder(startTime, endTime);
        } catch (Exception e) {
            //从本地备份拿出订单
            List<String> tradeIds = new ArrayList<>();
            for (TaoBaoOrder order : list) {
                tradeIds.add(order.getTradeId());
            }
            List<TaoBaoOrderBackUp> orderBackUps = taoBaoOrderBackUpDao.listByIds(tradeIds);
            List<TaoBaoOrder> orderList = new ArrayList<>();
            for (TaoBaoOrderBackUp backUp : orderBackUps) {
                orderList.add(TaoKeOrderApiUtil.parseTaoBaoOrderNew(JSONObject.fromObject(backUp.getContent())));
            }
            updateRelationAndSpecialOrderJob.addRelationAndSpecialOrder(orderList);
            updateOrderJob.addOrder(orderList);
        }
    }
 
 
}