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

com.dahuatech.icc.multiinone.face.PersonnelControlSDK Maven / Gradle / Ivy

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

import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.brm.constant.BrmConstant;
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.person.*;
import com.dahuatech.icc.common.ParamValidEnum;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.face.model.v202207.SDK.ChannelGroupAddBatchSDK;
import com.dahuatech.icc.face.model.v202207.SDK.DeptGroupSyncSDK;
import com.dahuatech.icc.face.model.v202207.SDK.GroupInfoDetailSDK;
import com.dahuatech.icc.face.model.v202207.SDK.PersonInfoAddSDK;
import com.dahuatech.icc.face.model.v202207.channelGroup.ChannelGroupAddBatchRequest;
import com.dahuatech.icc.face.model.v202207.channelGroup.ChannelGroupAddBatchResponse;
import com.dahuatech.icc.face.model.v202207.deptGroup.DeptGroupSyncRequest;
import com.dahuatech.icc.face.model.v202207.deptGroup.DeptGroupSyncResponse;
import com.dahuatech.icc.face.model.v202207.groupInfo.GroupInfoDetailRequest;
import com.dahuatech.icc.face.model.v202207.groupInfo.GroupInfoDetailResponse;
import com.dahuatech.icc.face.model.v202207.personInfo.PersonInfoAddRequest;
import com.dahuatech.icc.face.model.v202207.personInfo.PersonInfoAddResponse;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.accesscontrol.domain.PersonBatchAdd;
import com.dahuatech.icc.multiinone.face.vo.PersonnelControlRequest;
import com.dahuatech.icc.multiinone.face.vo.PersonnelControlResponse;
import com.dahuatech.icc.oauth.exception.BusinessException;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigBaseInfo;
import com.dahuatech.icc.oauth.model.v202010.oSDK.OauthParamConstant;
import com.dahuatech.icc.util.CollectionUtil;

import java.util.*;

/**
 * program:java-sdk
 *
 * Author: 312013
 * Date:2022-08-12 09:20
 * Description: 人员布控:批量人员添加(包含图像)、人像绑定、人脸布控
 */
public class PersonnelControlSDK {
    private final Log logger = LogFactory.get();

