@ -17,7 +17,9 @@ import com.fuyuanshen.customer.domain.Customer;
import com.fuyuanshen.customer.mapper.CustomerMapper ;
import com.fuyuanshen.equipment.constants.DeviceConstants ;
import com.fuyuanshen.equipment.domain.Device ;
import com.fuyuanshen.equipment.domain.DeviceAssignments ;
import com.fuyuanshen.equipment.domain.DeviceType ;
import com.fuyuanshen.equipment.domain.DeviceTypeGrants ;
import com.fuyuanshen.equipment.domain.dto.AppDeviceBo ;
import com.fuyuanshen.equipment.domain.form.DeviceForm ;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria ;
@ -26,9 +28,12 @@ import com.fuyuanshen.equipment.domain.vo.AppDeviceVo;
import com.fuyuanshen.equipment.domain.vo.CustomerVo ;
import com.fuyuanshen.equipment.enums.BindingStatusEnum ;
import com.fuyuanshen.equipment.enums.CommunicationModeEnum ;
import com.fuyuanshen.equipment.enums.DeviceActiveStatusEnum ;
import com.fuyuanshen.equipment.enums.DeviceStatusEnum ;
import com.fuyuanshen.equipment.mapper.DeviceMapper ;
import com.fuyuanshen.equipment.mapper.DeviceTypeGrantsMapper ;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper ;
import com.fuyuanshen.equipment.service.DeviceAssignmentsService ;
import com.fuyuanshen.equipment.service.DeviceService ;
import com.fuyuanshen.system.domain.vo.SysOssVo ;
import com.fuyuanshen.system.service.ISysOssService ;
@ -42,6 +47,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File ;
import java.io.IOException ;
import java.sql.Timestamp ;
import java.time.LocalDateTime ;
import java.util.* ;
/**
@ -61,11 +67,13 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
private final DeviceMapper deviceMapper ;
private final DeviceTypeMapper deviceTypeMapper ;
private final CustomerMapper customerMapper ;
private final ISysOssService ossService ;
private final DeviceAssignmentsService deviceAssignmentsService ;
private final DeviceTypeGrantsMapper deviceTypeGrantsMapper ;
/**
* 分页查询设备
@ -112,6 +120,43 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
}
// /**
// * 新增设备
// *
// * @param deviceForm
// * @throws Exception
// */
// @Override
// @Transactional(rollbackFor = Exception.class)
// public void addDevice(DeviceForm deviceForm) throws Exception {
// DeviceTypeQueryCriteria queryCriteria = new DeviceTypeQueryCriteria();
// queryCriteria.setDeviceTypeId(deviceForm.getDeviceType());
// queryCriteria.setCustomerId(LoginHelper.getUserId());
// List<DeviceType> deviceTypes = deviceTypeMapper.findAll(queryCriteria);
// if (deviceTypes.isEmpty()) {
// throw new Exception("设备类型不存在!!!");
// }
//
// // 保存图片并获取URL
// if (deviceForm.getFile() != null) {
// SysOssVo upload = ossService.upload(deviceForm.getFile());
// // 设置图片路径
// deviceForm.setDevicePic(upload.getUrl());
// }
//
// // 转换对象并插入数据库
// Device device = new Device();
// LoginUser loginUser = LoginHelper.getLoginUser();
// device.setCurrentOwnerId(loginUser.getUserId());
// device.setOriginalOwnerId(loginUser.getUserId());
// device.setCreateByName(loginUser.getNickname());
// device.setTypeName(deviceTypes.get(0).getTypeName());
// BeanUtil.copyProperties(deviceForm, device, true);
//
// deviceMapper.insert(device);
//
// }
/**
* 新增设备
*
@ -124,13 +169,13 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
DeviceTypeQueryCriteria queryCriteria = new DeviceTypeQueryCriteria ( ) ;
queryCriteria . setDeviceTypeId ( deviceForm . getDeviceType ( ) ) ;
queryCriteria . setCustomerId ( LoginHelper . getUserId ( ) ) ;
List < DeviceType> deviceType s = deviceTypeMapper . findAll ( queryCriteria ) ;
if ( deviceTypes . isEmpty ( ) ) {
DeviceTypeGrants typeGrant s = deviceTypeGrants Mapper . selectById ( queryCriteria . getDeviceTypeId ( ) );
DeviceType deviceTypes = deviceTypeMapper . selectById ( typeGrants . getDeviceTypeId ( ) ) ;
if ( deviceTypes = = null ) {
throw new Exception ( " 设备类型不存在!!! " ) ;
}
// 保存图片并获取URL
// String imageUrl = saveDeviceImage(deviceForm.getFile(), deviceForm.getDeviceName());
if ( deviceForm . getFile ( ) ! = null ) {
SysOssVo upload = ossService . upload ( deviceForm . getFile ( ) ) ;
// 设置图片路径
@ -143,11 +188,26 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
device . setCurrentOwnerId ( loginUser . getUserId ( ) ) ;
device . setOriginalOwnerId ( loginUser . getUserId ( ) ) ;
device . setCreateByName ( loginUser . getNickname ( ) ) ;
device . setTypeName ( deviceTypes . get ( 0 ) . getTypeName( ) ) ;
device . setTypeName ( deviceTypes . getTypeName ( ) ) ;
device . setDeviceType ( deviceTypes . getId ( ) ) ;
BeanUtil . copyProperties ( deviceForm , device , true ) ;
deviceMapper . insert ( device ) ;
// 新增设备类型记录
DeviceAssignments assignments = new DeviceAssignments ( ) ;
assignments . setDeviceId ( device . getId ( ) ) ;
assignments . setAssignedAt ( LocalDateTime . now ( ) ) ;
// 分配者
assignments . setAssignerId ( loginUser . getUserId ( ) ) ;
assignments . setAssignerName ( loginUser . getUsername ( ) ) ;
// 接收者
assignments . setAssigneeId ( loginUser . getUserId ( ) ) ;
assignments . setActive ( DeviceActiveStatusEnum . ACTIVE . getCode ( ) ) ;
String lever = loginUser . getUserId ( ) + " : " ;
assignments . setLever ( lever ) ;
deviceAssignmentsService . save ( assignments ) ;
}
@ -232,17 +292,20 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
public void deleteAll ( List < Long > ids ) {
List < Long > invalidIds = new ArrayList < > ( ) ;
for ( Long id : ids ) {
Device deviceType = deviceMapper . selectById ( id ) ;
if ( deviceType = = null | | ! Objects . equals ( deviceType . getCurrentOwnerId ( ) , LoginHelper . getUserId ( ) ) ) {
invalidIds . add ( id ) ;
}
}
if ( ! invalidIds . isEmpty ( ) ) {
throw new RuntimeException ( " 以下设备无法删除( ID 不存在或无权限): " + invalidIds ) ;
}
deviceMapper . deleteByIds ( ids ) ;
deviceTypeGrantsMapper . deleteByIds ( ids ) ;
//
// for (Long id : ids) {
//
// Device deviceType = deviceMapper.selectById(id);
// if (deviceType == null || !Objects.equals(deviceType.getCurrentOwnerId(), LoginHelper.getUserId())) {
// invalidIds.add(id);
// }
// }
// if (!invalidIds.isEmpty()) {
// throw new RuntimeException("以下设备无法删除( ID 不存在或无权限): " + invalidIds) ;
// }
//
// deviceMapper.deleteByIds(ids);
}