yanderong
2026-06-09 b19474186df25762866bd9323c3f3ac8be4de9b9
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.config.properties.PushEvaluationProperties;
import com.stylefeng.guns.modular.system.dao.PushEvaluationLogMapper;
import com.stylefeng.guns.modular.system.dto.PushEvaluationRequestDTO;
import com.stylefeng.guns.modular.system.dto.PushEvaluationResponseDTO;
import com.stylefeng.guns.modular.system.model.PushEvaluationLog;
import com.stylefeng.guns.modular.system.service.IPushEvaluationService;
import com.stylefeng.guns.modular.system.util.SignUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
 
import java.util.*;
 
/**
 * 上报数据到考评系统
 *
 * @author Lux
 * @version 1.0.0
 * @since 2026/6/5 11:29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PushEvaluationImpl extends ServiceImpl<PushEvaluationLogMapper, PushEvaluationLog> implements IPushEvaluationService {
 
    private final PushEvaluationProperties properties;
    private final RestTemplate restTemplate;
    private final static String API_PATH = "/api/taxiOrder/evaluation";
 
    /**
     * 推送数据到考评系统
     */
    @Transactional
    public void pushEvaluation(String startTime, String endTime) {
        // 获取出租车需要推送的数据
        List<Map<String, Object>>  mapList = this.baseMapper.getTaxiOrderList(startTime, endTime);
        List<PushEvaluationRequestDTO> taxiRequestList = PushEvaluationRequestDTO.convert(mapList);
        if(CollectionUtils.isNotEmpty(taxiRequestList)) {
            PushEvaluationLog evaluationLog = requestEvaluation(JSON.toJSONString(taxiRequestList));
            evaluationLog.setSerialNumber("taxi_"+startTime+"_"+endTime);
            // 记录日志
            this.insert(evaluationLog);
        }
        // 获取专车需要推送的数据
        List<Map<String, Object>>  mapPrivateList = this.baseMapper.getPrivateCarOrderList(startTime, endTime);
        List<PushEvaluationRequestDTO> privateRequestList = PushEvaluationRequestDTO.convert(mapPrivateList);
        if(CollectionUtils.isNotEmpty(privateRequestList)) {
            PushEvaluationLog evaluationLog = requestEvaluation(JSON.toJSONString(privateRequestList));
            evaluationLog.setSerialNumber("privateCar_"+startTime+"_"+endTime);
            // 记录日志
            this.insert(evaluationLog);
        }
 
 
    }
 
    public PushEvaluationLog requestEvaluation(String requestData) {
        // 构建请求头
        HttpHeaders headers = buildHeaders();
        String url = properties.getUrl() + API_PATH;
        PushEvaluationResponseDTO response = null;
        int status = 1;
        String errorMessage = "";
        try {
            // 发送请求
            HttpEntity<String> entity = new HttpEntity<>(requestData, headers);
            log.info("上报数据到考评系统-请求地址为:{},请求参数为 : {}", url, JSON.toJSONString(entity));
            ResponseEntity<PushEvaluationResponseDTO> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, PushEvaluationResponseDTO.class);
            // 返回数据
            response = responseEntity.getBody();
            if (Objects.isNull(response)) {
                status = 0;
                log.error("上报数据到考评系统-调用接口-失败,返回为空");
            } else if(response.getCode() != 200 ) {
                status = 0;
                log.error("上报数据到考评系统-调用接口-失败,返回失败数据为:{}", JSON.toJSONString(response));
            }
            log.info("上报数据到考评系统-调用接口-成功-请求URL:{},返回数据为:{}", url, JSON.toJSONString(response));
        } catch (Exception e) {
            status = 0;
            errorMessage = e.getLocalizedMessage();
            log.error("上报数据到考评系统-报错:", e);
        }
 
        PushEvaluationLog evaluationLog = new PushEvaluationLog();
        evaluationLog.setRequestUrl(url);
        evaluationLog.setRequestHeader(JSON.toJSONString(headers));
        evaluationLog.setRequestBody(requestData);
        Optional.ofNullable(response).map(JSON::toJSONString).ifPresent( evaluationLog::setResponseBody);
        evaluationLog.setStatus(status);
        evaluationLog.setRetryCount(0);
        evaluationLog.setMessage(errorMessage);
        evaluationLog.setCreateTime(new Date());
        evaluationLog.setUpdateTime(new Date());
        return evaluationLog;
    }
 
    /**
     * 构建请求头
     */
    public HttpHeaders buildHeaders() {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000); // 时间戳
        String nonce = RandomStringUtils.randomNumeric(6); // 6位随机数
        String sign = SignUtil.generateSign("POST", API_PATH, properties.getAccessKey(), properties.getAccessSecret(), timestamp, nonce);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("accessKey", properties.getAccessKey());
        headers.set("timestamp", timestamp);
        headers.set("nonce", nonce);
        headers.set("sign", sign);
        return headers;
    }
}