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

com.dahuatech.icc.multiinone.accesscontrol.AccessAuthByPersonSDK Maven / Gradle / Ivy

The newest version!
package com.dahuatech.icc.multiinone.accesscontrol;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.assesscontrol.model.v202103.SDK.AuthPersonAddADK;
import com.dahuatech.icc.assesscontrol.model.v202103.authPerson.AuthPersonAddRequest;
import com.dahuatech.icc.assesscontrol.model.v202103.authPerson.AuthPersonAddResponse;
import com.dahuatech.icc.brm.constant.BrmConstant;
import com.dahuatech.icc.brm.model.v202010.SDK.*;
import com.dahuatech.icc.brm.model.v202010.card.*;
import com.dahuatech.icc.brm.model.v202010.person.*;
import com.dahuatech.icc.common.ParamValidEnum;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.accesscontrol.domain.CardBatchAdd;
import com.dahuatech.icc.multiinone.accesscontrol.vo.AccessAuthByPersonRequest;
import com.dahuatech.icc.multiinone.accesscontrol.vo.AuthByPersonRequest;
import com.dahuatech.icc.multiinone.brm.domain.CardInfo;
import com.dahuatech.icc.multiinone.exception.BusinessException;
import com.dahuatech.icc.multiinone.utils.RandomUtils;
import com.dahuatech.icc.multiinone.utils.StringUtils;
import com.dahuatech.icc.multiinone.vo.BaseResponse;
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 com.dahuatech.icc.oauth.model.v202010.OauthConfigBaseInfo;
import com.dahuatech.icc.multiinone.accesscontrol.vo.AuthByPersonResponse;
import com.dahuatech.icc.util.CollectionUtil;

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

public class AccessAuthByPersonSDK extends AccessBaseAuthSDK{

    public BaseResponse auth(AccessAuthByPersonRequest request){
        BaseResponse baseResponse = new BaseResponse();
        try{

            request.valid();
            /**
             * 优先级:卡号>人员id>人员编码
             */
            String cardNumber = null;
            if(!StringUtils.isEmpty(request.getCardNumber())){//卡号存在
                cardNumber = request.getCardNumber();
            }else if(request.getPersonId() != null){//人员id存在
                CardInfo cardInfo = getMainCard(request.getOauthConfigBaseInfo(),request.getPersonId(),null);
                cardNumber = cardInfo.getCardNumber();
            }else {//人员编码存在
                CardInfo cardInfo = getMainCard(request.getOauthConfigBaseInfo(),null,request.getPersonCode());
                cardNumber = cardInfo.getCardNumber();
            }
            if(checkAuthIsExist(request.getOauthConfigBaseInfo(),cardNumber)){//不存在新增权限
                addAuth(request,cardNumber);
            }else{//存在 则增量更新
                updateAuth(request,cardNumber);
            }
            baseResponse.setCode(Constants.SUCESS_CODE);
            baseResponse.setErrMsg(Constants.SUCCESS_MSG);
        }catch (BusinessException businessException){
            logger.error("门禁-按人授权失败{}",businessException,businessException.getMessage());
            baseResponse.setCode(businessException.code);
            baseResponse.setErrMsg(businessException.msg);
        }catch (Exception e){
            logger.error("门禁-按人授权异常{}",e,e.getMessage());
            baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            baseResponse.setCode(Constants.SYSTEMERROR_CODE);
        }
        return baseResponse;
    }

