1
0

APP/小程序用户设备绑定

This commit is contained in:
2025-07-05 15:55:46 +08:00
parent 5322e84a92
commit ad82ab5fca
18 changed files with 376 additions and 98 deletions

View File

@ -73,7 +73,7 @@ public interface DeviceService extends IService<Device> {
/**
* 撤回设备
*/
void withdrawDevice(DeviceForm deviceForm);
void withdrawDevice(List<Long> ids);
/**
* 解绑设备

View File

@ -17,6 +17,7 @@ import com.fuyuanshen.equipment.domain.form.DeviceForm;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
import com.fuyuanshen.equipment.domain.query.DeviceTypeQueryCriteria;
import com.fuyuanshen.equipment.domain.vo.CustomerVo;
import com.fuyuanshen.equipment.enums.DeviceStatusEnum;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
import com.fuyuanshen.equipment.service.DeviceService;
@ -32,9 +33,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
* @Description:
@ -133,6 +132,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
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);
@ -171,9 +171,11 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
// 处理上传的图片
// String imageUrl = saveDeviceImage(deviceForm.getFile(), device.getDeviceName());
SysOssVo upload = ossService.upload(deviceForm.getFile());
// 设置图片路径
deviceForm.setDevicePic(upload.getUrl());
if (deviceForm.getFile() != null) {
SysOssVo upload = ossService.upload(deviceForm.getFile());
// 设置图片路径
deviceForm.setDevicePic(upload.getUrl());
}
// 更新字段
BeanUtil.copyProperties(deviceForm, device, true);
@ -240,8 +242,65 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
*
* @param customerVo
*/
// @Override
// @Transactional(rollbackFor = Exception.class)
// public void assignCustomer1(CustomerVo customerVo) {
// List<Long> deviceIds = customerVo.getDeviceIds();
// Long customerId = customerVo.getCustomerId();
//
// Customer customer = customerMapper.queryCustomerById(customerId, LoginHelper.getLoginUser().getPid());
// if (customer == null) {
// throw new RuntimeException("待分配的客户不存在!!!");
// }
//
// List<Long> invalidIds = new ArrayList<>();
// List<Device> devices = new ArrayList<>();
// for (Long id : deviceIds) {
// Device device = deviceMapper.selectById(id);
// if (device == null || !Objects.equals(device.getCurrentOwnerId(), LoginHelper.getUserId())) {
// invalidIds.add(id);
// continue;
// }
// Device assignCustomer = deviceMapper.getAssignCustomer(device.getId());
// if (assignCustomer != null) {
// invalidIds.add(id);
// continue;
// }
// device.setCustomerId(customerId);
// device.setCustomerName(customer.getNickName());
// devices.add(device);
// }
// if (!invalidIds.isEmpty()) {
// throw new RuntimeException("以下设备无法分配ID 不存在或无权限): " + invalidIds);
// }
//
// devices.forEach((device) -> {
//
// deviceMapper.updateById(device);
// device.setCurrentOwnerId(customerId);
// if (device.getDeviceType() == null) {
// throw new RuntimeException("设备类型有问题!!! ");
// }
// DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
// SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
// device.setOriginalDeviceId(device.getId());
// Long next = snowflakeGenerator.next();
// device.setId(next);
// device.setDeviceType(next);
//
// DeviceType assignType = deviceTypeMapper.getAssignType(device.getDeviceType(), customerId);
// if (assignType == null) {
// deviceType.setOriginalDeviceId(deviceType.getId());
// deviceType.setId(next);
// deviceType.setOwnerCustomerId(customerId);
// deviceTypeMapper.insert(deviceType);
// }
//
// deviceMapper.insert(device);
//
// });
// }
@Override
@Transactional(rollbackFor = Exception.class)
public void assignCustomer(CustomerVo customerVo) {
List<Long> deviceIds = customerVo.getDeviceIds();
Long customerId = customerVo.getCustomerId();
@ -251,8 +310,9 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
throw new RuntimeException("待分配的客户不存在!!!");
}
List<Device> devicesToAssign = new ArrayList<>();
List<Long> invalidIds = new ArrayList<>();
List<Device> devices = new ArrayList<>();
for (Long id : deviceIds) {
Device device = deviceMapper.selectById(id);
if (device == null || !Objects.equals(device.getCurrentOwnerId(), LoginHelper.getUserId())) {
@ -266,51 +326,94 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
}
device.setCustomerId(customerId);
device.setCustomerName(customer.getNickName());
devices.add(device);
devicesToAssign.add(device);
}
if (!invalidIds.isEmpty()) {
throw new RuntimeException("以下设备无法分配ID 不存在或无权限): " + invalidIds);
}
devices.forEach((device) -> {
// 批量处理设备分配
batchAssignDevices(devicesToAssign, customer);
}
deviceMapper.updateById(device);
device.setCurrentOwnerId(customerId);
@Transactional(rollbackFor = Exception.class)
public void batchAssignDevices(List<Device> devicesToAssign, Customer customer) {
Long userId = LoginHelper.getUserId();
SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
for (Device device : devicesToAssign) {
if (device.getDeviceType() == null) {
throw new RuntimeException("设备类型有问题!!! ");
}
DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
device.setOriginalOwnerId(device.getId());
Long next = snowflakeGenerator.next();
device.setId(next);
device.setDeviceType(next);
deviceType.setOriginalOwnerId(deviceType.getId());
deviceType.setId(next);
deviceType.setOwnerCustomerId(customerId);
deviceMapper.updateById(device);
DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
DeviceType assignType = deviceTypeMapper.getAssignType(device.getDeviceType(), customer.getCustomerId());
Long next = snowflakeGenerator.next();
device.setOriginalDeviceId(device.getId());
device.setCurrentOwnerId(customer.getCustomerId());
device.setOriginalOwnerId(device.getCurrentOwnerId());
device.setCustomerId(null);
device.setCustomerName("");
device.setId(next);
if (assignType == null) {
deviceType.setOriginalDeviceId(deviceType.getId());
deviceType.setOriginalOwnerId(deviceType.getOwnerCustomerId());
deviceType.setId(next);
device.setDeviceType(next);
deviceType.setOwnerCustomerId(customer.getCustomerId());
deviceTypeMapper.insert(deviceType);
} else {
device.setDeviceType(assignType.getId());
}
deviceMapper.insert(device);
deviceTypeMapper.insert(deviceType);
});
}
public void assign(List<Long> deviceIds) {
}
}
/**
* 撤回设备
*
* @param deviceForm
* @param ids
*/
@Override
public void withdrawDevice(DeviceForm deviceForm) {
public void withdrawDevice(List<Long> ids) {
ids.forEach((id) -> {
List<Device> deviceChain = getDeviceChain(id);
deviceChain.forEach((device) -> {
device.setDeviceStatus(DeviceStatusEnum.INVALID.getCode());
// device.setCustomerId(null);
// device.setCustomerName("");
deviceMapper.updateById(device);
});
});
}
public List<Device> getDeviceChain(Long originalDeviceId) {
List<Device> chain = new ArrayList<>();
Set<Long> visited = new HashSet<>(); // 防止循环引用
findNext(chain, visited, originalDeviceId);
return chain;
}
private void findNext(List<Device> chain, Set<Long> visited, Long currentOriginalDeviceId) {
if (visited.contains(currentOriginalDeviceId)) {
log.info("检测到循环引用,终止递归");
return;
}
visited.add(currentOriginalDeviceId);
List<Device> devices = deviceMapper.findByOriginalDeviceId(currentOriginalDeviceId);
for (Device device : devices) {
chain.add(device);
findNext(chain, visited, device.getId());
}
}

View File

@ -85,6 +85,7 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
LoginUser loginUser = LoginHelper.getLoginUser();
resources.setCustomerId(loginUser.getUserId());
resources.setOwnerCustomerId(loginUser.getUserId());
resources.setOriginalOwnerId(loginUser.getUserId());
resources.setCreateByName(loginUser.getNickname());
deviceTypeMapper.insert(resources);
}