OpsClientService.java
3.4 KB
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
package com.viontech.fanxing.task.service;
import com.alibaba.fastjson.JSONObject;
import com.viontech.fanxing.commons.constant.LogType;
import com.viontech.fanxing.commons.exception.FanXingException;
import com.viontech.fanxing.commons.feign.OpsClient;
import com.viontech.fanxing.commons.model.Channel;
import com.viontech.fanxing.commons.model.Content;
import com.viontech.fanxing.commons.model.DictCode;
import com.viontech.fanxing.commons.vo.LogVo;
import com.viontech.keliu.util.JsonMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;
/**
* .
*
* @author 谢明辉
* @date 2021/9/28
*/
@Service
@Slf4j
public class OpsClientService {
@Resource
private OpsClient opsClient;
public String getStoreConfigById(Long id) {
JSONObject res = opsClient.getStoreConfigById(id);
if (!res.getBoolean("success")) {
throw new FanXingException(res.getString("msg"));
} else {
JSONObject data = res.getJSONObject("data");
if (data == null) {
return null;
} else {
return data.getString("config");
}
}
}
public String getContentByName(String name) {
JsonMessageUtil.JsonMessage<List<Content>> res = opsClient.getContentByName(name);
if (res.getCode() != 200) {
throw new FanXingException(res.getMsg());
} else {
List<Content> data = res.getData();
return (data == null || data.size() == 0) ? null : data.get(0).getContent();
}
}
public Channel getChannelByChannelUnid(String channelUnid) {
JsonMessageUtil.JsonMessage<List<Channel>> res = opsClient.getChannelByChannelUnid(channelUnid);
if (res.getCode() != 200) {
throw new FanXingException(res.getMsg());
} else {
List<Channel> data = res.getData();
return (data == null || data.size() == 0) ? null : data.get(0);
}
}
public DictCode getDictCodeByUnid(String unid) {
JsonMessageUtil.JsonMessage<List<DictCode>> res = opsClient.getDictCodeByUnid(unid);
if (res.getCode() != 200) {
throw new FanXingException(res.getMsg());
} else {
List<DictCode> data = res.getData();
return (data == null || data.size() == 0) ? null : data.get(0);
}
}
public void addLog(String content) {
try {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
String username = null;
if (requestAttributes != null) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
username = URLDecoder.decode(request.getHeader("username"), "utf8");
}
LogVo logVo = new LogVo();
logVo.setUsername(username);
logVo.setLogType(LogType.TASK.value);
logVo.setContent(content);
opsClient.addLog(logVo);
} catch (Exception e) {
log.error("添加日志失败:", e);
}
}
}