jiapu пре 5 месеци
родитељ
комит
db5454c649

+ 136 - 5
code/backend/src/main/java/com/aijiuyi/admin/service/impl/UserDeviceServiceImpl.java

@@ -6,9 +6,13 @@ import com.aijiuyi.admin.common.util.LogUtil;
 import com.aijiuyi.admin.controller.dto.UserDeviceQueryDTO;
 import com.aijiuyi.admin.entity.AppUser;
 import com.aijiuyi.admin.entity.Device;
+import com.aijiuyi.admin.entity.DeviceGroup;
+import com.aijiuyi.admin.entity.DeviceGroupMember;
 import com.aijiuyi.admin.entity.UserDevice;
 import com.aijiuyi.admin.entity.UserProfile;
 import com.aijiuyi.admin.mapper.AppUserMapper;
+import com.aijiuyi.admin.mapper.DeviceGroupMapper;
+import com.aijiuyi.admin.mapper.DeviceGroupMemberMapper;
 import com.aijiuyi.admin.mapper.DeviceMapper;
 import com.aijiuyi.admin.mapper.UserDeviceMapper;
 import com.aijiuyi.admin.mapper.UserProfileMapper;
@@ -33,6 +37,9 @@ import java.util.List;
 public class UserDeviceServiceImpl extends ServiceImpl<UserDeviceMapper, UserDevice> implements UserDeviceService {
 
     private static final int NORMAL_USER_TYPE = 1;
+    private static final int DEVICE_TYPE_FAMILY = 1;
+    private static final int DEVICE_TYPE_PUBLIC = 2;
+    private static final int STATUS_ENABLED = 1;
 
     @Autowired
     private AppUserMapper appUserMapper;
@@ -40,6 +47,12 @@ public class UserDeviceServiceImpl extends ServiceImpl<UserDeviceMapper, UserDev
     @Autowired
     private DeviceMapper deviceMapper;
 
+    @Autowired
+    private DeviceGroupMapper deviceGroupMapper;
+
+    @Autowired
+    private DeviceGroupMemberMapper deviceGroupMemberMapper;
+
     @Autowired
     private UserProfileMapper userProfileMapper;
 
@@ -106,31 +119,149 @@ public class UserDeviceServiceImpl extends ServiceImpl<UserDeviceMapper, UserDev
             throw new BusinessException(ResultCode.DEVICE_NOT_FOUND);
         }
 
+        // 根据设备类型走不同绑定路径
+        if (isFamilyDevice(device)) {
+            bindFamilyDevice(device, profile, appUserId);
+        } else {
+            bindPublicDevice(device, profile);
+        }
+
+        // 确保 user_device 记录存在(两种类型都需要)
+        UserDevice userDevice = ensureUserDevice(profile, device);
+
+        LogUtil.info(UserDeviceServiceImpl.class,
+                "App用户[{}]为账号档案[{}]绑定设备[{}](类型:{})",
+                appUserId, profile.getId(), device.getDeviceCode(),
+                isFamilyDevice(device) ? "家庭" : "公共");
+        return userDevice;
+    }
+
+    /**
+     * 绑定家庭设备:创建/查找 DeviceGroup + DeviceGroupMember
+     */
+    private void bindFamilyDevice(Device device, UserProfile profile, Long appUserId) {
+        DeviceGroup group = ensureDeviceGroup(device.getDeviceCode());
+
+        // 检查是否已是该群组成员
+        DeviceGroupMember existingMember = deviceGroupMemberMapper.selectOne(
+                new LambdaQueryWrapper<DeviceGroupMember>()
+                        .eq(DeviceGroupMember::getGroupId, group.getId())
+                        .eq(DeviceGroupMember::getProfileId, profile.getId())
+                        .last("LIMIT 1")
+        );
+        if (existingMember != null && Integer.valueOf(STATUS_ENABLED).equals(existingMember.getStatus())) {
+            throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND);
+        }
+
+        // 检查同一 appUserId 是否已在群组中(避免重复)
+        if (appUserId != null) {
+            DeviceGroupMember appUserMember = deviceGroupMemberMapper.selectOne(
+                    new LambdaQueryWrapper<DeviceGroupMember>()
+                            .eq(DeviceGroupMember::getGroupId, group.getId())
+                            .eq(DeviceGroupMember::getAppUserId, appUserId)
+                            .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
+                            .last("LIMIT 1")
+            );
+            if (appUserMember != null) {
+                throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND);
+            }
+        }
+
+        if (existingMember == null) {
+            DeviceGroupMember member = new DeviceGroupMember();
+            member.setGroupId(group.getId());
+            member.setProfileId(profile.getId());
+            member.setAppUserId(appUserId);
+            member.setStatus(STATUS_ENABLED);
+            member.setJoinTime(LocalDateTime.now());
+            deviceGroupMemberMapper.insert(member);
+        } else {
+            // 重新激活已禁用的成员记录
+            LambdaUpdateWrapper<DeviceGroupMember> wrapper = new LambdaUpdateWrapper<>();
+            wrapper.eq(DeviceGroupMember::getId, existingMember.getId())
+                    .set(DeviceGroupMember::getAppUserId, appUserId)
+                    .set(DeviceGroupMember::getStatus, STATUS_ENABLED)
+                    .set(DeviceGroupMember::getJoinTime, LocalDateTime.now());
+            deviceGroupMemberMapper.update(null, wrapper);
+        }
+    }
+
+    /**
+     * 绑定公共设备:仅校验 user_device 是否已绑定
+     */
+    private void bindPublicDevice(Device device, UserProfile profile) {
         long count = lambdaQuery()
                 .eq(UserDevice::getUserId, profile.getId())
-                .eq(UserDevice::getDeviceCode, normalizedDeviceCode)
+                .eq(UserDevice::getDeviceCode, device.getDeviceCode())
                 .eq(UserDevice::getStatus, 1)
                 .count();
         if (count > 0) {
             throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND);
         }