    //人员布控:批量人员添加(包含图像)、人像绑定、人脸布控
    public PersonnelControlResponse personnelControl(PersonnelControlRequest request) {
        logger.info("PersonnelControlSDK,personnelControl,param:{}",new JSONObject(request).toJSONString(0));
        OauthConfigBaseInfo oauthConfigBaseInfo = request.getOauthConfigBaseInfo();
        if(request.getPersonInfos() == null && CollectionUtil.isEmpty(request.getOuterPersonInfo())){
            return new PersonnelControlResponse(ParamValidEnum.PARAM_FORMAT_ERROR.getCode(), "Inner person and outter person param carry at least one.", false);
        }

        /** 人员批量添加(包含图像),这里要分情况讨论,如果是内部库,从基础框架添加;如果是外部库,从人像子系统添加 **/
        //1.内部库,从基础框架添加人员
        if(request.getPersonInfos() != null && !CollectionUtil.isEmpty(request.getPersonInfos().getPersonList())){
            BrmPersonBatchAddResponse brmPersonBatchAddResponse = addPersonInfo(oauthConfigBaseInfo, request.getPersonInfos());
            if(!brmPersonBatchAddResponse.isSuccess()){
                PersonnelControlResponse personnelControlResponse = new PersonnelControlResponse();
                personnelControlResponse.setCode(brmPersonBatchAddResponse.getCode());
                personnelControlResponse.setErrMsg(brmPersonBatchAddResponse.getErrMsg());
                personnelControlResponse.setSuccess(brmPersonBatchAddResponse.isSuccess());
                personnelControlResponse.setArgs(brmPersonBatchAddResponse.getArgs());
                return personnelControlResponse;
            }
        }

        Map> innerOutterMap = new HashMap>();
        //根据库ID查询库信息
        if(!CollectionUtil.isEmpty(request.getOuterPersonInfo())){
            try{
                innerOutterMap = getGroupInfoDetail(oauthConfigBaseInfo, request.getOuterPersonInfo());
            }catch (BusinessException e){
                PersonnelControlResponse personnelControlResponse = new PersonnelControlResponse();
                personnelControlResponse.setCode(e.getCode());
                personnelControlResponse.setErrMsg(e.getMessage());
                personnelControlResponse.setSuccess(false);
                personnelControlResponse.setArgs(e.getArgs());
                return personnelControlResponse;
            }catch(Exception e){
                PersonnelControlResponse personnelControlResponse = new PersonnelControlResponse();
                personnelControlResponse.setErrMsg(BrmConstant.SYSTEMERROR_MSG);
                personnelControlResponse.setCode(BrmConstant.SYSTEMERROR_CODE);
                personnelControlResponse.setSuccess(false);
                return personnelControlResponse;
            }

            List innerStory = innerOutterMap.get("innerStory");
            List outterStory = innerOutterMap.get("outterStory");
            //如果是外部库,添加人员
            if(innerOutterMap != null && innerOutterMap.size() > 0 && !CollectionUtil.isEmpty(outterStory)){
                for(PersonnelControlRequest.OuterPersonInfo outerPersonInfo : outterStory){
                    PersonInfoAddResponse personInfoAddResponse = addPersonInfo(oauthConfigBaseInfo, outerPersonInfo);
                    if(!personInfoAddResponse.isSuccess()){
                        logger.info("Add person error.");
                        PersonnelControlResponse personnelControlResponse = new PersonnelControlResponse();
                        personnelControlResponse.setCode(personInfoAddResponse.getCode());
                        personnelControlResponse.setErrMsg(personInfoAddResponse.getErrMsg());
                        personnelControlResponse.setSuccess(personInfoAddResponse.isSuccess());
                        personnelControlResponse.setArgs(personInfoAddResponse.getArgs());
                        return personnelControlResponse;
                    }
                    logger.info("Add person success.");
                }
            }

            if(innerOutterMap != null && innerOutterMap.size() > 0 && !CollectionUtil.isEmpty(innerStory)){
                PersonnelControlResponse  personnelControlResponse = new PersonnelControlResponse(ParamValidEnum.PARAM_FORMAT_ERROR.getCode(), "人员对应的人像库属于内部库,不能通过外部库参数添加", false);
                String errorNames = "";
                for(PersonnelControlRequest.OuterPersonInfo outerPersonInfo : innerStory){
                    errorNames += outerPersonInfo.getName();
                }
                personnelControlResponse.setArgs(errorNames);
                return personnelControlResponse;
            }
        }

        //人像绑定
        DeptGroupSyncResponse deptGroupSyncResponse = personFaceBind(oauthConfigBaseInfo, request.getBindPersonData());
        if(!deptGroupSyncResponse.isSuccess()){
            PersonnelControlResponse personnelControlResponse = new PersonnelControlResponse();
            personnelControlResponse.setCode(deptGroupSyncResponse.getCode());
            personnelControlResponse.setErrMsg(deptGroupSyncResponse.getErrMsg());
            personnelControlResponse.setSuccess(deptGroupSyncResponse.isSuccess());
            personnelControlResponse.setArgs(deptGroupSyncResponse.getArgs());
            return personnelControlResponse;
        }
        logger.info("PersonnelControl operate success.");

        //人脸布控
        ChannelGroupAddBatchResponse groupAddBatchResponse = personFaceControl(oauthConfigBaseInfo, request.getChannelGroupAddBatchData());
        PersonnelControlResponse personnelControlResponse = new PersonnelControlResponse();
        personnelControlResponse.setCode(groupAddBatchResponse.getCode());
        personnelControlResponse.setErrMsg(groupAddBatchResponse.getErrMsg());
        personnelControlResponse.setSuccess(groupAddBatchResponse.isSuccess());
        personnelControlResponse.setArgs(groupAddBatchResponse.getArgs());
        return personnelControlResponse;
    }

