From 181c04bf7691dc87d6fcf31a5237a66442f68a6a Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期五, 05 六月 2020 09:35:27 +0800
Subject: [PATCH] 优化门禁对接 逻辑调整

---
 service-api/src/main/java/com/java110/api/listener/machineTranslate/MachineQueryUserInfoListener.java |  335 ++++++++++---------------------------------------------
 1 files changed, 61 insertions(+), 274 deletions(-)

diff --git a/service-api/src/main/java/com/java110/api/listener/machineTranslate/MachineQueryUserInfoListener.java b/service-api/src/main/java/com/java110/api/listener/machineTranslate/MachineQueryUserInfoListener.java
index d633143..a8dd40b 100644
--- a/service-api/src/main/java/com/java110/api/listener/machineTranslate/MachineQueryUserInfoListener.java
+++ b/service-api/src/main/java/com/java110/api/listener/machineTranslate/MachineQueryUserInfoListener.java
@@ -2,37 +2,32 @@
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.api.bmo.machineTranslate.IApplicationKeyMachineTranslateBMO;
+import com.java110.api.bmo.machineTranslate.IOwnerMachineTranslateBMO;
+import com.java110.api.bmo.machineTranslate.IVisitMachineTranslateBMO;
 import com.java110.core.annotation.Java110Listener;
 import com.java110.core.context.DataFlowContext;
+import com.java110.core.event.service.api.ServiceDataFlowEvent;
 import com.java110.core.smo.community.ICommunityInnerServiceSMO;
 import com.java110.core.smo.file.IFileInnerServiceSMO;
 import com.java110.core.smo.file.IFileRelInnerServiceSMO;
 import com.java110.core.smo.hardwareAdapation.IApplicationKeyInnerServiceSMO;
 import com.java110.core.smo.hardwareAdapation.IMachineInnerServiceSMO;
 import com.java110.core.smo.hardwareAdapation.IMachineTranslateInnerServiceSMO;
-import com.java110.core.smo.owner.IOwnerInnerServiceSMO;
-
 import com.java110.dto.community.CommunityDto;
-import com.java110.dto.file.FileDto;
-import com.java110.dto.file.FileRelDto;
-import com.java110.dto.hardwareAdapation.ApplicationKeyDto;
-import com.java110.dto.hardwareAdapation.MachineTranslateDto;
-import com.java110.dto.owner.OwnerDto;
-import com.java110.core.event.service.api.ServiceDataFlowEvent;
-import com.java110.utils.cache.CommonCache;
-import com.java110.utils.cache.MappingCache;
+import com.java110.dto.machine.MachineTranslateDto;
+import com.java110.dto.machine.MachineUserResultDto;
 import com.java110.utils.constant.ServiceCodeMachineTranslateConstant;
-import com.java110.utils.exception.ConfigDataException;
 import com.java110.utils.util.Assert;
-import com.java110.utils.util.DateUtil;
-import com.java110.utils.util.StringUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.*;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.client.RestTemplate;
 
-import java.text.ParseException;
 import java.util.List;
 import java.util.Map;
 
@@ -46,6 +41,11 @@
 
     private static Logger logger = LoggerFactory.getLogger(MachineQueryUserInfoListener.class);
 
+    public static final String TYPE_OWNER = "8899";//涓氫富浜鸿劯
+    public static final String TYPE_APPLICATION_KEY = "7788";//鐢宠閽ュ寵
+    public static final String TYPE_VISIT = "6677";//璁垮浜鸿劯
+    public static final String TYPE_STAFF = "5566";//鍛樺伐浜鸿劯
+
 
     private static final String DEFAULT_DOMAIN = "YUNLUN";
 
@@ -55,8 +55,6 @@
     @Autowired
     private IMachineInnerServiceSMO machineInnerServiceSMOImpl;
 
-    @Autowired
-    private IOwnerInnerServiceSMO ownerInnerServiceSMOImpl;
 
     @Autowired
     private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;
@@ -72,6 +70,16 @@
 
     @Autowired
     private IApplicationKeyInnerServiceSMO applicationKeyInnerServiceSMOImpl;
