diff --git a/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java b/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java index 19115d4..aebdf47 100644 --- a/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java +++ b/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java @@ -124,15 +124,58 @@ public class WechatAuthStrategy implements IAuthStrategy { * @return 系统用户信息 */ private SysUserVo loadUserByPhone(String phone, AquUser aquUser) { + // 1. 优先按 AquUser ID 查询 SysUser,保证 ID 一致性 SysUserVo user = userMapper.selectVoOne( new LambdaQueryWrapper() - .eq(SysUser::getPhonenumber, phone) + .eq(SysUser::getUserId, aquUser.getId()) ); - // 如果用户不存在,创建新用户 - if (ObjectUtil.isNull(user)) { - log.info("系统中不存在该手机号的用户,自动创建新用户: phone={}", phone); - user = createNewUser(phone, aquUser); + if (user != null) { + log.info("通过AquUser ID找到系统用户: aquUserId={}, sysUserId={}, phone={}", + aquUser.getId(), user.getUserId(), phone); + // 检查手机号是否一致 + if (!phone.equals(user.getPhonenumber())) { + log.warn("系统用户手机号与AquUser不一致,更新手机号: sysUserId={}, oldPhone={}, newPhone={}", + user.getUserId(), user.getPhonenumber(), phone); + // 更新手机号 + SysUser updateUser = new SysUser(); + updateUser.setUserId(user.getUserId()); + updateUser.setPhonenumber(phone); + updateUser.setUserName(phone); // 用户名也更新为手机号 + userMapper.updateById(updateUser); + // 重新查询获取更新后的数据 + user = userMapper.selectVoOne( + new LambdaQueryWrapper() + .eq(SysUser::getUserId, aquUser.getId()) + ); + } + } else { + // 2. 按手机号查询 SysUser + user = userMapper.selectVoOne( + new LambdaQueryWrapper() + .eq(SysUser::getPhonenumber, phone) + ); + + if (user != null) { + // 手机号匹配但 ID 不一致,说明是历史数据,需要更新 ID 以保持一致 + log.info("通过手机号找到系统用户,但ID不一致: sysUserId={}, aquUserId={}, phone={}", + user.getUserId(), aquUser.getId(), phone); + log.warn("将更新系统用户ID以与AquUser保持一致: oldSysUserId={}, newSysUserId={}", + user.getUserId(), aquUser.getId()); + + // 更新 SysUser 的 ID 以与 AquUser 一致 + // 注意:需要先删除旧记录,再插入新记录(因为 ID 是主键) + Long oldUserId = user.getUserId(); + userMapper.deleteById(oldUserId); + + // 创建新的 SysUser 记录,使用 AquUser 的 ID + user = createNewUser(phone, aquUser); + log.info("已将系统用户ID从 {} 更新为 {},与AquUser保持一致", oldUserId, aquUser.getId()); + } else { + // 3. 用户不存在,创建新用户 + log.info("系统中不存在该手机号的用户,自动创建新用户: phone={}, aquUserId={}", phone, aquUser.getId()); + user = createNewUser(phone, aquUser); + } } // 检查用户状态 diff --git a/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java b/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java index 0987940..5907301 100644 --- a/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java +++ b/intc-modules/intc-iot/src/main/java/com/intc/iot/handler/DeviceDataHandler.java @@ -1744,7 +1744,8 @@ public class DeviceDataHandler { try { Integer rawCode = params.getInt("sensorErrorCode"); if (rawCode == null) { - log.warn("[探头错误码] 解析 sensorErrorCode 失败: {}", deviceName); + // 部分设备/数据包不携带此字段,属于正常情况 + log.debug("[探头错误码] 设备上报数据不包含 sensorErrorCode 字段: {}", deviceName); return; } int sensorErrorCode = rawCode;