package com.fzzy.igds.service;
|
|
import com.fzzy.igds.constant.RedisConst;
|
import com.fzzy.igds.domain.Camera;
|
import com.fzzy.igds.repository.SecCameraRepository;
|
import com.fzzy.igds.utils.ContextUtil;
|
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.utils.StringUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @Description
|
* @Author CZT
|
* @Date 2025/11/28 10:48
|
*/
|
@Slf4j
|
@Service
|
public class SecCameraService {
|
|
@Resource
|
private SecCameraRepository secCameraRepository;
|
@Resource
|
private RedisCache redisCache;
|
|
/**
|
* JPA分页查询数据
|
*
|
* @param specification
|
* @param pageable
|
* @return
|
*/
|
public Page<Camera> findAll(Specification<Camera> specification, Pageable pageable) {
|
return secCameraRepository.findAll(specification, pageable);
|
}
|
|
/**
|
* JPA - 查询库区下所有监控
|
* @return
|
*/
|
public List<Camera> listCamera() {
|
return secCameraRepository.listCamera(ContextUtil.getCompanyId(), ContextUtil.subDeptId(null));
|
}
|
|
/**
|
* JPA - 保存数据
|
* @param data
|
*/
|
public void saveCamera(Camera data) {
|
if (StringUtils.isEmpty(data.getCompanyId())) {
|
data.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (StringUtils.isEmpty(data.getDeptId())) {
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
if (data.getChanNum() == 0) {
|
data.setChanNum(1);
|
}
|
if(StringUtils.isEmpty(data.getId())){
|
data.setId(ContextUtil.generateId());
|
data.setCreateBy(ContextUtil.getLoginUserName());
|
data.setCreateTime(new Date());
|
}
|
data.setUpdateBy(ContextUtil.getLoginUserName());
|
data.setUpdateTime(new Date());
|
secCameraRepository.save(data);
|
}
|
|
/**
|
* JPA - 删除数据
|
* @param data
|
* @return
|
*/
|
public String delCamera(Camera data) {
|
secCameraRepository.delete(data);
|
return null;
|
}
|
|
/**
|
* 设置缓存
|
* @param companyId
|
*/
|
public void refreshCache(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
List<Camera> list = secCameraRepository.listCameraByCompanyId(companyId);
|
redisCache.setCacheObject(RedisConst.buildKey(companyId, RedisConst.KEY_CAMERA_LIST), list);
|
}
|
|
/**
|
* 获取缓存中监控信息
|
* @param companyId
|
* @return
|
*/
|
public List<Camera> getAllCacheCamera(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_CAMERA_LIST);
|
List<Camera> list = redisCache.getCacheObject(key);
|
|
if (null == list || list.isEmpty()) {
|
list = secCameraRepository.listCameraByCompanyId(companyId);
|
redisCache.setCacheObject(key, list);
|
}
|
return list;
|
}
|
|
/**
|
* 根据库区编码获取监控信息
|
* @param companyId
|
* @param deptId
|
* @return
|
*/
|
public List<Camera> getCameraByDeptId(String companyId, String deptId) {
|
if (StringUtils.isEmpty(deptId)) {
|
return null;
|
}
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
List<Camera> list = this.getAllCacheCamera(companyId);
|
|
if (null == list || list.isEmpty()){
|
return null;
|
}
|
List<Camera> result = new ArrayList<>();
|
for (Camera data : list) {
|
if (data.getDeptId().equals(deptId)){
|
result.add(data);
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 根据监控ID获取监控信息
|
* @param companyId
|
* @param cameraId
|
* @return
|
*/
|
public Camera getCameraById(String companyId, String cameraId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
List<Camera> list = this.getAllCacheCamera(companyId);
|
|
if (null == list || list.isEmpty()){
|
return null;
|
}
|
|
for (Camera data : list) {
|
if (data.getId().equals(cameraId)){
|
return data;
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 更新设置位置
|
* @param param
|
*/
|
public void updatePos(Camera param) {
|
secCameraRepository.updatePosById(param.getId(), param.getPosX(), param.getPosY());
|
}
|
|
}
|