+
+
+    @Autowired
+    private IOwnerMachineTranslateBMO ownerMachineTranslateBMOImpl;
+
+    @Autowired
+    private IApplicationKeyMachineTranslateBMO applicationKeyMachineTranslateBMOImpl;
+
+    @Autowired
+    private IVisitMachineTranslateBMO visitMachineTranslateBMOImpl;
 
     /**
      * @param event   浜嬩欢瀵硅薄
@@ -99,278 +107,64 @@
         JSONArray data = null;
         String communityId = reqJson.containsKey("communityId") ? reqJson.getString("communityId") : reqHeader.get("communityId");
         HttpHeaders httpHeaders = super.getHeader(context);
-        //妫�鏌ユ槸鍚﹀瓨鍦ㄨ鐢ㄦ埛
-        OwnerDto ownerDto = new OwnerDto();
-        ownerDto.setCommunityId(communityId);
-        ownerDto.setMemberId(reqJson.getString("faceid"));
-        List<OwnerDto> ownerDtos = ownerInnerServiceSMOImpl.queryOwnerMembers(ownerDto);
-
-        if (ownerDtos != null && ownerDtos.size() == 1) {
-            getOwnerPhoto(communityId, outParam, context, httpHeaders, reqJson, ownerDtos.get(0));
+        CommunityDto communityDto = new CommunityDto();
+        communityDto.setCommunityId(communityId);
+        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
+        if (communityDtos == null || communityDtos.size() != 1) {
+            outParam.put("code", -1);
+            outParam.put("message", "鏈壘鍒扮浉搴斿皬鍖轰俊鎭�");
+            responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
+            context.setResponseEntity(responseEntity);
             return;
         }
-        ApplicationKeyDto applicationKeyDto = new ApplicationKeyDto();
-        applicationKeyDto.setCommunityId(communityId);
-        applicationKeyDto.setApplicationKeyId(reqJson.getString("faceid"));
-        List<ApplicationKeyDto> applicationKeyDtos = applicationKeyInnerServiceSMOImpl.queryApplicationKeys(applicationKeyDto);
 
-        if (applicationKeyDtos == null || applicationKeyDtos.size() != 1) {
+
+        MachineTranslateDto machineTranslateDto = new MachineTranslateDto();
+        machineTranslateDto.setCommunityId(communityId);
+        machineTranslateDto.setObjId(reqJson.getString("faceid"));
+        machineTranslateDto.setState("30000");//鏌ヨ鍚屾涓�
+        List<MachineTranslateDto> machineTranslateDtos = machineTranslateInnerServiceSMOImpl.queryMachineTranslates(machineTranslateDto);
+
+        if (machineTranslateDtos == null || machineTranslateDtos.size() < 1) {
             outParam.put("code", -1);
             outParam.put("message", "鏈壘鍒扮浉搴斾汉鑴镐俊鎭�");
             responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
             context.setResponseEntity(responseEntity);
             return;
         }
-        //鏌ヨ閽ュ寵浜鸿劯
-        getApplicationKeyPhoto(communityId, outParam, context, httpHeaders, reqJson, applicationKeyDtos.get(0));
 
+        MachineTranslateDto tmpMachineTranslateDto = machineTranslateDtos.get(0);
 
-    }
+        reqJson.put("communityId", communityId);
+        reqJson.put("communityName", communityDtos.get(0).getName());
+        reqJson.put("machineCode", httpHeaders.get("machinecode"));
 
-    private void getApplicationKeyPhoto(String communityId, JSONObject outParam,
-                                        DataFlowContext context,
-                                        HttpHeaders httpHeaders, JSONObject reqJson, ApplicationKeyDto applicationKeyDto) {
-        Map<String, String> reqHeader = context.getRequestHeaders();
-        ResponseEntity<String> responseEntity = null;
-        CommunityDto communityDto = new CommunityDto();
-        communityDto.setCommunityId(communityId);
-        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
-        if (communityDtos == null || communityDtos.size() != 1) {
-            outParam.put("code", -1);
-            outParam.put("message", "鏈壘鍒扮浉搴斿皬鍖轰俊鎭�");
-            responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
-            context.setResponseEntity(responseEntity);
-            return;
+        MachineUserResultDto machineUserResultDto = null;
+        switch (tmpMachineTranslateDto.getTypeCd()) {
+            case TYPE_OWNER:
+                machineUserResultDto = ownerMachineTranslateBMOImpl.getPhotoInfo(reqJson);
+                break;
+            case TYPE_APPLICATION_KEY:
+                machineUserResultDto = applicationKeyMachineTranslateBMOImpl.getPhotoInfo(reqJson);
+            case TYPE_VISIT:
+                machineUserResultDto = visitMachineTranslateBMOImpl.getPhotoInfo(reqJson);
+            default:
+                break;
         }
-        FileRelDto fileRelDto = new FileRelDto();
-        fileRelDto.setObjId(reqJson.getString("faceid"));
-        fileRelDto.setRelTypeCd("30000");
-        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
-        if (fileRelDtos == null || fileRelDtos.size() != 1) {
+
+        //妫�鏌ユ槸鍚﹀瓨鍦ㄨ鐢ㄦ埛
+        if (machineUserResultDto == null) {
             outParam.put("code", -1);
-            outParam.put("message", "鏈壘鍒伴挜鍖欑収鐗囷紝鍙兘鏈綍鍏ョ収鐗�");
-            responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
-            context.setResponseEntity(responseEntity);
-            return;
-        }
-        FileDto fileDto = new FileDto();
-        fileDto.setFileId(fileRelDtos.get(0).getFileSaveName());
-        fileDto.setCommunityId(communityId);
-        List<FileDto> fileDtos = fileInnerServiceSMOImpl.queryFiles(fileDto);
-        if (fileDtos == null || fileDtos.size() != 1) {
-            outParam.put("code", -1);
-            outParam.put("message", "鏈壘鍒扮収鐗囷紝鍙兘鏈綍鍏ョ収鐗�");
+            outParam.put("message", "鏈壘鍒扮浉搴斾汉鑴镐俊鎭�");
             responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
             context.setResponseEntity(responseEntity);
             return;
         }
 
-        JSONObject dataObj = new JSONObject();
-        dataObj.put("userid", applicationKeyDto.getApplicationKeyId());
-        dataObj.put("groupid", communityId);
-        dataObj.put("group", communityDtos.get(0).getName());
-        dataObj.put("name", applicationKeyDto.getName());
-        dataObj.put("faceBase64", "data:image/jpeg;base64," + fileDtos.get(0).getContext()
-                .replace("data:image/webp;base64,", "")
-                .replace("data:image/png;base64,", "")
-                .replace("data:image/jpeg;base64,", ""));
-        dataObj.put("idNumber", applicationKeyDto.getIdCard());
-        dataObj.put("startTime", applicationKeyDto.getCreateTime().getTime());
-        try {
-            dataObj.put("endTime", DateUtil.getLastDate().getTime());
-        } catch (ParseException e) {
-            dataObj.put("endTime", 2145891661);
-        }
-        dataObj.put("remarks", "HC灏忓尯绠$悊绯荤粺");
-        dataObj.put("reserved", applicationKeyDto.getApplicationKeyId());
 
 
-        //鐗规畩澶勭悊鏄惁鑾峰彇鐗瑰緛鍊�
-        if ("ON".equals(MappingCache.getValue(DEFAULT_DOMAIN, "getFeature"))) {
-            getFeature(dataObj);
-        }
-        outParam.put("data", dataObj);
-        //灏� 璁惧 寰呭悓姝� 鏀逛负鍚屾涓�
-        MachineTranslateDto tmpMtDto = new MachineTranslateDto();
-        tmpMtDto.setMachineCode(reqHeader.get("machinecode"));
-        tmpMtDto.setCommunityId(communityId);
-        tmpMtDto.setObjId(applicationKeyDto.getApplicationKeyId());
-        tmpMtDto.setState("20000");
-        machineTranslateInnerServiceSMOImpl.updateMachineTranslateState(tmpMtDto);
-
-        responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
+        responseEntity = new ResponseEntity<>(JSONObject.toJSONString(machineUserResultDto), httpHeaders, HttpStatus.OK);
         context.setResponseEntity(responseEntity);
-    }
-
-    private void getOwnerPhoto(String communityId, JSONObject outParam,
-                               DataFlowContext context,
-                               HttpHeaders httpHeaders, JSONObject reqJson, OwnerDto ownerDto) {
-        Map<String, String> reqHeader = context.getRequestHeaders();
-        ResponseEntity<String> responseEntity = null;
-        CommunityDto communityDto = new CommunityDto();
-        communityDto.setCommunityId(communityId);
-        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
-        if (communityDtos == null || communityDtos.size() != 1) {
-            outParam.put("code", -1);
-            outParam.put("message", "鏈壘鍒扮浉搴斿皬鍖轰俊鎭�");
-            responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
-            context.setResponseEntity(responseEntity);
-            return;
-        }
-        FileRelDto fileRelDto = new FileRelDto();
-        fileRelDto.setObjId(reqJson.getString("faceid"));
-        fileRelDto.setRelTypeCd("10000");
-        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
-        if (fileRelDtos == null || fileRelDtos.size() != 1) {
-            outParam.put("code", -1);
-            outParam.put("message", "鏈壘鍒颁笟涓荤収鐗囷紝鍙兘鏈綍鍏ョ収鐗�");
-            responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
-            context.setResponseEntity(responseEntity);
-            return;
-        }
-        FileDto fileDto = new FileDto();
-        fileDto.setFileId(fileRelDtos.get(0).getFileSaveName());
-        fileDto.setFileSaveName(fileRelDtos.get(0).getFileSaveName());
-        fileDto.setCommunityId(communityId);
-        List<FileDto> fileDtos = fileInnerServiceSMOImpl.queryFiles(fileDto);
-        if (fileDtos == null || fileDtos.size() != 1) {
-            outParam.put("code", -1);
-            outParam.put("message", "鏈壘鍒颁笟涓荤収鐗囷紝鍙兘鏈綍鍏ョ収鐗�");
-            responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
-            context.setResponseEntity(responseEntity);
-            return;
-        }
-
-        JSONObject dataObj = new JSONObject();
-        dataObj.put("userid", ownerDto.getMemberId());
-        dataObj.put("groupid", communityId);
-        dataObj.put("group", communityDtos.get(0).getName());
-        dataObj.put("name", ownerDto.getName());
-        String tmpImg = fileDtos.get(0).getContext();
-        dataObj.put("faceBase64",  tmpImg);
-        dataObj.put("idNumber", ownerDto.getIdCard());
-        dataObj.put("startTime", ownerDto.getCreateTime().getTime());
-        try {
-            dataObj.put("endTime", DateUtil.getLastDate().getTime());
-        } catch (ParseException e) {
-            dataObj.put("endTime", 2145891661);
-        }
-        dataObj.put("remarks", "HC灏忓尯绠$悊绯荤粺");
-        dataObj.put("reserved", ownerDto.getMemberId());
-
-
-        //鐗规畩澶勭悊鏄惁鑾峰彇鐗瑰緛鍊�
-        if ("ON".equals(MappingCache.getValue(DEFAULT_DOMAIN, "getFeature"))) {
-            getFeature(dataObj);
-        }
-        outParam.put("data", dataObj);
-        //灏� 璁惧 寰呭悓姝� 鏀逛负鍚屾涓�
-        MachineTranslateDto tmpMtDto = new MachineTranslateDto();
-        tmpMtDto.setMachineCode(reqHeader.get("machinecode"));
-        tmpMtDto.setCommunityId(communityId);
-        tmpMtDto.setObjId(ownerDto.getMemberId());
-        tmpMtDto.setState("20000");
-        machineTranslateInnerServiceSMOImpl.updateMachineTranslateState(tmpMtDto);
-
-        responseEntity = new ResponseEntity<>(outParam.toJSONString(), httpHeaders, HttpStatus.OK);
-        context.setResponseEntity(responseEntity);
-    }
-
-    /**
-     * 璋冪敤绗笁鏂瑰钩鍙拌幏鍙栫収鐗囩壒寰�
-     *
-     * @param dataObj
-     */
-    private void getFeature(JSONObject dataObj) {
-
-        logger.debug("寮�濮嬭幏鍙栫収鐗囩壒寰�");
-
-        String photo = dataObj.getString("faceBase64");
-
-        photo = photo
-                .replace("data:image/webp;base64,", "")
-                .replace("data:image/png;base64,", "")
-                .replace("data:image/jpeg;base64,", "");
-
-        //浠巖edis 涓幏鍙杢oken锛屾病鏈夊垯閲嶆柊鑾峰彇
-        String token = getToken();
-        logger.debug("鑾峰彇鍒扮殑token涓猴細" + token);
-
-        String url = MappingCache.getValue(DEFAULT_DOMAIN, "hc_feature_url");
-        if (StringUtil.isEmpty(url)) {
-            throw new ConfigDataException(199, "鏈厤缃甴c_feature_url鍙傛暟");
-        }
-
-        HttpHeaders headers = new HttpHeaders();
-        headers.add("accessToken", token);
-        headers.add("Content-Type", "application/json");
-        JSONObject paramIn = new JSONObject();
-        paramIn.put("image", photo);
-        paramIn.put("imageType", "BASE64");
-        HttpEntity httpEntity = new HttpEntity(paramIn.toJSONString(), headers);
-        ResponseEntity<String> responseEntity = restTemplateNoLoadBalanced.exchange(url, HttpMethod.POST, httpEntity, String.class);
-
-        if (responseEntity.getStatusCode() != HttpStatus.OK) {
-            throw new RuntimeException("鏌ヨ鐗瑰緛token澶辫触" + responseEntity.toString());
-        }
-
-        String outBody = responseEntity.getBody();
-
-        JSONObject outObj = JSONObject.parseObject(outBody);
-        if (!outObj.containsKey("code")) {
-            throw new RuntimeException("鏌ヨ鐗瑰緛澶辫触" + outBody);
-        }
-
-        if (!"0".equals(outObj.getString("code"))) {
-            throw new RuntimeException("鏌ヨ鐗瑰緛澶辫触" + outBody);
-        }
-
-        dataObj.put("faceBase64", outObj.getString("feature"));
-        logger.debug("鑾峰彇鍒扮殑鏁版嵁dataObj涓猴細" + dataObj.toJSONString());
-
-    }
-
-    /**
-     * 鑾峰彇token
-     *
-     * @return
-     */
-    private String getToken() {
-        String token = CommonCache.getValue("hc_getFeature_token");
-        if (!StringUtil.isEmpty(token)) {
-            return token;
-        }
-
-        String url = MappingCache.getValue(DEFAULT_DOMAIN, "hc_token_url");
-        if (StringUtil.isEmpty(url)) {
-            throw new ConfigDataException(199, "鏈厤缃甴c_token_url鍙傛暟");
-        }
-
-        url += ("?clientId=" + MappingCache.getValue(DEFAULT_DOMAIN, "hc_clientId"));
-        url += ("&clientSecret=" + MappingCache.getValue(DEFAULT_DOMAIN, "hc_clientSecret"));
-
-        String resultInfo = restTemplateNoLoadBalanced.getForObject(url, String.class);
-
-        logger.debug("璋冪敤 鑾峰彇鐗瑰緛token鎺ュ彛锛歶rl = " + url + "杩斿洖鎶ユ枃锛�" + resultInfo);
-
-        JSONObject resultObj = JSONObject.parseObject(resultInfo);
-
-        if (!resultObj.containsKey("code")) {
-            throw new RuntimeException("鏌ヨ鐗瑰緛token澶辫触" + resultInfo);
-        }
-
-        if (!"0".equals(resultObj.getString("code"))) {
-            throw new RuntimeException("鏌ヨ鐗瑰緛token澶辫触" + resultInfo);
-
-        }
-
-        token = resultObj.getString("accessToken");
-        int expiresIn = resultObj.getInteger("expiresIn");
-        CommonCache.setValue("hc_getFeature_token", token, (expiresIn / 1000) - 10);//鍑忓幓10绉� 闃叉缃戠粶瓒呮椂 鏃堕棿涓嶄竴鑷撮棶棰�
-
-
-        return token;
-
     }
 
     @Override
@@ -404,13 +198,6 @@
         this.machineInnerServiceSMOImpl = machineInnerServiceSMOImpl;
     }
 
-    public IOwnerInnerServiceSMO getOwnerInnerServiceSMOImpl() {
-        return ownerInnerServiceSMOImpl;
-    }
-
-    public void setOwnerInnerServiceSMOImpl(IOwnerInnerServiceSMO ownerInnerServiceSMOImpl) {
-        this.ownerInnerServiceSMOImpl = ownerInnerServiceSMOImpl;
-    }
 
     public ICommunityInnerServiceSMO getCommunityInnerServiceSMOImpl() {
         return communityInnerServiceSMOImpl;

--
Gitblit v1.8.0