forked from dyf/fys-Multi-tenant
添加设备
This commit is contained in:
@ -4,7 +4,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.fuyuanshen.common.core.domain.PageResult;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.dto.DeviceQueryCriteria;
|
||||
import com.fuyuanshen.equipment.domain.form.DeviceForm;
|
||||
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -24,6 +25,13 @@ public interface DeviceService extends IService<Device> {
|
||||
*/
|
||||
PageResult<Device> queryAll(DeviceQueryCriteria criteria, Page<Object> page) throws IOException;
|
||||
|
||||
/**
|
||||
* 新增设备
|
||||
*
|
||||
* @param resources
|
||||
*/
|
||||
void addDevice(DeviceForm resources) throws Exception;
|
||||
|
||||
// /**
|
||||
// * 查询所有数据不分页
|
||||
// *
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.fuyuanshen.equipment.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@ -7,15 +8,22 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.fuyuanshen.common.core.domain.PageResult;
|
||||
import com.fuyuanshen.common.core.utils.PageUtil;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.dto.DeviceQueryCriteria;
|
||||
import com.fuyuanshen.equipment.constants.DeviceConstants;
|
||||
import com.fuyuanshen.equipment.domain.DeviceType;
|
||||
import com.fuyuanshen.equipment.domain.form.DeviceForm;
|
||||
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
|
||||
import com.fuyuanshen.equipment.enums.DeviceStatusEnum;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.equipment.service.DeviceService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@ -28,6 +36,12 @@ import java.util.*;
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
|
||||
|
||||
@Value("${file.device.pic}")
|
||||
private String filePath;
|
||||
@Value("${file.device.ip}")
|
||||
private String ip;
|
||||
|
||||
|
||||
private final DeviceMapper deviceMapper;
|
||||
|
||||
|
||||
@ -54,97 +68,60 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
return PageUtil.toPage(devices);
|
||||
}
|
||||
|
||||
//
|
||||
// @Override
|
||||
// public List<Device> queryAll(DeviceQueryCriteria criteria) {
|
||||
// return deviceMapper.findAll(criteria);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public List<Device> queryAllDevices(DeviceQueryCriteria criteria) {
|
||||
// return deviceMapper.findAllDevices(criteria);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public void create(Device resources) {
|
||||
// deviceMapper.insert(resources);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 新增设备
|
||||
// *
|
||||
// * @param deviceForm
|
||||
// * @throws Exception
|
||||
// */
|
||||
// @Override
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
// public void addDevice(DeviceForm deviceForm) throws Exception {
|
||||
//
|
||||
// // 获取当前登录用户信息
|
||||
// // Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// // String username1 = authentication.getName();
|
||||
// // 从缓存获取
|
||||
// // UserDetails currentUser = SecurityUtils.getCurrentUser();
|
||||
// // String username = currentUser.getUsername();
|
||||
// // JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
||||
// User currentUser = userMapper.findByUsername(SecurityUtils.getCurrentUsername());
|
||||
//
|
||||
// if (StringUtils.isNotEmpty(deviceForm.getDeviceMac())) {
|
||||
// QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
|
||||
// queryWrapper.eq("device_mac", deviceForm.getDeviceMac());
|
||||
// // queryWrapper.eq("tenant_id", currentUser.getTenantId());
|
||||
// if ((deviceMapper.selectOne(queryWrapper)) != null) {
|
||||
// throw new BadRequestException("设备 mac地址 有误,请仔细核对!!!");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// QueryWrapper<DeviceTypeGrants> deviceTypeGrantsQueryWrapper = new QueryWrapper<>();
|
||||
// deviceTypeGrantsQueryWrapper.eq("customer_id", currentUser.getId());
|
||||
// deviceTypeGrantsQueryWrapper.eq("device_type_id", deviceForm.getDeviceType());
|
||||
// Long count = deviceTypeGrantsMapper.selectCount(deviceTypeGrantsQueryWrapper);
|
||||
// if (count <= 0) {
|
||||
// throw new BadRequestException("请先授权设备类型!!!");
|
||||
// }
|
||||
//
|
||||
// // 保存图片并获取URL
|
||||
// String imageUrl = saveDeviceImage(deviceForm.getFile(), deviceForm.getDeviceMac());
|
||||
// // 设置图片路径
|
||||
// deviceForm.setDevicePic(imageUrl);
|
||||
//
|
||||
// // 转换对象并插入数据库
|
||||
// Device device = new Device();
|
||||
// BeanUtil.copyProperties(deviceForm, device, true);
|
||||
//
|
||||
// // 添加租户ID
|
||||
// device.setTenantId(currentUser.getTenantId());
|
||||
// // 默认状态正常
|
||||
// device.setDeviceStatus(DeviceStatusEnum.NORMAL.getCode());
|
||||
// // SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
|
||||
// // device.setId(snowflakeGenerator.next());
|
||||
// device.setCurrentOwnerId(currentUser.getId());
|
||||
// device.setOriginalOwnerId(currentUser.getId());
|
||||
// DeviceType deviceType = deviceTypeMapper.selectById(deviceForm.getDeviceType());
|
||||
// device.setTypeName(deviceType.getTypeName());
|
||||
//
|
||||
// deviceMapper.insert(device);
|
||||
//
|
||||
// // 新增设备类型记录
|
||||
// DeviceAssignments assignments = new DeviceAssignments();
|
||||
// assignments.setDeviceId(device.getId());
|
||||
// assignments.setAssignedAt(LocalDateTime.now());
|
||||
// // 分配者
|
||||
// assignments.setAssignerId(currentUser.getId());
|
||||
// assignments.setAssignerName(currentUser.getUsername());
|
||||
// // 接收者
|
||||
// assignments.setAssigneeId(currentUser.getId());
|
||||
// assignments.setActive(DeviceActiveStatusEnum.ACTIVE.getCode());
|
||||
// String lever = currentUser.getId() + ":";
|
||||
// assignments.setLever(lever);
|
||||
// deviceAssignmentsService.save(assignments);
|
||||
//
|
||||
// }
|
||||
|
||||
/**
|
||||
* 新增设备
|
||||
*
|
||||
* @param deviceForm
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void addDevice(DeviceForm deviceForm) throws Exception {
|
||||
|
||||
// 获取当前登录用户信息
|
||||
// LoginUser user = LoginHelper.getLoginUser();
|
||||
|
||||
// 保存图片并获取URL
|
||||
String imageUrl = saveDeviceImage(deviceForm.getFile(), deviceForm.getDeviceMac());
|
||||
// 设置图片路径
|
||||
deviceForm.setDevicePic(imageUrl);
|
||||
|
||||
// 转换对象并插入数据库
|
||||
Device device = new Device();
|
||||
BeanUtil.copyProperties(deviceForm, device, true);
|
||||
|
||||
deviceMapper.insert(device);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存设备图片并返回访问路径
|
||||
*
|
||||
* @param file MultipartFile
|
||||
* @param deviceMac 设备MAC用于生成唯一文件名
|
||||
* @return 文件存储路径 URL 形式
|
||||
*/
|
||||
private String saveDeviceImage(MultipartFile file, String deviceMac) throws IOException {
|
||||
if (file == null || file.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String originalFileName = file.getOriginalFilename();
|
||||
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
|
||||
String newFileName = "PS_" + deviceMac + "." + fileExtension;
|
||||
|
||||
File newFile = new File(filePath + DeviceConstants.FILE_ACCESS_ISOLATION + File.separator + newFileName);
|
||||
|
||||
if (!newFile.getParentFile().exists()) {
|
||||
newFile.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
log.info("图片保存路径: {}", newFile.getAbsolutePath());
|
||||
file.transferTo(newFile);
|
||||
|
||||
return ip + DeviceConstants.FILE_ACCESS_PREFIX + "/" + DeviceConstants.FILE_ACCESS_ISOLATION + "/" + newFileName;
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
|
Reference in New Issue
Block a user