lbq
2 天以前 1f2ab3faafd2c0e06d4aceb4bf5a815235a32608
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.vincent.rsf.openApi.service.phyz.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.openApi.config.PlatformProperties;
import com.vincent.rsf.openApi.entity.dto.CommonResponse;
import com.vincent.rsf.openApi.entity.phyz.ErpReportParams;
import com.vincent.rsf.openApi.service.phyz.ErpReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Objects;
 
@Slf4j
@Service
public class ErpReportServiceImpl implements ErpReportService {
 
    private static String ERP_REPORT_URL;
 
    @Resource
    private PlatformProperties.ErpApi erpApi;
    @Resource
    private RestTemplate restTemplate;
 
 
    @PostConstruct
    public void init() {
        ERP_REPORT_URL = erpApi.getErpUrl();
    }
 
 
 
    // 登录参数依次为账套ID、用户名、应用ID、时间戳、签名信息、语言ID
    public void loginBySign() throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String url = ERP_REPORT_URL + "/Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginBySign.common.kdsvc";
        JSONObject params = new JSONObject();
        params.put("parameters", loginParams());
        JSONObject result = postRequest(url, params, false);
    }
 
 
    // 入/出库任务完成上报
    public CommonResponse reportInOrOutBound(Object params) {
        if (Objects.isNull(params)) {
            throw new CoolException("入/出库任务信息参数不能为空!!");
        }
 
        // TODO:参数转换
        ErpReportParams erpReportParams = new ErpReportParams();
        erpReportParams = (ErpReportParams) params;
 
        String erpUrl = ERP_REPORT_URL + "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc";
        log.info("Erp入/出库任务完成上报: {}, 请求参数: {}", erpUrl, JSONObject.toJSONString(erpReportParams));
        try {
            JSONObject jsonObject = postRequest(erpUrl, erpReportParams, true);
            boolean sendSuccess = jsonObject.getJSONObject("Result").getJSONObject("ResponseStatus").getBoolean("IsSuccess");
 
            // TODO:转换后返回
            if (sendSuccess) {
                return CommonResponse.ok();
            } else {
                JSONArray errors = jsonObject.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors");
                String errorMsg = "";
                for (int i = 0; i < errors.size(); i++) {
                    errorMsg += errors.getJSONObject(i).getString("Message") + " ";
                }
                return CommonResponse.error(errorMsg);
            }
        } catch (Exception e) {
            log.error("Erp入/出库任务上报响应失败", e);
            throw new CoolException("Erp解析响应失败:" + e.getMessage());
        }
    }
 
 
    // 其他入/出库主动上报
 
 
    // 盘点结果上报
 
 
 
 
 
 
    private Object[] loginParams() throws UnsupportedEncodingException, NoSuchAlgorithmException {
        //时间戳
        long timestamp = System.currentTimeMillis() / 1000;
        //数据中心ID
        String dbId = "69368c1051a322";
        //用户名称
        String userName = "楼坚伟";
        //第三方系统应用Id
        String appId = "330678_w2eBwdkp0oCW2XxuW16D4w/NRhTaTPKp";
        //第三方系统应用秘钥
        String appSecret = "31c9e5da6472456193e0c8a7dd2160d9";
        //将账套ID、用户名、应用ID、应用秘钥、时间戳 放到数组里面
        String[] arr = new String[] { dbId, userName, appId, appSecret, String.valueOf(timestamp) };
        //生成签名信息
        String sign = getSha256(arr);
 
        return new Object[] { dbId, userName, appId, String.valueOf(timestamp), sign,  2052};
    }
 
    public static String getSha256(String[] input) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchAlgorithmException {
        Arrays.sort(input);
        //SHA1加密的话改成MessageDigest.getInstance("SHA-1");
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        for (String str : input) {
            sha256.update(str.getBytes("UTF-8"));
        }
        byte[] hashBytes = sha256.digest();
        StringBuilder hashString = new StringBuilder();
        for (byte b : hashBytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hashString.append('0');
            }
            hashString.append(hex);
        }
        return hashString.toString();
    }
 
    /**
     * 通用HTTP POST请求方法
     *
     * @param url 请求URL
     * @param params 请求参数
     * @param needToken 是否需要token认证
     * @return 响应结果
     */
    public JSONObject postRequest(String url, Object params, boolean needToken) {
        if (StringUtils.isBlank(url)) {
            throw new CoolException("请求URL不能为空!!");
        }
        if (Objects.isNull(params)) {
            throw new CoolException("请求参数不能为空!!");
        }
 
        log.info("Erp POST请求: {}, 请求参数: {}", url, JSONObject.toJSONString(params));
 
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=utf-8");
 
        if (needToken) {
//            String token = getToken();
//            headers.add("Authorization", "Bearer " + token);
        }
 
        HttpEntity<Object> httpEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        log.info("Erp POST请求响应结果: {}", exchange);
 
        if (Objects.isNull(exchange.getBody())) {
            throw new CoolException("请求失败!!");
        }
 
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.coercionConfigDefaults().setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
            return objectMapper.readValue(exchange.getBody(), JSONObject.class);
        } catch (JsonProcessingException e) {
            log.error("Erp解析响应失败", e);
            throw new CoolException("Erp解析响应失败:" + e.getMessage());
        }
    }
}