kimi
2020-05-18 c8aee7b9bfd79cfd741d7e5692520f4f51a31a86
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
package com.highdatas.mdm.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.highdatas.mdm.entity.SysMenu;
import com.highdatas.mdm.pojo.Operate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
 
@ConfigurationProperties(prefix = "notice")
@Component
public class NoticeClient {
 
    @Value("${notice.url}")
    String url;
 
    String prefix ;
 
    private NoticeClient() {
        this.prefix = "/api/datacvg/msg/";
    }
 
 
    public boolean EditMenuMapping(SysMenu menuByTableName, SysMenu menu, String userId) {
        if (menu == null) {
            String content = MessageFormat.format("编辑{0}主数据信息", menuByTableName.getName());
            String theme = MessageFormat.format("{0}主数据",  menuByTableName.getName());
            return  open(content, theme, userId, "MAIN_001_001_007");
        }
        String content = MessageFormat.format("编辑{0}主题下的{1}主数据信息", menu.getName(), menuByTableName.getName());
        String theme = MessageFormat.format("{0}主数据",  menuByTableName.getName());
        return  open(content, theme, userId,"MAIN_001_001_007");
    }
 
    public boolean EditMasterData(SysMenu menuByTableName, SysMenu menu, String userId, Operate operate) {
        if (menu == null) {
            String content = MessageFormat.format("{1}主数据" + operate.getLogMessage(),1, menuByTableName.getName());
            String theme = MessageFormat.format("{0}主数据",  menuByTableName.getName());
            return  open(content, theme, userId, "MAIN_001_001_002_002");
        }
        String content = MessageFormat.format("{1}主题下的{2}主数据" + operate.getLogMessage(),1, menu.getName(), menuByTableName.getName());
        String theme = MessageFormat.format("{0}主数据",  menuByTableName.getName());
        return  open(content, theme, userId,"MAIN_001_001_002_002");
    }
 
    public boolean openByAddMenu(SysMenu menuByTableName, SysMenu menu, String userId) {
        if (menu == null) {
            String content = MessageFormat.format("{0}主数据发布",  menuByTableName.getName());
            String theme = MessageFormat.format("{0}主数据",  menuByTableName.getName());
            return  open(content, theme, userId, "MAIN_001_001_001");
        }
        String content = MessageFormat.format("{0}主题下的{1}主数据发布", menu.getName(), menuByTableName.getName());
        String theme = MessageFormat.format("{0}主数据",  menuByTableName.getName());
        return  open(content, theme, userId, "MAIN_001_001_001");
    }
 
    public boolean open(String msg, String theme, String userId, String code) {
            String url =  this.url + prefix + "sendMsg";
 
        HashMap<String, Object> params = new LinkedHashMap<>();
            params.put("module","主数据管理");
            params.put("userId",userId);
            params.put("msgContent", msg);
            params.put("msgTheme", theme);
            params.put("detailCode", code);
 
            String s = HttpRestClient(url, HttpMethod.POST, params, null, MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return false;
            }else {
                return true;
            }
    }
 
    public String HttpRestClient(String url, HttpMethod method, HashMap<String, Object> formParams, String getParams,  MediaType mediaType) {
        if (!StringUtils.isEmpty(getParams)) {
            url = url + Constant.QUESTION + getParams;
        }
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10*1000);
        requestFactory.setReadTimeout(10*1000);
        RestTemplate client = new RestTemplate(requestFactory);
        client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        if (mediaType == null){
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        }else {
            headers.setContentType(mediaType);
        }
 
        HttpEntity<HashMap<String, Object>> requestEntity = new HttpEntity<HashMap<String, Object>>(formParams, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
 
        return response.getBody();
    }
}