wlzboy
2025-09-20 b62bc392f3c1658381107be1d5f737a3389e7f5f
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.ruoyi.common.utils.civilAviation;
 
 
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.ServiceOrderAppVo;
import com.ruoyi.common.core.domain.entity.ServiceOrderAppResultVo;
import com.ruoyi.common.core.domain.entity.ServiceOrderDelResultVo;
import com.ruoyi.common.core.domain.entity.ServiceOrderDelVo;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
 
/**
 * 民航医疗接口处理
 */
@Component
public class ServiceOrderUtil {
    private static final Logger log = LoggerFactory.getLogger(ServiceOrderUtil.class);
    private static String addServiceOrederAPI;
 
    @Value("${min.apiUrl}")
    public void setAddServiceOrederAPI(String apiUrl) {
        ServiceOrderUtil.addServiceOrederAPI = apiUrl;
    }
 
    public static String getAddServiceOrederAPI() {
        return addServiceOrederAPI;
    }
 
    /**
     * 创建服务单
     *
     * @param model 服务单应用值对象
     * @return 服务单接口调用结果
     */
    public static ServiceOrderAppResultVo CreateServiceOrder(ServiceOrderAppVo model,String appSecret) {
        if (addServiceOrederAPI.isEmpty()) {
            // log.info("AddServiceOrederAPI 未设置,请先设置接口地址。");
            return null;
        }
 
        log.info("接口地址:"+addServiceOrederAPI);
 
        //计算时间戳和签名
        long unixTime = System.currentTimeMillis() / 1000; // 获取当前时间戳(秒)  Long.valueOf("1746444543") ;//
        model.setUnixTime(unixTime); // 设置时间戳到模型中
 
        // 将 model 转换为 Map<String, String>
        Map<String, String> params = new HashMap<>();
        java.lang.reflect.Field[] fields = model.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(model);
                if (value != null) {
 
                    // 将字段名首字母大写
                    String fieldName = field.getName();
 
                    String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    if(fieldName.contains("method"))
                    {
                        capitalizedFieldName = fieldName;
                    }
 
                    params.put(capitalizedFieldName,URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.toString()));
                }
            } catch (IllegalAccessException e) {
                // log.error("获取对象属性值时出错", e);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
 
        model.setSign(ServiceOrderSign.generateSign(params,appSecret)); // 生成签名并设置到模型中
 
        log.info("Sign值="+model.getSign());
 
        // 替换为
        StringBuilder formData = new StringBuilder();
        java.lang.reflect.Field[] fieldsQuest = model.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : fieldsQuest) {
            field.setAccessible(true);
            try {
                Object value = field.get(model);
                if (value != null) {
                    if (formData.length() > 0) {
                        formData.append("&");
                    }
                    // 将字段名首字母大写
                    String fieldName = field.getName();
 
                    String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
 
                    if(fieldName.contains("method"))
                    {
                        capitalizedFieldName = fieldName;
                    }
 
                    formData.append(capitalizedFieldName)
                            .append("=")
                            .append(URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.toString()));
                }
            } catch (Exception e) {
                // log.error("拼接表单数据时出错", e);
            }
        }
 
        log.info("请求参数="+formData.toString());
 
        String responseData = HttpUtils.sendPost(addServiceOrederAPI, formData.toString(), "application/x-www-form-urlencoded");
 
 
        if (responseData != null && !responseData.isEmpty()) {
            // log.info("接口响应: " + responseData);
            try {
                // 使用 FastJSON 将响应数据转换为 ServiceOrderAppResultVo 对象
                return JSON.parseObject(responseData, ServiceOrderAppResultVo.class);
            } catch (Exception e) {
                // log.info("解析接口响应数据失败: " + e.getMessage());
            }
        } else {
            // log.info("接口调用失败,未获取到有效响应数据");
        }
 
        return null;
    }
 
 
    /**
     * 取消服务单
     *
     * @param model 服务订单删除请求值对象
     * @return 服务订单删除接口调用结果
     */
    public static ServiceOrderDelResultVo CancelServiceOrder(ServiceOrderDelVo model,String appSecret) {
        if (addServiceOrederAPI.isEmpty()) {
            // log.info("AddServiceOrederAPI 未设置,请先设置接口地址。");
            return null;
        }
 
        // 计算时间戳和签名
        long unixTime = System.currentTimeMillis() / 1000;
        model.setUnixTime(unixTime);
 
        // 将 model 转换为 Map<String, String>
        Map<String, String> params = new HashMap<>();
        java.lang.reflect.Field[] fields = model.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(model);
                if (value != null) {
                    // 将字段名首字母大写
                    String fieldName = field.getName();
                    String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    if(fieldName.contains("method"))
                    {
                        capitalizedFieldName = fieldName;
                    }
                    params.put(capitalizedFieldName,URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.toString()));
                }
            } catch (IllegalAccessException e) {
                // log.error("获取对象属性值时出错", e);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
 
        model.setSign(ServiceOrderSign.generateSign(params,appSecret));
 
        // 构建表单数据
        StringBuilder formData = new StringBuilder();
        java.lang.reflect.Field[] fieldsQuest = model.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : fieldsQuest) {
            field.setAccessible(true);
            try {
                Object value = field.get(model);
                if (value != null) {
                    if (formData.length() > 0) {
                        formData.append("&");
                    }
                    // 将字段名首字母大写
                    String fieldName = field.getName();
                    String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    if(fieldName.contains("method"))
                    {
                        capitalizedFieldName = fieldName;
                    }
                    formData.append(capitalizedFieldName)
                            .append("=")
                            .append(URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.toString()));
                }
            } catch (Exception e) {
                // log.error("拼接表单数据时出错", e);
            }
        }
 
        String responseData = HttpUtils.sendPost(addServiceOrederAPI, formData.toString(), "application/x-www-form-urlencoded");
 
        if (responseData != null && !responseData.isEmpty()) {
            // log.info("接口响应: " + responseData);
            try {
                return JSON.parseObject(responseData, ServiceOrderDelResultVo.class);
            } catch (Exception e) {
                // log.info("解析接口响应数据失败: " + e.getMessage());
            }
        } else {
            // log.info("接口调用失败,未获取到有效响应数据");
        }
 
        return null;
    }
 
 
    public static void main(String[] args) {
        // 创建服务单应用值对象并设置相关模拟属性
        test();
 
//        testCancelServiceOrder();
    }
 
    //测试取消服务单
    public static void testCancelServiceOrder() {
        // 创建服务单应用值对象并设置相关模拟属性
        ServiceOrderDelVo model = new ServiceOrderDelVo();
        model.setMethod("ServiceOrder_DEL"); // 设置接口名称
        model.setAPPID("GDS-000001"); // 设置商户 APPID
        model.setServiceOrdID("1016302854"); // 设置服务单ID
        model.setDELRemarks("测试");
 
        // 调用 CancelServiceOrder 方法取消服务单
//        ServiceOrderAppResultVo result = CancelServiceOrder(model);
//        System.out.println("1");
    }
 
    public static  void test()
    {
        ServiceOrderAppVo model = new ServiceOrderAppVo();
         model.setMethod("ServiceOrder_APP"); // 设置接口名称
         model.setAppId("GDS-000001"); // 设置商户 APPID
         model.setOrdType(2);
         model.setCoPhone("13" + (int) (Math.random() * 900000000 + 100000000)); // 随机生成客户联系电话
         model.setCoName("客户" + (int) (Math.random() * 100)); // 随机生成客户姓名
         model.setCoTies("家属"); // 随机设置客户联系人与患者关系
        // 设置预约时间为一天后
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 1); // 增加一天
 
        String testTime = "2025-05-03%2021:33";
        model.setApptDate(testTime); // 设置预约运送时间为明天
         model.setPtName("患者姓名" + (int) (Math.random() * 100)); // 随机生成患者姓名
         model.setPtAge((int) (Math.random() * 100) + "岁"); // 随机生成患者年龄
         model.setPtSex(Math.random() > 0.5 ? "男" : "女"); // 随机生成患者性别
         model.setPtNat("中国"); // 随机设置患者国籍
         model.setOutHosp("广州天河"); // 随机设置转出医院
         model.setInHosp("广州海珠"); // 随机设置转入医院
         model.setPtServices("科室" + (int) (Math.random() * 10)); // 随机设置患者所在科室
         model.setPtDiagnosis("随机诊断信息"); // 随机设置诊断信息
         model.setCondition("随机备注信息"); // 随机设置备注信息
         model.setDoctor("医生" + (int) (Math.random() * 10)); // 随机设置患者医生
         model.setDoctorPhone("13" + (int) (Math.random() * 900000000 + 100000000)); // 随机生成患者医生电话
         model.setOfferPrice(Math.random() * 1000); // 随机生成报价
         model.setReferrals("推介人" + (int) (Math.random() * 10)); // 随机设置推介人信息
         model.setUnitRemarks("商户随机备注信息"); // 随机设置商户备注
         model.setPayQRcodeURL("https://example.com/qrcode/" + (int) (Math.random() * 100)); // 随机生成外部支付二维码链接
 
         // 调用 CreateServiceOrder 方法创建服务单
//         ServiceOrderAppResultVo result = CreateServiceOrder(model);
    }
    
}