com.dahuatech.icc.multiinone.vims.VimsPersonAuthSDK Maven / Gradle / Ivy
The newest version!
package com.dahuatech.icc.multiinone.vims;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.brm.model.v202010.SDK.BrmCardBatchAddSDK;
import com.dahuatech.icc.brm.model.v202010.SDK.BrmCardBatchGenIdSDK;
import com.dahuatech.icc.brm.model.v202010.SDK.BrmPersonBatchAddSDK;
import com.dahuatech.icc.brm.model.v202010.SDK.BrmPersonBatchGenIdSDK;
import com.dahuatech.icc.brm.model.v202010.card.*;
import com.dahuatech.icc.brm.model.v202010.person.BrmPersonBatchAddRequest;
import com.dahuatech.icc.brm.model.v202010.person.BrmPersonBatchAddResponse;
import com.dahuatech.icc.brm.model.v202010.person.BrmPersonBatchGenIdRequest;
import com.dahuatech.icc.brm.model.v202010.person.BrmPersonBatchGenIdResponse;
import com.dahuatech.icc.common.ParamValidEnum;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.vims.vo.PersonAuthRequest;
import com.dahuatech.icc.multiinone.vims.vo.PersonAuthResponse;
import com.dahuatech.icc.oauth.exception.BusinessException;
import com.dahuatech.icc.oauth.model.v202010.oSDK.OauthParamConstant;
import com.dahuatech.icc.util.CollectionUtil;
import com.dahuatech.icc.vims.model.v202207.SDK.CardAuthSDK;
import com.dahuatech.icc.vims.model.v202207.SDK.FaceAuthSDK;
import com.dahuatech.icc.vims.model.v202207.auth.CardAuthRequest;
import com.dahuatech.icc.vims.model.v202207.auth.CardAuthResponse;
import com.dahuatech.icc.vims.model.v202207.auth.FaceAuthRequest;
import com.dahuatech.icc.vims.model.v202207.auth.FaceAuthResponse;
import java.util.ArrayList;
import java.util.List;
/**
* program:java-sdk
*
* Author: 312013
* Date:2022-08-16 09:22
* Description: 可视对讲,批量人员添加(包含图像)、卡片添加、异步授权
*/
public class VimsPersonAuthSDK {
protected final Log logger = LogFactory.get();
/**
* 人员授权:添加卡片、添加人员、授权,授权分两步:卡片授权+人像授权
* **/
public PersonAuthResponse personAuth(PersonAuthRequest personAuthRequest){
logger.info("VimsPersonAuthSDK,personAuth,param:{}",new JSONObject(personAuthRequest).toJSONString(0));
PersonAuthResponse personAuthResponse;
//卡片授权信息和人像授权信息都不传,直接返回
if(personAuthRequest.getCardAuthInfo() == null && personAuthRequest.getFaceAuthInfo() == null){
logger.error("Card auth param and person auth param only one can be empty.");
return new PersonAuthResponse(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode(),"Card auth param and person auth param only one can be empty.", false);
}
//添加人员
if(personAuthRequest.getPersonInfos() != null && !CollectionUtil.isEmpty(personAuthRequest.getPersonInfos().getPersonList())){
BrmPersonBatchAddResponse brmPersonBatchAddResponse=addPerson(personAuthRequest);
//判断是否添加人员成功,添加失败直接设置报错信息返回
if(!brmPersonBatchAddResponse.isSuccess()||brmPersonBatchAddResponse.getCode()==ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode()
||brmPersonBatchAddResponse.getCode()==ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode()){
return new PersonAuthResponse(brmPersonBatchAddResponse.getCode(),brmPersonBatchAddResponse.getErrMsg(),false);
}
}
//添加卡片,若未传卡片,不自动生成卡片
if(personAuthRequest.getCardInfos() != null && !CollectionUtil.isEmpty(personAuthRequest.getCardInfos().getCardList())){
BrmCardBatchAddResponse brmCardBatchAddResponse=addCard(personAuthRequest);
if(!brmCardBatchAddResponse.isSuccess()){
return new PersonAuthResponse(brmCardBatchAddResponse.getCode(),brmCardBatchAddResponse.getErrMsg(),false);
}
}
//人像+卡片授权
personAuthResponse = personImageAndCardAuth(personAuthRequest);
return personAuthResponse;
}
/**
* 添加人员
*/
public BrmPersonBatchAddResponse addPerson(PersonAuthRequest personAuthRequest){
BrmPersonBatchAddResponse brmPersonBatchAddResponse = new BrmPersonBatchAddResponse();
ListpersonIds;
//生成人员id
try{
personIds = generatePersonIds(personAuthRequest);
}catch (BusinessException businessException){
brmPersonBatchAddResponse.setCode(businessException.getCode());
brmPersonBatchAddResponse.setSuccess(false);
brmPersonBatchAddResponse.setErrMsg(businessException.getErrorMsg());
return brmPersonBatchAddResponse;
}
//检验人员id是否生成成功
if(personIds==null||personIds.size()==0){
brmPersonBatchAddResponse.setCode(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode());
brmPersonBatchAddResponse.setErrMsg("Auto generate person id fail.");
brmPersonBatchAddResponse.setSuccess(false);
return new BrmPersonBatchAddResponse();
}
//设置人员id
setPersonId(personIds, personAuthRequest);
//添加人员
try{
BrmPersonBatchAddRequest brmPersonBatchAddRequest = personAuthRequest.parseToBrmPersonBatchAddRequest();
brmPersonBatchAddRequest.setOauthConfigBaseInfo(personAuthRequest.getOauthConfigBaseInfo());
brmPersonBatchAddResponse = new BrmPersonBatchAddSDK().personBatchAdd(brmPersonBatchAddRequest);
if(!brmPersonBatchAddResponse.isSuccess()) {
logger.error("Batch add person fail.");
}
logger.info("Batch add person success.");
}catch (Exception e){
logger.error("System error");
e.printStackTrace();
brmPersonBatchAddResponse.setCode(OauthParamConstant.SYSTEME_RROR.getCode());
brmPersonBatchAddResponse.setErrMsg("System error");
brmPersonBatchAddResponse.setSuccess(false);
return brmPersonBatchAddResponse;
}
return brmPersonBatchAddResponse;
}
/**
* 添加卡片
*/
public BrmCardBatchAddResponse addCard(PersonAuthRequest personAuthRequest){
BrmCardBatchAddRequest brmCardBatchAddRequest ;
BrmCardBatchAddResponse brmCardBatchAddResponse= new BrmCardBatchAddResponse();
//用户未传卡片信息,直接返回,不自动创建卡片
if(personAuthRequest.getCardInfos() == null || CollectionUtil.isEmpty(personAuthRequest.getCardInfos().getCardList())){
brmCardBatchAddResponse.setSuccess(false);
brmCardBatchAddResponse.setCode(ParamValidEnum.OAUTH_PARAM_NOT_EMPTY_ERROR.getCode());
brmCardBatchAddResponse.setErrMsg("Card info is null, create card fail.");
return brmCardBatchAddResponse;
} else {
//生成卡片ID
List cardIds = generateCardIds(personAuthRequest);
//判断卡片ids是否生成成功,失败则直接返回
if(cardIds==null||cardIds.size()==0){
brmCardBatchAddResponse.setSuccess(false);
brmCardBatchAddResponse.setCode(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode());
brmCardBatchAddResponse.setErrMsg("Auto generate person id fail.");
return brmCardBatchAddResponse;
}
//设置卡片id
for(int i = 0; i < personAuthRequest.getCardInfos().getCardList().size(); i++){
personAuthRequest.getCardInfos().getCardList().get(i).setId(cardIds.get(i));
}
}
//添加卡片
try{
brmCardBatchAddRequest = personAuthRequest.parseToBrmCardBatchAddRequest();
brmCardBatchAddRequest.setOauthConfigBaseInfo(personAuthRequest.getOauthConfigBaseInfo());
brmCardBatchAddResponse = new BrmCardBatchAddSDK().brmBatchCardAdd(brmCardBatchAddRequest);
if(!brmCardBatchAddResponse.isSuccess()) {
logger.error("Batch add card fail.");
// throw new BusinessException(brmCardBatchAddResponse.getCode(), brmCardBatchAddResponse.getErrMsg());
return brmCardBatchAddResponse;
}
logger.info("Batch add card success.");
}catch (Exception e){
logger.error("System error");
e.printStackTrace();
brmCardBatchAddResponse.setSuccess(false);
brmCardBatchAddResponse.setCode(OauthParamConstant.SYSTEME_RROR.getCode());
brmCardBatchAddResponse.setErrMsg("Batch add card fail.");
return brmCardBatchAddResponse;
}
return brmCardBatchAddResponse;
}
/**
* 人像+卡片授权
*/
public PersonAuthResponse personImageAndCardAuth(PersonAuthRequest personAuthRequest){
PersonAuthResponse personAuthResponse = null;
//卡片授权信息和人像授权信息都不传,直接返回
if(personAuthRequest.getCardAuthInfo() == null && personAuthRequest.getFaceAuthInfo() == null){
logger.error("Card auth param and person auth param only one can be empty.");
return new PersonAuthResponse(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode(), ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getErrMsg(), false);
}
//人像授权
if(personAuthRequest.getFaceAuthInfo() != null){
try{
Thread.sleep(5000);
}catch (Exception e){
e.printStackTrace();
}
personAuthResponse=personImageAuth(personAuthRequest);
if(!personAuthResponse.isSuccess()){
return personAuthResponse;
}
}
//卡片授权
if(personAuthRequest.getCardAuthInfo() != null){
try{
Thread.sleep(5000);
}catch (Exception e){
e.printStackTrace();
}
personAuthResponse=cardAuth(personAuthRequest);
}
return personAuthResponse;
}
/**
* 批量生成人员id
* **/
public List generatePersonIds(PersonAuthRequest personAuthRequest)throws BusinessException{
List personIds = new ArrayList();
try{
//批量生成人员id
BrmPersonBatchGenIdRequest request = new BrmPersonBatchGenIdRequest(personAuthRequest.getPersonInfos().getPersonList().size());
request.setOauthConfigBaseInfo(personAuthRequest.getOauthConfigBaseInfo());
BrmPersonBatchGenIdResponse brmPersonBatchGenIdResponse = new BrmPersonBatchGenIdSDK().personBatchGenId(request);
if(!brmPersonBatchGenIdResponse.isSuccess()){
logger.error("Batch generate person id fail.");
throw new BusinessException(brmPersonBatchGenIdResponse.getCode(),brmPersonBatchGenIdResponse.getErrMsg());
}
logger.info("Batch generate person id success.");
personIds = brmPersonBatchGenIdResponse.getData().getIdList();
}catch (ClientException clientException){
logger.error("Batch generate person id fail.");
clientException.printStackTrace();
return personIds;
}
return personIds;
}
/**
* 设置人员id
* **/
public void setPersonId(List personIds, PersonAuthRequest personAuthRequest){
if(!CollectionUtil.isEmpty(personIds) && !CollectionUtil.isEmpty(personAuthRequest.getPersonInfos().getPersonList())){
for (int i = 0; i < personAuthRequest.getPersonInfos().getPersonList().size(); i++){
personAuthRequest.getPersonInfos().getPersonList().get(i).setId(personIds.get(i));
if(personAuthRequest.getFaceAuthInfo().getOwners()!=null&&i generateCardIds(PersonAuthRequest personAuthRequest){
List cardIds = new ArrayList();
try{
Integer idCount = CollectionUtil.isEmpty(personAuthRequest.getCardInfos().getCardList()) ? 1 : personAuthRequest.getCardInfos().getCardList().size();
BrmCardBatchGenIdRequest request = new BrmCardBatchGenIdRequest(idCount);
request.setOauthConfigBaseInfo(personAuthRequest.getOauthConfigBaseInfo());
BrmCardBatchGenIdResponse response = new BrmCardBatchGenIdSDK().brmCardBatchGenId(request);
if(!response.isSuccess()){
logger.error("Batch generate card id fail.");
}
logger.info("Batch generate card id success.");
cardIds = response.getData().getIdList();
}catch (Exception e){
logger.error("System error");
e.printStackTrace();
return cardIds;
}
return cardIds;
}
/**
* 生成一张卡片
* **/
public BrmCard generateCard(PersonAuthRequest personAuthRequest){
BrmCard brmCard = null;
try{
String cardNumber = String.valueOf((int)((Math.random()*9+1)*10000000));
brmCard = BrmCard.builder().cardNumber(cardNumber).cardType("1").category("1").departmentId(1L).startDate("2022-01-01 00:00:00").endDate("2099-01-01 00:00:00").build();
}catch (ClientException clientException){
logger.error("Auto generate cardId fail.");
clientException.printStackTrace();
}
return brmCard;
}
/**
* 卡片授权
* **/
public PersonAuthResponse cardAuth(PersonAuthRequest personAuthRequest){
PersonAuthResponse personAuthResponse ;
CardAuthSDK cardAuthSDK = new CardAuthSDK();
CardAuthRequest cardAuthRequest = personAuthRequest.converToCardAuthRequest();
cardAuthRequest.setOauthConfigBaseInfo(personAuthRequest.getOauthConfigBaseInfo());
CardAuthResponse cardAuthResponse = cardAuthSDK.cardAuth(cardAuthRequest);
if(!cardAuthResponse.isSuccess()){
logger.info("Card batch auth fail.");
return new PersonAuthResponse(cardAuthResponse.getCode(),"Card batch auth fail.",false);
}
logger.info("Card batch auth success.");
personAuthResponse = new PersonAuthResponse(cardAuthResponse.getCode(), cardAuthResponse.getErrMsg(), cardAuthResponse.isSuccess());
return personAuthResponse;
}
/**
* 人像授权
* **/
public PersonAuthResponse personImageAuth(PersonAuthRequest personAuthRequest){
PersonAuthResponse personAuthResponse = null;
FaceAuthSDK faceAuthSDK = new FaceAuthSDK();
FaceAuthRequest faceAuthRequest = personAuthRequest.converToFaceAuthRequest();
faceAuthRequest.setOauthConfigBaseInfo(personAuthRequest.getOauthConfigBaseInfo());
FaceAuthResponse faceAuthResponse = faceAuthSDK.faceAuth(faceAuthRequest);
if(!faceAuthResponse.isSuccess()){
logger.info("Person face auth fail.");
return new PersonAuthResponse(faceAuthResponse.getCode(),faceAuthResponse.getErrMsg(),false);
}
logger.info("Person face auth success.");
personAuthResponse = new PersonAuthResponse(faceAuthResponse.getCode(), faceAuthResponse.getErrMsg(), faceAuthResponse.isSuccess());
return personAuthResponse;
}
}