    private void addAuth(AccessAuthByPersonRequest request , String cardNumber) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = String.format("/evo-apigw/evo-accesscontrol/%s/card/accessControl/doorAuthority", InitVersionProcessor.systemVersionMap.get(request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-accesscontrol"));
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cardNumber",cardNumber);
            jsonObject.put("cardPrivilegeDetails",request.getCardPrivilegeDetails());
            jsonObject.put("timeQuantumId",request.getTimeQuantumId());
            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());
        }
    }

    private void updateAuth(AccessAuthByPersonRequest request , String cardNumber) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = String.format("/evo-apigw/evo-accesscontrol/%s/card/accessControl/doorAuthority/deltaUpdate", InitVersionProcessor.systemVersionMap.get(request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-accesscontrol"));
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cardNumber",cardNumber);
            jsonObject.put("cardPrivilegeDetails",request.getCardPrivilegeDetails());
            jsonObject.put("timeQuantumId",request.getTimeQuantumId());
            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());
        }
    }

    private boolean checkAuthIsExist(OauthConfigBaseInfo oauthConfigBaseInfo,String cardNumber) throws ClientException {
        IccClient iccClient = new IccClient(oauthConfigBaseInfo);
        String url = String.format("/evo-apigw/evo-accesscontrol/%s/card/accessControl/doorAuthority/bycondition/combined", InitVersionProcessor.systemVersionMap.get(oauthConfigBaseInfo.getHttpConfigInfo().getHost() + "evo-accesscontrol"));
        GeneralRequest generalRequest = new GeneralRequest(oauthConfigBaseInfo.getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cardNumber",cardNumber);
            jsonObject.put("authorizeStatus","-1");
            jsonObject.put("taskStatus","-1");
            jsonObject.put("pageNum","1");
            jsonObject.put("pageSize","20");
            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());
        if(jsonObject.getJSONObject("data").getLong("totalRows") > 0){
            return true;
        }
        return false;
    }

    /***************************** 新SDK版本新增按人授权 **********************************/
    public AuthByPersonResponse authByPerson(AuthByPersonRequest authByPersonRequest){
        logger.info("AccessAuthByPersonSDK,authByPersonSDK,param:{}",new JSONObject(authByPersonRequest).toJSONString(0));
        AuthByPersonResponse validResponse = valid(authByPersonRequest);
        if(validResponse != null){
           return validResponse;
        }

        List personIds = new ArrayList();
        List cardIds = new ArrayList();
        BrmPersonBatchGenIdResponse brmPersonBatchGenIdResponse = null;

        //1.批量添加人员信息
        try {
            //批量生成人员id
            BrmPersonBatchGenIdRequest request = new BrmPersonBatchGenIdRequest(authByPersonRequest.personInfos.getPersonList().size());
            request.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
            brmPersonBatchGenIdResponse = new BrmPersonBatchGenIdSDK().personBatchGenId(request);
        }catch (ClientException clientException){
            logger.error("Person batch add fail.");
            return new AuthByPersonResponse(brmPersonBatchGenIdResponse.getCode(),brmPersonBatchGenIdResponse.getErrMsg(), brmPersonBatchGenIdResponse.isSuccess(), brmPersonBatchGenIdResponse.getArgs());
        }
        if(!brmPersonBatchGenIdResponse.isSuccess()){
            logger.error("Batch generate person'id fail.");
            return new AuthByPersonResponse(brmPersonBatchGenIdResponse.getCode(),brmPersonBatchGenIdResponse.getErrMsg(), brmPersonBatchGenIdResponse.isSuccess(), brmPersonBatchGenIdResponse.getArgs());
        }
        logger.info("Batch generate person'id success.");
        personIds = brmPersonBatchGenIdResponse.getData().getIdList();

        //设置人员id
        for(int i = 0; i < authByPersonRequest.getPersonInfos().getPersonList().size(); i++){
            authByPersonRequest.getPersonInfos().getPersonList().get(i).setId(personIds.get(i));
        }

        //添加人员
        BrmPersonBatchAddResponse brmPersonBatchAddResponse = null;
        BrmPersonBatchAddRequest brmPersonBatchAddRequest = authByPersonRequest.parseToBrmPersonBatchAddRequest();
        brmPersonBatchAddRequest.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
        brmPersonBatchAddResponse = new BrmPersonBatchAddSDK().personBatchAdd(brmPersonBatchAddRequest);
        if(!brmPersonBatchAddResponse.isSuccess()) {
            logger.error("Person batch add fail.");
            return new AuthByPersonResponse(brmPersonBatchAddResponse.getCode(),brmPersonBatchAddResponse.getErrMsg(), brmPersonBatchAddResponse.isSuccess(), brmPersonBatchAddResponse.getArgs());
        }
        logger.info("Person batch add success.");


        //2.批量添加卡片信息
       BrmCardBatchAddRequest brmCardBatchAddRequest = null;
       try {
           //如果未传卡片,生成一张卡片
           if(authByPersonRequest.cardInfos == null || CollectionUtil.isEmpty(authByPersonRequest.cardInfos.getCardList())){
               if(authByPersonRequest.cardInfos == null){
                   authByPersonRequest.cardInfos = new CardBatchAdd();
               }
               if(authByPersonRequest.cardInfos.getCardList() == null){
                   authByPersonRequest.cardInfos.setCardList(new ArrayList());
                   if(authByPersonRequest.cardInfos.getCardList().size() <= 0){
                       CardBatchAdd cardBatchAdd = generateCard(authByPersonRequest);
                       authByPersonRequest.setCardInfos(cardBatchAdd);
                   }
               }
           }
           else {
               //批量生成卡片id
               BrmCardBatchGenIdRequest request = new BrmCardBatchGenIdRequest(authByPersonRequest.cardInfos.getCardList().size());
               request.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
               BrmCardBatchGenIdResponse response = new BrmCardBatchGenIdSDK().brmCardBatchGenId(request);
               if(!response.isSuccess()){
                   logger.error("Batch generate card id fail.");
                   return new AuthByPersonResponse(response.getCode(), response.getErrMsg(), response.isSuccess(), response.getArgs());
               }
               logger.info("Batch generate card id success.");
               cardIds = response.getData().getIdList();

               //设置卡片id
               for(int i = 0; i < authByPersonRequest.getCardInfos().getCardList().size(); i++){
                   authByPersonRequest.getCardInfos().getCardList().get(i).setId(cardIds.get(i));
               }
           }

           //添加卡片
           brmCardBatchAddRequest = authByPersonRequest.parseToBrmCardBatchAddRequest();
           brmCardBatchAddRequest.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
           BrmCardBatchAddResponse brmCardBatchAddResponse = new BrmCardBatchAddSDK().brmBatchCardAdd(brmCardBatchAddRequest);
           if(!brmCardBatchAddResponse.isSuccess()) {
               logger.error("Batch add cards fail.");
               return new AuthByPersonResponse(brmCardBatchAddResponse.getCode(), brmCardBatchAddResponse.getErrMsg(), brmCardBatchAddResponse.isSuccess(), brmCardBatchAddResponse.getArgs());
           }
           logger.info("Batch add cards success.");
       } catch (Exception e){
           logger.error("System error.");
           e.printStackTrace();
       }

        //3.按人授权
        try {
            //todo: 这里暂时将重试时间设置为:1000ms * 5次 = 5m
            int flag = 0;

            AuthPersonAddADK authPersonAddADK = new AuthPersonAddADK();
            AuthPersonAddRequest authPersonAddRequest = authByPersonRequest.parseToAuthPersonAddRequest();
            authPersonAddRequest.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
            AuthPersonAddResponse authPersonAddResponse = authPersonAddADK.authPersonAdd(authPersonAddRequest);
            //卡片不存在,重试
            while (authPersonAddResponse.getCode() != null && authPersonAddResponse.getCode().equals("44999999") && flag < 3){
                logger.info("Cards information has not been synchronized, and the {} retry...",flag + 1);
                Thread.sleep(5000);

                authPersonAddADK = new AuthPersonAddADK();
                authPersonAddRequest = authByPersonRequest.parseToAuthPersonAddRequest();
                authPersonAddRequest.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
                authPersonAddResponse = authPersonAddADK.authPersonAdd(authPersonAddRequest);
                flag ++;
            }
            if(authPersonAddResponse.isSuccess()){
                logger.info("Auth by person success.");
            }
            //存在,直接返回
            return new AuthByPersonResponse(authPersonAddResponse.getCode(), authPersonAddResponse.getErrMsg(), authPersonAddResponse.isSuccess(), authPersonAddResponse.getArgs());
        } catch (Exception e){
            logger.error("System error.");
            e.printStackTrace();
        }
        return new AuthByPersonResponse();
    }

    /** 生成卡片 **/
    public CardBatchAdd generateCard(AuthByPersonRequest authByPersonRequest) {
        List personList = authByPersonRequest.getPersonInfos().getPersonList();
        CardBatchAdd cardBatchAdd = new CardBatchAdd();
        List cardIds = null;
        BrmCard card = null;
        //生成卡片id
        if(!CollectionUtil.isEmpty(personList)){
            BrmCardBatchGenIdRequest request = new BrmCardBatchGenIdRequest(personList.size());
            request.setOauthConfigBaseInfo(authByPersonRequest.getOauthConfigBaseInfo());
            BrmCardBatchGenIdResponse response = new BrmCardBatchGenIdSDK().brmCardBatchGenId(request);
            if(!response.isSuccess()){
                return null;
            }
            cardIds = (List)response.getData().getIdList();
        }

        try {
            List cards = new ArrayList();

            //生成和人员个数相同的卡片,每个人绑定一张卡片
            int i = 0;
            if(!CollectionUtil.isEmpty(personList)){
                for(BrmPersonBatchAddRequest.PersonBatchData personBatchData : personList){
                    card = BrmCard.builder().cardId(cardIds.get(i)).cardNumber(RandomUtils.generateDigitString(8)).cardType("1").category("1").departmentId(1L).startDate("2022-01-01 00:00:00").endDate("2099-01-01 00:00:00").build();
                    card.setPersonId(personBatchData.getId());
                    cards.add(card);
                    i++;
                }
            }
            cardBatchAdd.setCardList(cards);
        }catch (ClientException clientException){
            logger.error("Generate card fail.");
        }
        return cardBatchAdd;
    }

    public AuthByPersonResponse valid(AuthByPersonRequest authByPersonRequest){
        if(authByPersonRequest.getPersonInfos() == null){
            return new AuthByPersonResponse(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode(), ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getErrMsg(), false, "personInfos");
        }
        if(authByPersonRequest.getAuthInfo() == null){
            return new AuthByPersonResponse(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode(), ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getErrMsg(), false, "authInfo");
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy