Blame view

src/components/room/roomTreeDiv.vue 4.31 KB
cd8d442f   wuxw   开始处理水电抄表功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  <template>
    <div class="room-tree-container">
      <el-tree ref="tree" :data="treeData" node-key="id" :props="defaultProps" :highlight-current="true"
        :expand-on-click-node="false" @node-click="handleNodeClick">
        <span slot-scope="{ node, data }" class="custom-tree-node">
          <span>
            <i :class="data.icon" style="margin-right: 5px"></i>
            {{ node.label }}
          </span>
        </span>
      </el-tree>
    </div>
  </template>
  
  <script>
  import { queryUnits, queryRoomsTree } from '@/api/fee/meterWaterManageApi'
  import { getCommunityId } from '@/api/community/communityApi'
  
  export default {
    name: 'RoomTreeDiv',
    data() {
      return {
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'text'
        },
        communityId: ''
      }
    },
    created() {
      this.communityId = getCommunityId()
      this.loadTreeData()
    },
    methods: {
      async loadTreeData() {
        try {
          const units = await queryUnits({
            communityId: this.communityId
          })
          this.buildTreeData(units)
        } catch (error) {
          console.error('Failed to load tree data:', error)
9d8dc2e6   wuxw   开发完成水电抄表
44
          this.$message.error(this.$t('roomTree.loadError'))
cd8d442f   wuxw   开始处理水电抄表功能
45
46
47
        }
      },
      buildTreeData(units) {
9d8dc2e6   wuxw   开发完成水电抄表
48
        const floorMap = {}
cd8d442f   wuxw   开始处理水电抄表功能
49
  
9d8dc2e6   wuxw   开发完成水电抄表
50
        // Build floor nodes and unit nodes
cd8d442f   wuxw   开始处理水电抄表功能
51
        units.forEach(unit => {
9d8dc2e6   wuxw   开发完成水电抄表
52
53
          if (!floorMap[unit.floorId]) {
            floorMap[unit.floorId] = {
cd8d442f   wuxw   开始处理水电抄表功能
54
55
56
              id: `f_${unit.floorId}`,
              floorId: unit.floorId,
              floorNum: unit.floorNum,
9d8dc2e6   wuxw   开发完成水电抄表
57
58
              icon: "/img/floor.png",
              text: `${unit.floorNum}${this.$t('room.floorUnitTree.building')}`,
cd8d442f   wuxw   开始处理水电抄表功能
59
              children: []
9d8dc2e6   wuxw   开发完成水电抄表
60
            }
cd8d442f   wuxw   开始处理水电抄表功能
61
          }
cd8d442f   wuxw   开始处理水电抄表功能
62
  
9d8dc2e6   wuxw   开发完成水电抄表
63
64
65
66
67
68
69
70
71
          floorMap[unit.floorId].children.push({
            id: `u_${unit.unitId}`,
            unitId: unit.unitId,
            unitNum: unit.unitNum,
            floorId: unit.floorId, // Add floorId reference
            icon: "/img/unit.png",
            text: `${unit.unitNum}${this.$t('room.floorUnitTree.unit')}`,
            children: []
          })
cd8d442f   wuxw   开始处理水电抄表功能
72
73
        })
  
9d8dc2e6   wuxw   开发完成水电抄表
74
        this.treeData = Object.values(floorMap)
24d3590f   wuxw   房屋收费页面开发完成
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        this.$nextTick(() => {
          this.expandAndSelectFirstRoom()
        })
      },
      async expandAndSelectFirstRoom() {
        if (!this.treeData.length) return
        const firstFloor = this.treeData[0]
        if (!firstFloor.children || !firstFloor.children.length) return
        const firstUnit = firstFloor.children[0]
        const treeRef = this.$refs.tree
        if (treeRef && treeRef.store) {
          treeRef.store.nodesMap[firstFloor.id] && (treeRef.store.nodesMap[firstFloor.id].expanded = true)
          treeRef.store.nodesMap[firstUnit.id] && (treeRef.store.nodesMap[firstUnit.id].expanded = true)
        }
        await this.loadRooms(firstUnit, { data: firstUnit })
cd8d442f   wuxw   开始处理水电抄表功能
90
      },
9d8dc2e6   wuxw   开发完成水电抄表
91
      async handleNodeClick(data, node) {
cd8d442f   wuxw   开始处理水电抄表功能
92
        if (data.id.startsWith('u_')) {
9d8dc2e6   wuxw   开发完成水电抄表
93
94
95
96
          if (!node.expanded) {
            await this.loadRooms(data, node)
            node.expanded = true
          }
cd8d442f   wuxw   开始处理水电抄表功能
97
98
99
100
101
102
103
        } else if (data.id.startsWith('r_')) {
          this.$emit('selectRoom', {
            roomId: data.roomId,
            roomName: data.roomName
          })
        }
      },
9d8dc2e6   wuxw   开发完成水电抄表
104
      async loadRooms(unitData, node) {
cd8d442f   wuxw   开始处理水电抄表功能
105
        try {
9d8dc2e6   wuxw   开发完成水电抄表
106
107
          const { rooms } = await queryRoomsTree({
            unitId: unitData.unitId,
cd8d442f   wuxw   开始处理水电抄表功能
108
109
110
111
112
            communityId: this.communityId,
            page: 1,
            row: 1000
          })
  
9d8dc2e6   wuxw   开发完成水电抄表
113
114
115
116
117
118
119
          if (rooms && rooms.length > 0) {
            const roomNodes = rooms.map(room => ({
              id: `r_${room.roomId}`,
              roomId: room.roomId,
              roomName: `${room.floorNum}-${room.unitNum}-${room.roomNum}`,
              icon: "/img/room.png",
              text: room.ownerName
5f798b88   wuxw   费用功能继续完善
120
                ? `${room.roomNum}(${room.ownerName})`
9d8dc2e6   wuxw   开发完成水电抄表
121
122
                : `${room.roomNum}`
            }))
cd8d442f   wuxw   开始处理水电抄表功能
123
  
9d8dc2e6   wuxw   开发完成水电抄表
124
125
            // Update the node's children
            this.$set(node.data, 'children', roomNodes)
24d3590f   wuxw   房屋收费页面开发完成
126
127
128
129
            this.$emit('selectRoom', {
              roomId: roomNodes[0].roomId,
              roomName: roomNodes[0].roomName
            })
cd8d442f   wuxw   开始处理水电抄表功能
130
131
132
          }
        } catch (error) {
          console.error('Failed to load rooms:', error)
9d8dc2e6   wuxw   开发完成水电抄表
133
          this.$message.error(this.$t('roomTree.loadRoomError'))
cd8d442f   wuxw   开始处理水电抄表功能
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        }
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .room-tree-container {
    height: 100%;
    overflow-y: auto;
    padding: 10px;
  
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      font-size: 14px;
      padding: 5px 0;
    }
  }
  </style>