diff --git a/fys-admin/src/main/resources/application.yml b/fys-admin/src/main/resources/application.yml index b99b5d2..f550a10 100644 --- a/fys-admin/src/main/resources/application.yml +++ b/fys-admin/src/main/resources/application.yml @@ -216,6 +216,8 @@ springdoc: packages-to-scan: com.fuyuanshen.equipment - group: 客户管理模块 packages-to-scan: com.fuyuanshen.customer + - group: APP模块 + packages-to-scan: com.fuyuanshen.app # 防止XSS攻击 xss: diff --git a/fys-modules/fys-app/pom.xml b/fys-modules/fys-app/pom.xml index 1dd5471..de945a3 100644 --- a/fys-modules/fys-app/pom.xml +++ b/fys-modules/fys-app/pom.xml @@ -15,6 +15,11 @@ + + com.fuyuanshen + fys-equipment + + com.fuyuanshen diff --git a/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/enums/UserType.java b/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/enums/UserType.java new file mode 100644 index 0000000..1e7cb35 --- /dev/null +++ b/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/enums/UserType.java @@ -0,0 +1,48 @@ +package com.fuyuanshen.app.enums; + +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * 用户类型枚举 + * + * @author: 默苍璃 + * @date: 2025-06-1811:14 + */ +public enum UserType { + + APP(0, "APP"), MINI_PROGRAM(1, "小程序"); + + private final int value; + private final String description; + + UserType(int value, String description) { + this.value = value; + this.description = description; + } + + @JsonValue + public int getValue() { + return value; + } + + public String getDescription() { + return description; + } + + /** + * 根据值获取对应的枚举 + * + * @param value 枚举值 + * @return 对应的枚举对象 + */ + public static UserType fromValue(int value) { + for (UserType userType : values()) { + if (userType.getValue() == value) { + return userType; + } + } + throw new IllegalArgumentException("Invalid user type value: " + value); + } + +} + diff --git a/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/mapper/equipment/AppDeviceTypeMapper.java b/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/mapper/equipment/AppDeviceTypeMapper.java new file mode 100644 index 0000000..774ae54 --- /dev/null +++ b/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/mapper/equipment/AppDeviceTypeMapper.java @@ -0,0 +1,29 @@ +package com.fuyuanshen.app.mapper.equipment; + + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.fuyuanshen.app.domain.APPDeviceType; +import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author 97433 + * @description 针对表【app_device_type(设备类型表)】的数据库操作Mapper + * @createDate 2025-06-24 11:16:18 + * @Entity system.domain.AppDeviceType + */ +@Mapper +public interface AppDeviceTypeMapper extends BaseMapper { + + /** + * 查询设备类型列表 + * + * @param criteria 查询条件 + * @return 设备类型列表 + */ + List appTypeList(@Param("criteria") DeviceQueryCriteria criteria); + +} diff --git a/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/service/impl/equipment/APPDeviceServiceImpl.java b/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/service/impl/equipment/APPDeviceServiceImpl.java index d5c659b..b5715a7 100644 --- a/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/service/impl/equipment/APPDeviceServiceImpl.java +++ b/fys-modules/fys-app/src/main/java/com/fuyuanshen/app/service/impl/equipment/APPDeviceServiceImpl.java @@ -10,9 +10,19 @@ import com.fuyuanshen.app.domain.APPDevice; import com.fuyuanshen.app.domain.APPDeviceType; import com.fuyuanshen.app.domain.dto.APPUnbindDTO; import com.fuyuanshen.app.domain.query.APPDeviceQueryCriteria; +import com.fuyuanshen.app.enums.UserType; import com.fuyuanshen.app.mapper.equipment.APPDeviceMapper; +import com.fuyuanshen.app.mapper.equipment.AppDeviceTypeMapper; import com.fuyuanshen.app.service.equipment.APPDeviceService; import com.fuyuanshen.common.core.domain.PageResult; +import com.fuyuanshen.common.core.exception.BadRequestException; +import com.fuyuanshen.common.satoken.utils.LoginHelper; +import com.fuyuanshen.equipment.domain.Device; +import com.fuyuanshen.equipment.domain.DeviceType; +import com.fuyuanshen.equipment.enums.BindingStatusEnum; +import com.fuyuanshen.equipment.enums.CommunicationModeEnum; +import com.fuyuanshen.equipment.mapper.DeviceMapper; +import com.fuyuanshen.equipment.mapper.DeviceTypeMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -34,6 +44,9 @@ import java.util.stream.Collectors; public class APPDeviceServiceImpl extends ServiceImpl implements APPDeviceService { private final APPDeviceMapper appDeviceMapper; + private final DeviceMapper deviceMapper; + private final AppDeviceTypeMapper appDeviceTypeMapper; + private final DeviceTypeMapper deviceTypeMapper; /** @@ -72,56 +85,58 @@ public class APPDeviceServiceImpl extends ServiceImpl devices = new ArrayList<>(); - // - // if (criteria.getCommunicationMode().equals(CommunicationModeEnum.BLUETOOTH.getValue())) { - // devices = deviceMapper.selectList(new QueryWrapper().eq("device_mac", criteria.getDeviceMac())); - // if (CollectionUtil.isEmpty(devices)) { - // throw new BadRequestException("请先将设备入库!!!"); - // } - // List appDevices = appDeviceMapper.selectList(new QueryWrapper() - // .eq("device_mac", criteria.getDeviceMac()).eq("binding_type", UserType.APP.getValue())); - // if (CollectionUtil.isNotEmpty(appDevices)) { - // throw new BadRequestException("该设备已绑定!!!"); - // } - // } - // - // if (criteria.getCommunicationMode().equals(CommunicationModeEnum.FOUR_G.getValue())) { - // devices = deviceMapper.selectList(new QueryWrapper().eq("device_imei", criteria.getDeviceImei())); - // if (CollectionUtil.isEmpty(devices)) { - // throw new BadRequestException("请先将设备入库!!!"); - // } - // List appDevices = appDeviceMapper.selectList(new QueryWrapper() - // .eq("device_imei", criteria.getDeviceImei()).eq("binding_type", UserType.APP.getValue())); - // if (CollectionUtil.isNotEmpty(appDevices)) { - // throw new BadRequestException("该设备已绑定!!!"); - // } - // } - // - // Device device = devices.get(0); - // device.setBindingStatus(BindingStatusEnum.BOUND.getCode()); - // deviceMapper.updateById(device); - // - // APPDevice appDevice = new APPDevice(); - // BeanUtil.copyProperties(device, appDevice); - // appDevice.setBindingType(UserType.APP.getValue()); - // appDevice.setBindingStatus(BindingStatusEnum.BOUND.getCode()); - // Long currentUserId = SecurityUtils.getCurrentUserId(); - // appDevice.setCustomerId(currentUserId); - // appDevice.setCreateTime(new Timestamp(System.currentTimeMillis())); - // // 设备类型名称 - // appDevice.setDeviceTypeName(device.getTypeName()); - // appDeviceMapper.insert(appDevice); - // - // APPDeviceType appDeviceType = appDeviceTypeMapper.selectById(device.getDeviceType()); - // if (appDeviceType == null) { - // DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType()); - // APPDeviceType type = new APPDeviceType(); - // BeanUtil.copyProperties(deviceType, type); - // type.setCustomerId(currentUserId); - // appDeviceTypeMapper.insert(type); - // } + List devices = new ArrayList<>(); + if (criteria.getCommunicationMode().equals(CommunicationModeEnum.BLUETOOTH.getValue())) { + devices = deviceMapper.selectList(new QueryWrapper() + .eq("original_device_id", null) + .eq("device_mac", criteria.getDeviceMac())); + if (CollectionUtil.isEmpty(devices)) { + throw new BadRequestException("请先将设备入库!!!"); + } + List appDevices = appDeviceMapper.selectList(new QueryWrapper() + .eq("device_mac", criteria.getDeviceMac()).eq("binding_type", UserType.APP.getValue())); + if (CollectionUtil.isNotEmpty(appDevices)) { + throw new BadRequestException("该设备已绑定!!!"); + } + } + + if (criteria.getCommunicationMode().equals(CommunicationModeEnum.FOUR_G.getValue())) { + devices = deviceMapper.selectList(new QueryWrapper() + .eq("original_device_id", null) + .eq("device_imei", criteria.getDeviceImei())); + if (CollectionUtil.isEmpty(devices)) { + throw new BadRequestException("请先将设备入库!!!"); + } + List appDevices = appDeviceMapper.selectList(new QueryWrapper() + .eq("device_imei", criteria.getDeviceImei()).eq("binding_type", UserType.APP.getValue())); + if (CollectionUtil.isNotEmpty(appDevices)) { + throw new BadRequestException("该设备已绑定!!!"); + } + } + + Device device = devices.get(0); + device.setBindingStatus(BindingStatusEnum.BOUND.getCode()); + deviceMapper.updateById(device); + + APPDevice appDevice = new APPDevice(); + BeanUtil.copyProperties(device, appDevice); + appDevice.setBindingType(UserType.APP.getValue()); + appDevice.setBindingStatus(BindingStatusEnum.BOUND.getCode()); + Long userId = LoginHelper.getUserId(); + appDevice.setCustomerId(userId); + appDevice.setCreateTime(new Timestamp(System.currentTimeMillis())); + // 设备类型名称 + appDevice.setDeviceTypeName(device.getTypeName()); + appDeviceMapper.insert(appDevice); + + APPDeviceType appDeviceType = appDeviceTypeMapper.selectById(device.getDeviceType()); + if (appDeviceType == null) { + DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType()); + APPDeviceType type = new APPDeviceType(); + BeanUtil.copyProperties(deviceType, type); + type.setCustomerId(userId); + appDeviceTypeMapper.insert(type); + } } diff --git a/fys-modules/fys-app/src/main/resources/mapper/app/equipment/AppDeviceTypeMapper.xml b/fys-modules/fys-app/src/main/resources/mapper/app/equipment/AppDeviceTypeMapper.xml new file mode 100644 index 0000000..5c1fd86 --- /dev/null +++ b/fys-modules/fys-app/src/main/resources/mapper/app/equipment/AppDeviceTypeMapper.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + id + ,type_name,is_support_ble,locate_mode,network_way,create_by, + update_by,create_time,update_time,customer_id,communication_mode + + + + + diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/controller/DeviceController.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/controller/DeviceController.java index e6405d8..01b89fb 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/controller/DeviceController.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/controller/DeviceController.java @@ -135,7 +135,7 @@ public class DeviceController { @PostMapping(value = "/withdraw") public ResponseVO withdrawDevice(@RequestBody List ids) { try { - // deviceService.withdrawDevice(deviceForm); + deviceService.withdrawDevice(ids); } catch (Exception e) { log.error("updateDevice error: " + e.getMessage()); return ResponseVO.fail("出错了"); diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/Device.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/Device.java index 5536e1b..2361024 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/Device.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/Device.java @@ -1,7 +1,5 @@ package com.fuyuanshen.equipment.domain; -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.bean.copier.CopyOptions; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; @@ -55,6 +53,11 @@ public class Device extends TenantEntity { @Schema(name = "原始所有者(创建者)") private Long originalOwnerId; + /** + * 原始设备 + */ + @Schema(name = "原始设备") + private Long originalDeviceId; /*@Schema( name = "设备编号") private String deviceNo;*/ @@ -99,7 +102,6 @@ public class Device extends TenantEntity { @Schema(name = "绑定状态") private Integer bindingStatus; - /** * 创建人名称 */ diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/DeviceType.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/DeviceType.java index dc6d542..76e554e 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/DeviceType.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/DeviceType.java @@ -34,6 +34,11 @@ public class DeviceType extends TenantEntity { @Schema(name = "原始所有者(创建者)") private Long originalOwnerId; + /** + * 原始设备 + */ + @Schema(name = "原始设备类型") + private Long originalDeviceId; @NotBlank(message = "设备类型名称不能为空") @Schema(name = "类型名称", required = true) diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/dto/DeviceExcelImportDTO.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/dto/DeviceExcelImportDTO.java index 7fce9e8..f2f9a87 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/dto/DeviceExcelImportDTO.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/domain/dto/DeviceExcelImportDTO.java @@ -26,7 +26,6 @@ public class DeviceExcelImportDTO { @ColumnWidth(20) private String deviceName; - @ExcelProperty(value = "设备图片", converter = ByteArrayImageConverter.class) @ColumnWidth(15) private byte[] devicePic; diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/excel/UploadDeviceDataListener.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/excel/UploadDeviceDataListener.java index 7869e0d..7647eaa 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/excel/UploadDeviceDataListener.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/excel/UploadDeviceDataListener.java @@ -29,7 +29,6 @@ import java.util.*; @Slf4j public class UploadDeviceDataListener implements ReadListener { - // 存储图片数据的映射 private final Map rowImageMap = new HashMap<>(); @@ -48,6 +47,7 @@ public class UploadDeviceDataListener implements ReadListener deviceList = params.getDeviceMapper().findAll(criteria); - if (!deviceList.isEmpty()) { - throw new RuntimeException("设备MAC重复"); - } - device.setTenantId(params.getTenantId()); + // DeviceQueryCriteria criteria = new DeviceQueryCriteria(); + // criteria.setDeviceMac(device.getDeviceMac()); + // criteria.setTenantId(params.getTenantId()); + // List deviceList = params.getDeviceMapper().findAll(criteria); + // if (!deviceList.isEmpty()) { + // throw new RuntimeException("设备MAC重复"); + // } + // device.setTenantId(params.getTenantId()); // 设备类型 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("type_name", device.getTypeName()); - wrapper.eq("customer_id", params.getUserId()); + // wrapper.eq("customer_id", params.getUserId()); List deviceTypes = params.getDeviceTypeMapper().selectList(wrapper); if (CollectionUtil.isNotEmpty(deviceTypes)) { device.setDeviceType(deviceTypes.get(0).getId()); } + device.setCurrentOwnerId(loginUser.getUserId()); + device.setOriginalOwnerId(loginUser.getUserId()); + device.setCreateByName(loginUser.getNickname()); params.getDeviceService().save(device); successCount++; log.info("行 {} 数据插入成功", rowIndex); diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceMapper.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceMapper.java index be09e6e..7ff7d3c 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceMapper.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceMapper.java @@ -38,4 +38,12 @@ public interface DeviceMapper extends BaseMapper { * @return */ Device getAssignCustomer(Long customerId); + + /** + * 获取设备链 + * + * @param originalDeviceId + * @return + */ + List findByOriginalDeviceId(Long originalDeviceId); } diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceTypeMapper.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceTypeMapper.java index 15f0fd1..90799fe 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceTypeMapper.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/mapper/DeviceTypeMapper.java @@ -36,4 +36,12 @@ public interface DeviceTypeMapper extends BaseMapper { */ List findAll(@Param("criteria") DeviceTypeQueryCriteria criteria); + /** + * 获取已经分配的设备类型 + * + * @param customerId + * @return + */ + DeviceType getAssignType(@Param("deviceType") Long deviceType, @Param("customerId") Long customerId); + } diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/DeviceService.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/DeviceService.java index 4990517..4ff019e 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/DeviceService.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/DeviceService.java @@ -73,7 +73,7 @@ public interface DeviceService extends IService { /** * 撤回设备 */ - void withdrawDevice(DeviceForm deviceForm); + void withdrawDevice(List ids); /** * 解绑设备 diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceServiceImpl.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceServiceImpl.java index f43c918..ddd6813 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceServiceImpl.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceServiceImpl.java @@ -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 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 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 impleme * * @param customerVo */ + // @Override + // @Transactional(rollbackFor = Exception.class) + // public void assignCustomer1(CustomerVo customerVo) { + // List deviceIds = customerVo.getDeviceIds(); + // Long customerId = customerVo.getCustomerId(); + // + // Customer customer = customerMapper.queryCustomerById(customerId, LoginHelper.getLoginUser().getPid()); + // if (customer == null) { + // throw new RuntimeException("待分配的客户不存在!!!"); + // } + // + // List invalidIds = new ArrayList<>(); + // List 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 deviceIds = customerVo.getDeviceIds(); Long customerId = customerVo.getCustomerId(); @@ -251,8 +310,9 @@ public class DeviceServiceImpl extends ServiceImpl impleme throw new RuntimeException("待分配的客户不存在!!!"); } + List devicesToAssign = new ArrayList<>(); List invalidIds = new ArrayList<>(); - List 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 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 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 deviceIds) { - + } } /** * 撤回设备 * - * @param deviceForm + * @param ids */ @Override - public void withdrawDevice(DeviceForm deviceForm) { + public void withdrawDevice(List ids) { + ids.forEach((id) -> { + List deviceChain = getDeviceChain(id); + deviceChain.forEach((device) -> { + device.setDeviceStatus(DeviceStatusEnum.INVALID.getCode()); + // device.setCustomerId(null); + // device.setCustomerName(""); + deviceMapper.updateById(device); + }); + }); + } + public List getDeviceChain(Long originalDeviceId) { + List chain = new ArrayList<>(); + Set visited = new HashSet<>(); // 防止循环引用 + findNext(chain, visited, originalDeviceId); + return chain; + } + + private void findNext(List chain, Set visited, Long currentOriginalDeviceId) { + if (visited.contains(currentOriginalDeviceId)) { + log.info("检测到循环引用,终止递归"); + return; + } + visited.add(currentOriginalDeviceId); + + List devices = deviceMapper.findByOriginalDeviceId(currentOriginalDeviceId); + for (Device device : devices) { + chain.add(device); + findNext(chain, visited, device.getId()); + } } diff --git a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceTypeServiceImpl.java b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceTypeServiceImpl.java index 426cc6c..fa9e8f0 100644 --- a/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceTypeServiceImpl.java +++ b/fys-modules/fys-equipment/src/main/java/com/fuyuanshen/equipment/service/impl/DeviceTypeServiceImpl.java @@ -85,6 +85,7 @@ public class DeviceTypeServiceImpl extends ServiceImpl SELECT * FROM device - WHERE original_owner_id = #{customerId} + WHERE original_device_id = #{customerId} AND device_status = 1 + + + \ No newline at end of file diff --git a/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceTypeMapper.xml b/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceTypeMapper.xml index ede9a98..a48b7be 100644 --- a/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceTypeMapper.xml +++ b/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceTypeMapper.xml @@ -37,4 +37,11 @@ ORDER BY dt.create_time DESC + + + \ No newline at end of file