|  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.haikang.model.HKConstants; | 
 |  |  | import com.doumee.core.haikang.model.param.BaseListPageResponse; | 
 |  |  | import com.doumee.core.haikang.model.param.BaseResponse; | 
 |  |  | import com.doumee.core.haikang.model.param.request.*; | 
 |  |  | import com.doumee.core.haikang.model.param.request.event.visit.EventVisitInfoRequest; | 
 |  |  | import com.doumee.core.haikang.model.param.respose.*; | 
 |  |  | import com.doumee.core.haikang.service.HKService; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import com.doumee.core.utils.DESUtil; | 
 |  |  | import com.doumee.core.utils.DateUtil; | 
 |  |  | import com.doumee.core.utils.ImageBase64Util; | 
 |  |  | import com.doumee.core.wx.wxPlat.WxPlatConstants; | 
 |  |  | import com.doumee.core.wx.wxPlat.WxPlatNotice; | 
 |  |  | import com.doumee.dao.business.DeviceRoleMapper; | 
 |  |  | import com.doumee.dao.business.RetentionMapper; | 
 |  |  | import com.doumee.dao.business.WxNoticeConfigMapper; | 
 |  |  | import com.doumee.dao.business.join.VisitsJoinMapper; | 
 |  |  | import com.doumee.dao.business.model.DeviceRole; | 
 |  |  | import com.doumee.dao.business.model.Member; | 
 |  |  | 
 |  |  | public class HkSyncVisitServiceImpl extends HkSyncBaseServiceImpl { | 
 |  |  |     @Autowired | 
 |  |  |     private VisitsJoinMapper visitsMapper; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private WxPlatNotice wxPlatNotice; | 
 |  |  |     @Autowired | 
 |  |  |     private WxNoticeConfigMapper wxNoticeConfigMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private RetentionMapper retentionMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz; | 
 |  |  |     @Autowired | 
 |  |  |     private DeviceRoleMapper deviceRoleMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private WxPlatNotice wxPlatNotice; | 
 |  |  |     /** | 
 |  |  |      * 同步海康访客信息是否已签离状态 | 
 |  |  |      */ | 
 |  |  | 
 |  |  |                 AppointmentInfoResponse model =  getVisitRecord(c.getHkId()); | 
 |  |  |                 if(model == null){ | 
 |  |  |                     continue; | 
 |  |  |                    /* Visits update = new Visits(); | 
 |  |  |                     //已失效 | 
 |  |  |                     update.setStatus(Constants.VisitStatus.invalid); | 
 |  |  |                     update.setEditDate(date); | 
 |  |  |                     update.setId(c.getId()); | 
 |  |  |                     update.setRemark("预约已被清除"); | 
 |  |  |                     visitsMapper.updateById(update); | 
 |  |  |                     continue;*/ | 
 |  |  |                 } | 
 |  |  |                 /** | 
 |  |  |                  *      * 0:待审核(预约待审批)、 | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 查询ICCM访客数据状态 | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public void getOutTimeVisitRecordIccm(){ | 
 |  |  |         if(Constants.DEALING_HK_VISIT_EXPIRE){ | 
 |  |  |             return   ; | 
 |  |  |         } | 
 |  |  |         Constants.DEALING_HK_VISIT_EXPIRE =true; | 
 |  |  |         try { | 
 |  |  |             //查询所有需要同步的数据 | 
 |  |  |             List<Visits> list = getExpireVisitList(); | 
 |  |  |             //按照父级申请分批处理每次申请数据 | 
 |  |  |             if(list ==null || list.size()==0){ | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |             Date date = new Date(); | 
 |  |  |             for(Visits c : list) { | 
 |  |  |                 //发起海康预约接口(需要登记),根据预约返回接口封装申请记录更新字段 | 
 |  |  |                 IccmAppointmentListResponse data =  getVisitRecordIccm(c.getHkId()); | 
 |  |  |                 if(data == null || data.getVisitorList() ==null || data.getVisitorList().size()==0){ | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |                 /** | 
 |  |  |                  * 访客状态(0:未签到,1:已签到,2:已签退,3:滞留,4:未访问,5:自动签离,6:未签退) | 
 |  |  |                  */ | 
 |  |  |                 IccmAppointmentVisitorResponse model = data.getVisitorList().get(0); | 
 |  |  |                 if (model.getVisitorStatus()!=null && ",4,".contains( ","  +model.getVisitorStatus()+"," )){ | 
 |  |  |                     //对相应状态下的数据进行【已失效】处理 | 
 |  |  |                     Visits update = new Visits(); | 
 |  |  |                     //已失效 | 
 |  |  |                     update.setStatus(Constants.VisitStatus.invalid); | 
 |  |  |                     update.setEditDate(date); | 
 |  |  |                     update.setId(c.getId()); | 
 |  |  |                     update.setInDate(DateUtil.getISO8601DateByStr2(data.getVisitStartTime())); | 
 |  |  |                     update.setOutDate(DateUtil.getISO8601DateByStr2(data.getVisitEndTime())); | 
 |  |  |                     update.setRemark("超时未登记"); | 
 |  |  |                     visitsMapper.updateById(update); | 
 |  |  |                 } | 
 |  |  |                 if (model.getVisitorStatus()!=null && ",2,5,".contains( ","  +model.getVisitorStatus()+"," )){ | 
 |  |  |                     // 对相应状态下的数据进行【已签离】处理 | 
 |  |  |                     if(!Constants.equalsInteger(c.getStatus(),Constants.VisitStatus.signout)){ | 
 |  |  |                         Visits update = new Visits(); | 
 |  |  |                         //已签离 | 
 |  |  |                         update.setStatus(Constants.VisitStatus.signout); | 
 |  |  |                         update.setEditDate(date); | 
 |  |  |                         update.setId(c.getId()); | 
 |  |  |                         update.setInDate(DateUtil.getISO8601DateByStr2(data.getVisitStartTime())); | 
 |  |  |                         update.setOutDate(DateUtil.getISO8601DateByStr2(data.getVisitEndTime())); | 
 |  |  |                         update.setOutInfo(model.getVisitorStatus().equals(5)?"过期自动签离":"查询已签离"); | 
 |  |  |                         update.setRemark("已签离"); | 
 |  |  |                         visitsMapper.updateById(update); | 
 |  |  |  | 
 |  |  |                         //先删除原有的在场人员(普通访客) | 
 |  |  |                         retentionMapper.delete(new UpdateWrapper<Retention>().lambda() | 
 |  |  |                                 .eq(Retention::getType,Constants.memberType.visitor) | 
 |  |  |                                 .eq(Retention::getMemberId,c.getMemberId())); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 if (model.getVisitorStatus()!=null&& ",1,3,6,".contains( ","  +model.getVisitorStatus()+"," )){ | 
 |  |  |                     //如果已登记 | 
 |  |  |                     if(!Constants.equalsInteger(c.getStatus(),Constants.VisitStatus.signin)){ | 
 |  |  |                         Visits update = new Visits(); | 
 |  |  |                         //已失效 | 
 |  |  |                         update.setStatus(Constants.VisitStatus.signin); | 
 |  |  |                         update.setEditDate(date); | 
 |  |  |                         update.setId(c.getId()); | 
 |  |  |                         update.setInDate(DateUtil.getISO8601DateByStr2(data.getVisitStartTime())); | 
 |  |  |                         update.setOutDate(DateUtil.getISO8601DateByStr2(data.getVisitEndTime())); | 
 |  |  |                         update.setRemark("已签到"); | 
 |  |  |                         visitsMapper.updateById(update); | 
 |  |  |                         //先删除原有的在场人员(普通访客) | 
 |  |  |                         retentionMapper.delete(new UpdateWrapper<Retention>().lambda() | 
 |  |  |                                 .eq(Retention::getType,Constants.memberType.visitor) | 
 |  |  |                                 .eq(Retention::getMemberId,c.getMemberId())); | 
 |  |  |                         //再插入最新的在厂人员 | 
 |  |  |                         retentionMapper.insert(getRetentionModelByVisitRequest(c,update.getInDate())); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         }finally { | 
 |  |  |             Constants.DEALING_HK_VISIT_EXPIRE =false; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private Retention getRetentionModelByVisitRequest(Visits visits,Date date) { | 
 |  |  |         Retention retention = new Retention(); | 
 |  |  |         retention.setIsdeleted(Constants.ZERO); | 
 |  |  | 
 |  |  |         } | 
 |  |  |         return response.getData().getList().get(0); | 
 |  |  |     } | 
 |  |  |     public IccmAppointmentListResponse getVisitRecordIccm(String orderId){ | 
 |  |  |         //分页遍历循环查询所有门禁设备数据 | 
 |  |  |         if(StringUtils.isBlank(orderId)){ | 
 |  |  |             return  null; | 
 |  |  |         } | 
 |  |  |         IccmAppointmentListRequest param =  new IccmAppointmentListRequest(); | 
 |  |  |         param.setPageNo(1); | 
 |  |  |         param.setPageSize(1); | 
 |  |  |         param.setOrderId(orderId); | 
 |  |  |         BaseResponse<BaseListPageResponse<IccmAppointmentListResponse>> response = HKService.iccmAppointmentRecords(param); | 
 |  |  |         if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)  ){ | 
 |  |  |            return null; | 
 |  |  |         } | 
 |  |  |         if(response.getData() == null || response.getData().getList() == null|| response.getData().getList().size() ==0){ | 
 |  |  |            return  null; | 
 |  |  |         } | 
 |  |  |         return response.getData().getList().get(0); | 
 |  |  |     } | 
 |  |  |     @Override | 
 |  |  |     public  void syncVisitData(){ | 
 |  |  |         if(Constants.DEALING_HK_VISIT){ | 
 |  |  |             return   ; | 
 |  |  |         } | 
 |  |  |         Constants.DEALING_HK_VISIT =true; | 
 |  |  |         String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode() | 
 |  |  |         String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_LOCAL_RESOURCE_PATH).getCode() | 
 |  |  |                 +systemDictDataBiz.queryByCode(Constants.FTP,Constants.MEMBER_IMG).getCode(); | 
 |  |  |         List<DeviceRole> roleList = deviceRoleMapper.selectList(new QueryWrapper<DeviceRole>().lambda() | 
 |  |  |                 .eq(DeviceRole::getType, Constants.ONE)); | 
 |  |  |         //标记是否走免登记预约 | 
 |  |  | //        String applyType =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MDJ_VISIT_REQUIRED).getCode(); | 
 |  |  | //        String applyLwType =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MDJ_LW_REQUIRED).getCode(); | 
 |  |  |         try { | 
 |  |  |             //查询所有需要同步的数据 | 
 |  |  |             List<Visits> list = getVisitList(); | 
 |  |  |             //按照父级申请分批处理每次申请数据 | 
 |  |  | //            List<Visits> list = getParentList(alllist); | 
 |  |  |             if(list ==null || list.size()==0){ | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  | 
 |  |  | //                getUpdateModelByResponse(c,date,roleList,path); | 
 |  |  |                 getUpdateModelByResponseIccm(c,date,roleList,path); | 
 |  |  |                 visitsMapper.updateById(c); | 
 |  |  |                 if(Objects.isNull(c.getParentId())){ | 
 |  |  |                     wxPlatNotice.sendVisitAuditTemplateNotice(c, | 
 |  |  |                             systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_PLATFORM_PREFIX).getCode(), | 
 |  |  |                             systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_PLATFORM_AUDIT_VISIT).getCode()); | 
 |  |  |                 //给申请人发送微信公众号 下发权限失败 | 
 |  |  |                 if(StringUtils.isNotBlank(c.getOpenid()) && Constants.equalsInteger(c.getStatus(),Constants.VisitStatus.xfFail)){ | 
 |  |  |                     wxPlatNotice.sendVisitTemplateNotice(systemDictDataBiz, | 
 |  |  |                             wxNoticeConfigMapper,c, WxPlatConstants.visitContent.visitApplyHkFail, | 
 |  |  |                             systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_PLATFORM_ACCESS_TOKEN).getCode(), | 
 |  |  |                             Arrays.asList(c.getOpenid().split(",")) | 
 |  |  |                     ); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  | 
 |  |  |                     && response.getData().getAppointmentInfoList() !=null | 
 |  |  |                     && response.getData().getAppointmentInfoList().size()>0)){ | 
 |  |  |                 id =response.getData().getAppointmentInfoList().get(0).getOrderId(); | 
 |  |  |                 qrcode =response.getData().getAppointmentInfoList().get(0).getQRCode(); | 
 |  |  |                 qrcode =response.getData().getAppointmentInfoList().get(0).getVerificationCode(); | 
 |  |  | //                qrcode =response.getData().getAppointmentInfoList().get(0).getQRCode(); | 
 |  |  |             } | 
 |  |  |             reson = response!=null?JSONObject.toJSONString(response):""; | 
 |  |  |         }else{ | 
 |  |  | 
 |  |  |         c.setHkStatus(Constants.ONE); | 
 |  |  |         c.setHkDate(date); | 
 |  |  |     } | 
 |  |  |     private void getUpdateModelByResponseIccm(  Visits c,Date date, List<DeviceRole> roleList,String path ) { | 
 |  |  |     public static void getUpdateModelByResponseIccm(  Visits c,Date date, List<DeviceRole> roleList,String path ) { | 
 |  |  |         String code = null; | 
 |  |  |         String id = null; | 
 |  |  |         String qrcode = null; | 
 |  |  | 
 |  |  |                     && response.getData().getAppointmentInfoList() !=null | 
 |  |  |                     && response.getData().getAppointmentInfoList().size()>0)){ | 
 |  |  |                 id =response.getData().getOrderId(); | 
 |  |  |                 qrcode =response.getData().getAppointmentInfoList().get(0).getQRCode(); | 
 |  |  | //                qrcode =response.getData().getAppointmentInfoList().get(0).getQRCode(); | 
 |  |  |                 qrcode =response.getData().getAppointmentInfoList().get(0).getVerificationCode(); | 
 |  |  |             } | 
 |  |  |             reson = response!=null?JSONObject.toJSONString(response):""; | 
 |  |  |         }else{ | 
 |  |  | 
 |  |  |         request.setVisitorPermissionSet(getVisitPermissonRequest(roleList,c)); | 
 |  |  |         return  request; | 
 |  |  |     } | 
 |  |  |     private IccmAppointmentMDJRequest getHkMDJRequestParamIccm(Visits c,List<DeviceRole> roleList,String path) { | 
 |  |  |     public static IccmAppointmentMDJRequest getHkMDJRequestParamIccm(Visits c,List<DeviceRole> roleList,String path) { | 
 |  |  |         IccmAppointmentMDJRequest request = new IccmAppointmentMDJRequest(); | 
 |  |  |         IccmAppointmentVistorRequest info =getRequestInfoByVisitIccm(c,path); | 
 |  |  |         if(info == null ){ | 
 |  |  | 
 |  |  |         request.setVisitStartTime(DateUtil.getISO8601Timestamp2(c.getStarttime())); | 
 |  |  |         request.setVisitEndTime(DateUtil.getISO8601Timestamp2(c.getEndtime())); | 
 |  |  |         request.setReceptionistId(c.getReceptMemberHkId());//被访人海康编码 | 
 |  |  |         if(StringUtils.isNotBlank(c.getReason())){ | 
 |  |  |        /* if(StringUtils.isNotBlank(c.getReason())){ | 
 |  |  |             char[] charArray = c.getReason().toCharArray(); | 
 |  |  |             int length = charArray.length; | 
 |  |  |             if(length>32){ | 
 |  |  | 
 |  |  |             }else{ | 
 |  |  |                 request.setVisitPurpose(c.getReason()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         }*/ | 
 |  |  |         request.setVisitorInfo(info); | 
 |  |  |         request.setVisitorPermissionSet(getVisitPermissonRequestIccm(roleList,c)); | 
 |  |  |         return  request; | 
 |  |  | 
 |  |  |         } | 
 |  |  |         return  p; | 
 |  |  |     } | 
 |  |  |     private IccmPermissionSetRequest getVisitPermissonRequestIccm(List<DeviceRole> roleList, Visits c) { | 
 |  |  |     public static IccmPermissionSetRequest getVisitPermissonRequestIccm(List<DeviceRole> roleList, Visits c) { | 
 |  |  |         IccmPermissionSetRequest p = new IccmPermissionSetRequest(); | 
 |  |  |         //获取权限组集合 | 
 |  |  |         String[] roles = getHkRoles(c.getDoors(),roleList); | 
 |  |  | 
 |  |  |         request.setVisitorInfoList(infolist); | 
 |  |  |         return  request; | 
 |  |  |     } | 
 |  |  |     private IccmAppointmentRequest getHkRequestParaIccm(Visits c,List<DeviceRole> roleList,String path ) { | 
 |  |  |     public static  IccmAppointmentRequest getHkRequestParaIccm(Visits c,List<DeviceRole> roleList,String path ) { | 
 |  |  |         IccmAppointmentRequest request = new IccmAppointmentRequest(); | 
 |  |  |         //申请人信息 | 
 |  |  |         IccmAppointmentVistorRequest info =getRequestInfoByVisitIccm(c,path); | 
 |  |  | 
 |  |  |             //人脸为空,不能进行推送 | 
 |  |  |             return  null; | 
 |  |  |         } | 
 |  |  |         request.setVisitStartTime(DateUtil.getISO8601Timestamp(c.getStarttime())); | 
 |  |  |         request.setVisitEndTime(DateUtil.getISO8601Timestamp(c.getEndtime())); | 
 |  |  |         request.setVisitStartTime(DateUtil.getISO8601Timestamp2(c.getStarttime())); | 
 |  |  |         request.setVisitEndTime(DateUtil.getISO8601Timestamp2(c.getEndtime())); | 
 |  |  |         request.setReceptionistId(c.getReceptMemberHkId());//被访人海康编码 | 
 |  |  |  | 
 |  |  |         if(StringUtils.isNotBlank(c.getReason())){ | 
 |  |  |       /*  if(StringUtils.isNotBlank(c.getReason())){ | 
 |  |  |             char[] charArray = c.getReason().toCharArray(); | 
 |  |  |             int length = charArray.length; | 
 |  |  |             if(length>32){ | 
 |  |  | 
 |  |  |             }else{ | 
 |  |  |                 request.setVisitPurpose(c.getReason()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         }*/ | 
 |  |  |         //获取权限组集合 | 
 |  |  |         request.setVisitorPermissionSet(getVisitPermissonRequestIccm(roleList,c)); | 
 |  |  |         List<IccmAppointmentVistorRequest> infolist = new ArrayList<>(); | 
 |  |  | 
 |  |  |  | 
 |  |  |         return info; | 
 |  |  |     } | 
 |  |  |     private IccmAppointmentVistorRequest getRequestInfoByVisitIccm(Visits c,String path) { | 
 |  |  |     public static IccmAppointmentVistorRequest getRequestInfoByVisitIccm(Visits c,String path) { | 
 |  |  |         IccmAppointmentVistorRequest info = new IccmAppointmentVistorRequest(); | 
 |  |  |         //人脸数据 | 
 |  |  |         if(StringUtils.isBlank(c.getName()) | 
 |  |  | 
 |  |  |             return  null; | 
 |  |  |         } | 
 |  |  |         if(StringUtils.isNotBlank(c.getFaceImg())){ | 
 |  |  |             log.info("===================================="+path+c.getFaceImg()+"======start===="); | 
 |  |  |             info.setVisitorPhoto(ImageBase64Util.Image2Base64(path+c.getFaceImg())); | 
 |  |  |             log.info("===================================="+path+c.getFaceImg()+"========end==="); | 
 |  |  |         } | 
 |  |  |         if(info.getVisitorPhoto() == null){ | 
 |  |  |             log.info("===================================="+path+c.getFaceImg()+"======imgerror====="); | 
 |  |  |             return  null; | 
 |  |  |         } | 
 |  |  |         if(StringUtils.isNotBlank(c.getName())){ | 
 |  |  | 
 |  |  |                 info.setVisitorName(c.getName()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if(StringUtils.isNotBlank(c.getCompanyName())){ | 
 |  |  |             char[] charArray = c.getCompanyName().toCharArray(); | 
 |  |  |             int length = charArray.length; | 
 |  |  | 
 |  |  |             info.setCertificateType(HKConstants.CertificateType.HUZHAO.getKey()+""); | 
 |  |  |             info.setCertificateNo(DESUtil.decrypt(Constants.EDS_PWD,  c.getIdcardNo())); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return info; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private String[] getHkRoles(String doors, List<DeviceRole> roleList) { | 
 |  |  |     public static String[] getHkRoles(String doors, List<DeviceRole> roleList) { | 
 |  |  |         if(StringUtils.isBlank(doors) || roleList.size()==0|| roleList.size()==0){ | 
 |  |  |             return null; | 
 |  |  |         } |