package com.midm.model.device; import com.google.common.base.Strings; import com.midm.model.GeneralProfile; import com.midm.model.battery.Battery; import com.midm.model.file.OtaFile; import com.midm.model.org.User; import com.mitac.cell.db.DbObject; import com.mitac.cell.utils.McJsonUtils; import java.util.Date; import java.util.List; import java.util.UUID; import org.bson.Document; import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.index.CompoundIndex; import org.springframework.data.mongodb.core.index.CompoundIndexes; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.DBRef; /** * ====================================================================================== * 核心实体模型:受控终端设备档案 (Device Entity) * ====================================================================================== * *

【数据建模与 MongoDB 索引架构】 * 1. 业务地位: * - 作为整个 MDM / IoT 系统的核心资产抽象,代表一台具体的受控智能硬件(PDA / 移动平板 / 车载终端)。 * 2. 【生产级复合索引设计】: * - `{'profile.hardware_info.serial_no': 1}` (unique = true): * 以出厂序列号构建唯一业务主键索引,杜绝重复设备接入,提供 O(1) 复杂度的极速检索。 * - `{'deviceGroup.$id': 1}`: * 按部门/仓库/车辆编队等组织分组高效检索关联设备。 * - `{'owner.$id': 1, 'deviceGroup.$id': 1}`: * 加速管理员按员工责任人与所属部门进行组合过滤统计。 * 3. 关联映射({@link DBRef}): * - 关联所属企业员工 (User owner)、商业授权凭据 (DeviceLicense license)、组织分组 (DeviceGroup deviceGroup)。 * 4. 动态属性集(DbObject): * - `profile`:静态硬件特征(IMEI、MAC、CPU型号、屏幕分辨率、出厂镜像版本); * - `states`:动态运行快照(当前电量、实时网络类型、内存剩余空间、活跃IP); * - `installedApps`:端侧已安装的应用清单快照(用于合规检测与版本审计)。 * * @author MiDM Core Platform Team */ @org.springframework.data.mongodb.core.mapping.Document @CompoundIndexes({@CompoundIndex( def = "{'deviceGroup.$id':1}" ), @CompoundIndex( def = "{'profile.hardware_info.serial_no':1}", unique = true ), @CompoundIndex( def = "{'owner.$id':1, 'deviceGroup.$id':1}" )}) public class Device extends GeneralProfile { /** 所属企业租户主键 UUID(实现多租户严格逻辑隔离) */ private UUID siteUuid; /** 临时字段:前端传递的目标分组 UUID,不持久化到 MongoDB */ @Transient private UUID groupUuid; /** 临时字段:展示给前端的分组全路径(如 "总公司/华东大区/智能物流一号仓") */ @Transient private String groupPath; /** 临时字段:归属负责人主键 UUID */ @Transient private UUID ownerUuid; /** MongoDB 跨集合引用:设备领用人档案 */ @DBRef private User owner; /** MongoDB 跨集合引用:设备绑定的商业 License 授权凭据 */ @DBRef private DeviceLicense license; /** MongoDB 跨集合引用:所属层级组织分组 */ @DBRef private DeviceGroup deviceGroup; /** 临时字段:设备当前生效的策略清单缓存 */ @Transient private List deviceProfiles; /** 许可激活时间点索引 */ @Indexed private Date licensed; /** 许可到期时间点索引(用于定时扫描批量过期告警与自动续约) */ @Indexed private Date licenseExpiry; /** 临时字段:RSA 鉴权私钥字符串 */ @Transient private String rsa; /** 终端动态遥测快照数据字典(电池状态、网络、存储剩余空间等) */ protected DbObject states; /** 首次入网注册时间戳 */ private Date enrolled; /** 最近一次活跃心跳上报时间(Check-in,用于离线判定) */ private Date lastCheckedIn; /** 最近一次 TCP / WebSocket 连接断开时间点 */ private Date lastDisconnected; /** 终端已安装的应用程序包信息清单快照 */ private List installedApps; /** * 获取 {@code rsa}。 * @return 字符串结果 */ public String getRsa() { return this.rsa; } /** * 设置 {@code rsa}。 * * @param rsa 参数 {@code rsa}({@code String}) */ public void setRsa(String rsa) { this.rsa = rsa; } /** * 获取 {@code siteUuid}。 * @return UUID */ public UUID getSiteUuid() { return this.siteUuid; } /** * 设置 {@code siteUuid}。 * * @param siteUuid 站点 UUID({@code UUID}) */ public void setSiteUuid(UUID siteUuid) { this.siteUuid = siteUuid; } /** * 获取 {@code groupUuid}。 * @return UUID */ public UUID getGroupUuid() { return this.groupUuid; } /** * 获取 {@code groupPath}。 * @return 字符串结果 */ public String getGroupPath() { return this.groupPath; } /** * 设置 {@code groupPath}。 * * @param groupPath 参数 {@code groupPath}({@code String}) */ public void setGroupPath(String groupPath) { this.groupPath = groupPath; } /** * 获取 {@code owner}。 * @return 用户对象;未找到时返回 {@code null} */ public User getOwner() { return this.owner; } /** * 设置 {@code owner}。 * * 实现要点:包含 1 个条件分支、0 个循环。 * * @param owner 参数 {@code owner}({@code User}) */ public void setOwner(User owner) { this.owner = owner; if (owner != null) { this.removeProfile("owner"); } } /** * 获取 {@code ownerUuid}。 * @return UUID */ public UUID getOwnerUuid() { return this.ownerUuid; } /** * 设置 {@code ownerUuid}。 * * @param ownerUuid 所有者用户 UUID({@code UUID}) */ public void setOwnerUuid(UUID ownerUuid) { this.ownerUuid = ownerUuid; } /** * 判断 {@code owner}。 * @return 字段/派生属性 {@code owner} 的值 */ public boolean hasOwner() { return this.getOwner() != null; } /** * 获取所有者信息。 * * 实现要点:包含 1 个条件分支、0 个循环。 * * @param key 键/配置项({@code String}) * @param defaultValue 参数 {@code defaultValue}({@code String}) * @return 字符串结果 */ public String getOwnerInfo(String key, String defaultValue) { if (this.owner == null) { DbObject owner = this.getProfileCategory(new String[]{"owner"}); return (String)owner.get(key, defaultValue); } else { return key.equals("email") ? this.owner.getEmail() : (String)this.owner.getProfile(key, defaultValue); } } /** * 判断 {@code associated}。 * @return 字段/派生属性 {@code associated} 的值 */ public boolean isAssociated() { return this.getDeviceGroup() != null; } /** * 判断 {@code register}。 * @return 字段/派生属性 {@code register} 的值 */ public boolean canRegister() { return !Strings.isNullOrEmpty(this.getSerialNo()); } /** * 获取 {@code deviceGroup}。 * @return 设备分组;未找到时返回 {@code null} */ public DeviceGroup getDeviceGroup() { return this.deviceGroup; } /** * 设置 {@code deviceGroup}。 * * 实现要点:包含 1 个条件分支、0 个循环。 * * @param deviceGroup 设备分组对象({@code DeviceGroup}) */ public void setDeviceGroup(DeviceGroup deviceGroup) { if (deviceGroup != null && deviceGroup.getSiteUuid().equals(this.getSiteUuid())) { this.deviceGroup = deviceGroup; } else { this.groupUuid = null; this.deviceGroup = null; } } /** * 获取 {@code states}。 * * 实现要点:包含 1 个条件分支、0 个循环;主要调用:new DbObject。 * @return MongoDB 文档扩展对象 */ public DbObject getStates() { if (this.states == null) { this.states = new DbObject(); } return this.states; } /** * 设置 {@code states}。 * * @param states 参数 {@code states}({@code DbObject}) */ public void setStates(DbObject states) { this.states = states; } /** * 获取 {@code state}。 * @return 字符串结果 */ public String getState() { return this.getStates().getString("state", "unknown"); } /** * 设置 {@code state}。 * * @param state 参数 {@code state}({@code String}) */ public void setState(String state) { this.setState("state", state); } /** * 判断 {@code online}。 * @return 字段/派生属性 {@code online} 的值 */ public boolean isOnline() { return this.getStates().getBoolean("isOnline", false); } /** * 设置 {@code online}。 * * @param online 参数 {@code online}({@code boolean}) */ public void setOnline(boolean online) { this.setState("isOnline", online); } /** * 判断 {@code blocked}。 * @return 字段/派生属性 {@code blocked} 的值 */ public boolean isBlocked() { return this.getStates().containsKey("blocked"); } /** * 设置状态。 * * @param key 键/配置项({@code String}) * @param value 值({@code Object}) */ public void setState(String key, Object value) { this.getStates().append(key, value); } /** * 设置 {@code clientVersion}。 * * @param version 参数 {@code version}({@code int}) */ public void setClientVersion(int version) { this.getStates().put("clientVersion", version); } /** * 获取 {@code clientVersion}。 * @return 数量/长度 */ public int getClientVersion() { return this.getStates().getInteger("clientVersion", -1); } /** * 获取 {@code batteryCondition}。 * @return 字段/派生属性 {@code batteryCondition} 的值 */ public Battery.BATTERY_CONDITION getBatteryCondition() { return Battery.BATTERY_CONDITION.forValue(this.getStates().getInteger("batteryCondition", -1)); } /** * 判断 {@code oldClient}。 * @return 字段/派生属性 {@code oldClient} 的值 */ public boolean hasOldClient() { return this.getClientVersion() < 0; } /** * 获取 {@code enrolled}。 * @return 字段/派生属性 {@code enrolled} 的值 */ public Date getEnrolled() { return this.enrolled; } /** * 判断 {@code enrolled}。 * @return 字段/派生属性 {@code enrolled} 的值 */ public boolean isEnrolled() { return this.enrolled != null; } /** * 设置 {@code enrolled}。 * * @param enrolled 参数 {@code enrolled}({@code Date}) */ public void setEnrolled(Date enrolled) { this.enrolled = enrolled; this.setState("enrolled"); this.setLicenseExpiry(); } /** * 判断 {@code retired}。 * @return 字段/派生属性 {@code retired} 的值 */ public boolean isRetired() { return this.getState().equals("retired"); } /** * 获取 {@code lastCheckedIn}。 * @return 字段/派生属性 {@code lastCheckedIn} 的值 */ public Date getLastCheckedIn() { return this.lastCheckedIn; } /** * 设置最后Checked在。 * * 实现要点:主要调用:new Date。 */ public void setLastCheckedIn() { this.setLastCheckedIn(new Date()); } /** * 设置 {@code lastCheckedIn}。 * * @param lastCheckedIn 参数 {@code lastCheckedIn}({@code Date}) */ public void setLastCheckedIn(Date lastCheckedIn) { this.lastCheckedIn = lastCheckedIn; } /** * 获取 {@code lastDisconnected}。 * @return 字段/派生属性 {@code lastDisconnected} 的值 */ public Date getLastDisconnected() { return this.lastDisconnected; } /** * 设置最后Disconnected。 * * 实现要点:主要调用:new Date。 */ public void setLastDisconnected() { this.lastDisconnected = new Date(); } /** * 设置 {@code lastDisconnected}。 * * @param disconnected 参数 {@code disconnected}({@code Date}) */ public void setLastDisconnected(Date disconnected) { this.lastDisconnected = disconnected; } /** * 获取 {@code installedApps}。 * @return 应用信息列表,可能为空 */ public List getInstalledApps() { return this.installedApps; } /** * 设置 {@code installedApps}。 * * @param installedApps 参数 {@code installedApps}({@code List}) */ public void setInstalledApps(List installedApps) { this.installedApps = installedApps; } /** * 设置 {@code deviceProfiles}。 * * @param deviceProfiles 参数 {@code deviceProfiles}({@code List}) */ public void setDeviceProfiles(List deviceProfiles) { this.deviceProfiles = deviceProfiles; } /** * 获取 {@code serialNo}。 * @return 字符串结果 */ public String getSerialNo() { return (String)this.getCategoryProfile("hardware_info", "serial_no"); } /** * 设置 {@code serialNo}。 * * @param serialNo 设备序列号({@code String}) */ public void setSerialNo(String serialNo) { this.setProfile("hardware_info", "serial_no", serialNo.trim()); } /** * 获取 {@code modelId}。 * @return 字符串结果 */ public String getModelId() { return (String)this.getCategoryProfile("hardware_info", "product_id"); } /** * 获取 {@code buildNo}。 * @return 字符串结果 */ public String getBuildNo() { return this.getProfileCategory(new String[]{"software_info"}).getString("build_no"); } /** * 获取 {@code displayId}。 * @return 字符串结果 */ public String getDisplayId() { return this.getProfileCategory(new String[]{"software_info"}).getString("display_id"); } /** * 获取 {@code productDisplayId}。 * @return 字符串结果 */ public String getProductDisplayId() { return this.getProfileCategory(new String[]{"software_info"}).getString("product_display_id"); } /** * 获取 {@code buildDate}。 * @return 数量/长度 */ public long getBuildDate() { return this.getProfileCategory(new String[]{"software_info"}).getLong("build_date", -1L); } /** * 获取 {@code sdkVersion}。 * * 实现要点:包含 8 个条件分支、0 个循环。 * @return 数量/长度 */ public int getSdkVersion() { if (this.isEnrolled()) { int version = this.getProfileCategory(new String[]{"software_info"}).getInteger("sdk_version", -1); if (version < 0) { String osVersion = this.getProfileCategory(new String[]{"software_info"}).getString("os_version"); if (!Strings.isNullOrEmpty(osVersion)) { if (osVersion.startsWith("5.")) { version = 22; } else if (osVersion.startsWith("6.")) { version = 23; } else if (osVersion.startsWith("7.")) { version = 25; } else if (osVersion.startsWith("8.")) { version = 27; } else if (osVersion.equals("9")) { version = 28; } } } return version; } else { return -1; } } /** * 获取 {@code customerId}。 * @return 字符串结果 */ public String getCustomerId() { return (String)this.getCategoryProfile("software_info", "customer_id"); } /** * 获取 {@code regionImgVersion}。 * @return 字符串结果 */ public String getRegionImgVersion() { return this.getProfile().getDbObject(new String[]{"software_info"}).getString("region_version"); } /** * 获取 {@code scriptVersion}。 * @return 字符串结果 */ public String getScriptVersion() { return (String)this.getCategoryProfile("software_info", "script_version"); } /** * 获取 {@code regionBuildDate}。 * @return 数量/长度 */ public long getRegionBuildDate() { return this.getProfile().getDbObject(new String[]{"software_info"}).getLong("region_build_date"); } /** * 判断 {@code gmsAvailable}。 * @return 字段/派生属性 {@code gmsAvailable} 的值 */ public boolean isGmsAvailable() { return this.getProfile().getDbObject(new String[]{"software_info"}).getBoolean("gms_available"); } /** * 获取 {@code skuId}。 * * 实现要点:包含 7 个条件分支、0 个循环。 * @return 字符串结果 */ public String getSkuId() { String skuId = (String)this.getCategoryProfile("hardware_info", "sku_id"); if (skuId == null) { if (this.isGmsAvailable()) { String buildNo = this.getBuildNo(); if (buildNo == null) { buildNo = this.getDisplayId(); } if (buildNo != null) { skuId = buildNo.substring(0, buildNo.indexOf(".")); } } else { skuId = OtaFile.parseSkuId(this.getDisplayId()); } if (skuId == null) { skuId = OtaFile.parseSkuId(this.getProductDisplayId()); } if (skuId == null) { skuId = OtaFile.parseSkuId(this.getBuildNo()); } if (skuId != null) { this.setProfile("hardware_info", "sku_id", skuId); } } return skuId; } /** * 获取 {@code deviceId}。 * @return 字符串结果 */ public String getDeviceId() { return (String)this.getCategoryProfile("hardware_info", "device_id"); } /** * 获取 {@code buildFingerPrint}。 * @return 字符串结果 */ public String getBuildFingerPrint() { return (String)this.getCategoryProfile("software_info", "build_fingerprint"); } /** * 判断 {@code mobileConnected}。 * @return 字段/派生属性 {@code mobileConnected} 的值 */ public boolean isMobileConnected() { String type = this.getProfileCategory(new String[]{"network", "active"}).getString("type"); return "MOBILE".equalsIgnoreCase(type); } /** * 获取 {@code wifiMacAddr}。 * @return 字符串结果 */ public String getWifiMacAddr() { return this.getProfileCategory(new String[]{"network", "wifi"}).getString("mac_address"); } /** * 获取 {@code license}。 * @return 字段/派生属性 {@code license} 的值 */ public DeviceLicense getLicense() { return this.license; } /** * 设置 {@code license}。 * * 实现要点:包含 1 个条件分支、0 个循环;主要调用:new Date。 * * @param license 参数 {@code license}({@code DeviceLicense}) */ public void setLicense(DeviceLicense license) { this.license = license; if (license != null) { this.licensed = new Date(); this.setLicenseExpiry(); } } /** * 获取 {@code licensed}。 * @return 字段/派生属性 {@code licensed} 的值 */ public Date getLicensed() { return this.licensed; } /** * 设置 {@code licensed}。 * * @param licensed 参数 {@code licensed}({@code Date}) */ public void setLicensed(Date licensed) { this.licensed = licensed; } /** * 设置许可证Expiry。 * * 实现要点:包含 2 个条件分支、0 个循环。 */ private void setLicenseExpiry() { if (this.hasLicense()) { if (!this.license.isActivated() && this.isEnrolled()) { this.license.activate(); } this.setLicenseExpiry(this.license.getExpiry()); } } /** * 获取 {@code licenseExpiry}。 * * 实现要点:包含 1 个条件分支、0 个循环。 * @return 字段/派生属性 {@code licenseExpiry} 的值 */ public Date getLicenseExpiry() { if (this.licenseExpiry == null && this.hasLicense()) { this.licenseExpiry = this.license.getExpiry(); } return this.licenseExpiry; } /** * 判断 {@code licenseExpired}。 * @return 字段/派生属性 {@code licenseExpired} 的值 */ public boolean isLicenseExpired() { return this.license != null && !this.license.isPerpetual() && this.license.isExpired(); } /** * 设置 {@code licenseExpiry}。 * * @param expiry 参数 {@code expiry}({@code Date}) */ public void setLicenseExpiry(Date expiry) { this.licenseExpiry = expiry; } /** * 判断 {@code license}。 * @return 字段/派生属性 {@code license} 的值 */ public boolean hasLicense() { return this.license != null; } /** * 判断 {@code trial}。 * @return 字段/派生属性 {@code trial} 的值 */ public boolean isTrial() { return !this.hasLicense() || this.license.isTrial(); } /** * 移除许可证。 */ public void removeLicense() { this.setLicense((DeviceLicense)null); } /** * 获取 {@code plan}。 * @return 字符串结果 */ public String getPlan() { return this.license.getPlan(); } /** * 处理重置。 */ public void reset() { this.setEnrolled((Date)null); this.getStates().clear(); } /** * 处理clone。 * @return 匹配的设备对象;未找到时返回 {@code null} */ public Device clone() { return (Device)McJsonUtils.clone(this); } }