cy
2022-11-18 08013d9f35bdfaa462916716f5c77b438af874fb
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
185
186
package com.integrated.zyyt.enetity.business;
 
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import com.alibaba.fastjson.JSONObject;
import com.integrated.zyyt.enetity.TokenResult;
import com.integrated.zyyt.enetity.ZyytDataResult;
import com.integrated.zyyt.util.GetBeanUtil;
import com.integrated.zyyt.util.IdGenerator;
import com.integrated.zyyt.util.ZyytUtil;
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;
 
public class Zyyt<T> {
 
    private JdbcTemplate jdbcTemplate = GetBeanUtil.getBean(JdbcTemplate.class);
    //    private RedisService redisService = GetBeanUtil.getBean(RedisService.class);
//    private JdbcTemplate jdbcTemplate = null;
    // 替代redis
    private TimedCache<String, String> timedCache = CacheUtil.newTimedCache(DateUnit.MINUTE.getMillis() * 5);
 
 
    //    用于认证
    private String authonUrl;
    private String dataUrl;
    private boolean eciphrtext;
    private String client;
    private String secret;
 
    // 获取到的权限
//    private String bearToken;
 
    //    用于获取数据
    private String sourceSysKey;
    // 请求参数
    private JSONObject params;
    // 返回对象
    private List<T> queryData;
 
 
    private String getToken() {
        Class listType = this.getListType();
        String typeName = listType.getName();
        String businessName = typeName.substring(typeName.lastIndexOf(".") + 1);
        String redisKey = "zyytBear_" + businessName + "_";
 
        String bearStr = timedCache.get(redisKey);
        if (bearStr == null) {
            TokenResult tokenResult = ZyytUtil.getAuth(authonUrl, eciphrtext, client, secret,sourceSysKey);
            if (tokenResult == null) {
                return null;
            }
            bearStr = tokenResult.getToken();
            timedCache.put(redisKey, bearStr, DateUnit.MINUTE.getMillis() * 5);
        }
        return bearStr;
    }
 
    /**
     * 获取List内的泛型
     * <p>
     * //     * @param listFiledName
     *
     * @return
     */
    private Class getListType() {
        ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();
        Class clazz = (Class) ptClass.getActualTypeArguments()[0];
        return clazz;
    }
 
 
    public ZyytDataResult<T> getData(int pageNum, int pageSize) {
        String bearToken = this.getToken();
        Class listType = getListType();
        return ZyytUtil.getData(dataUrl, bearToken, sourceSysKey, params, pageNum, pageSize, listType);
 
    }
 
    /**
     * 插入记录表 返回记录Id
     *
     * @param tableName
     * @param batchNo
     * @return
     */
    public Long insertRecord(String tableName, String batchNo) {
        Long id = IdGenerator.getId();
        String insertSql = "INSERT INTO ZYYT_RECORD (id, BATCH_NO, COUNT_NUM, IS_FINISH, IS_ERROR, START_TIME, END_TIME, TABLE_NAME) " +
                "VALUES (" + id + ", '" + batchNo + "', '0', '0', '0', SYSDATE, null, '" + tableName + "')";
        jdbcTemplate.update(insertSql);
        return id;
    }
 
    /**
     * 更改记录表
     *
     * @param id
     * @param isError
     * @param countNum
     * @return
     */
    public int updateRecord(Long id, int isError, Long countNum, String errMsg) {
        String updateSql = "update ZYYT_RECORD set IS_FINISH=1, IS_ERROR = " + isError + ",COUNT_NUM=" + countNum + ",END_TIME=SYSDATE,err_msg='" + errMsg + "' where id = " + id;
        return jdbcTemplate.update(updateSql);
    }
 
    public Zyyt(String authonUrl,String dataUrl, boolean eciphrtext, String client, String secret, String sourceSysKey, JSONObject params) {
        this.authonUrl = authonUrl;
        this.dataUrl = dataUrl;
        this.eciphrtext = eciphrtext;
        this.client = client;
        this.secret = secret;
        this.sourceSysKey = sourceSysKey;
        this.params = params;
    }
 
    public String getAuthonUrl() {
        return authonUrl;
    }
 
    public void setAuthonUrl(String authonUrl) {
        this.authonUrl = authonUrl;
    }
 
    public String getDataUrl() {
        return dataUrl;
    }
 
    public void setDataUrl(String dataUrl) {
        this.dataUrl = dataUrl;
    }
 
    public boolean isEciphrtext() {
        return eciphrtext;
    }
 
    public void setEciphrtext(boolean eciphrtext) {
        this.eciphrtext = eciphrtext;
    }
 
    public String getClient() {
        return client;
    }
 
    public void setClient(String client) {
        this.client = client;
    }
 
    public String getSecret() {
        return secret;
    }
 
    public void setSecret(String secret) {
        this.secret = secret;
    }
 
    public String getSourceSysKey() {
        return sourceSysKey;
    }
 
    public void setSourceSysKey(String sourceSysKey) {
        this.sourceSysKey = sourceSysKey;
    }
 
    public Map getParams() {
        return params;
    }
 
    public void setParams(JSONObject params) {
        this.params = params;
    }
 
    public List<T> getQueryData() {
        return queryData;
    }
 
    public void setQueryData(List<T> queryData) {
        this.queryData = queryData;
    }
 
}