    /**
     * 根据groupId查询库详情
     */
    public Map> getGroupInfoDetail(OauthConfigBaseInfo oauthConfigBaseInfo, List outerPersonInfos){
        Map> innerOutterMap = null;
        if(CollectionUtil.isEmpty(outerPersonInfos)){
            return innerOutterMap;
        }

        try{
            innerOutterMap = new HashMap>();
            for(PersonnelControlRequest.OuterPersonInfo outerPersonInfo : outerPersonInfos){
                GroupInfoDetailSDK groupInfoDetailSDK = new GroupInfoDetailSDK();
                GroupInfoDetailRequest groupInfoDetailRequest = new GroupInfoDetailRequest(outerPersonInfo.getGroupid());
                groupInfoDetailRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);
                GroupInfoDetailResponse groupInfoDetailResponse = groupInfoDetailSDK.groupInfoDetail(groupInfoDetailRequest);
                if(!groupInfoDetailResponse.isSuccess()){
                    throw new BusinessException(groupInfoDetailResponse.getCode(),groupInfoDetailResponse.getErrMsg(),groupInfoDetailResponse.getArgs());
                }
                Integer groupType = groupInfoDetailResponse.getData().getGrouptype();
                //内部库
                if(groupType == Constants.INNER_STORE){
                    List innerStory = innerOutterMap.get("innerStory");
                    if(CollectionUtil.isEmpty(innerStory)){
                        innerStory = new ArrayList();
                        innerOutterMap.put("innerStory", innerStory);
                    }
                    innerStory.add(outerPersonInfo);
                }
                //黑名单库或白名单库
                if(groupType == Constants.BLACK_STORE || groupType == Constants.WHITE_STORE){
                    List outterStory = innerOutterMap.get("outterStory");
                    if(CollectionUtil.isEmpty(outterStory)){
                        outterStory = new ArrayList();
                        innerOutterMap.put("outterStory", outterStory);
                    }
                    outterStory.add(outerPersonInfo);
                }
            }

        }catch (BusinessException e){
            throw e;
        }catch (Exception e){
            logger.error("System error.");
            e.printStackTrace();
        }
        return innerOutterMap;
    }

    /**
     * 内部库,添加人员
     */
    public BrmPersonBatchAddResponse addPersonInfo(OauthConfigBaseInfo oauthConfigBaseInfo, PersonBatchAdd personBatchAdd) {
        BrmPersonBatchAddResponse brmPersonBatchAddResponse = null;
        List personIds = new ArrayList();
        try {
            //批量生成人员id
            BrmPersonBatchGenIdRequest request = new BrmPersonBatchGenIdRequest(personBatchAdd.getPersonList().size());
            request.setOauthConfigBaseInfo(oauthConfigBaseInfo);
            BrmPersonBatchGenIdResponse brmPersonBatchGenIdResponse = new BrmPersonBatchGenIdSDK().personBatchGenId(request);
            if(!brmPersonBatchGenIdResponse.isSuccess()){
                logger.error("Batch add person ID fail.");
                brmPersonBatchAddResponse =  new BrmPersonBatchAddResponse(brmPersonBatchGenIdResponse.getCode(),brmPersonBatchGenIdResponse.getErrMsg(), brmPersonBatchGenIdResponse.isSuccess());
                brmPersonBatchAddResponse.setArgs(brmPersonBatchGenIdResponse.getArgs());
                return brmPersonBatchAddResponse;
            }
            logger.info("Batch add person ID success.");
            personIds = brmPersonBatchGenIdResponse.getData().getIdList();

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

            //添加人员
            BrmPersonBatchAddRequest brmPersonBatchAddRequest = personBatchAdd.parseToBrmPersonBatchAddRequest();
            brmPersonBatchAddRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);
            brmPersonBatchAddResponse = new BrmPersonBatchAddSDK().personBatchAdd(brmPersonBatchAddRequest);
            if(!brmPersonBatchAddResponse.isSuccess()) {
                logger.info("Batch add person fail.");
                return brmPersonBatchAddResponse;
            }
            logger.info("Batch add person success.");

        }  catch (Exception e){
            logger.error("System error.");
            brmPersonBatchAddResponse = new BrmPersonBatchAddResponse();
            brmPersonBatchAddResponse.setErrMsg(BrmConstant.SYSTEMERROR_MSG);
            brmPersonBatchAddResponse.setCode(BrmConstant.SYSTEMERROR_CODE);
            brmPersonBatchAddResponse.setSuccess(false);
        }
        return brmPersonBatchAddResponse;
    }

    /**
     * 外部库,添加人员
     */
    public PersonInfoAddResponse addPersonInfo(OauthConfigBaseInfo oauthConfigBaseInfo, PersonnelControlRequest.OuterPersonInfo outerPersonInfo) {
        PersonInfoAddResponse personInfoAddResponse = null;
        PersonInfoAddSDK personInfoAddSDK = new PersonInfoAddSDK();
        PersonInfoAddRequest personInfoAddRequest = outerPersonInfo.convertToPersonInfoAddRequest();
        personInfoAddRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);

        personInfoAddResponse = personInfoAddSDK.addPersonInfo(personInfoAddRequest);
        return personInfoAddResponse;
    }

    /**
     * 人像绑定
     */
    public DeptGroupSyncResponse personFaceBind(OauthConfigBaseInfo oauthConfigBaseInfo, PersonnelControlRequest.BindPersonData bindPersonData) {
        DeptGroupSyncResponse deptGroupSyncResponse = null;
        DeptGroupSyncSDK deptGroupSyncSDK = new DeptGroupSyncSDK();
        DeptGroupSyncRequest deptGroupSyncRequest = bindPersonData.convertToDeptGroupSyncRequest();
        deptGroupSyncRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);

        try{
            deptGroupSyncResponse = deptGroupSyncSDK.deptGroupSync(deptGroupSyncRequest);
        }catch (Exception e){
            deptGroupSyncResponse = new DeptGroupSyncResponse();
            deptGroupSyncResponse.setErrMsg(BrmConstant.SYSTEMERROR_MSG);
            deptGroupSyncResponse.setCode(BrmConstant.SYSTEMERROR_CODE);;
            deptGroupSyncResponse.setSuccess(false);
            return deptGroupSyncResponse;
        }

        return deptGroupSyncResponse;
    }

    /**
     * 人脸布控
     */
    public ChannelGroupAddBatchResponse personFaceControl(OauthConfigBaseInfo oauthConfigBaseInfo, PersonnelControlRequest.ChannelGroupAddBatchData request) {
        ChannelGroupAddBatchSDK channelGroupAddBatchSDK = new ChannelGroupAddBatchSDK();
        ChannelGroupAddBatchRequest channelGroupAddBatchRequest = request.converToChannelGroupAddBatchRequest();
        channelGroupAddBatchRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);
        ChannelGroupAddBatchResponse channelGroupAddBatchResponse = null;

        try{
            channelGroupAddBatchResponse = channelGroupAddBatchSDK.channelGroupAddBatch(channelGroupAddBatchRequest);
        }catch (Exception e){
            logger.info("System error:{}",e.getMessage());
            channelGroupAddBatchResponse = new ChannelGroupAddBatchResponse();
            channelGroupAddBatchResponse.setErrMsg(OauthParamConstant.SYSTEME_RROR.getErrMsg());
            channelGroupAddBatchResponse.setCode(OauthParamConstant.SYSTEME_RROR.getCode());
            channelGroupAddBatchResponse.setSuccess(false);
            channelGroupAddBatchResponse.setSuccess(false);
        }
        return channelGroupAddBatchResponse;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy