package com.iailab.module.data.video.service.impl; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.iailab.framework.common.constant.Constant; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.framework.web.core.util.WebFrameworkUtils; import com.iailab.module.data.mq.consumer.ImageMessage; import com.iailab.module.data.video.controller.admin.camera.vo.CameraPageReqVO; import com.iailab.module.data.video.config.VideoConfiguration; import com.iailab.module.data.video.dao.CameraDao; import com.iailab.module.data.video.dao.ImageDao; import com.iailab.module.data.video.dto.CameraDTO; import com.iailab.module.data.video.dto.ImageDTO; import com.iailab.module.data.video.entity.CameraEntity; import com.iailab.module.data.video.entity.ImageEntity; import com.iailab.module.data.video.service.CameraService; import org.apache.commons.lang3.StringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.validation.Valid; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2024年02月27日 */ @Service public class CameraServiceImpl extends BaseServiceImpl implements CameraService { @Value("${video.capture-dir}") public String captureDir; @Resource private CameraDao cameraDao; @Resource private ImageDao imageDao; @Resource private RabbitTemplate rabbitTemplate; @Override public PageResult getPage(CameraPageReqVO cameraPageReqVO) { PageResult pageResult = cameraDao.selectPage(cameraPageReqVO); return BeanUtils.toBean(pageResult, CameraDTO.class); } @Override public List list(@Valid CameraPageReqVO cameraPageReqVO) { QueryWrapper queryWrapper = getWrapper(cameraPageReqVO); queryWrapper.orderByDesc(Constant.CREATE_DATE); List list = baseDao.selectList(queryWrapper); return ConvertUtils.sourceToTarget(list, CameraDTO.class); } @Override public List apiCameraList(Map params) { return baseDao.apiCameraList(); } @Override public List getByNVRId(String nvrId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("nvr_id", nvrId).orderByAsc("channel"); List list = baseDao.selectList(queryWrapper); return ConvertUtils.sourceToTarget(list, CameraDTO.class); } @Override public CameraDTO get(String id) { CameraEntity entity = baseDao.selectById(id); return ConvertUtils.sourceToTarget(entity, CameraDTO.class); } @Override public CameraDTO getByCode(String code) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("code", code); CameraEntity entity = baseDao.selectOne(wrapper); return ConvertUtils.sourceToTarget(entity, CameraDTO.class); } @Override @DSTransactional(rollbackFor = Exception.class) public void dealCapture(Map params) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); Date captureDate = calendar.getTime(); CameraDTO devCamera = getByCode(params.get("cmdSerial")); String strFileName = params.get("strFileName"); String brand = params.get("brand"); String capturePath = ""; String routingKey = ""; switch (brand) { case "DH": capturePath = VideoConfiguration.staticDhCapturePath; routingKey = ImageMessage.DAHUA_ROUTING_KEY; break; case "HK": capturePath = VideoConfiguration.staticHkCapturePath; routingKey = ImageMessage.HIKVISION_ROUTING_KEY; break; } String imagePath = strFileName.replace("\\", "/"); String imageUrl = imagePath.replace(capturePath, ""); ImageEntity image = new ImageEntity(); image.setCameraId(devCamera.getId()); image.setImagePath(imagePath); image.setImageUrl(capturePath.replace(captureDir, "") + imageUrl); image.setCreateDate(captureDate); imageDao.insert(image); //存入rabbitmq消息队列,等待模型处理 sendRabbitMQMessage(image, routingKey); } @Override public void setStatus(String id, Integer status) { CameraEntity cameraEntity = new CameraEntity(); cameraEntity.setId(id); cameraEntity.setStatus(status); baseDao.updateById(cameraEntity); } private QueryWrapper getWrapper(CameraPageReqVO cameraPageReqVO){ String location = cameraPageReqVO.getLocation(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("type", cameraPageReqVO.getType()); wrapper.like(StringUtils.isNotBlank(location), "location", location); return wrapper; } @Override public void save(CameraDTO dto) { CameraEntity entity = ConvertUtils.sourceToTarget(dto, CameraEntity.class); entity.setCreateDate(new Date()); entity.setCreator(WebFrameworkUtils.getLoginUserId()); insert(entity); } @Override public void update(CameraDTO dto) { CameraEntity entity = ConvertUtils.sourceToTarget(dto, CameraEntity.class); entity.setUpdateDate(new Date()); entity.setUpdater(WebFrameworkUtils.getLoginUserId()); updateById(entity); } @Override @DSTransactional(rollbackFor = Exception.class) public void delete(String id) { baseDao.deleteById(id); } @Override public Long cheack(CameraDTO dto) { String id = dto.getId(); String code = dto.getCode(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.ne(StringUtils.isNotBlank(id), "id", id); queryWrapper.and(wrapper -> wrapper.eq("code", code)); return baseDao.selectCount(queryWrapper); } // 发送rabbitMQ消息 private void sendRabbitMQMessage(ImageEntity image, String routingKey) { String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); ImageMessage imageMessage = new ImageMessage(); imageMessage.setMessageId(image.getId()); imageMessage.setCreateTime(createTime); imageMessage.setImageDTO(BeanUtils.toBean(image, ImageDTO.class)); rabbitTemplate.convertAndSend(ImageMessage.EXCHANGE, routingKey, imageMessage); } }