package com.ycl.service.video.impl; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.dto.video.AlarmDataParam; import com.ycl.dto.video.AlarmParam; import com.ycl.dto.video.CameraInfParam; import com.ycl.entity.video.VideoAlarmReport; import com.ycl.mapper.video.VideoAlarmReportMapper; import com.ycl.service.caseHandler.IViolationsService; import com.ycl.service.video.IVideoAlarmReportService; import com.ycl.utils.MediaFileUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.xml.bind.DatatypeConverter; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * VideoAlarmReportServiceImpl * * @version V1.0 * @author: AI * @date: 2022-09-20 17:36 **/ @Service public class VideoAlarmReportServiceImpl extends ServiceImpl implements IVideoAlarmReportService { private MediaFileUtil mediaFileUtil; private IViolationsService violationsService; @Autowired public void setMediaFileUtil(MediaFileUtil mediaFileUtil) { this.mediaFileUtil = mediaFileUtil; } @Autowired public void setViolationsService(IViolationsService violationsService) { this.violationsService = violationsService; } @Override public void save(AlarmParam alarmParam) { if (alarmParam != null && alarmParam.getAlarmData().size() > 0) { CameraInfParam cameraInf = alarmParam.getCameraInf(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); String picData = ""; String[] strings = alarmParam.getPicData().split(","); String extension = "jpg"; byte[] data = DatatypeConverter.parseBase64Binary(strings[1]); InputStream inputStream = new ByteArrayInputStream(data); try { picData = mediaFileUtil.savePicture(inputStream, extension); List videoAlarmReports = new ArrayList<>(); for (int i = 0; i < alarmParam.getAlarmData().size(); i++) { AlarmDataParam alarmData = alarmParam.getAlarmData().get(i); /******************** 处理图片 **********************/ try { Date date = sdf.parse(alarmData.getAlarmTime()); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); VideoAlarmReport videoAlarmReport = VideoAlarmReport.builder() .ip(cameraInf.getIp()).port(cameraInf.getPort()) .user(cameraInf.getUser()).password(cameraInf.getPassword()) .channel(cameraInf.getChannel()).platResourceId(cameraInf.getPlatResourceID()) .picData(picData).alarmTime(instant.atZone(zoneId).toLocalDateTime()) .alarmId(alarmData.getAlarmID()).algoType(alarmData.getAlgoType()) .algoName(alarmData.getAlgoName()).alarmObj(JSON.toJSONString(alarmData.getAlarmObject())) .build(); videoAlarmReports.add(videoAlarmReport); } catch (ParseException e) { e.printStackTrace(); } } this.saveBatch(videoAlarmReports, videoAlarmReports.size()); violationsService.saveFromVideo(videoAlarmReports); } catch (Exception e) { e.printStackTrace(); } } } }