OpsClientService.java
3.22 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
package com.viontech.fanxing.task.service;
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.model.StoreConfig;
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.util.List;
/**
* .
*
* @author 谢明辉
* @date 2021/9/28
*/
@Service
@Slf4j
public class OpsClientService {
@Resource
private OpsClient opsClient;
public StoreConfig getStoreConfigById(Long id) {
JsonMessageUtil.JsonMessage<StoreConfig> res = opsClient.getStoreConfigById(id);
if (res.getCode() != 200) {
throw new FanXingException(res.getMsg());
} else {
return res.getData();
}
}
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 = request.getHeader("username");
}
LogVo logVo = new LogVo();
logVo.setUsername(username);
logVo.setLogType(LogType.TASK.value);
logVo.setContent(content);
opsClient.addLog(logVo);
} catch (Exception e) {
log.error("添加日志失败:", e);
}
}
}