zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package com.changhong.epc.zuul.filter.version;
 
import com.changhong.epc.constter.base.Context;
import com.changhong.epc.rely.api.epc.tenant.NormApi;
import com.changhong.epc.rely.api.tool.FilterTool;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import com.iemsoft.framework.cloud.zuul.tool.RequestContextTool;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import com.netflix.zuul.http.ServletInputStreamWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.util.StreamUtils;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
public class NormVersion implements Version{
 
    @Override
    public String getUpdateUrl() {
        return "epc/epc-tenant/tenant/normType/insert";
    }
 
    @Override
    public String getUpdateKey() {
        return getUpdateKey(RequestContextTool.getCurrentContext().getRequest());
    }
 
    protected String getUpdateKey(HttpServletRequest request) {
        FilterTool.FILTER_TYPE.get(FilterTool.FilterType.PRIVATE).filter(request);
        Integer cId = getCid(request);
        String key = String.format("version_norm_%s_%s_%s"
                , Context.getTenantId()
                , Context.getCompanyId()
                , cId);
        return key;
    }
 
    private Integer getCid(HttpServletRequest request) {
        RequestContext context = RequestContextTool.getCurrentContext();
 
        try (InputStream in = context.getRequest().getInputStream()) {
            String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
 
            final byte[] reqBodyBytes = body.getBytes();
            context.setRequest(new HttpServletRequestWrapper(context.getRequest()) {
                @Override
                public ServletInputStream getInputStream(){
                    return new ServletInputStreamWrapper(reqBodyBytes);
                }
 
                @Override
                public int getContentLength() {
                    return reqBodyBytes.length;
                }
 
                @Override
                public long getContentLengthLong() {
                    return reqBodyBytes.length;
                }
            });
            Map<String, Object> bodyObj = JSONTool.toObj(body, Map.class);
            if(ObjectUtil.empty(bodyObj) || ObjectUtil.empty(bodyObj.get("suppStanderExtend"))){
                return null;
            }
            List<Map<String, Object>> suppStanderExtend = (List<Map<String, Object>>)bodyObj.get("suppStanderExtend");
            return NumberUtils.createInteger(Objects.toString(suppStanderExtend.get(0).get("ctId"), "0"));
        } catch (IOException e) {
            return null;
        }
    }
 
    @Override
    public String getCacheKey(HttpServletRequest request, String key) {
        String cacheKey = String.format("version_norm_%s_%s_%s"
                , Context.getTenantId()
                , Context.getCompanyId()
                , key);
        return cacheKey;
    }
 
 
    @Override
    public Set<String> getKeys() {
        return SpringUtil.getBean(NormApi.class).getAllNormCostType()
                .stream()
                .map(costType -> Objects.toString(costType.getId(), "")).collect(Collectors.toSet());
    }
 
}