| package com.doumee.service.business.impl.hksync; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.doumee.biz.system.SystemDictDataBiz; | 
| import com.doumee.core.haikang.model.HKConstants; | 
| import com.doumee.core.haikang.model.param.request.*; | 
| import com.doumee.core.haikang.service.HKService; | 
| import com.doumee.core.utils.Constants; | 
| import com.doumee.core.utils.DateUtil; | 
| import com.doumee.core.utils.FtpUtil; | 
| import com.doumee.core.utils.HttpsUtil; | 
| import com.doumee.dao.business.*; | 
| import com.doumee.dao.business.model.*; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.io.IOUtils; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import java.io.*; | 
| import java.net.URL; | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.UUID; | 
|   | 
| /** | 
|  * 设备信息表Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2023/11/30 15:33 | 
|  */ | 
| @Service | 
| @Slf4j | 
| public class HkSyncImgServiceImpl extends HkSyncBaseServiceImpl { | 
|   | 
|     @Autowired | 
|     private CarEventMapper carEventMapper; | 
|     @Autowired | 
|     private MemberMapper memberMapper; | 
|     @Autowired | 
|     private PlatformEventMapper platformEventMapper; | 
|     @Autowired | 
|     private DeviceEventMapper deviceEventMapper; | 
|     @Autowired | 
|     private VisitEventMapper visitEventMapper; | 
|     @Autowired | 
|     private SystemDictDataBiz systemDictDataBiz; | 
|     @Override | 
|     public  String downHKImgs(int type){ | 
|         if(Constants.DEALING_HK_IMG){ | 
|             return null ; | 
|         } | 
|         Constants.DEALING_HK_IMG =true; | 
|         try { | 
|             //查询所有访客事件记录中所有下载的海康端 抓拍照片 和 照片 | 
|             startDealVisitImg(); | 
|             //查询所有停车场事件记录中所有下载的海康端 车牌图片 和   车辆图片 | 
|             startDealCarImg(); | 
|             //查询所有门禁事件记录中所有下载的海康端 抓拍照片 和   身份证图片URL | 
|             startDealDeviceImg(); | 
|             //查询所有人员人脸照片 | 
|             startDealMemberImg(); | 
|             //查询所有月台事件中抓拍图照片 | 
|             startDealPlatformEventImg(); | 
|         }catch (Exception e){ | 
|             e.printStackTrace(); | 
|         }finally { | 
|             Constants.DEALING_HK_IMG =false; | 
|         } | 
|         return  null; | 
|     } | 
|   | 
|     @Override | 
|     public void startDealMemberImg() { | 
|         List<Member> list = memberMapper.selectList(new QueryWrapper<Member>().lambda() | 
|                 .eq(Member::getIsdeleted,Constants.ZERO) | 
|                 .likeRight(Member::getFaceImg, HKConstants.IMG_INDEX)); | 
|         Date date= new Date(); | 
|         if(list !=null && list.size()>0){ | 
|             for(Member model:list){ | 
|                 String serverIndex = model.getFaceServerIndexCode();//服务器编码 | 
|                 if(StringUtils.isBlank(serverIndex)){ | 
|                     continue; | 
|                 } | 
|                 String rs1 = null; | 
|                 Member event = new Member(); | 
|                 event.setId(model.getId()); | 
|                 event.setEditDate(date); | 
|                 if(StringUtils.isNotBlank(model.getFaceImg())){ | 
|                     String img1 = model.getFaceImg().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs1 = downHkImgToFtp(img1,serverIndex,Constants.MEMBER_IMG); | 
|                     if(StringUtils.isNotBlank(rs1)){ | 
|                         event.setFaceImg(rs1); | 
|                     }else{ | 
|                         event.setFaceImg(model.getFaceImg().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|                 memberMapper.updateById(event); | 
|             } | 
|         } | 
|   | 
|     } | 
|     @Override | 
|     public void startDealPlatformEventImg() { | 
|         List<PlatformEvent> list = platformEventMapper.selectList(new QueryWrapper<PlatformEvent>().lambda() | 
|                 .eq(PlatformEvent::getIsdeleted,Constants.ZERO) | 
|                 .isNotNull(PlatformEvent::getBackgroundImageContent) | 
|                 .notLikeRight(PlatformEvent::getBackgroundImageContent, HKConstants.IMG_INDEX_ERROR) | 
|                 .isNull(PlatformEvent::getBackgroundImageDown)); | 
|         Date date= new Date(); | 
|         if(list !=null && list.size()>0){ | 
|             for(PlatformEvent model:list){ | 
|                 String rs1 = null; | 
|                 PlatformEvent event = new PlatformEvent(); | 
|                 event.setId(model.getId()); | 
|                 event.setEditDate(date); | 
|                 if(StringUtils.isNotBlank(model.getBackgroundImageContent())){ | 
|                     String img1 = model.getBackgroundImageContent(); | 
|                     rs1 = downHkImgToFtp(img1,"",Constants.PLATFORM_EVENT_IMG); | 
|                     if(StringUtils.isNotBlank(rs1)){ | 
|                         event.setBackgroundImageDown(rs1); | 
|                     }else{ | 
|                         event.setBackgroundImageContent(HKConstants.IMG_INDEX_ERROR+model.getBackgroundImageContent()); | 
|                     } | 
|                 } | 
|                 platformEventMapper.updateById(event); | 
|             } | 
|         } | 
|   | 
|     } | 
|     private void startDealDeviceImg() { | 
|         List<DeviceEvent> deviceEventList = deviceEventMapper.selectList(new QueryWrapper<DeviceEvent>().lambda() | 
|                 .eq(DeviceEvent::getIsdeleted,Constants.ZERO) | 
|                 .and(wrapper ->{wrapper.likeRight(DeviceEvent::getExtEventPictureURL,HKConstants.IMG_INDEX) | 
|                         .or() | 
|                         .likeRight(DeviceEvent::getExtEventIDCardPictureURL,HKConstants.IMG_INDEX) ;})); | 
|         Date date= new Date(); | 
|         if(deviceEventList !=null && deviceEventList.size()>0){ | 
|             for(DeviceEvent model:deviceEventList){ | 
|                 String serverIndex = model.getSvrIndexCode();//服务器编码 | 
|                 if(StringUtils.isBlank(serverIndex)){ | 
|                     continue; | 
|                 } | 
|                 String rs1 = null; | 
|                 String rs2 = null; | 
|                 DeviceEvent event = new DeviceEvent(); | 
|                 event.setId(model.getId()); | 
|                 event.setEditDate(date); | 
|   | 
|                 if(StringUtils.isNotBlank(model.getExtEventPictureURL())){ | 
|                     String img1 = model.getExtEventPictureURL().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs1 = downHkImgToFtp(img1,serverIndex,Constants.DEVICE_EVENT_IMG); | 
|                     if(StringUtils.isNotBlank(rs1)){ | 
|                         event.setExtEventPictureURL(rs1); | 
|                     }else{ | 
|                         event.setExtEventPictureURL(model.getExtEventPictureURL().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|                 if(StringUtils.isNotBlank(model.getExtEventIDCardPictureURL())){ | 
|                     String img2 = model.getExtEventIDCardPictureURL().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs2 = downHkImgToFtp(img2,serverIndex,Constants.DEVICE_EVENT_IMG); | 
|   | 
|                     if(StringUtils.isNotBlank(rs2)){ | 
|                         event.setExtEventIDCardPictureURL(rs2); | 
|                     }else{ | 
|                         event.setExtEventIDCardPictureURL(model.getExtEventIDCardPictureURL().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|                 deviceEventMapper.updateById(event); | 
|             } | 
|         } | 
|   | 
|     } | 
|   | 
|     private void startDealCarImg() { | 
|         List<CarEvent> carEventList = carEventMapper.selectList(new QueryWrapper<CarEvent>().lambda() | 
|                 .eq(CarEvent::getIsdeleted,Constants.ZERO) | 
|                 .and(wrapper ->{wrapper.likeRight(CarEvent::getPlatePicUrl,HKConstants.IMG_INDEX) | 
|                         .or() | 
|                         .likeRight(CarEvent::getVehiclePicUrl,HKConstants.IMG_INDEX) ;})); | 
|         Date date= new Date(); | 
|         if(carEventList !=null && carEventList.size()>0){ | 
|             for(CarEvent model:carEventList){ | 
|                 String serverIndex = model.getSvrIndex();//服务器编码 | 
|                 if(StringUtils.isBlank(serverIndex)){ | 
|                     continue; | 
|                 } | 
|                 CarEvent event = new CarEvent(); | 
|                 event.setId(model.getId()); | 
|                 event.setEditDate(date); | 
|                 String rs1 = null; | 
|                 String rs2 = null; | 
|   | 
|                 if(StringUtils.isNotBlank(model.getPlatePicUrl())){ | 
|                     String img1 = model.getPlatePicUrl().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs1 = downHkImgToFtp(img1,serverIndex,Constants.CAR_EVENT_IMG); | 
|                     if(StringUtils.isNotBlank(rs1)){ | 
|                         event.setPlatePicUrl(rs1); | 
|                     }else{ | 
|                         event.setPlatePicUrl(model.getPlatePicUrl().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|                 if(StringUtils.isNotBlank(model.getVehiclePicUrl())){ | 
|                     String img2 = model.getVehiclePicUrl().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs2 = downHkImgToFtp(img2,serverIndex,Constants.CAR_EVENT_IMG); | 
|                     if(StringUtils.isNotBlank(rs2)){ | 
|                         event.setVehiclePicUrl(rs2); | 
|                     }else{ | 
|                         event.setVehiclePicUrl(model.getVehiclePicUrl().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|   | 
|                 carEventMapper.updateById(event); | 
|             } | 
|         } | 
|   | 
|     } | 
|   | 
|     private void startDealVisitImg() { | 
|         List<VisitEvent> visitEventList = visitEventMapper.selectList(new QueryWrapper<VisitEvent>().lambda() | 
|                 .eq(VisitEvent::getIsdeleted,Constants.ZERO) | 
|                 .and(wrapper ->{wrapper.likeRight(VisitEvent::getCaptureUrl,HKConstants.IMG_INDEX) | 
|                         .or() | 
|                         .likeRight(VisitEvent::getPhotoUrl,HKConstants.IMG_INDEX) ;})); | 
|   | 
|         Date date= new Date(); | 
|         if(visitEventList !=null && visitEventList.size()>0){ | 
|             for(VisitEvent model:visitEventList){ | 
|                 String serverIndex = model.getSvrIndexCode();//服务器编码 | 
|                 if(StringUtils.isBlank(serverIndex)){ | 
|                     continue; | 
|                 } | 
|                 VisitEvent event = new VisitEvent(); | 
|                 event.setId(model.getId()); | 
|                 event.setEditDate(date); | 
|                 String rs1 = null; | 
|                 String rs2 = null; | 
|                 if(StringUtils.isNotBlank(model.getCaptureUrl())){ | 
|                     String img1 = model.getCaptureUrl().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs1 = downHkImgToFtp(img1,serverIndex,Constants.VISIT_EVENT_IMG); | 
|                     if(StringUtils.isNotBlank(rs1)){ | 
|                         event.setCaptureUrl(rs1); | 
|                     }else{ | 
|                         event.setCaptureUrl(model.getCaptureUrl().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|                 if(StringUtils.isNotBlank(model.getPhotoUrl())){ | 
|                     String img2 = model.getPhotoUrl().replace(HKConstants.IMG_INDEX_ERROR, "").replace(HKConstants.IMG_INDEX, ""); | 
|                     rs2 = downHkImgToFtp(img2,serverIndex,Constants.VISIT_EVENT_IMG); | 
|                     if(StringUtils.isNotBlank(rs2)){ | 
|                         event.setPhotoUrl(rs2); | 
|                     }else{ | 
|                         event.setPhotoUrl(model.getPhotoUrl().replace(HKConstants.IMG_INDEX, HKConstants.IMG_INDEX_ERROR)); | 
|                     } | 
|                 } | 
|   | 
|                 visitEventMapper.updateById(event); | 
|             } | 
|         } | 
|   | 
|     } | 
|   | 
|     public static  FtpUtil ftp  = null; | 
|     private String downHkImgToFtp(String img1, String serverIndex, String folder_code) { | 
|         String folder = systemDictDataBiz.queryByCode(Constants.FTP,folder_code).getCode(); | 
|         InputStream is = null; | 
|         if(StringUtils.equals(folder_code,Constants.CAR_EVENT_IMG)){ | 
|             //停车场抓拍图 | 
|             CarPictureRequest param = new CarPictureRequest(); | 
|             param.setPicUri(img1.replace(HKConstants.IMG_INDEX_ERROR,"").replace(HKConstants.IMG_INDEX,"")); | 
|             param.setAswSyscode(serverIndex); | 
|             is = HKService.getCarPicture(param); | 
|         }else if(StringUtils.equals(folder_code,Constants.DEVICE_EVENT_IMG)){ | 
|             //门禁事件图片 | 
|             DevicePictureRequest param = new DevicePictureRequest(); | 
|             param.setPicUri(img1.replace(HKConstants.IMG_INDEX_ERROR,"").replace(HKConstants.IMG_INDEX,"")); | 
|             param.setSvrIndexCode(serverIndex); | 
|             is = HKService.getDevicePicture(param); | 
|         }else if(StringUtils.equals(folder_code,Constants.VISIT_EVENT_IMG)){ | 
|             //访客事件图片 | 
|             VisitPictureRequest param = new VisitPictureRequest(); | 
|             param.setPicUri(img1.replace(HKConstants.IMG_INDEX_ERROR,"").replace(HKConstants.IMG_INDEX,"")); | 
|             param.setSvrIndexCode(serverIndex); | 
|             is = HKService.getVisitPicture(param); | 
|         }else if(StringUtils.equals(folder_code,Constants.MEMBER_IMG)){ | 
|             //访客事件图片 | 
|             FacePictureRequest param = new FacePictureRequest(); | 
|             param.setPicUri(img1.replace(HKConstants.IMG_INDEX_ERROR,"").replace(HKConstants.IMG_INDEX,"")); | 
|             param.setServerIndexCode(serverIndex); | 
|             is = HKService.getFaceInputStream(param); | 
|         }else if(StringUtils.equals(folder_code,Constants.PLATFORM_EVENT_IMG)){ | 
|             //月台事件图片 | 
|             log.info("月台图片下载开始:=========================="+img1); | 
|             try { | 
|                 is = new URL(img1).openStream(); | 
|                 byte[] bytes = IOUtils.toByteArray(is); | 
|                 is = new ByteArrayInputStream(bytes); | 
| //              is = HttpsUtil.connectionInputsteam(img1,"GET",null,null); | 
|             }catch (Exception e){ | 
|                 e.printStackTrace(); | 
|                 log.error("月台图片下载失败:=========================="+img1+"\n"+e.getMessage()); | 
|             } | 
|         } | 
|         if(is ==null){ | 
|             return  null; | 
|         } | 
|         try { | 
|             if(ftp == null){ | 
|                  ftp = new FtpUtil(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_HOST).getCode(), | 
|                         Integer.parseInt(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PORT).getCode()), | 
|                         systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_USERNAME).getCode(), | 
|                         systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PWD).getCode()); | 
|             }else{ | 
|                 ftp.connect(); | 
|             } | 
|             String date = DateUtil.getNowShortDate(); | 
|             String fName =  date+"/"+ UUID.randomUUID().toString()+".png"; | 
|             String fileName = folder+fName; | 
|             boolean r = ftp.uploadInputstream(is,fileName);//上传 | 
|             if(r){ | 
|                 log.info("【海康图片下载上传FTP成功】======================" ); | 
|                 return  fName; | 
|             }else{ | 
|                 log.error("【海康图片下载上传FTP失败】======================" ); | 
|             } | 
|         } catch (Exception e) { | 
|             log.error("【海康图片下载上传FTP失败】======================"+e.getMessage()); | 
|         } | 
|         return  null; | 
|     } | 
| } |