ZhiMaOcrUpdateAgainJob.java
6.41 KB
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
package com.yohoufo.user.quartz;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.quartz.annotation.JobType;
import com.yoho.quartz.annotation.MisfiredPolicy;
import com.yoho.quartz.annotation.YhJobDef;
import com.yoho.quartz.domain.JobProcessResult;
import com.yoho.quartz.job.YhJob;
import com.yoho.service.model.reviewed.request.ImageBO;
import com.yoho.service.model.reviewed.response.IdCardRspBO;
import com.yoho.service.model.reviewed.response.ImageReviewResp;
import com.yoho.tools.common.beans.Response;
import com.yohoufo.common.constant.CertPhotoEnum;
import com.yohoufo.dal.user.IZhiMaCertDao;
import com.yohoufo.dal.user.model.ZhiMaCert;
import com.yohoufo.user.component.CertPhotoSwitchComponent;
import com.yohoufo.user.service.impl.OcrCertPhotoService;
import com.yohoufo.user.service.impl.ZhiMaCallUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by craig.qin
*/
@Service
@YhJobDef(jobName = "ZhiMaOcrUpdateAgainScheduler", desc = "实名认证身份证图片ocr认证失败再次校验" , cron = "0 0 0/1 * * ?",
misfiredPolicy = MisfiredPolicy.CRON_DO_NOTHING, jobType = JobType.CRON, jobGroup = "ufo-gateway")
public class ZhiMaOcrUpdateAgainJob implements YhJob {
private final Logger logger = LoggerFactory.getLogger(ZhiMaOcrUpdateAgainJob.class);
@Autowired
private IZhiMaCertDao zhiMaCertDao;
@Autowired
private CertPhotoSwitchComponent certPhotoSwitchComponent;
@Autowired
private OcrCertPhotoService ocrCertPhotoService;
private final int MAX_COUNT = 3;
@Override
public JobProcessResult process(String s) {
logger.info("enter ZhiMaOcrUpdateAgainJob.process");
//如果ocr认证开关还是关闭的,则直接返回
boolean open = certPhotoSwitchComponent.getCertPhotoSwitch();
if(!open){
logger.info("ZhiMaOcrUpdateAgainJob return cause of switch is not open!");
return null;
}
for(int i=0;i<MAX_COUNT;i++){
//需要再次认证的数据(valid_status = 1 and valid_photo = 2),限定300条
List<ZhiMaCert> ls = zhiMaCertDao.selectTopCountValidStatusAndValidPhotoStatus(1, CertPhotoEnum.valid_but_uncheck.getStatus(),300);
if(CollectionUtils.isEmpty(ls)){
logger.info("ZhiMaOcrUpdateAgainJob return cause of list is empty! times {}",i);
break;
}
//循环开始认证
int targetPhotoStatus ;
for(ZhiMaCert zhiMaCert:ls){
if(StringUtils.isEmpty(zhiMaCert.getImageUrl())){
targetPhotoStatus = CertPhotoEnum.un_valid.getStatus();
}else{
String[] urlArray=zhiMaCert.getImageUrl().split(",");
if(urlArray==null||urlArray.length!=2){
targetPhotoStatus = CertPhotoEnum.un_valid.getStatus();
}else{
List<ImageBO> imageBoList=new ArrayList<>();
ImageBO frontUploadModel=new ImageBO();
frontUploadModel.setImageSide("front");
frontUploadModel.setImageUrl(ZhiMaCallUtil.signPrivateUrl(urlArray[0]));
imageBoList.add(frontUploadModel);
Response<ImageReviewResp> imageReviewRespResponse =ocrCertPhotoService.ocrCheck(zhiMaCert.getUid(),imageBoList);
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto result old zhiMaCert {} ,resp {} ",zhiMaCert, JSON.toJSONString(imageReviewRespResponse));
targetPhotoStatus = check(zhiMaCert,imageReviewRespResponse);
}
}
//更新
if(targetPhotoStatus != CertPhotoEnum.valid_but_uncheck.getStatus()){
int num = zhiMaCertDao.updatePhotoStatusByPrimaryKeyAndValidStatus(zhiMaCert.getId(),targetPhotoStatus);
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto update success old zhiMaCert {} ,num {} ,new photo status {} ",zhiMaCert, num,targetPhotoStatus);
}
}
}
return null;
}
private int check(ZhiMaCert zhiMaCert ,Response<ImageReviewResp> imageReviewRespResponse){
int photo_status = CertPhotoEnum.un_valid.getStatus();
if(imageReviewRespResponse==null||imageReviewRespResponse.getCode()!=200){
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto check return photo_status {} imageReviewRespResponse is null or code is not 200 ,old zhiMaCert {} ",photo_status ,zhiMaCert );
return photo_status;
}
ImageReviewResp reviewResp = imageReviewRespResponse.getData();
if(reviewResp==null||!"success".equals(reviewResp.getResultCode())){
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto check return photo_status {} imageReviewRespResponse getResultCode is not success ,old zhiMaCert {} ",photo_status ,zhiMaCert );
return photo_status;
}
//比较信息
JSONObject jo = JSON.parseObject(reviewResp.getResultDatas());
if(jo!=null&&jo.containsKey("front")){
IdCardRspBO front = JSON.parseObject(jo.getJSONObject("front").getString("resultData"),IdCardRspBO.class);
if(front==null){
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto check return photo_status {} front side is null ,old zhiMaCert {} ",photo_status ,zhiMaCert );
return photo_status;
}
logger.info("checkCertPhoto front {} ",front);
if(!zhiMaCert.getCertName().equals(front.getName())
||!zhiMaCert.getCertNo().equals(front.getCode())){
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto check return photo_status {} front name or id not match ,old zhiMaCert {} ",photo_status ,zhiMaCert );
return photo_status;
}
//验证成功
photo_status = CertPhotoEnum.valid.getStatus();
logger.info("ZhiMaOcrUpdateAgainJob checkCertPhoto check return photo_status {} match ok ,old zhiMaCert {} ",photo_status ,zhiMaCert );
}
return photo_status;
}
}