package com.ozs.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.github.pagehelper.PageHelper;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.entity.SysDictData;
import com.ozs.common.exception.ServiceException;
import com.ozs.common.utils.AppendUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.bean.BeanUtils;
import com.ozs.common.utils.bean.BeanValidators;
import com.ozs.entity.*;
import com.ozs.entity.vo.BaseCameraManagementHomeVo;
import com.ozs.entity.vo.BaseCameraManagementVo;
import com.ozs.entity.vo.BaseCameraManagementVos;
import com.ozs.entity.vo.BaseVehicleTerminalDto;
import com.ozs.mapper.*;
import com.ozs.service.BaseCameraManagementService;
import com.ozs.service.BaseRailwayManagementService;
import com.ozs.service.BaseUserService;
import com.ozs.system.service.ISysDictTypeService;
import com.ozs.utils.CameraUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import javax.validation.Validator;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
/**
*
* 相机台账管理表 服务实现类
*
*
* @author ozs
* @since 2023-02-17
*/
@Service
public class BaseCameraManagementServiceImpl extends ServiceImpl implements BaseCameraManagementService {
public static final String PATTERN = "^(\\d+.\\d{1,3}|\\d+)$";
@Autowired
protected Validator validator;
@Autowired
BaseDeviceDynamicManagementMapper baseDeviceDynamicManagementMapper;
@Resource
private BaseCameraManagementMapper baseCameraManagementMapper;
@Resource
private BaseUserService baseUserService;
@Resource
private BaseUserMapper baseUserMapper;
@Resource
private MsgAlarmMapper msgAlarmMapper;
@Autowired
private BaseDeptMapper baseDeptMapper;
@Autowired
private BaseRailwayManagementService baseRailwayManagementService;
@Autowired
private SkynetHeartbeatLogMapper skynetHeartbeatLogMapper;
@Autowired
private BaseRailwayManagementMapper baseRailwayManagementMapper;
@Autowired
private ISysDictTypeService dictTypeService;
/**
* 相机台账管理表导入
*
* @param accountManageList 相机台账列表
* @param updateSupport 是否更新支持,如果已存在,则进行更新数据
* @return
*/
@Override
public String importBaseAccountManagement(List accountManageList, boolean updateSupport, String userId) {
if (StringUtils.isEmptySunhh(accountManageList) || accountManageList.size() == 0) {
throw new ServiceException("导入相机台账列表信息不能为空!");
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
for (BaseCameraManagementVos cameraManagementVos : accountManageList) {
try {
BaseCameraManagement accountManagement = new BaseCameraManagement();
BeanUtils.copyProperties(cameraManagementVos, accountManagement);
accountManagement.setBeginMile(AppendUtils.stringSplit(accountManagement.getBeginMiles()));
accountManagement.setEndMile(AppendUtils.stringSplit(accountManagement.getEndMiles()));
accountManagement.setInstallMile(AppendUtils.stringSplit(accountManagement.getInstallMiles()));
// 判断相机编码是否存在
LambdaQueryWrapper lw = new LambdaQueryWrapper();
if (!StringUtils.isEmptySunhh(accountManagement.getCameraCode())) {
lw.eq(BaseCameraManagement::getCameraCode, accountManagement.getCameraCode());
}
BaseCameraManagement baseAccountManagement = baseCameraManagementMapper.selectOne(lw);
if (StringUtils.isEmptySunhh(baseAccountManagement)) {
BeanValidators.validateWithException(validator, accountManagement);
accountManagement.setUpdateBy(userId);
accountManagement.setCreateBy(userId);
// Long deptId = baseDeptMapper.selectDeptByDeptName(accountManagement.getDeptName());
// log.debug(accountManagement.getDeptName() + "主键id:" + deptId);
// if (!ObjectUtils.isEmpty(deptId)) {
// accountManagement.setDeptId(deptId);
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
if (!StringUtils.isEmptySunhh(accountManagement.getRailwayName())) {
queryWrapper.eq(BaseRailwayManagement::getRailwayName, accountManagement.getRailwayName());
}
// List list = baseRailwayManagementService.list(queryWrapper);
BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(queryWrapper);
if (!StringUtils.isEmptySunhh(baseRailwayManagement)) {
if (!accountManagement.getBeginMile().toString().matches(PATTERN) || !accountManagement.getInstallMile().toString().matches(PATTERN) || !accountManagement.getEndMile().toString().matches(PATTERN)) {
failureNum++;
failureMsg.append(failureNum + "、相机编码为: " + accountManagement.getCameraCode() + "的数据,里程位置填写格式不正确,请填写数字");
} else {
accountManagement.setRailwayCode(baseRailwayManagement.getRailwayCode());
if (!StringUtils.isEmptySunhh(accountManagement.getBeginMileBD())) {
if (!accountManagement.getBeginMileBD().toString().matches(PATTERN)) {
failureNum++;
String msg = failureNum + "、监控范围开始里程位置填写格式不正确";
failureMsg.append(msg);
log.error(msg);
// return error("监控范围开始里程位置填写格式不正确");
} else {
BigDecimal begin = accountManagement.getBeginMileBD().setScale(3, RoundingMode.HALF_UP);
// BigDecimal begin = BigDecimal.parseDouble(baseCameraManagement.getBeginMile().toString());
accountManagement.setBeginMile(begin.multiply(new BigDecimal("1000")).intValue());
}
}
if (!StringUtils.isEmptySunhh(accountManagement.getInstallMileBD())) {
if (!accountManagement.getInstallMileBD().toString().matches(PATTERN)) {
failureNum++;
String msg = failureNum + "、监控相机安装里程位置填写格式不正确";
failureMsg.append(msg);
log.error(msg);
// return error("监控相机安装里程位置填写格式不正确");
} else {
BigDecimal install = accountManagement.getInstallMileBD().setScale(3, RoundingMode.HALF_UP);
// double install = Double.parseDouble(baseCameraManagement.getInstallMile().toString());
accountManagement.setInstallMile(install.multiply(new BigDecimal("1000")).intValue());
}
}
if (!StringUtils.isEmptySunhh(accountManagement.getInstallMileBD())) {
if (!accountManagement.getInstallMileBD().toString().matches(PATTERN)) {
failureNum++;
String msg = failureNum + "、监控范围结束里程位置填写格式不正确";
failureMsg.append(msg);
log.error(msg);
// return error("监控范围结束里程位置填写格式不正确");
} else {
BigDecimal end = accountManagement.getInstallMileBD().setScale(3, RoundingMode.HALF_UP);
// double end = Double.parseDouble(baseCameraManagement.getEndMile().toString());
accountManagement.setEndMile(end.multiply(new BigDecimal("1000")).intValue());
}
}
// accountManagement.setInstallMile(accountManagement.getInstallMileBD() * 1000);
// accountManagement.setEndMile(accountManagement.getEndMile() * 1000);
// accountManagement.setBeginMile(accountManagement.getBeginMile() * 1000);
if (!StringUtils.isEmpty(cameraManagementVos.getMonitoringDirectionName())) {
List data = dictTypeService.selectDictDataByType("monitoring_direction");
for (SysDictData datum : data) {
if (datum.getDictLabel().equals(cameraManagementVos.getMonitoringDirectionName())) {
accountManagement.setMonitoringDirection(Integer.valueOf(datum.getDictValue()));
}
}
}
if (!StringUtils.isEmpty(cameraManagementVos.getEnableOrNotName())) {
accountManagement.setEnableOrNot(cameraManagementVos.getEnableOrNotName().equals("启用") ? 1 : 2);
}
baseCameraManagementMapper.insert(accountManagement);
BaseDeviceDynamicManagement baseDeviceDynamicManagement = new BaseDeviceDynamicManagement();
baseDeviceDynamicManagement.setCameraCode(accountManagement.getCameraCode());
baseDeviceDynamicManagement.setUpdateBy(userId);
baseDeviceDynamicManagement.setCreateBy(userId);
baseDeviceDynamicManagementMapper.insert(baseDeviceDynamicManagement);
successNum++;
successMsg.append(successNum + "、相机编码 " + accountManagement.getCameraCode() + " 导入成功");
}
} else {
failureNum++;
String msg = failureNum + "、相机所属线路名称 " + accountManagement.getRailwayName() + "不存在, 导入失败:";
failureMsg.append(msg);
log.error(msg);
}
// } else {
// failureNum++;
// String msg = failureNum + "、相机所属工务段 " + accountManagement.getDeptName() + "不存在, 导入失败:";
// failureMsg.append(msg);
// log.error(msg);
// }
} else if (updateSupport) {
BeanValidators.validateWithException(validator, accountManagement);
accountManagement.setUpdateBy(userId);
Long deptId = baseDeptMapper.selectDeptByDeptName(accountManagement.getDeptName());
log.debug(accountManagement.getDeptName() + "主键id:" + deptId);
// if (!ObjectUtils.isEmpty(deptId)) {
accountManagement.setDeptId(deptId);
if (!StringUtils.isEmpty(cameraManagementVos.getMonitoringDirectionName())) {
List data = dictTypeService.selectDictDataByType("monitoring_direction");
for (SysDictData datum : data) {
if (datum.getDictLabel().equals(cameraManagementVos.getMonitoringDirectionName())) {
accountManagement.setMonitoringDirection(Integer.valueOf(datum.getDictValue()));
}
}
}
if (!StringUtils.isEmpty(cameraManagementVos.getEnableOrNotName())) {
accountManagement.setEnableOrNot(cameraManagementVos.getEnableOrNotName().equals("启用") ? 1 : 2);
}
baseCameraManagementMapper.update(accountManagement, lw);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
if (!StringUtils.isEmptySunhh(baseAccountManagement.getCameraCode())) {
wrapper.eq(BaseDeviceDynamicManagement::getCameraCode, baseAccountManagement.getCameraCode());
}
BaseDeviceDynamicManagement baseDeviceDynamicManagement = baseDeviceDynamicManagementMapper.selectOne(wrapper);
baseDeviceDynamicManagement.setCameraCode(accountManagement.getCameraCode());
baseDeviceDynamicManagement.setUpdateBy(userId);
baseDeviceDynamicManagementMapper.updateById(baseDeviceDynamicManagement);
successNum++;
successMsg.append(successNum + "、相机编码 " + accountManagement.getCameraCode() + " 更新成功");
// } else {
// failureNum++;
// String msg = failureNum + "、相机所属工务段 " + accountManagement.getDeptName() + "不存在, 导入失败:";
// failureMsg.append(msg);
// log.error(msg);
// }
} else {
failureNum++;
failureMsg.append(failureNum + "、相机编码 " + accountManagement.getCameraCode() + " 已存在");
}
} catch (Exception e) {
String msg = null;
failureNum++;
if (ObjectUtils.isEmpty(cameraManagementVos)) {
msg = "导入的文档格式不正确";
} else {
msg = failureNum + "、相机编码 " + cameraManagementVos.getCameraCode() + " 导入失败:";
}
failureMsg.append(msg);
log.error(msg, e);
}
}
if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new ServiceException(failureMsg.toString());
} else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条。");
}
return successMsg.toString();
}
/**
* 获取相机列表+数据权限
*
* @return
*/
@Override
public List selectCameraAll(BaseCameraManagementVo baseCameraManagementVo) {
// List userIdList = getUserIds(userId);
// LambdaQueryWrapper lw = new LambdaQueryWrapper();
// if (!StringUtils.isEmpty(userIdList)) {
// lw.in(BaseCameraManagement::getCreateBy, userIdList);
// }
// List baseCameraManagementList = baseCameraManagementMapper.selectList(lw);
List baseCameraManagementList = baseCameraManagementMapper.selectCameraManagementList(baseCameraManagementVo);
List allListVo = BeanUtils.entityListToVOList(baseCameraManagementList, BaseCameraManagementHomeVo.class);
for (BaseCameraManagementHomeVo baseCameraManagementHomeVo : allListVo) {
Integer isRelease = msgAlarmMapper.selectCameraCode(baseCameraManagementHomeVo.getCameraCode());
baseCameraManagementHomeVo.setIsRelease(isRelease != null ? isRelease : 1);
}
return allListVo;
}
/**
* 根据当前登陆用户ID 查询当前用户数据权限
*
* @param userId
* @return
*/
/* public List getUserIds(String userId) {
BaseUser baseUser = baseUserService.getUser(userId);
if (StringUtils.isNull(baseUser) || StringUtils.isNull(baseUser.getDataPermission())
|| StringUtils.isNull(baseUser.getDeptId())) {
return null;
}
// 数据权限 1:本人;2:本部门;3:本部门及下级部门;4:本人及本级部门;5:本人及本部门及下级部门;6:全部
Integer dataPermission = baseUser.getDataPermission();
Long deptId = baseUser.getDeptId();
Set set = new HashSet<>();
if (1 == dataPermission) {
set = baseUserMapper.getUserIdList(userId);
} else if (2 == dataPermission) {
set = baseUserMapper.getUserIdByDeptId(deptId);
} else if (3 == dataPermission) {
// 获取部门数据
List deptList = baseUserMapper.getDeptId(deptId);
// 获取用户列表
set = baseUserMapper.getUserIdByDeptIds(deptList);
} else if (4 == dataPermission) {
set = baseUserMapper.getUserIdByDeptIdAndUserId(userId, deptId);
} else if (5 == dataPermission) {
// 获取本人
Set setUser = baseUserMapper.getUserIdListBy5(userId);
// 获取部门数据
List deptList5 = baseUserMapper.getDeptId(deptId);
// 获取用户列表
Set setUsers = baseUserMapper.getUserIdByDeptIdsBy5(deptList5);
set.addAll(setUser);
set.addAll(setUsers);
} else if (6 == dataPermission) {
set = baseUserMapper.getUserIdList6();
}
List list = new ArrayList<>(set);
return list;
}*/
/**
* 根据当前登陆用户ID 查询当前用户数据权限
*
* @param userId
* @return
*/
@Override
public List getUserIdList(String userId) {
BaseUser baseUser = baseUserService.getUser(userId);
if (StringUtils.isNull(baseUser) || StringUtils.isNull(baseUser.getDataPermission())
|| StringUtils.isNull(baseUser.getDeptId())) {
return null;
}
// 数据权限 1:本人;2:本部门;3:本部门及下级部门;4:本人及本级部门;5:本人及本部门及下级部门;6:全部
Integer dataPermission = baseUser.getDataPermission();
Long deptId = baseUser.getDeptId();
Set set = new HashSet<>();
if (1 == dataPermission) {
set = baseUserMapper.getUserIdList(userId);
} else if (2 == dataPermission) {
set = baseUserMapper.getUserIdByDeptId(deptId);
} else if (3 == dataPermission) {
// 获取部门数据
List deptList = baseUserMapper.getDeptId(deptId);
// 获取用户列表
set = baseUserMapper.getUserIdByDeptIds(deptList);
} else if (4 == dataPermission) {
set = baseUserMapper.getUserIdByDeptIdAndUserId(userId, deptId);
} else if (5 == dataPermission) {
// 获取本人
Set setUser = baseUserMapper.getUserIdListBy5(userId);
// 获取部门数据
List deptList5 = baseUserMapper.getDeptId(deptId);
// 获取用户列表
Set setUsers = baseUserMapper.getUserIdByDeptIdsBy5(deptList5);
set.addAll(setUser);
set.addAll(setUsers);
} else if (6 == dataPermission) {
set = baseUserMapper.getUserIdsList();
}
List list = new ArrayList<>(set);
return list;
}
@Override
public IPage queryPage(BaseCameraManagementVo baseCameraManagementVo) {
int pageNum = Integer.parseInt(baseCameraManagementVo.getPageNum().toString());
int pageSize = Integer.parseInt(baseCameraManagementVo.getPageSize().toString());
com.github.pagehelper.Page page = PageHelper.startPage(pageNum, pageSize)
.doSelectPage(() -> baseCameraManagementMapper.selectCameraManagementList(baseCameraManagementVo));
com.baomidou.mybatisplus.extension.plugins.pagination.Page pageR =
new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize);
pageR.setRecords(page.getResult());
pageR.setTotal(page.getTotal());
return pageR;
}
@Override
public List baseCameraManagementList(BaseCameraManagementVo baseCameraManagementVo) {
return baseCameraManagementMapper.selectCameraManagementList(baseCameraManagementVo);
}
@Override
public Map getMessageStatisticsCamera() {
LambdaQueryWrapper lw = new LambdaQueryWrapper<>();
List baseCameraManagements = baseCameraManagementMapper.selectList(lw);
int online = 0;
int offline = 0;
for (BaseCameraManagement baseCameraManagement : baseCameraManagements) {
if (!StringUtils.isEmptySunhh(baseCameraManagement) && !StringUtils.isEmptySunhh(baseCameraManagement.getCameraCode())) {
SkynetHeartbeatLog skynetHeartbeatLog = baseCameraManagementMapper.selectSkynetHeartbeatLogByCameraCode(baseCameraManagement.getCameraCode());
if (StringUtils.isEmptySunhh(skynetHeartbeatLog)) {
offline++;
// baseCameraManagement.setOnline(1);
} else {
long timeNow = System.currentTimeMillis();
long createTime = skynetHeartbeatLog.getCreateTime().getTime();
long thereHourMillis = 60 * 60 * 1000 * 3;
long timeNew = timeNow - thereHourMillis;
if (timeNew <= createTime) {
baseCameraManagement.setOnline(2);
online++;
} else {
// baseCameraManagement.setOnline(1);
offline++;
}
}
baseCameraManagementMapper.updateById(baseCameraManagement);
}
}
Map map = new HashMap<>();
map.put("countAllCamera", baseCameraManagements.size());
map.put("online", online);
map.put("offline", offline);
return map;
}
@Override
public AjaxResult nonContactAlarmCamera(String cameraCode) {
List list = new ArrayList<>();
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("is_release", 2);
List msgAlarms = msgAlarmMapper.selectList(wrapper);
if (!ObjectUtils.isEmpty(msgAlarms)) {
for (MsgAlarm msgAlarm : msgAlarms) {
LambdaQueryWrapper camera = new LambdaQueryWrapper();
camera.ge(BaseCameraManagement::getEndMile, msgAlarm.getAlarmMile());
camera.le(BaseCameraManagement::getBeginMile, msgAlarm.getAlarmMile());
camera.eq(BaseCameraManagement::getLineDir, msgAlarm.getLineDir());
camera.eq(BaseCameraManagement::getRailwayCode, msgAlarm.getRailwayCode());
camera.eq(BaseCameraManagement::getOnline, 2);
camera.eq(BaseCameraManagement::getEnableOrNot, 1);
if (StringUtils.isNotEmpty(cameraCode)) {
wrapper.eq("camera_code", cameraCode);
}
List baseCameraManagementList = this.list(camera);
for (BaseCameraManagement baseCameraManagement : baseCameraManagementList) {
String end = "";
if (!StringUtils.isEmptySunhh(baseCameraManagement.getEndMile())) {
end = AppendUtils.stringAppend(baseCameraManagement.getEndMile());
baseCameraManagement.setEndMiles(end);
baseCameraManagement.setEndMileBD(new BigDecimal(baseCameraManagement.getEndMile()).divide(new BigDecimal(1000)).setScale(3, RoundingMode.HALF_UP));
}
String begin = "";
if (!StringUtils.isEmptySunhh(baseCameraManagement.getBeginMile())) {
begin = AppendUtils.stringAppend(baseCameraManagement.getBeginMile());
baseCameraManagement.setBeginMiles(begin);
baseCameraManagement.setBeginMileBD(new BigDecimal(baseCameraManagement.getBeginMile()).divide(new BigDecimal(1000)).setScale(3, RoundingMode.HALF_UP));
}
baseCameraManagement.setMilesRange(begin + "~" + end);
String mils = AppendUtils.stringAppend(baseCameraManagement.getInstallMile());
baseCameraManagement.setInstallMiles(mils);
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
if (!ObjectUtils.isEmpty(baseCameraManagement.getRailwayCode())) {
queryWrapper.eq(BaseRailwayManagement::getRailwayCode, baseCameraManagement.getRailwayCode());
}
BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(queryWrapper);
baseCameraManagement.setRailwayName(baseRailwayManagement.getRailwayName());
}
if (!ObjectUtils.isEmpty(baseCameraManagementList)) {
list.addAll(baseCameraManagementList);
}
}
}
return AjaxResult.success(list);
}
@Override
public List videoCaptureUrl(BaseVehicleTerminalDto baseVehicleTerminalDto) {
List urlList = new ArrayList<>();
LambdaQueryWrapper camera = new LambdaQueryWrapper();
camera.ge(BaseCameraManagement::getBeginMile, baseVehicleTerminalDto.getTravelMile());
camera.eq(BaseCameraManagement::getLineDir, baseVehicleTerminalDto.getLineDir());
camera.eq(BaseCameraManagement::getRailwayCode, baseVehicleTerminalDto.getRailwayCode());
camera.eq(BaseCameraManagement::getOnline, 2);
camera.eq(BaseCameraManagement::getEnableOrNot, 1);
camera.orderByDesc(BaseCameraManagement::getCreateTime);
camera.last("limit 4");
List baseCameraManagementList = this.list(camera);
if (!ObjectUtils.isEmpty(baseCameraManagementList)) {
List list = new ArrayList<>(baseCameraManagementList);
for (BaseCameraManagement baseCameraManagement : list) {
String flv = CameraUtil.getPlayFlv(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel());
BaseVehicleTerminalDto dto = new BaseVehicleTerminalDto();
dto.setUrl(flv);
dto.setCameraCode(baseCameraManagement.getCameraCode());
dto.setTime(new Date());
BaseRailwayManagement baseRailwayManagement = baseRailwayManagementMapper.selectOne(new QueryWrapper().eq("railway_code", baseVehicleTerminalDto.getRailwayCode()));
dto.setRailwayName(baseRailwayManagement.getRailwayName());
String mils = AppendUtils.stringAppend(baseVehicleTerminalDto.getTravelMile());
dto.setTravelMileName(mils);
dto.setLineDirStr(baseVehicleTerminalDto.getLineDir() == 1 ? "上行" : "下行");
urlList.add(dto);
}
}
return urlList;
}
}