wzp
2025-05-05 94ff46df856f6d4f8d8393e22001351c793f4e5b
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.ruoyi.quartz.task;
 
import com.ruoyi.common.utils.HttpUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IAOrderStatusService;
import com.ruoyi.system.service.IDispatchOrdService;
import com.ruoyi.system.service.ISysClientAppService;
import com.ruoyi.system.service.ITbOrdersService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    private static final Logger log = LoggerFactory.getLogger(RyTask.class);
    @Autowired
    private IAOrderStatusService orderStatusService;
 
    @Autowired
    private ITbOrdersService tbOrdersService;
 
    @Autowired
    private IDispatchOrdService dispatchOrdService;
 
    @Autowired
    private ISysClientAppService sysClientAppService;
 
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
    }
 
    public void ryParams(String params)
    {
        System.out.println("执行有参方法:" + params);
    }
 
    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }
 
    /**
     * 回调推送状态
     */
    public void ryOrderStatus() {
        try {
            List<AOrderStatus> orderStatusList = orderStatusService.selectAOrderStatusList();
            if (orderStatusList.size() < 0) {
                System.out.println("没有数据");
                return;
            }
            for (AOrderStatus orderStatus : orderStatusList) {
                int count = tbOrdersService.checkServiceOrdIDExists(orderStatus.getServiceOrdID());
                if (count > 0) {
                    System.out.println("服务订单ID存在:" + orderStatus.getServiceOrdID());
 
                    TbOrders tbOrders = tbOrdersService.selectTbOrdersByServiceOrdID(orderStatus.getServiceOrdID());
 
 
 
                    //获取车牌号
                    String carLicense = "";
                    StringBuilder driverNames = new StringBuilder();
                    StringBuilder driverMobiles = new StringBuilder();
                    try{
                        DispatchOrd dispatchOrd = dispatchOrdService.selectDispatchOrdByServiceOrdIDDt(orderStatus.getServiceOrdID());
                        if(dispatchOrd!=null){
                            carLicense = dispatchOrdService.selectCarLicenseByCarId(Integer.valueOf(dispatchOrd.getDispatchOrdCarID()));
 
                            String dispatchOAEntourage = dispatchOrd.getDispatchOrd_OAEntourage();
                            String dispatchOAName = dispatchOrd.getDispatchOrd_OAName();
 
                            if (dispatchOAEntourage != null && dispatchOAName != null) {
                                
                                String[] entourages = dispatchOAEntourage.split(",");
                                String[] names = dispatchOAName.split(",");
                                
                                // 查找所有司机信息
                                for (int i = 0; i < Math.min(entourages.length, names.length); i++) {
                                    if ("司机".equals(entourages[i].trim())) {
                                        String driverMobile = dispatchOrdService.selectDriverMobileByName(names[i].trim());
                                        if (driverNames.length() > 0) {
                                            driverNames.append(",");
                                        }
                                        driverNames.append(names[i].trim());
                                        if (driverMobiles.length() > 0) {
                                            driverMobiles.append(",");
                                        }
                                        driverMobiles.append(driverMobile);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        log.error("获取调度单异常:"+e.getMessage());
                    }
 
                    SysClientApp clientApp = sysClientAppService.selectSysClientAppByAppKey("appId1");
                    if(clientApp==null){
                        log.error("回调地址不存在");
                        return;
                    }
 
                    String callbackUrl = clientApp.getCallbackUrl();
                    try {
                        // 构建请求参数
                        Map<String, String> map = new HashMap<>();
                        map.put("orderID", tbOrders.getOrderID().toString());
                        map.put("thirdOrderNo", orderStatus.getServiceOrdID());
                        map.put("orderStatus","1");
                        map.put("orderRemark", orderStatus.getMsg());
                        map.put("plateNo",carLicense);
                        map.put("driverMobile",driverMobiles.toString());
                        map.put("driverName", driverNames.toString());
 
                        // 第一次尝试
                        String response = sendCallback(callbackUrl, map);
                        
                        if (response == null) {
                            // 如果第一次失败,等待3秒后重试
                            Thread.sleep(3000);
                            log.info("第一次回调失败,准备重试");
                            response = sendCallback(callbackUrl, map);
                        }
                        
                        if (response != null) {
                            // 更新状态
                            log.info("回调推送成功,参数如下:");
                            map.forEach((key, value) -> log.info("    {} : {}", key, value));
                            orderStatusService.updateAOrderStatusFlag(orderStatus.getId());
                        } else {
                            log.error("回调请求重试后仍然失败");
                            orderStatusService.updateAOrderStatusFlag(orderStatus.getId());
                        }
                    } catch (Exception e) {
                        log.error("回调请求失败: " + e.getMessage());
                        orderStatusService.updateAOrderStatusFlag(orderStatus.getId());
                    }
                }
                else {
                    log.info("服务订单ID不存在:" + orderStatus.getServiceOrdID());
                    orderStatusService.updateAOrderStatusFlag(orderStatus.getId());
                }
            }
        } catch (Exception e) {
            log.error("执行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
 
    // 新增发送回调的方法
    private String sendCallback(String callbackUrl, Map<String, String> params) {
        try {
            // 打印请求参数
            log.info("发送回调请求,URL: " + callbackUrl);
            log.info("请求参数: " + params);
 
            // 发送请求并获取响应
            String response = HttpUtil.post(callbackUrl, params);
 
            // 打印响应结果
            log.info("回调响应结果: " + response);
            
            return response;
        } catch (Exception e) {
            log.error("回调请求异常: " + e.getMessage());
            return null;
        }
    }
}