+    }
+
+    /**
+     * 确保 user_device 记录存在并处于启用状态
+     */
+    private UserDevice ensureUserDevice(UserProfile profile, Device device) {
+        UserDevice existing = lambdaQuery()
+                .eq(UserDevice::getUserId, profile.getId())
+                .eq(UserDevice::getDeviceCode, device.getDeviceCode())
+                .last("LIMIT 1")
+                .one();
+        if (existing != null) {
+            if (!Integer.valueOf(STATUS_ENABLED).equals(existing.getStatus())) {
+                LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
+                wrapper.eq(UserDevice::getId, existing.getId())
+                        .set(UserDevice::getStatus, STATUS_ENABLED)
+                        .set(UserDevice::getDeviceName, device.getDeviceName())
+                        .set(UserDevice::getDeviceModel, device.getDeviceModel())
+                        .set(UserDevice::getOnlineStatus, device.getOnlineStatus() != null ? device.getOnlineStatus() : 0)
+                        .set(UserDevice::getBindTime, LocalDateTime.now());
+                update(wrapper);
+                existing.setStatus(STATUS_ENABLED);
+            }
+            return existing;
+        }
 
         UserDevice userDevice = new UserDevice();
         userDevice.setUserId(profile.getId());
         userDevice.setDeviceCode(device.getDeviceCode());
         userDevice.setDeviceName(device.getDeviceName());
         userDevice.setDeviceModel(device.getDeviceModel());
-        userDevice.setStatus(1);
+        userDevice.setStatus(STATUS_ENABLED);
         userDevice.setIsPrimary(0);
         userDevice.setOnlineStatus(device.getOnlineStatus() != null ? device.getOnlineStatus() : 0);
         userDevice.setBindTime(LocalDateTime.now());
         save(userDevice);
-
-        LogUtil.info(UserDeviceServiceImpl.class,
-                "App用户[{}]为账号档案[{}]绑定设备[{}]", appUserId, profile.getId(), device.getDeviceCode());
         return userDevice;
     }
 
+    /**
+     * 获取或创建设备对应的家庭群组
+     */
+    private DeviceGroup ensureDeviceGroup(String deviceCode) {
+        DeviceGroup group = deviceGroupMapper.selectOne(
+                new LambdaQueryWrapper<DeviceGroup>()
+                        .eq(DeviceGroup::getDeviceCode, deviceCode)
+                        .eq(DeviceGroup::getStatus, STATUS_ENABLED)
+                        .last("LIMIT 1")
+        );
+        if (group != null) {
+            return group;
+        }
+        group = new DeviceGroup();
+        group.setDeviceCode(deviceCode);
+        group.setStatus(STATUS_ENABLED);
+        deviceGroupMapper.insert(group);
+        return group;
+    }
+
+    private boolean isFamilyDevice(Device device) {
+        Integer type = device.getDeviceType();
+        return type == null || type == DEVICE_TYPE_FAMILY;
+    }
+
     private AppUser requireAppUser(Long appUserId) {
         AppUser appUser = appUserMapper.selectById(appUserId);
         if (appUser == null) {

+ 23 - 0
code/backend/src/test/java/com/aijiuyi/admin/common/util/AcupointLocationParserTest.java

@@ -29,6 +29,29 @@ class AcupointLocationParserTest {
         assertEquals(new BigDecimal("550.00"), y);
     }
 
+    @Test
+    void parsesProductCoordinateRelativeToDazhui() {
+        String[] positions = new String[]{
+                "产品坐标换算:相对大椎下3寸,旁开3寸,双侧。",
+                "产品坐标换算:大椎下3寸,旁开3寸,双侧。",
+                "产品坐标换算:大椎穴下3寸,旁开3寸,双侧。"
+        };
+        for (String position : positions) {
+            Acupoint acupoint = acupoint("秉风", "双穴", "手太阳小肠经", "肩胛区",
+                    "在肩胛区,肩胛冈中点上方冈上窝中。");
+            acupoint.setAcupointCode("xiaochang-12");
+            acupoint.setRelativePosition(position);
+
+            AcupointLocationParser.ParseResult result = AcupointLocationParser.parse(acupoint);
+
+            assertEquals(AcupointLocationParser.STATUS_PARSED, result.getParseStatus());
+            assertEquals(0, new BigDecimal("3.00").compareTo(result.getLongitudeOffset()));
+            assertEquals(0, new BigDecimal("3.00").compareTo(result.getLateralOffset()));
+            assertEquals(4, result.getSide());
+            assertEquals(AcupointLocationParser.CONFIDENCE_MEDIUM, result.getParseConfidence());
+        }
+    }
+
     @Test
     void parsesAllBackAcupointsFromDataModel() {
         String[][] rows = new String[][]{

+ 1 - 1
code/frontend/src/views/device/users.vue

@@ -132,7 +132,7 @@ const route = useRoute()
 const router = useRouter()
 
 // 从路由参数获取设备信息
-const deviceId = computed(() => route.query.deviceId ? Number(route.query.deviceId) : null)
+const deviceId = computed(() => route.query.deviceId || null)
 const deviceCode = computed(() => route.query.deviceCode || '')
 const deviceName = computed(() => route.query.deviceName || '')
 const deviceType = computed(() => Number(route.query.deviceType || 1))