All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahuatech.icc.multiinone.brm.AddPersonCardSDK Maven / Gradle / Ivy

There is a newer version: 1.0.13.7
Show newest version
package com.dahuatech.icc.multiinone.brm;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.brm.model.v202010.card.BrmCardGenIdRequest;
import com.dahuatech.icc.brm.model.v202010.card.BrmCardGenIdResponse;
import com.dahuatech.icc.brm.model.v202010.person.BrmPersonGenIdRequest;
import com.dahuatech.icc.brm.model.v202010.person.BrmPersonGenIdResponse;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.brm.domain.CardInfo;
import com.dahuatech.icc.multiinone.brm.domain.PersonBioSignatures;
import com.dahuatech.icc.multiinone.brm.vo.AddPersonCardRequest;
import com.dahuatech.icc.multiinone.brm.vo.AddPersonCardResponse;
import com.dahuatech.icc.multiinone.exception.BusinessException;
import com.dahuatech.icc.oauth.handle.InitVersionProcessor;
import com.dahuatech.icc.oauth.http.IccClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;

import java.util.ArrayList;
import java.util.List;


/**
 * 基础资源人员卡片信息同步场景的SDK封装
 */
public class AddPersonCardSDK {

    private static final Log logger = LogFactory.get();

    /**
     * 新增人员同步
     * @param request
     * @return
     * @throws ClientException
     */
    public AddPersonCardResponse addPersonAndCards(AddPersonCardRequest request) throws ClientException {
        try{
            request.valid();//参数校验
            Long personId = generatePersonId(request);//生成人员主键id
            if(request.getPersonInfo().getFaceImage() != null){
                String path = uploadImg(request);//上传照片
                List personBiosignatures = new ArrayList();
                PersonBioSignatures personBioSignatures = new PersonBioSignatures();
                personBioSignatures.setIndex(1);
                personBioSignatures.setPath(path);
                personBioSignatures.setType(3);
                personBiosignatures.add(personBioSignatures);
                if(request.getPersonInfo().getPersonBiosignatures() == null){
                    request.getPersonInfo().setPersonBiosignatures(personBiosignatures);
                }else{
                    request.getPersonInfo().getPersonBiosignatures().addAll(personBiosignatures);
                }
            }
            generateCardIds(request);
            addPersonCard(request,personId);//新增人员与卡片
            AddPersonCardResponse addPersonCardResponse = new AddPersonCardResponse();
            addPersonCardResponse.setPersonId(personId);
            addPersonCardResponse.setErrMsg(Constants.SUCCESS_MSG);
            addPersonCardResponse.setCode(Constants.SUCESS_CODE);
            return addPersonCardResponse;
        }catch (BusinessException businessException){
            logger.error("人员卡片同步失败{}",businessException,businessException.getMessage());
            AddPersonCardResponse addPersonCardResponse = new AddPersonCardResponse();
            addPersonCardResponse.setCode(businessException.code);
            addPersonCardResponse.setErrMsg(businessException.msg);
            return addPersonCardResponse;
        }catch (Exception e){
            logger.error("人员卡片同步异常{}",e,e.getMessage());
            AddPersonCardResponse addPersonCardResponse = new AddPersonCardResponse();
            addPersonCardResponse.setCode(Constants.SYSTEMERROR_CODE);
            addPersonCardResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            return addPersonCardResponse;
        }

    }

    private Long generatePersonId(AddPersonCardRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        BrmPersonGenIdRequest brmPersonGenIdRequest = new BrmPersonGenIdRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo());
        BrmPersonGenIdResponse brmPersonGenIdResponse = null;
        try{
            brmPersonGenIdResponse = iccClient.doAction(brmPersonGenIdRequest,brmPersonGenIdRequest.getResponseClass());//获取主键id
        }catch (Exception e){
            logger.error("生成人员主键id异常{}",e,e.getMessage());
            throw new BusinessException("生成人员主键id异常");
        }
        if(!brmPersonGenIdResponse.isSuccess()){
            logger.error("生成人员主键id失败code:{}-msg:{}",brmPersonGenIdResponse.getCode(),brmPersonGenIdResponse.getErrMsg());
            throw new BusinessException(brmPersonGenIdResponse.getCode(),brmPersonGenIdResponse.getErrMsg());
        }
        return brmPersonGenIdResponse.getData().getId();
    }

    private void generateCardIds(AddPersonCardRequest request) throws ClientException {
        if(request.getCards() != null && request.getCards().size() > 0){
            for(CardInfo cardInfo : request.getCards()){
                cardInfo.setId(generateCardId(request));
            }
        }
    }

    private Long generateCardId(AddPersonCardRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        BrmCardGenIdRequest brmPersonGenIdRequest = new BrmCardGenIdRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo());
        BrmCardGenIdResponse brmPersonGenIdResponse = null;
        try{
            brmPersonGenIdResponse = iccClient.doAction(brmPersonGenIdRequest,brmPersonGenIdRequest.getResponseClass());//获取主键id
        }catch (Exception e){
            logger.error("生成卡片主键id异常{}",e,e.getMessage());
            throw new BusinessException("生成人员主键id异常");
        }
        if(!brmPersonGenIdResponse.isSuccess()){
            logger.error("生成卡片主键id失败code:{}-msg:{}",brmPersonGenIdResponse.getCode(),brmPersonGenIdResponse.getErrMsg());
            throw new BusinessException(brmPersonGenIdResponse.getCode(),brmPersonGenIdResponse.getErrMsg());
        }
        return brmPersonGenIdResponse.getData().getId();
    }

    private String uploadImg(AddPersonCardRequest request) throws ClientException {
        if(request.getPersonInfo().getFaceImage() == null){
            return null;
        }
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = String.format("/evo-apigw/evo-brm/%s/person/upload/img", InitVersionProcessor.systemVersionMap.get(request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-brm"));
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url,Method.POST);
        generalRequest.form("file",request.getPersonInfo().getFaceImage());
        GeneralResponse response = null;
        try{
            response = iccClient.doAction(generalRequest, generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("上传照片异常{}",e,e.getMessage());
            throw new BusinessException("上传照片异常");
        }
        if(!response.isSuccess()){
            throw new BusinessException(response.getCode(),response.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(response.getResult());
        return jsonObject.getJSONObject("data").getStr("fileUrl");
    }

    private String addPersonCard(AddPersonCardRequest request,Long personId) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = String.format("/evo-apigw/evo-brm/%s/person/subsystem/save-person-all", InitVersionProcessor.systemVersionMap.get(request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-brm"));
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject jsonObject = JSONUtil.parseObj(JSONUtil.toJsonStr(request.getPersonInfo()));
            jsonObject.put("id",personId);
            jsonObject.put("service",Constants.service);
            jsonObject.put("cards",request.getCards());
            generalRequest.setBody(jsonObject.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());

        }catch (Exception e){
            logger.error("新增人员卡片接口异常{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!generalResponse.isSuccess()){
            logger.error("新增人员卡片失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        return jsonObject.getJSONObject("data").getStr("id");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy