huang
2025-05-20 2c2413760b6467bf62402dba7338bd3bbcbd7341
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
 
// 数据定义
const formData = ref({
  recordDate: new Date().toISOString().slice(0, 10),
  energyValue: null
})
const loading = ref(false)
const energyData = ref([])
 
// 日期格式化
const formatDate = (dateStr) => {
  const date = new Date(dateStr)
  return date.toISOString().slice(0, 10)
}
 
// 检查日期是否重复
const checkDateExists = (date, excludeId = null) => {
  return energyData.value.some(item => 
    item.recordDate === date && (!excludeId || item.id !== excludeId)
  )
}
 
// API 请求
const loadDataFromApi = async () => {
  loading.value = true
  try {
    const { data } = await request({
      url: '/deviceInteraction/energy/consumption/listEnergy',
      method: 'post',
      data: { page: 1, pageSize: 10 }
    })
    if (data?.records) {
      energyData.value = data.records.map(item => ({
        ...item,
        editing: false,
        originalData: { ...item }
      }))
    }
  } catch (error) {
    ElMessage.error('数据加载失败')
    console.error(error)
  } finally {
    loading.value = false
  }
}
 
// 表单操作
const handleSubmit = async () => {
  if (!formData.value.recordDate || formData.value.energyValue === null) {
    ElMessage.error('请填写完整信息')
    return
  }
 
  if (checkDateExists(formData.value.recordDate)) {
    ElMessage.error('该日期已存在能耗记录')
    return
  }
 
  try {
    await request({
      url: '/deviceInteraction/energy/consumption/addEnergy',
      method: 'post',
      data: formData.value
    })
    ElMessage.success('添加成功')
    await loadDataFromApi()
    resetForm()
  } catch (error) {
    ElMessage.error('添加失败')
    console.error(error)
  }
}
 
const resetForm = () => {
  formData.value = {
    recordDate: new Date().toISOString().slice(0, 10),
    energyValue: null
  }
}
 
// 表格操作
const handleRowEdit = async (index, row) => {
  if (!row.editing) {
    row.editing = true
    return
  }
 
  if (!row.recordDate || row.energyValue === null) {
    ElMessage.error('请填写完整信息')
    return
  }
 
  if (checkDateExists(row.recordDate, row.id)) {
    ElMessage.error('该日期已存在能耗记录')
    return
  }
 
  try {
    await request({
      url: '/deviceInteraction/energy/consumption/updateEnergy',
      method: 'post',
      data: {
        id: row.id,
        recordDate: row.recordDate,
        energyValue: row.energyValue
      }
    })
    row.editing = false
    row.originalData = { ...row }
    ElMessage.success('修改成功')
  } catch (error) {
    ElMessage.error('修改失败')
    console.error(error)
  }
}
 
const cancelEdit = (index, row) => {
  Object.assign(row, row.originalData)
  row.editing = false
}
 
const handleDelete = async (index) => {
  try {
    await ElMessageBox.confirm('确认删除该记录?', '警告', {
      type: 'warning'
    })
    const id = energyData.value[index].id
    await request({
      url: '/deviceInteraction/energy/consumption/deleteEnergy',
      method: 'post',
      data: { id }
    })
    energyData.value.splice(index, 1)
    ElMessage.success('删除成功')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
      console.error(error)
    }
  }
}
 
// 初始化
loadDataFromApi()
</script>
 
<template>
  <el-container>
    <el-header class="header" style="height: auto; margin:20px 0 -10px ; ">
      <el-form :inline="true" :model="formData" label-width="80px" class="form-container">
        <el-form-item label="日期" prop="recordDate">
          <el-date-picker
            v-model="formData.recordDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择日期"
            :default-value="new Date()"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="能耗值" prop="energyValue">
          <el-input-number
            v-model="formData.energyValue"
            :precision="2"
            :step="0.1"
            :min="0"
            controls-position="right"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    
    <el-main class="main">
      <div class="table-container">
        <el-table 
          :data="energyData" 
          border 
          style="width: 100%" 
          height="500"
          v-loading="loading"
        >
          <el-table-column prop="recordDate" label="日期" width="180">
            <template #default="scope">
              <el-date-picker
                v-if="scope.row.editing"
                v-model="scope.row.recordDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="选择日期"
                style="width: 140px"
              />
              <span v-else>{{ formatDate(scope.row.recordDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="energyValue" label="能耗值" width="180">
            <template #default="scope">
              <el-input-number
                v-if="scope.row.editing"
                v-model="scope.row.energyValue"
                :precision="2"
                :step="0.1"
                :min="0"
                controls-position="right"
                style="width: 140px"
              />
              <span v-else>{{ scope.row.energyValue }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" fixed="right">
            <template #default="scope">
              <el-button-group>
                <el-button 
                  size="small" 
                  :type="scope.row.editing ? 'success' : 'primary'"
                  @click="handleRowEdit(scope.$index, scope.row)"
                >
                  {{ scope.row.editing ? '保存' : '编辑' }}
                </el-button>
                <el-button 
                  v-if="scope.row.editing" 
                  size="small"
                  @click="cancelEdit(scope.$index, scope.row)"
                >
                  取消
                </el-button>
                <el-button 
                  v-else 
                  size="small" 
                  type="danger" 
                  @click="handleDelete(scope.$index)"
                >
                  删除
                </el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-main>
  </el-container>
</template>
 
<style scoped>
 
</style>