deviceLayerStore.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /**
  2. * 关联监测设备临时图层(票 42)。
  3. *
  4. * ── 为什么单独一个 store,而不是塞进 detailStore 或 mapStore ──
  5. * 这条图层的输入**横跨两个既有 store**:设备编码清单来自 `mapStore`(当前快照该点位的
  6. * `deviceCodes`),状态与坐标来自 `detailStore`(点位详情的 `devices[]`)。两边都不该为了
  7. * 对方长出依赖(票 10 的 F4、票 02 的层次纪律),所以由本 store 做**唯一的合成点**:
  8. * 它读两边、按 `domain/deviceLayer.js` 的纯函数判定,再把「可上图的行」交给地图服务。
  9. *
  10. * ── 四条规格硬要求,各自落在哪一行 ──
  11. * ① **跟随选择**:`syncForSelection()` 是唯一入口,选中即加载、关闭/换点即撤下;
  12. * ② **并行不阻塞**:它不 await 相机飞行,设备数据一到就渲染(票 41 已把定位与详情解耦);
  13. * ③ **迟到响应不得覆盖**:与详情同级——每次同步领一个序号,回来时序号变了就整批丢弃;
  14. * ④ **同快照翻页不重建**:重算的输入是「当前快照的 `deviceCodes` + 详情卡片」,
  15. * 翻页只往合并态追加对象;**同一点位、同一份清单**算出的计划不变,就不重画
  16. * (见 `lastPlanKey`),所以翻页不会让设备图层闪烁或重取。
  17. */
  18. import { markRaw, ref, shallowRef } from 'vue'
  19. import { defineStore } from 'pinia'
  20. import {
  21. DEVICE_LAYER_KIND,
  22. DEVICE_PLACEMENT,
  23. DEVICE_PLACEMENT_TEXT,
  24. associatedLegend,
  25. countPlacements,
  26. crossEndpointNotice,
  27. planAssociatedDevices,
  28. uniqueCodes,
  29. } from '../domain/deviceLayer.js'
  30. // 票 46:模式可见性判据**唯一**来自 `domain/modeLayerPlan.js`(规格 18:不做第二次过滤)。
  31. // `deviceLayer.isVisibleInMode` 保留为该判据的别名引用(t42 闸门仍在用)。
  32. import { isAssociatedDeviceVisible as isDeviceVisibleInModePlan } from '../domain/modeLayerPlan.js'
  33. import { DEVICE_FLIGHT, DIM_THRESHOLD, planDeviceFlight } from '../domain/deviceCorrespondence.js'
  34. // 票 44:资产计划与设备状态词表(判定与资产都在这一层合成,两处共用同一份行)。
  35. import { planDeviceAsset } from '../domain/deviceAssets.js'
  36. import { deviceDisplayStatusIndex, deviceVisualForStatus } from '../domain/deviceStatus.js'
  37. import { groupOverlappingDevices } from '../domain/overlapDevices.js'
  38. // 票 47:设备状态动效计划(映射/门控/30 台降级判定都在 domain)。
  39. import { planDeviceEffects } from '../domain/deviceEffects.js'
  40. import { DETAIL_LEVEL } from '../domain/detailFlow.js'
  41. import {
  42. clearAssociatedDevices,
  43. clearOverlapNudge as clearOverlapNudgeService,
  44. getLifecycleStats,
  45. nudgeOverlap as nudgeOverlapService,
  46. resolveDeviceEffectsOptions,
  47. locateMonitoringPoint,
  48. cancelPointLocate,
  49. setHoveredDevice,
  50. setSelectedDevice,
  51. syncAssociatedDevices,
  52. syncDeviceReactions as syncDeviceReactionsService,
  53. getEffectsAbnormalBase,
  54. // 票 70(2026-10-27):上一份计划是否已落地 —— 指纹短路的自愈条件。
  55. isDeviceApplyPending,
  56. } from '../services/mapService.js'
  57. import { TERRAIN_STATE } from '../domain/mapLayers.js'
  58. import { useDetailStore } from './detailStore.js'
  59. import { useMapStore } from './mapStore.js'
  60. import { useDeviceStatusStore } from './deviceStatusStore.js'
  61. export const useDeviceLayerStore = defineStore('deviceLayerStore', () => {
  62. /** 当前图层所归属的监测点编码(空串=没有活动图层)。 */
  63. const pointCode = ref('')
  64. /** 全部关联设备行(**保留全部**,含不上图的;界面清单与计数都读它)。 */
  65. const rows = shallowRef([])
  66. /** 互斥计数(`drawn`/`no-location`/`hidden`/`pending`/`inconsistent`/`unavailable`)。 */
  67. const counts = ref({})
  68. /**
  69. * **关联总数**:当前快照该点位去重后的 `deviceCodes` 条数(规格:总数只认它)。
  70. *
  71. * 刻意与 `rows.length` 分开:清单会**多出**「详情独有」的行(那些设备不在当前快照里),
  72. * 拿清单行数当总数会虚报关联设备数。界面两个数都显示、各有明确含义。
  73. */
  74. const total = ref(0)
  75. /** 独立设备图例(与摘要图例分开)。 */
  76. const legend = ref([])
  77. /**
  78. * 票 45:重叠呈现摘要。`k`=可逐台绘制设备数,`n`=快照关联总数;
  79. * `degraded` 组只逐台摊开 maxFan 个,其余继续留在清单,不虚报逐台已上图。
  80. */
  81. const overlap = ref({ k: 0, n: 0, maxFan: 10, groups: [], active: false, degraded: false })
  82. /**
  83. * 票 47:设备状态动效计划(`{ enabled, limit, abnormalCount, degraded, animated, staticAbnormal }`)。
  84. * 界面据此显示降级提示「N 台异常」;关掉特效**不隐藏静态状态表达**(色/形/文字仍在)。
  85. */
  86. const effectsPlan = ref(null)
  87. /** 跨端点不同步的风险提示(规格强制行为,不得静默删除)。 */
  88. const notice = ref({ risky: false, text: '' })
  89. /** 正在加载(界面只做轻提示;成败由 `rows`/`counts` 如实体现)。 */
  90. const loading = ref(false)
  91. /** 最近一次同步是否真的把设备画到了地图上。 */
  92. const painted = ref(false)
  93. /** 每次同步的序号:换点/关闭/刷新时旧的在途结果整批作废(与详情同款纪律)。 */
  94. let token = 0
  95. /**
  96. * 上一份已上图的计划指纹(`pointCode + 已上图编码序列`)。
  97. * **只有它变了才重画** —— 这就是「同一 `snapshotAt` 翻页不重建设备图层」的落点:
  98. * 翻页只追加摘要对象,本点位的 `deviceCodes` 与详情卡片不变 ⇒ 指纹不变 ⇒ 不重画。
  99. */
  100. let lastPlanKey = ''
  101. /** 只留可上图的那些行给引擎(判定已在 domain 收口,这里不再二次过滤业务语义)。 */
  102. function renderableRows(list) {
  103. return list.filter((row) => row.placement === 'drawn')
  104. }
  105. /** #131:定位关联设备时,即使它在当前模式/状态本应隐藏,也生成一条可上图行。 */
  106. function revealRow(device, pointCode) {
  107. if (!device || !Number.isFinite(device.longitude) || !Number.isFinite(device.latitude)) return null
  108. const visual = deviceVisualForStatus(device.status)
  109. const asset = planDeviceAsset({ device })
  110. return {
  111. code: device.code,
  112. pointCode,
  113. name: device.name || device.code,
  114. typeName: device.typeName || '',
  115. status: device.status || '',
  116. statusLabel: visual.label,
  117. statusReasons: [],
  118. longitude: device.longitude,
  119. latitude: device.latitude,
  120. coordinate: device.coordinate,
  121. // 内层类型徽标靠 industry;强制行也必须带上,否则中心只剩母版水滴。
  122. industry: device.industry || '',
  123. placement: DEVICE_PLACEMENT.DRAWN,
  124. placementText: DEVICE_PLACEMENT_TEXT[DEVICE_PLACEMENT.DRAWN],
  125. reason: 'LOCATE_REVEAL',
  126. inSnapshot: true,
  127. shape: asset.shape,
  128. size: asset.size,
  129. assetMode: asset.mode,
  130. assetReason: asset.reason,
  131. assetReasonText: asset.reasonText,
  132. visualKey: asset.visualKey,
  133. tone: visual.tone,
  134. }
  135. }
  136. function planKeyOf(code, drawn, maxFan = 10, effects = null) {
  137. return JSON.stringify([code, maxFan, effects, drawn.map((row) => [
  138. row.code, row.longitude, row.latitude, row.status, row.shape, row.size,
  139. row.assetMode, row.assetReason, row.tone,
  140. ])])
  141. }
  142. /**
  143. * 依据当前选择与快照重算设备图层。**这是唯一入口**(选择变化、详情到达、快照变化都调它)。
  144. * @param {object} [options] `{ force, mode, entered, filterActive }`
  145. * · `force` 强制重画(快照换了要撤旧图层时用);
  146. * · `mode` 一级模式(可见性判据的唯一来源是 `domain/modeLayerPlan.js`);
  147. * · `entered` 票 46:`false` = 引导态,关联设备图层整体不画(计划层输出全空的同款纪律);
  148. * · `filterActive` 票 14:设备级筛选生效(行业/范围/类型)——快照清单是命中子集时,
  149. * 详情独有按「未命中当前筛选」归类(另有 N 台),而不是「与当前快照不一致」。
  150. * 只有快照对象确实已加载时才生效(翻页没到该点位时无从判断命中)。
  151. */
  152. // 引擎未就绪时的重试(最多 40 次 × 250ms ≈ 10s,覆盖冷启动;成功后清零)。
  153. let engineRetryTimer = null
  154. let engineRetryCount = 0
  155. const ENGINE_RETRY_LIMIT = 40
  156. function clearEngineRetry() {
  157. if (engineRetryTimer) clearTimeout(engineRetryTimer)
  158. engineRetryTimer = null
  159. engineRetryCount = 0
  160. }
  161. function scheduleEngineRetry(options = {}) {
  162. if (engineRetryTimer || engineRetryCount >= ENGINE_RETRY_LIMIT) return
  163. engineRetryCount += 1
  164. engineRetryTimer = setTimeout(() => {
  165. engineRetryTimer = null
  166. syncForSelection({ ...options, force: true })
  167. }, 250)
  168. }
  169. function syncForSelection(options = {}) {
  170. const detailStore = useDetailStore()
  171. const mapStore = useMapStore()
  172. // #127:关联设备也按「基础属性 + 可叠加标签」的显示状态归类与着色;
  173. // 状态池未就绪时索引为空,statusOf 回退设备卡原值(不伪造)。
  174. const statusStore = useDeviceStatusStore()
  175. const statusIndex = deviceDisplayStatusIndex(statusStore.pools.devices)
  176. const statusOf = (card) => {
  177. const code = String((card && card.code) || '').trim()
  178. return (code && statusIndex.get(code)) || (card && card.status) || ''
  179. }
  180. // 票 46:引导态(进入前)不显示任何业务对象 —— 关联设备图层直接撤下。
  181. // 数据仍可预取(详情/快照照常),只是**呈现**被门控。
  182. if (options.entered === false || options.parentVisible === false) {
  183. token += 1
  184. pointCode.value = ''
  185. rows.value = []
  186. counts.value = {}
  187. total.value = 0
  188. legend.value = []
  189. overlap.value = { k: 0, n: 0, maxFan: 10, groups: [], active: false, degraded: false }
  190. effectsPlan.value = null
  191. notice.value = { risky: false, text: '' }
  192. painted.value = false
  193. lastPlanKey = ''
  194. clearAssociatedDevices()
  195. return { total: 0, drawn: 0 }
  196. }
  197. const selected = detailStore.selection
  198. const code = selected && selected.pointCode ? selected.pointCode : ''
  199. /**
  200. * 独立设备(没有宿主点位)也要能上图。
  201. *
  202. * 这类目标 `pointCode` 为空,早先会直接落到下面的「没有点位级选择 → 图层撤下」分支,
  203. * 于是揭示行**根本没机会生成**:点「定位设备」只飞相机,图标与锚定都没有依据
  204. * (维护者 2026-12-02:Elements 里 data-enhancement-device-status 为 0 of 0)。
  205. * 这里在撤下之前先认一次定位目标自带的设备行。
  206. */
  207. const revealDevice = options.revealDevice && options.revealDevice.device
  208. ? revealRow(options.revealDevice.device, options.revealDevice.code)
  209. : null
  210. if (!code) {
  211. if (revealDevice) {
  212. token += 1
  213. pointCode.value = ''
  214. rows.value = markRaw([revealDevice])
  215. counts.value = countPlacements([revealDevice])
  216. total.value = 1
  217. legend.value = associatedLegend(counts.value)
  218. overlap.value = { k: 1, n: 1, maxFan: 10, groups: [], active: false, degraded: false }
  219. effectsPlan.value = null
  220. notice.value = { risky: false, text: '' }
  221. painted.value = false
  222. lastPlanKey = ''
  223. return { total: 1, drawn: 1 }
  224. }
  225. token += 1
  226. pointCode.value = ''
  227. rows.value = []
  228. counts.value = {}
  229. total.value = 0
  230. legend.value = []
  231. overlap.value = { k: 0, n: 0, maxFan: 10, groups: [], active: false, degraded: false }
  232. effectsPlan.value = null
  233. notice.value = { risky: false, text: '' }
  234. painted.value = false
  235. lastPlanKey = ''
  236. clearAssociatedDevices()
  237. return { total: 0, drawn: 0 }
  238. }
  239. // 当前快照该点位的 `deviceCodes`:这是**总数**的唯一依据。
  240. const object = (mapStore.summary.objects || []).find(
  241. (item) => item && item.kind === 'monitoring-point' && item.code === code,
  242. )
  243. const snapshotCodes = object && Array.isArray(object.deviceCodes) ? uniqueCodes(object.deviceCodes) : []
  244. // 票 14:命中子集语义只在「快照对象确实已加载」时成立——翻页没到该点位时
  245. // deviceCodes 缺失是"还没取到",不是"被筛选裁剪",详情独有仍按快照不一致呈现。
  246. const filterActive = options.filterActive === true && !!object
  247. // 详情卡片:来自已加载的点位摘要分区(未取到或不属于本点位时给空数组)。
  248. const section = detailStore.sections.pointDetail
  249. const data = (section && section.data) || null
  250. const cards = data && data.code === code && Array.isArray(data.devices) ? data.devices : []
  251. // 地形是否就绪(票 44):**坐标有效但地形未就绪**要单列"位置待确认",
  252. // 不能算坐标无效、也不能算已上图。地形状态由地图服务如实给出。
  253. //
  254. // 三态语义(与票 41 的投影取同一口径):
  255. // `none` = 本部署没配地形 → 设备按椭球面贴地,**可以上图**(不是"待确认");
  256. // `ready` = 已就绪 → 可以上图;
  257. // `invalid`= 加载失败 → 位置待确认(不上图,但保留清单)。
  258. const terrainState = getLifecycleStats().terrain
  259. const terrainReady = terrainState !== TERRAIN_STATE.INVALID
  260. const plan = planAssociatedDevices({
  261. snapshotCodes,
  262. cards,
  263. mode: options.mode || '',
  264. pointCode: code,
  265. // 批次 3:内层行业图标继承点位 industry(设备卡契约实测无此字段)。
  266. pointIndustry: (mapStore.summary.objects.find((item) => item.kind === 'monitoring-point' && item.code === code) || {}).industry || '',
  267. terrainReady,
  268. filterActive,
  269. statusOf,
  270. })
  271. // 票 44:**资产计划在这里就一起算好**,让清单行自带 `shape`/`size`/`assetMode`/`assetReason`。
  272. //
  273. // 为什么不能只在地图服务里算(本票实测抓到的缺陷):`mapService` 只在**引擎就绪**时
  274. // 才把资产计划写回它自己那份 `deviceRows`;而详情卡片读的是**本 store 的行**。
  275. // 两处各持一份 ⇒ 引擎没起来时(或纯逻辑断言里)清单行永远没有资产字段,
  276. // 详情卡片也就永远显示不出"正式图片素材未提供"这类原因 —— 而那正是验收第 5 条要的。
  277. // 判定与资产都是同一层的**纯数据**,所以在这里合成一次、两处共用。
  278. const withAssets = plan.rows.map((row) => {
  279. const asset = planDeviceAsset({ device: row, color: '' })
  280. return {
  281. ...row,
  282. // 行里已有的状态形状优先(`planAssociatedDevices` 已按设备词表给出),资产层只补尺寸与降级。
  283. shape: row.shape || asset.shape,
  284. size: asset.size,
  285. assetMode: asset.mode,
  286. assetReason: asset.reason,
  287. assetReasonText: asset.reasonText,
  288. visualKey: asset.visualKey,
  289. tone: deviceVisualForStatus(row.status).tone,
  290. }
  291. })
  292. pointCode.value = code
  293. // #131:关联设备定位时,即使它当前状态(NORMAL)/模式本应隐藏,也临时补一条可上图行。
  294. // 揭示判据认 code 也认 deviceCode:独立设备定位时 deviceCode 是空的(它靠 code 揭示),
  295. // 只认 deviceCode 会让这类目标永远补不上图层。
  296. const reveal = options.revealDevice && (options.revealDevice.code || options.revealDevice.deviceCode) ? options.revealDevice : null
  297. let layerRows = withAssets
  298. if (reveal) {
  299. const forced = reveal.device ? revealRow(reveal.device, reveal.code) : null
  300. if (forced) {
  301. const exclude = reveal.deviceCode || (reveal.device && reveal.device.code) || reveal.code || ''
  302. layerRows = [...layerRows.filter((row) => row.code !== exclude), forced]
  303. } else {
  304. layerRows = layerRows.map((row) => (row.code === reveal.deviceCode && row.placement !== DEVICE_PLACEMENT.DRAWN
  305. ? {
  306. ...row,
  307. placement: DEVICE_PLACEMENT.DRAWN,
  308. placementText: DEVICE_PLACEMENT_TEXT[DEVICE_PLACEMENT.DRAWN],
  309. reason: 'LOCATE_REVEAL',
  310. }
  311. : row))
  312. }
  313. }
  314. rows.value = markRaw(layerRows)
  315. counts.value = countPlacements(layerRows)
  316. // 总数=当前快照去重 deviceCodes 条数(**不是** rows.length:清单含详情独有那几行)。
  317. total.value = plan.total
  318. legend.value = associatedLegend(counts.value)
  319. notice.value = crossEndpointNotice(layerRows)
  320. const drawn = renderableRows(layerRows)
  321. const groups = groupOverlappingDevices(drawn, { maxFan: options.maxFan, total: plan.total })
  322. const activeGroups = groups.filter((group) => group && group.k > 1)
  323. overlap.value = {
  324. k: groups.reduce((sum, group) => sum + (group.k || 0), 0),
  325. n: plan.total,
  326. maxFan: options.maxFan || 10,
  327. groups,
  328. active: activeGroups.length > 0,
  329. degraded: activeGroups.some((group) => group.degraded),
  330. }
  331. // 票 47:设备状态动效计划(映射/门控/30 台降级都在 domain,本层只提供输入)。
  332. // 只对**已绘制**的设备算动效 —— 清单里那些没上图的设备没有实体可动。
  333. // 票 85:降级阈值按三类对象**合计**判定,所以把另外两类(监测点/未关联设备)的异常数带进来。
  334. const effects = planDeviceEffects({ devices: drawn, ...resolveDeviceEffectsOptions(), extraAbnormalCount: getEffectsAbnormalBase() })
  335. effectsPlan.value = effects
  336. const key = planKeyOf(code, drawn, options.maxFan || 10, effects)
  337. // 同点位的同一份计划**不重画**(翻页/无关重渲染都不会让设备层闪烁)。
  338. // `force` 用于「快照换了、旧图层必须先撤下」这条路径(规格要求先撤后取)。
  339. //
  340. // 票 70(2026-10-27):短路必须让开「已下单但还没落地」的情况 ——
  341. // `syncAssociatedDevices` 是先入队再返回 true,若那份订单随后被清层吃掉,
  342. // 指纹相同会让后续同步永久跳过,只剩 force(刷新地图)能救。
  343. if (!options.force && key === lastPlanKey && !isDeviceApplyPending()) {
  344. return { total: plan.total, drawn: drawn.length }
  345. }
  346. lastPlanKey = key
  347. loading.value = true
  348. const mine = token
  349. // 地图服务是同步下发,但**序号纪律仍然要在**:换点后旧的一次同步不得再写读数。
  350. painted.value = syncAssociatedDevices(drawn, { maxFan: options.maxFan, overlapGroups: groups, effects })
  351. // 2026-10-27 走查修正:引擎未就绪时 sync 返回 false,但上面已写入 lastPlanKey ——
  352. // 若不同时安排重试,之后同一点位会被"计划未变"短路掉,表现为「子设备点要刷新地图才出现」。
  353. if (painted.value) clearEngineRetry()
  354. else scheduleEngineRetry(options)
  355. // 图层的实体是**整份重建**的,所以重建后必须刷新一次反应 —— 否则选中/hover/弱化
  356. // 会停在重建前的值(新实体默认 scale 1、无外环),表现为"翻一页选中态就没了"。
  357. syncDeviceReactions()
  358. if (mine === token) loading.value = false
  359. return { total: plan.total, drawn: drawn.length }
  360. }
  361. /**
  362. * 快照变化(刷新 / 筛选重载 / 快照失效)时的收场:**先撤下旧图层**,再等新的快照与详情到达。
  363. * 规格原句:「刷新或快照失效时先撤下旧设备图层,以便旧快照的设备不会被误读为新范围的数据」。
  364. * @param {object} [options] 透传给 `syncForSelection`(含票 14 的 `filterActive`)。
  365. */
  366. function invalidateForSnapshot(options = {}) {
  367. token += 1
  368. lastPlanKey = ''
  369. rows.value = []
  370. counts.value = {}
  371. total.value = 0
  372. legend.value = []
  373. notice.value = { risky: false, text: '' }
  374. painted.value = false
  375. clearAssociatedDevices()
  376. return syncForSelection({ force: true, ...options })
  377. }
  378. /**
  379. * 票 42(规格 14:刷新核对链):刷新**发起**或快照失效时的挂起——撤下旧设备图层,
  380. * 但**不**按当前摘要立即重算(此刻摘要还是旧的,重算等于把旧层画回来)。
  381. * 图层在挂起期间保持撤下;新快照首页到达后由既有的快照/对象监听自然重算,
  382. * 或由核对链在点位重载后恢复。
  383. */
  384. function suspendForSnapshot() {
  385. token += 1
  386. lastPlanKey = ''
  387. rows.value = []
  388. counts.value = {}
  389. total.value = 0
  390. legend.value = []
  391. notice.value = { risky: false, text: '' }
  392. overlap.value = { k: 0, n: 0, maxFan: 10, groups: [], active: false, degraded: false }
  393. effectsPlan.value = null
  394. painted.value = false
  395. clearAssociatedDevices()
  396. }
  397. // ---- 票 43:面板与三维设备点的对应反应 ----
  398. /** 当前选中设备编码(空串=没选设备;`data-map-device-picked` 读它)。 */
  399. const selectedDevice = ref('')
  400. /** 当前三维 hover 设备编码(瞬态;`data-map-device-hovered` 读它)。 */
  401. const hoveredDevice = ref('')
  402. /** 上一次的详情层级与设备编码 —— 镜头联动判"该怎么动"要拿它做对照。 */
  403. let lastLevel = DETAIL_LEVEL.NONE
  404. let lastDeviceCode = ''
  405. let pendingDeviceTier = false
  406. let deviceFlightToken = 0
  407. /** 是否需要弱化(该点位设备数 ≥ 阈值)。界面按它决定要不要给"其余设备压暗"的样式。 */
  408. const dimming = ref(false)
  409. /**
  410. * 选中一台设备(**面板卡片与三维点选共用这一个入口**)。
  411. *
  412. * 三件事分开做,顺序有意:
  413. * ① 选中态写进服务层(它负责把反应画到三维,并写 `data-map-device-picked`);
  414. * ② 进设备级详情(复用 `detailStore.selectDevice`,取数链路不变);
  415. * ③ 按**层级变化**决定镜头怎么动(`planDeviceFlight` 纯判定:首次飞设备档、
  416. * A→B 只换中心、返回点位回点位档)。
  417. * @param {string} deviceCode 设备编码
  418. * @param {object} [options] `{ fly }` 传 false 表示只换选中不联动镜头(三维 hover 不调它)
  419. */
  420. function selectDevice(deviceCode, options = {}) {
  421. const code = typeof deviceCode === 'string' ? deviceCode.trim() : ''
  422. if (!code) return ''
  423. const detailStore = useDetailStore()
  424. const before = { level: lastLevel, deviceCode: lastDeviceCode }
  425. selectedDevice.value = code
  426. setSelectedDevice(code)
  427. // 进设备级(**唯一**的取数入口,票 11 的契约)。
  428. void detailStore.selectDevice(code)
  429. const flight = planDeviceFlight({
  430. fromLevel: before.level,
  431. toLevel: DETAIL_LEVEL.EQUIPMENT,
  432. fromDeviceCode: before.deviceCode,
  433. toDeviceCode: code,
  434. })
  435. if (options.fly !== false) applyDeviceFlight(flight, code)
  436. lastLevel = DETAIL_LEVEL.EQUIPMENT
  437. lastDeviceCode = code
  438. syncDeviceReactions()
  439. return code
  440. }
  441. /**
  442. * 按层级变化驱动镜头(规格的三段行为)。
  443. *
  444. * `DEVICE_TIER` = 飞设备档(`mapService` 的定位入口用**配置里的** `map.locate.device`);
  445. * `CENTER_ONLY` = 只换中心不重飞;`POINT_TIER` = 回点位档。
  446. * 有意不在这里写死 range 数字 —— 数值归票 03 的 `map.locate` 配置。
  447. */
  448. function applyDeviceFlight(flight, code) {
  449. if (flight === DEVICE_FLIGHT.NONE) return ''
  450. const mine = ++deviceFlightToken
  451. if (flight === DEVICE_FLIGHT.DEVICE_TIER) pendingDeviceTier = true
  452. const device = rows.value.find((row) => row.code === code) || null
  453. if (!device || device.placement !== 'drawn') {
  454. cancelPointLocate()
  455. return ''
  456. }
  457. const centerOnly = flight === DEVICE_FLIGHT.CENTER_ONLY && !pendingDeviceTier
  458. void locateMonitoringPoint(device, { tier: 'device', centerOnly }).then((applied) => {
  459. if (applied && mine === deviceFlightToken && !centerOnly) pendingDeviceTier = false
  460. })
  461. return flight
  462. }
  463. /** 选中设备的引线要在选中后立刻跟上(服务层的 `syncDeviceReactions` 已负责画)。 */
  464. function syncDeviceLeadTo() {
  465. syncDeviceReactions()
  466. }
  467. /** hover 一台设备(三维 pointermove 与面板卡片 hover 共用)。 */
  468. function hoverDevice(deviceCode) {
  469. const code = typeof deviceCode === 'string' ? deviceCode.trim() : ''
  470. if (code === hoveredDevice.value) return code
  471. hoveredDevice.value = code
  472. setHoveredDevice(code)
  473. return code
  474. }
  475. /** 「返回点位」:清设备强化、回点位级(**监测点仍选中**,不是取消选中)。 */
  476. function backToPointLevel() {
  477. deviceFlightToken += 1
  478. pendingDeviceTier = false
  479. cancelPointLocate()
  480. const detailStore = useDetailStore()
  481. const before = { level: lastLevel, deviceCode: lastDeviceCode }
  482. detailStore.backToPoint()
  483. selectedDevice.value = ''
  484. setSelectedDevice('')
  485. const flight = planDeviceFlight({
  486. fromLevel: before.level,
  487. toLevel: DETAIL_LEVEL.POINT,
  488. fromDeviceCode: before.deviceCode,
  489. toDeviceCode: '',
  490. })
  491. lastLevel = DETAIL_LEVEL.POINT
  492. lastDeviceCode = ''
  493. if (flight === DEVICE_FLIGHT.POINT_TIER) {
  494. const point = useMapStore().summary.objects.find((item) => item.kind === 'monitoring-point' && item.code === pointCode.value)
  495. if (point) void locateMonitoringPoint(point)
  496. }
  497. syncDeviceReactions()
  498. return flight
  499. }
  500. /** 详情层级变了(外壳订阅)→ 同步内部对照值,避免下一次判定拿错"变化前"。 */
  501. function noteLevel(level, deviceCode) {
  502. if (level !== DETAIL_LEVEL.EQUIPMENT) {
  503. deviceFlightToken += 1
  504. pendingDeviceTier = false
  505. }
  506. if (level === DETAIL_LEVEL.NONE) cancelPointLocate()
  507. lastLevel = level || DETAIL_LEVEL.NONE
  508. lastDeviceCode = deviceCode || ''
  509. }
  510. /**
  511. * 票 45:面板 hover 聚合行 → 三维聚合符号弱提示(不摊开、不写设备选中态)。
  512. * group 查找收在 store(它持有 `overlap.groups`),组件只递行对象、不碰服务层。
  513. * @returns {boolean} 是否真的对聚合组做了弱提示(false = 该行不属于聚合组)
  514. */
  515. function nudgeOverlapRow(row) {
  516. const code = row && row.code
  517. if (!code) return false
  518. const group = (overlap.value.groups || []).find((g) => (g.rows || []).some((r) => r.code === code))
  519. if (!group || group.k < 2) return false
  520. return nudgeOverlapService(group.key) === true
  521. }
  522. /** 票 45:面板离开聚合行 → 清除三维弱提示(无弱提示时调用也安全)。 */
  523. function clearOverlapNudge() {
  524. return clearOverlapNudgeService()
  525. }
  526. /** 按当前选中/hover 重算反应(设备行或选中变化后调)。 */
  527. function syncDeviceReactions() {
  528. const plan = syncDeviceReactionsService({ devices: rows.value })
  529. dimming.value = plan.dimming
  530. return plan
  531. }
  532. /** 仅测试/走查用:彻底复位(含地图上的图层)。 */
  533. function reset() {
  534. token += 1
  535. pointCode.value = ''
  536. rows.value = []
  537. counts.value = {}
  538. total.value = 0
  539. legend.value = []
  540. overlap.value = { k: 0, n: 0, maxFan: 10, groups: [], active: false, degraded: false }
  541. effectsPlan.value = null
  542. notice.value = { risky: false, text: '' }
  543. loading.value = false
  544. painted.value = false
  545. lastPlanKey = ''
  546. // 票 43:选中/hover/层级对照一并复位。
  547. selectedDevice.value = ''
  548. hoveredDevice.value = ''
  549. dimming.value = false
  550. lastLevel = DETAIL_LEVEL.NONE
  551. lastDeviceCode = ''
  552. pendingDeviceTier = false
  553. deviceFlightToken += 1
  554. cancelPointLocate()
  555. clearAssociatedDevices()
  556. }
  557. return {
  558. KIND: DEVICE_LAYER_KIND,
  559. pointCode,
  560. rows,
  561. counts,
  562. total,
  563. legend,
  564. overlap,
  565. effectsPlan,
  566. notice,
  567. loading,
  568. painted,
  569. // 票 43
  570. selectedDevice,
  571. hoveredDevice,
  572. dimming,
  573. dimThreshold: DIM_THRESHOLD,
  574. selectDevice,
  575. hoverDevice,
  576. backToPointLevel,
  577. noteLevel,
  578. // 票 45:聚合 hover 弱提示(component 经 store 调 mapService,维护票 10 F4 纪律)。
  579. nudgeOverlapRow,
  580. clearOverlapNudge,
  581. syncDeviceReactions,
  582. syncForSelection,
  583. invalidateForSnapshot,
  584. suspendForSnapshot,
  585. reset,
  586. }
  587. })