ZhiMaOcrUpdateAgainJob.java 6.41 KB
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;
    }


}