forked from dyf/fys-Multi-tenant
Merge remote-tracking branch 'origin/dyf-device'
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
package com.fuyuanshen.equipment.service;
|
||||
|
||||
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
|
||||
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||
import com.fuyuanshen.equipment.domain.bo.UserAppBo;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* APP用户信息Service接口
|
||||
*
|
||||
* @author Lion Li
|
||||
* @date 2025-06-27
|
||||
*/
|
||||
public interface AppUserService {
|
||||
|
||||
/**
|
||||
* 修改APP用户信息
|
||||
*
|
||||
* @param bo APP用户信息
|
||||
* @return 是否修改成功
|
||||
*/
|
||||
Boolean updateByBo(UserAppBo bo);
|
||||
|
||||
|
||||
}
|
@ -76,7 +76,7 @@ public interface DeviceService extends IService<Device> {
|
||||
/**
|
||||
* 撤回设备
|
||||
*/
|
||||
void withdrawDevice(DeviceForm deviceForm);
|
||||
void withdrawDevice(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 解绑设备
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.fuyuanshen.equipment.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.lang.generator.SnowflakeGenerator;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
@ -25,6 +26,7 @@ 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.DeviceStatusEnum;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
|
||||
import com.fuyuanshen.equipment.service.DeviceService;
|
||||
@ -40,9 +42,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:
|
||||
@ -122,7 +122,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void addDevice(DeviceForm deviceForm) throws Exception {
|
||||
DeviceTypeQueryCriteria queryCriteria = new DeviceTypeQueryCriteria();
|
||||
queryCriteria.setDeviceTypeId(deviceForm.getId());
|
||||
queryCriteria.setDeviceTypeId(deviceForm.getDeviceType());
|
||||
queryCriteria.setCustomerId(LoginHelper.getUserId());
|
||||
List<DeviceType> deviceTypes = deviceTypeMapper.findAll(queryCriteria);
|
||||
if (deviceTypes.isEmpty()) {
|
||||
@ -141,6 +141,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);
|
||||
@ -161,7 +162,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
public void update(DeviceForm deviceForm) throws Exception {
|
||||
|
||||
DeviceTypeQueryCriteria deviceTypeQueryCriteria = new DeviceTypeQueryCriteria();
|
||||
deviceTypeQueryCriteria.setDeviceTypeId(deviceForm.getId());
|
||||
deviceTypeQueryCriteria.setDeviceTypeId(deviceForm.getDeviceType());
|
||||
deviceTypeQueryCriteria.setCustomerId(LoginHelper.getUserId());
|
||||
List<DeviceType> deviceTypes = deviceTypeMapper.findAll(deviceTypeQueryCriteria);
|
||||
if (deviceTypes.isEmpty()) {
|
||||
@ -179,9 +180,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);
|
||||
@ -248,8 +251,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();
|
||||
@ -259,51 +319,117 @@ 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())) {
|
||||
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);
|
||||
|
||||
devicesToAssign.add(device);
|
||||
}
|
||||
|
||||
if (!invalidIds.isEmpty()) {
|
||||
throw new RuntimeException("以下设备无法分配(ID 不存在或无权限): " + invalidIds);
|
||||
}
|
||||
|
||||
devices.forEach((device) -> {
|
||||
deviceMapper.updateById(device);
|
||||
device.setId(null);
|
||||
device.setCurrentOwnerId(customerId);
|
||||
deviceMapper.insert(device);
|
||||
|
||||
DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
|
||||
deviceType.setId(null);
|
||||
device.setCurrentOwnerId(customerId);
|
||||
deviceTypeMapper.insert(deviceType);
|
||||
|
||||
});
|
||||
// 批量处理设备分配
|
||||
batchAssignDevices(devicesToAssign, customer);
|
||||
}
|
||||
|
||||
public void assign(List<Long> deviceIds) {
|
||||
@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("设备类型有问题!!! ");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 撤回设备
|
||||
*
|
||||
* @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());
|
||||
deviceMapper.updateById(device);
|
||||
});
|
||||
});
|
||||
|
||||
ids.forEach((id) -> {
|
||||
Device device = new Device();
|
||||
device.setId(id);
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -11,10 +11,12 @@ import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||
import com.fuyuanshen.common.satoken.utils.LoginHelper;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.DeviceType;
|
||||
import com.fuyuanshen.equipment.domain.DeviceTypeGrants;
|
||||
import com.fuyuanshen.equipment.domain.form.DeviceTypeForm;
|
||||
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
|
||||
import com.fuyuanshen.equipment.domain.query.DeviceTypeQueryCriteria;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceTypeGrantsMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
|
||||
import com.fuyuanshen.equipment.service.DeviceTypeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -22,6 +24,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -37,6 +40,8 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
|
||||
private final DeviceTypeMapper deviceTypeMapper;
|
||||
private final DeviceMapper deviceMapper;
|
||||
|
||||
private final DeviceTypeGrantsMapper deviceTypeGrantsMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 分页查询设备类型
|
||||
@ -85,8 +90,17 @@ 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);
|
||||
|
||||
// 自动授权给自己
|
||||
DeviceTypeGrants deviceTypeGrants = new DeviceTypeGrants();
|
||||
deviceTypeGrants.setDeviceTypeId(resources.getId());
|
||||
deviceTypeGrants.setCustomerId(loginUser.getUserId());
|
||||
deviceTypeGrants.setGrantorCustomerId(loginUser.getUserId());
|
||||
deviceTypeGrants.setGrantedAt(new Date());
|
||||
deviceTypeGrantsMapper.insert(deviceTypeGrants);
|
||||
}
|
||||
|
||||
|
||||
@ -98,6 +112,11 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(DeviceTypeForm resources) {
|
||||
DeviceTypeGrants deviceTypeGrants = deviceTypeGrantsMapper.selectById(resources.getId());
|
||||
if (deviceTypeGrants == null) {
|
||||
throw new RuntimeException("设备类型不存在");
|
||||
}
|
||||
resources.setId(deviceTypeGrants.getDeviceTypeId());
|
||||
DeviceType deviceType = deviceTypeMapper.selectById(resources.getId());
|
||||
if (deviceType == null) {
|
||||
throw new RuntimeException("设备类型不存在");
|
||||
@ -121,28 +140,30 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteAll(List<Long> ids) {
|
||||
|
||||
List<Long> invalidIds = new ArrayList<>();
|
||||
List<Long> invalidId2 = new ArrayList<>();
|
||||
for (Long id : ids) {
|
||||
DeviceType deviceType = deviceTypeMapper.selectById(id);
|
||||
if (deviceType == null || !Objects.equals(deviceType.getCustomerId(), LoginHelper.getUserId())) {
|
||||
invalidIds.add(id);
|
||||
}
|
||||
DeviceQueryCriteria deviceQueryCriteria = new DeviceQueryCriteria();
|
||||
deviceQueryCriteria.setDeviceTypeId(id);
|
||||
List<Device> devices = deviceMapper.findAll(deviceQueryCriteria);
|
||||
if (!devices.isEmpty()) {
|
||||
invalidId2.add(id);
|
||||
}
|
||||
}
|
||||
if (!invalidIds.isEmpty()) {
|
||||
throw new RuntimeException("以下设备类型无法删除(ID 不存在或无权限): " + invalidIds);
|
||||
}
|
||||
if (!invalidId2.isEmpty()) {
|
||||
throw new RuntimeException("以下设备类型无法删除(已绑定设备): " + invalidId2);
|
||||
}
|
||||
|
||||
deviceTypeMapper.deleteByIds(ids);
|
||||
deviceTypeGrantsMapper.deleteByIds(ids);
|
||||
//
|
||||
// List<Long> invalidIds = new ArrayList<>();
|
||||
// List<Long> invalidId2 = new ArrayList<>();
|
||||
// for (Long id : ids) {
|
||||
// DeviceType deviceType = deviceTypeMapper.selectById(id);
|
||||
// if (deviceType == null || !Objects.equals(deviceType.getOwnerCustomerId(), LoginHelper.getUserId())) {
|
||||
// invalidIds.add(id);
|
||||
// }
|
||||
// DeviceQueryCriteria deviceQueryCriteria = new DeviceQueryCriteria();
|
||||
// deviceQueryCriteria.setDeviceType(id);
|
||||
// List<Device> devices = deviceMapper.findAll(deviceQueryCriteria);
|
||||
// if (!devices.isEmpty()) {
|
||||
// invalidId2.add(id);
|
||||
// }
|
||||
// }
|
||||
// if (!invalidIds.isEmpty()) {
|
||||
// throw new RuntimeException("以下设备类型无法删除(ID 不存在或无权限): " + invalidIds);
|
||||
// }
|
||||
// if (!invalidId2.isEmpty()) {
|
||||
// throw new RuntimeException("以下设备类型无法删除(已绑定设备): " + invalidId2);
|
||||
// }
|
||||
//
|
||||
// deviceTypeMapper.deleteByIds(ids);
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.fuyuanshen.equipment.service.impl;
|
||||
|
||||
import com.fuyuanshen.common.core.utils.MapstructUtils;
|
||||
import com.fuyuanshen.equipment.domain.UserApp;
|
||||
import com.fuyuanshen.equipment.domain.bo.UserAppBo;
|
||||
import com.fuyuanshen.equipment.mapper.UserAppMapper;
|
||||
import com.fuyuanshen.equipment.service.AppUserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Service业务层处理
|
||||
*
|
||||
* @author Lion Li
|
||||
* @date 2025-06-27
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class UserAppServiceImpl implements AppUserService {
|
||||
|
||||
private final UserAppMapper baseMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 修改APP用户信息
|
||||
*
|
||||
* @param bo APP用户信息
|
||||
* @return 是否修改成功
|
||||
*/
|
||||
@Override
|
||||
public Boolean updateByBo(UserAppBo bo) {
|
||||
UserApp update = MapstructUtils.convert(bo, UserApp.class);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user