com.dahuatech.icc.multiinone.accesscontrol.AccessRecordSynSDK Maven / Gradle / Ivy
package com.dahuatech.icc.multiinone.accesscontrol;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONArray;
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.assesscontrol.enums.AccessBusinessEnum;
import com.dahuatech.icc.assesscontrol.enums.OpenTypeEnums;
import com.dahuatech.icc.event.enums.CategoryEnums;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.accesscontrol.domain.AccessRecordInfo;
import com.dahuatech.icc.multiinone.accesscontrol.vo.AccessRecordsQueryPageRequest;
import com.dahuatech.icc.multiinone.accesscontrol.vo.AccessRecordsQueryPageResponse;
import com.dahuatech.icc.multiinone.event.EventSubcribeSDK;
import com.dahuatech.icc.multiinone.event.domain.BaseEventSubcribeInfo;
import com.dahuatech.icc.multiinone.event.vo.SimpleEventSubcribeRequest;
import com.dahuatech.icc.multiinone.exception.BusinessException;
import com.dahuatech.icc.multiinone.utils.EventParamsUtils;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 门禁数据同步
*/
public class AccessRecordSynSDK {
private final Log logger = LogFactory.get();
/**
* 实时推送第三方
* @param request
* @return
*/
public BaseResponse eventSubscribe(BaseEventSubcribeInfo request){
try{
request.valid();
EventSubcribeSDK eventSubcribeSDK = new EventSubcribeSDK();
SimpleEventSubcribeRequest eventSubcribeRequest = EventParamsUtils.buildSimpleEventSubcribeRequest(request);
Map> config = new HashMap>();
List list = new ArrayList();
list.addAll(AccessBusinessEnum.getBusinessTypes());//门禁业务类型
List alarmList = new ArrayList();
alarmList.addAll(OpenTypeEnums.getOpenTypeList());
config.put(CategoryEnums.alarm.code, alarmList);
config.put(CategoryEnums.business.code, list);
eventSubcribeRequest.setTypesConfigs(config);
return eventSubcribeSDK.simpleEventSubcribe(eventSubcribeRequest);
}catch (BusinessException businessException){
logger.error("门禁/可视对讲事件订阅失败{}",businessException,businessException.getMessage());
BaseResponse baseResponse = new BaseResponse();
baseResponse.setCode(businessException.code);
baseResponse.setErrMsg(businessException.msg);
return baseResponse;
}catch (Exception e){
BaseResponse baseResponse = new BaseResponse();
logger.error("门禁/可视对讲事件订阅异常{}",e,e.getMessage());
baseResponse.setCode(Constants.SYSTEMERROR_CODE);
baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
return baseResponse;
}
}
public AccessRecordsQueryPageResponse accessRecordsQueryPage(AccessRecordsQueryPageRequest accessRecordsQueryPageRequest){
AccessRecordsQueryPageResponse accessRecordsQueryPageResponse = new AccessRecordsQueryPageResponse();
try{
accessRecordsQueryPageRequest.valid();
List list = queryPage(accessRecordsQueryPageRequest);
int count = queryCount(accessRecordsQueryPageRequest);
accessRecordsQueryPageResponse.setCurrentPage(accessRecordsQueryPageRequest.getPageNum());
accessRecordsQueryPageResponse.setPageSize(accessRecordsQueryPageRequest.getPageSize());
if(count > 0){
int totalPage = count%accessRecordsQueryPageRequest.getPageSize()==0?count/accessRecordsQueryPageRequest.getPageSize():count/accessRecordsQueryPageRequest.getPageSize() + 1;
accessRecordsQueryPageResponse.setTotalPage(totalPage);
}
accessRecordsQueryPageResponse.setTotalRows(count);
accessRecordsQueryPageResponse.setPageData(list);
}catch (BusinessException businessException){
logger.error("门禁刷卡记录分页查询失败{}",businessException,businessException.getMessage());
AccessRecordsQueryPageResponse baseResponse = new AccessRecordsQueryPageResponse();
baseResponse.setCode(businessException.code);
baseResponse.setErrMsg(businessException.msg);
return baseResponse;
}catch (Exception e){
AccessRecordsQueryPageResponse baseResponse = new AccessRecordsQueryPageResponse();
logger.error("门禁刷卡记录分页查异常{}",e,e.getMessage());
baseResponse.setCode(Constants.SYSTEMERROR_CODE);
baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
return baseResponse;
}
return accessRecordsQueryPageResponse;
}
private List queryPage(AccessRecordsQueryPageRequest request) throws ClientException {
IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
String url = String.format("/evo-apigw/evo-accesscontrol/%s/card/accessControl/swingCardRecord/bycondition/combined", InitVersionProcessor.systemVersionMap.get(request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-accesscontrol"));
GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
GeneralResponse generalResponse = null;
try{
generalRequest.setBody(JSONUtil.toJsonStr(request));
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());
JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("pageData");
if(jsonArray == null){
return new ArrayList();
}
List list = jsonArray.toList(AccessRecordInfo.class);
return list;
}
private Integer queryCount(AccessRecordsQueryPageRequest request) throws ClientException {
IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
String url = String.format("/evo-apigw/evo-accesscontrol/%s/card/accessControl/swingCardRecord/bycondition/combinedCount", InitVersionProcessor.systemVersionMap.get(request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-accesscontrol"));
GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
GeneralResponse generalResponse = null;
try{
generalRequest.setBody(JSONUtil.toJsonStr(request));
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(StringUtils.isEmpty(jsonObject.getStr("data"))){
return 0;
}
return jsonObject.getInt("data");
}
}