cl
17 小时以前 43d6ae86bc229a1a75637fae33be378e105016e3
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
package com.vincent.rsf.server.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.vincent.rsf.server.api.config.RemotesInfoProperties;
import com.vincent.rsf.server.api.entity.CommonResponse;
import com.vincent.rsf.server.api.entity.constant.RcsConstant;
import com.vincent.rsf.server.api.service.RcsBusTaskNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.Map;
 
/** 管理后台工作档完成/取消时通知 RCS */
@Slf4j
@Service
public class RcsBusTaskNoticeServiceImpl implements RcsBusTaskNoticeService {
 
    @Autowired(required = false)
    private RemotesInfoProperties.RcsApi rcsApi;
    @Autowired(required = false)
    private RestTemplate restTemplate;
 
    @Override
    public void notifyTaskStatus(String taskNo, Integer status) {
        if (StringUtils.isBlank(taskNo) || status == null) {
            return;
        }
        if (rcsApi == null || StringUtils.isBlank(rcsApi.getHost()) || StringUtils.isBlank(rcsApi.getPort())) {
            log.debug("跳过 RCS 任务状态通知:未配置 platform.rcs");
            return;
        }
        if (restTemplate == null) {
            log.warn("跳过 RCS 任务状态通知:RestTemplate 未注入");
            return;
        }
        String url = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.TASK_STATUS_NOTICE;
        Map<String, String> body = new HashMap<>(2);
        body.put("taskNo", taskNo);
        body.put("status", String.valueOf(status));
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            headers.add("api-version", "v2.0");
            HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
            log.info("RCS 任务状态通知 POST {} body={}", url, JSONObject.toJSONString(body));
            ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
            if (exchange.getBody() == null) {
                log.warn("RCS 任务状态通知响应体为空 taskNo={}", taskNo);
                return;
            }
            ObjectMapper om = new ObjectMapper();
            om.coercionConfigDefaults().setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
            CommonResponse res = om.readValue(exchange.getBody(), CommonResponse.class);
            if (res.getCode() != null && res.getCode() == 200) {
                log.info("RCS 任务状态通知成功 taskNo={} status={}", taskNo, status);
            } else {
                log.warn("RCS 任务状态通知非成功 taskNo={} status={} code={} msg={}",
                        taskNo, status, res.getCode(), res.getMsg());
            }
        } catch (Exception e) {
            log.warn("RCS 任务状态通知异常 taskNo={} status={}:{}", taskNo, status, e.getMessage());
        }
    }
}