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;
|
}
|
|
}
|