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 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> entity = new HttpEntity<>(body, headers); log.info("RCS 任务状态通知 POST {} body={}", url, JSONObject.toJSONString(body)); ResponseEntity 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()); } } }