use of com.itrus.portal.db.EvidenceCallback in project portal by ixinportal.
the class EvidenceCallbackTask method init.
public void init() {
try {
EvidenceCallbackExample callbackExample = new EvidenceCallbackExample();
EvidenceCallbackExample.Criteria criteria = callbackExample.createCriteria();
criteria.andCounntLessThan(3);
List<EvidenceCallback> callbacks = callbackService.selectAll(callbackExample);
if (callbacks == null || callbacks.isEmpty()) {
log.error("暂无需系统存证回调");
return;
}
String url = null;
String app_id = null;
String secret_key = null;
// 需要回执报告
Map<Long, String> dataRet = new HashMap<Long, String>();
Map<Long, List<String>> data = new HashMap<Long, List<String>>();
Map<String, EvidenceCallback> cacheCallBackBySnMap = new HashMap<>();
for (int i = 0; i < callbacks.size() && i < 1000; i++) {
EvidenceCallback callback = callbacks.get(i);
cacheCallBackBySnMap.put(callback.getEvidenceSn(), callback);
if (callback.getAppServiceId() == null) {
if (data.containsKey(callback.getAppId())) {
data.get(callback.getAppId()).add(callback.getEvidenceSn());
} else {
List<String> sns = new ArrayList<String>();
sns.add(callback.getEvidenceSn());
data.put(callback.getAppId(), sns);
}
} else {
dataRet.put(callback.getAppId(), callback.getEvidenceSn());
}
}
// <---------------------------- 处理data start ------------------------------>
for (Map.Entry<Long, List<String>> entry : data.entrySet()) {
ApplicationInfo applicationInfo = CacheCustomer.getAPP_INFO_MAP_BY_ID().get(entry.getKey());
if (applicationInfo == null) {
applicationInfo = infoService.selectById(entry.getKey());
}
if (applicationInfo.getAppId() != app_id || url == null || app_id == null || secret_key == null) {
if (StringUtils.isEmpty(applicationInfo.getCallback()) || StringUtils.isEmpty(applicationInfo.getAppId()) || StringUtils.isEmpty(applicationInfo.getSecretKey())) {
log.error("回调失败:缺少配置。");
return;
}
if (url == null) {
url = applicationInfo.getCallback().trim();
}
if (app_id == null) {
app_id = applicationInfo.getAppId();
}
if (secret_key == null) {
secret_key = applicationInfo.getSecretKey();
}
log.error("EvidenceQueryQueue : URL=" + url + ",APP_ID=" + app_id + ",SECRET_KEY=" + secret_key);
if (StringUtils.isEmpty(url) || StringUtils.isEmpty(app_id) || StringUtils.isEmpty(secret_key)) {
log.error("回调失败:缺少配置。");
return;
}
}
Map<String, Object> param = new HashMap<String, Object>();
param.put("status", 1);
param.put("message", "存证成功");
param.put("appId", app_id);
param.put("evidenceSns", JSONObject.toJSONString(entry.getValue()));
String header = "HMAC-SHA1 " + Base64.encode(HMACSHA1.getHmacSHA1(app_id + entry.getValue(), secret_key), false);
String retStr = OkHttpClientManagerCallback.post(url, header, param);
JSONObject ret_data = JSONObject.parseObject(retStr);
if (retStr == null || ret_data.getInteger("status") != 1) {
log.error("EvidenceCallbackTask failure count [" + entry.getValue().size() + "]");
for (String sn : entry.getValue()) {
EvidenceCallback callback = null;
if (cacheCallBackBySnMap.get(sn) == null) {
EvidenceCallbackExample callbackExample0 = new EvidenceCallbackExample();
EvidenceCallbackExample.Criteria criteria0 = callbackExample.createCriteria();
criteria0.andEvidenceSnEqualTo(sn);
callback = callbackService.selectAll(callbackExample0).get(0);
} else {
callback = cacheCallBackBySnMap.get(sn);
}
int counnt = callback.getCounnt() + 1;
if (counnt == 3) {
sqlSession.delete("com.itrus.portal.db.EvidenceCallbackMapper.deleteByPrimaryKey", callback.getId());
} else {
callback.setCounnt(counnt);
sqlSession.update("com.itrus.portal.db.EvidenceCallbackMapper.updateByPrimaryKeySelective", callback);
}
}
} else {
log.error("EvidenceCallbackTask success count [" + entry.getValue().size() + "]");
sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updates", entry.getValue().toArray());
EvidenceCallbackExample callbackExample1 = new EvidenceCallbackExample();
EvidenceCallbackExample.Criteria criteria1 = callbackExample.createCriteria();
criteria1.andEvidenceSnIn(entry.getValue());
sqlSession.delete("com.itrus.portal.db.EvidenceCallbackMapper.deleteByExample", callbackExample1);
// for(String sn:entry.getValue()) {
// EvidenceBasicInformation basicInformation = informationService
// .getInformationBySn(sn, null);
// basicInformation.setIsCallback(1);
// sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updateByPrimaryKeySelective",
// basicInformation);
//
// EvidenceCallbackExample callbackExample1 = new EvidenceCallbackExample();
// EvidenceCallbackExample.Criteria criteria1 = callbackExample.createCriteria();
// criteria1.andEvidenceSnEqualTo(sn);
// sqlSession.delete("com.itrus.portal.db.EvidenceCallbackMapper.deleteByExample",
// callbackExample1);
//
// }
}
}
// <---------------------------- 处理dataRet start ------------------------------>
for (Entry<Long, String> entry : dataRet.entrySet()) {
ApplicationInfo applicationInfo = CacheCustomer.getAPP_INFO_MAP_BY_ID().get(entry.getKey());
if (applicationInfo == null) {
applicationInfo = infoService.selectById(entry.getKey());
}
if (applicationInfo.getAppId() != app_id || url == null || app_id == null || secret_key == null) {
if (StringUtils.isEmpty(applicationInfo.getCallback()) || StringUtils.isEmpty(applicationInfo.getAppId()) || StringUtils.isEmpty(applicationInfo.getSecretKey())) {
log.error("回调失败:缺少配置。");
return;
}
if (url == null) {
url = applicationInfo.getCallback().trim();
}
if (app_id == null) {
app_id = applicationInfo.getAppId();
}
if (secret_key == null) {
secret_key = applicationInfo.getSecretKey();
}
log.error("EvidenceQueryQueue : URL=" + url + ",APP_ID=" + app_id + ",SECRET_KEY=" + secret_key);
if (StringUtils.isEmpty(url) || StringUtils.isEmpty(app_id) || StringUtils.isEmpty(secret_key)) {
log.error("回调失败:缺少配置。");
return;
}
}
EvidenceBasicInformation basicInformation = informationService.getInformationBySn(entry.getValue(), null);
// 得到证据回执base64
String reportBase64 = null;
// reportBase64 = EvidenceSaveServiceApi.cacheReportBySnMap.get(basicInformation.getEvidenceSn());
if (StringUtils.isEmpty(reportBase64)) {
// 获取原子服务地址 1.实名认证 2.密码服务 3.时间戳
RealNameAuthentication realNameAuthentication = CacheCustomer.getAUTH_CONFIG_MAP().get(2);
if (realNameAuthentication == null) {
try {
realNameAuthentication = realNameAuthenticationSerivce.getRealNameAuthenticationByTwo();
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取回执报告附件表信息
EvidenceEnclosureExample enclosureExample = new EvidenceEnclosureExample();
EvidenceEnclosureExample.Criteria ec = enclosureExample.createCriteria();
ec.andEvidenceSnEqualTo(basicInformation.getEvidenceSn());
ec.andPdfTypeEqualTo("3");
List<EvidenceEnclosure> enclosures = sqlSession.selectList("com.itrus.portal.db.EvidenceEnclosureMapper.selectByExample", enclosureExample);
if (enclosures != null && !enclosures.isEmpty()) {
EvidenceEnclosure enclosure = enclosures.get(0);
// 调解密下载接口从原子服务拿到回执报告base64
reportBase64 = EvidenceSaveServiceApi.decryptedAndDownload(sqlSession, enclosure.getBuid(), realNameAuthentication.getRealNameddress());
} else {
// 调用生成回执报告方法
Map<String, Object> mapRet = null;
mapRet = reportTemplate.returnreceipt(basicInformation.getEvidenceSn(), applicationInfo, null);
if (mapRet.get("status") != null && (int) mapRet.get("status") == 0) {
// 修改证据基本信息回执状态为已出
reportBase64 = (String) mapRet.get("pdfBase64");
basicInformation.setReceiptStatus(1);
sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updateByPrimaryKey", basicInformation);
}
}
} else {
// EvidenceSaveServiceApi.cacheReportBySnMap.remove(basicInformation.getEvidenceSn());
}
Map<String, Object> param = new HashMap<String, Object>();
param.put("status", 1);
param.put("appId", app_id);
param.put("evidenceSn", basicInformation.getEvidenceSn());
if (reportBase64 == null) {
param.put("message", "存证成功,回执报告生成失败!");
} else {
param.put("message", "存证成功");
param.put("reportBase64", reportBase64);
}
String header = "HMAC-SHA1 " + Base64.encode(HMACSHA1.getHmacSHA1(app_id + basicInformation.getEvidenceSn(), secret_key), false);
String retStr = OkHttpClientManagerCallback.post(url, header, param);
JSONObject ret_data = JSONObject.parseObject(retStr);
String dataSn = basicInformation.getEvidenceSn();
Long appId = entry.getKey();
// 判断是否成功 true ——> 失败
if (retStr == null || ret_data.getInteger("status") != 1) {
EvidenceCallback callback = null;
if (cacheCallBackBySnMap.get(dataSn) == null) {
EvidenceCallbackExample callbackExample0 = new EvidenceCallbackExample();
EvidenceCallbackExample.Criteria criteria0 = callbackExample.createCriteria();
criteria0.andEvidenceSnEqualTo(dataSn);
callback = callbackService.selectAll(callbackExample0).get(0);
} else {
callback = cacheCallBackBySnMap.get(dataSn);
}
int counnt = callback.getCounnt() + 1;
if (counnt == 3) {
sqlSession.delete("com.itrus.portal.db.EvidenceCallbackMapper.deleteByPrimaryKey", callback.getId());
} else {
callback.setCounnt(counnt);
sqlSession.update("com.itrus.portal.db.EvidenceCallbackMapper.updateByPrimaryKeySelective", callback);
}
} else {
List<String> sns = new ArrayList<>();
sns.add(dataSn);
sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updates", sns.toArray());
EvidenceCallbackExample callbackExample1 = new EvidenceCallbackExample();
EvidenceCallbackExample.Criteria criteria1 = callbackExample.createCriteria();
criteria1.andEvidenceSnIn(sns);
sqlSession.delete("com.itrus.portal.db.EvidenceCallbackMapper.deleteByExample", callbackExample1);
}
}
// <---------------------------- 处理dataRet end ------------------------------>
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
LogUtil.evidencelog(sqlSession, null, "存证回调接口", "失败原因:" + e.toString());
}
}
use of com.itrus.portal.db.EvidenceCallback in project portal by ixinportal.
the class EvidenceQueryQueue method run.
@Override
public void run() {
// 回调
while (true) {
if (OBJECT_QUEUE.isEmpty()) {
try {
// 若队列中没有信息则等待10秒
sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
String url = null;
String app_id = null;
String secret_key = null;
// 需要回执报告 // TODO: 2018/3/15 有问题
Map<Long, EvidenceBasicInformation> dataRet = new HashMap<Long, EvidenceBasicInformation>();
// 不需要回执报告
Map<Long, List<String>> data = new HashMap<Long, List<String>>();
for (int i = 0; OBJECT_QUEUE != null && i < OBJECT_QUEUE.size() && i < 1000; i++) {
EvidenceBasicInformation obj = (EvidenceBasicInformation) OBJECT_QUEUE.poll();
if (obj.getOutAppService() == null || obj.getEvidenceType() == 1) {
if (data.containsKey(obj.getApplicationInfo())) {
data.get(obj.getApplicationInfo()).add(obj.getEvidenceSn());
} else {
List<String> sns = new ArrayList<String>();
sns.add(obj.getEvidenceSn());
data.put(obj.getApplicationInfo(), sns);
}
} else {
dataRet.put(obj.getApplicationInfo(), obj);
}
}
try {
// <---------------------------- 处理data start ------------------------------>
for (Map.Entry<Long, List<String>> entry : data.entrySet()) {
ApplicationInfo applicationInfo = CacheCustomer.getAPP_INFO_MAP_BY_ID().get(entry.getKey());
if (applicationInfo == null) {
// TODO: 2018/3/15 从缓存取
applicationInfo = infoService.selectById(entry.getKey());
}
// 得到配置信息
if (applicationInfo.getAppId() != app_id || url == null || app_id == null || secret_key == null) {
if (StringUtils.isEmpty(applicationInfo.getCallback()) || StringUtils.isEmpty(applicationInfo.getAppId()) || StringUtils.isEmpty(applicationInfo.getSecretKey())) {
log.error("回调失败:缺少配置。");
return;
}
if (url == null) {
url = applicationInfo.getCallback().trim();
}
if (app_id == null) {
app_id = applicationInfo.getAppId();
}
if (secret_key == null) {
secret_key = applicationInfo.getSecretKey();
}
log.error("EvidenceQueryQueue : URL=" + url + ",APP_ID=" + app_id + ",SECRET_KEY=" + secret_key);
if (StringUtils.isEmpty(url) || StringUtils.isEmpty(app_id) || StringUtils.isEmpty(secret_key)) {
log.error("回调失败:缺少配置。");
return;
}
}
Map<String, Object> param = new HashMap<String, Object>();
param.put("status", 1);
param.put("message", "存证成功");
param.put("appId", app_id);
param.put("evidenceSns", JSONObject.toJSONString(entry.getValue()));
String header = "HMAC-SHA1 " + Base64.encode(HMACSHA1.getHmacSHA1(app_id + entry.getValue(), secret_key), false);
String retStr = OkHttpClientManagerCallback.post(url, header, param);
JSONObject ret_data = JSONObject.parseObject(retStr);
final List<String> dataSn = entry.getValue();
final Long appId = entry.getKey();
// 判断是否成功 true ——> 失败
if (retStr == null || ret_data.getInteger("status") != 1) {
log.error("EvidenceQueryQueue failure count [" + entry.getValue().size() + "]");
// 创建新线程 处理数据批量添加
List<String> sns = dataSn;
Long aId = appId;
List<EvidenceCallback> callbacks = new ArrayList<EvidenceCallback>();
;
for (String sn : sns) {
EvidenceCallback evidenceCallback = new EvidenceCallback();
evidenceCallback.setAppId(aId);
evidenceCallback.setCounnt(1);
evidenceCallback.setEvidenceSn(sn);
callbacks.add(evidenceCallback);
}
sqlSession.insert("com.itrus.portal.db.EvidenceCallbackMapper.insert", callbacks.toArray());
} else {
log.error("EvidenceQueryQueue success count [" + entry.getValue().size() + "]");
// 创建新线程 处理数据批量修改
List<String> sns = dataSn;
sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updates", sns.toArray());
}
}
// <---------------------------- 处理dataRet start ------------------------------>
for (Entry<Long, EvidenceBasicInformation> entry : dataRet.entrySet()) {
ApplicationInfo applicationInfo = CacheCustomer.getAPP_INFO_MAP_BY_ID().get(entry.getKey());
if (applicationInfo == null) {
// TODO: 2018/3/15 从缓存取
applicationInfo = infoService.selectById(entry.getKey());
}
if (applicationInfo.getAppId() != app_id || url == null || app_id == null || secret_key == null) {
if (StringUtils.isEmpty(applicationInfo.getCallback()) || StringUtils.isEmpty(applicationInfo.getAppId()) || StringUtils.isEmpty(applicationInfo.getSecretKey())) {
log.error("回调失败:缺少配置。");
return;
}
if (url == null) {
url = applicationInfo.getCallback().trim();
}
if (app_id == null) {
app_id = applicationInfo.getAppId();
}
if (secret_key == null) {
secret_key = applicationInfo.getSecretKey();
}
log.error("EvidenceQueryQueue : URL=" + url + ",APP_ID=" + app_id + ",SECRET_KEY=" + secret_key);
if (StringUtils.isEmpty(url) || StringUtils.isEmpty(app_id) || StringUtils.isEmpty(secret_key)) {
log.error("回调失败:缺少配置。");
return;
}
}
// 得到证据回执base64
String reportBase64 = null;
// reportBase64 = EvidenceSaveServiceApi.cacheReportBySnMap.get(entry.getValue().getEvidenceSn());
if (StringUtils.isEmpty(reportBase64)) {
// 获取原子服务地址 1.实名认证 2.密码服务 3.时间戳
RealNameAuthentication realNameAuthentication = CacheCustomer.getAUTH_CONFIG_MAP().get(2);
if (realNameAuthentication == null) {
try {
realNameAuthentication = realNameAuthenticationSerivce.getRealNameAuthenticationByTwo();
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取回执报告附件表信息
EvidenceEnclosureExample enclosureExample = new EvidenceEnclosureExample();
EvidenceEnclosureExample.Criteria ec = enclosureExample.createCriteria();
ec.andEvidenceSnEqualTo(entry.getValue().getEvidenceSn());
ec.andPdfTypeEqualTo("3");
List<EvidenceEnclosure> enclosures = sqlSession.selectList("com.itrus.portal.db.EvidenceEnclosureMapper.selectByExample", enclosureExample);
if (enclosures != null && !enclosures.isEmpty()) {
EvidenceEnclosure enclosure = enclosures.get(0);
// 调解密下载接口从原子服务拿到回执报告base64
reportBase64 = EvidenceSaveServiceApi.decryptedAndDownload(sqlSession, enclosure.getBuid(), realNameAuthentication.getRealNameddress());
} else {
// 调用生成回执报告方法
Map<String, Object> mapRet = reportTemplate.returnreceipt(entry.getValue().getEvidenceSn(), applicationInfo, null);
log.error("EvidenceQueryQueue mapRet is " + mapRet.get("status"));
;
if (mapRet.get("status") != null && (int) mapRet.get("status") == 0) {
// 修改证据基本信息回执状态为已出
reportBase64 = (String) mapRet.get("pdfBase64");
EvidenceBasicInformation basicInformation = informationService.getInformationBySn(entry.getValue().getEvidenceSn(), null);
basicInformation.setReceiptStatus(1);
sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updateByPrimaryKey", basicInformation);
}
}
} else {
// EvidenceSaveServiceApi.cacheReportBySnMap.remove(entry.getValue().getEvidenceSn());
}
Map<String, Object> param = new HashMap<String, Object>();
param.put("status", 1);
param.put("appId", app_id);
param.put("evidenceSn", entry.getValue().getEvidenceSn());
log.error("EvidenceQueryQueue reportBase64 is " + (reportBase64 == null) + ", evidenceSn is " + entry.getValue().getEvidenceSn());
if (reportBase64 == null) {
param.put("message", "存证成功,回执报告生成失败!");
} else {
param.put("message", "存证成功");
param.put("reportBase64", reportBase64);
}
String header = "HMAC-SHA1 " + Base64.encode(HMACSHA1.getHmacSHA1(app_id + entry.getValue().getEvidenceSn(), secret_key), false);
String retStr = OkHttpClientManagerCallback.post(url, header, param);
JSONObject ret_data = JSONObject.parseObject(retStr);
String dataSn = entry.getValue().getEvidenceSn();
Long appId = entry.getKey();
// 判断是否成功 true ——> 失败
if (retStr == null || ret_data.getInteger("status") != 1) {
log.error("EvidenceQueryQueue failure sn is [" + entry.getValue().getEvidenceSn() + "]" + "retStr is [" + retStr + "]");
EvidenceCallback evidenceCallback = new EvidenceCallback();
evidenceCallback.setAppId(appId);
evidenceCallback.setCounnt(1);
evidenceCallback.setEvidenceSn(dataSn);
evidenceCallback.setAppServiceId(entry.getValue().getOutAppService());
sqlSession.insert("com.itrus.portal.db.EvidenceCallbackMapper.insert", evidenceCallback);
} else {
log.error("EvidenceQueryQueue success sn is [" + entry.getValue().getEvidenceSn() + "]" + "retStr is [" + retStr + "]");
List<String> sns = new ArrayList<>();
sns.add(dataSn);
sqlSession.update("com.itrus.portal.db.EvidenceBasicInformationMapper.updates", sns.toArray());
}
}
// <---------------------------- 处理dataRet end ------------------------------>
} catch (Exception e) {
e.printStackTrace();
LogUtil.evidencelog(sqlSession, null, "存证回调接口", "失败原因:" + e.toString());
}
}
}
Aggregations