<template>
|
<div class="app-container">
|
<el-row :gutter="20">
|
<el-col :span="24">
|
<el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
|
<el-form-item label="状态" prop="status">
|
<el-select v-model="queryParams.status" placeholder="定时任务状态" clearable>
|
<el-option
|
v-for="dict in statusOptions"
|
:key="dict.dictValue"
|
:label="dict.label"
|
:value="dict.dictValue"
|
/>
|
</el-select>
|
</el-form-item>
|
<el-form-item>
|
<el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
|
<el-button :icon="Refresh" @click="resetQuery">重置</el-button>
|
</el-form-item>
|
</el-form>
|
|
<el-row :gutter="10" class="mb8">
|
<el-col :span="1.5">
|
<el-button
|
type="primary"
|
plain
|
:icon="Plus"
|
@click="handleAdd"
|
v-hasPermi="['system:airconditioner:schedule:add']"
|
>新增</el-button>
|
</el-col>
|
<el-col :span="1.5">
|
<el-button
|
type="success"
|
plain
|
:icon="Edit"
|
:disabled="single"
|
@click="handleUpdate"
|
v-hasPermi="['system:airconditioner:schedule:edit']"
|
>修改</el-button>
|
</el-col>
|
<el-col :span="1.5">
|
<el-button
|
type="danger"
|
plain
|
:icon="Delete"
|
:disabled="multiple"
|
@click="handleDelete"
|
v-hasPermi="['system:airconditioner:schedule:remove']"
|
>删除</el-button>
|
</el-col>
|
<right-toolbar v-model:show-search="showSearch" @queryTable="getList"></right-toolbar>
|
</el-row>
|
|
<el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
|
<el-table-column type="selection" width="55" align="center" />
|
<el-table-column label="任务ID" align="center" prop="id" />
|
<el-table-column label="空调名称" align="center" prop="airConditionerName">
|
{{ currentAirConditionerName }}
|
<!-- <template #default="scope">
|
<span>{{ getAirConditionerName(scope.row.airConditionerId) }}</span>
|
</template> -->
|
</el-table-column>
|
<el-table-column label="开始时间" align="center" prop="startTime" width="120">
|
<!-- <template #default="scope">-->
|
<!-- <span>{{ formatTimeOnly(scope.row.startTime) }}</span>-->
|
<!-- </template>-->
|
</el-table-column>
|
<el-table-column label="结束时间" align="center" prop="offTime" width="120">
|
<!-- <template #default="scope">-->
|
<!-- <span>{{ formatTimeOnly(scope.row.offTime) }}</span>-->
|
<!-- </template>-->
|
</el-table-column>
|
<el-table-column label="状态" align="center" prop="status">
|
<template #default="scope">
|
<dict-tag :options="statusOptions" :value="scope.row.status"/>
|
</template>
|
</el-table-column>
|
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
|
<template #default="scope">
|
<el-button
|
type="primary"
|
link
|
:icon="Edit"
|
@click="handleUpdate(scope.row)"
|
v-hasPermi="['system:airconditioner:schedule:edit']"
|
>修改</el-button>
|
<el-button
|
type="primary"
|
link
|
:icon="Delete"
|
@click="handleDelete(scope.row)"
|
v-hasPermi="['system:airconditioner:schedule:remove']"
|
>删除</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
|
<pagination
|
v-show="total>0"
|
:total="total"
|
v-model:page="queryParams.pageNum"
|
v-model:limit="queryParams.pageSize"
|
@pagination="getList"
|
/>
|
</el-col>
|
</el-row>
|
|
<!-- 添加或修改空调定时任务对话框 -->
|
<el-dialog :title="title" v-model="open" width="500px" append-to-body>
|
<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
|
<el-form-item label="空调" prop="airConditionerId">
|
<el-select v-model="form.airConditionerId" placeholder="请选择空调" style="width: 100%" :disabled="true">
|
<el-option
|
v-for="item in airConditionerOptions"
|
:key="item.id"
|
:label="item.name"
|
:value="item.id"
|
/>
|
</el-select>
|
</el-form-item>
|
<el-form-item label="开始时间" prop="startTime">
|
<el-time-picker
|
v-model="form.startTime"
|
format="HH:mm:ss"
|
placeholder="选择开始时间"
|
style="width: 100%"
|
/>
|
</el-form-item>
|
<el-form-item label="结束时间" prop="offTime">
|
<el-time-picker
|
v-model="form.offTime"
|
format="HH:mm:ss"
|
placeholder="选择结束时间"
|
style="width: 100%"
|
/>
|
</el-form-item>
|
<el-form-item label="状态">
|
<el-radio-group v-model="form.status">
|
<el-radio
|
v-for="dict in statusOptions"
|
:key="dict.value"
|
:label="dict.value"
|
>{{dict.label}}</el-radio>
|
</el-radio-group>
|
</el-form-item>
|
</el-form>
|
<template #footer>
|
<div class="dialog-footer">
|
<el-button type="primary" @click="submitForm">确 定</el-button>
|
<el-button @click="cancel">取 消</el-button>
|
</div>
|
</template>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script setup name="AirConditionerSchedule">
|
import { ref, reactive, onMounted, getCurrentInstance, nextTick } from 'vue'
|
import { Search, Refresh, Plus, Edit, Delete } from '@element-plus/icons-vue'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { listSchedule, getSchedule, addSchedule, updateSchedule, delSchedule, getScheduleByAirConditionerId } from "@/api/airconditioner/schedule"
|
import { listAirConditioner } from "@/api/airconditioner/airconditioner"
|
import { parseTime } from '@/utils/ruoyi'
|
|
/** 格式化时间为时:分:秒 */
|
function formatTimeOnly(time) {
|
if (!time) return ''
|
const date = new Date(time)
|
return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
|
}
|
|
// 获取父组件实例
|
const { proxy } = getCurrentInstance()
|
|
// 遮罩层
|
const loading = ref(true)
|
// 选中数组
|
const ids = ref([])
|
// 非单个禁用
|
const single = ref(true)
|
// 非多个禁用
|
const multiple = ref(true)
|
// 显示搜索条件
|
const showSearch = ref(true)
|
// 总条数
|
const total = ref(0)
|
// 定时任务表格数据
|
const scheduleList = ref([])
|
// 空调选项
|
const airConditionerOptions = ref([])
|
// 当前选中的空调ID
|
const currentAirConditionerId = ref(null)
|
|
const currentAirConditionerName= ref('')
|
// 弹出层标题
|
const title = ref('')
|
// 是否显示弹出层
|
const open = ref(false)
|
// 表单参数
|
const form = ref({})
|
// 表单ref
|
const formRef = ref()
|
const queryFormRef = ref()
|
|
// 状态字典
|
const statusOptions = [
|
{
|
label: "正常",
|
value: "0"
|
},
|
{
|
label: "停用",
|
value: "1"
|
}
|
]
|
|
// 查询参数
|
const queryParams = ref({
|
pageNum: 1,
|
pageSize: 10,
|
airConditionerId: null,
|
status: null
|
})
|
|
// 表单校验
|
const rules = {
|
airConditionerId: [
|
{ required: true, message: "空调不能为空", trigger: "change" }
|
],
|
startTime: [
|
{ required: true, message: "开始时间不能为空", trigger: "blur" }
|
],
|
offTime: [
|
{ required: true, message: "结束时间不能为空", trigger: "blur" }
|
]
|
}
|
|
/** 查询定时任务列表 */
|
function getList() {
|
loading.value = true
|
// 如果有当前选中的空调ID,则只查询该空调的定时任务
|
if (currentAirConditionerId.value) {
|
getScheduleByAirConditionerId(currentAirConditionerId.value).then(response => {
|
|
scheduleList.value = response.data.sort((a, b) => {
|
return new Date(a.startTime) - new Date(b.startTime)
|
})
|
total.value = response.data.length
|
loading.value = false
|
})
|
} else {
|
// 兼容原有逻辑,但正常情况下不会执行到这里
|
listSchedule(queryParams.value).then(response => {
|
|
// 先按空调名称排序,再按开始时间排序
|
scheduleList.value = response.rows.sort((a, b) => {
|
const nameA = getAirConditionerName(a.airConditionerId)
|
const nameB = getAirConditionerName(b.airConditionerId)
|
if (nameA !== nameB) {
|
return nameA.localeCompare(nameB)
|
}
|
return new Date(a.startTime) - new Date(b.startTime)
|
})
|
total.value = response.total
|
loading.value = false
|
})
|
}
|
}
|
|
/** 查询空调列表 */
|
function getAirConditioners() {
|
listAirConditioner().then(response => {
|
airConditionerOptions.value = response.rows
|
})
|
}
|
|
/** 根据空调ID获取空调名称 */
|
// function getAirConditionerName(airConditionerId) {
|
// const airConditioner = airConditionerOptions.value.find(item => item.id === airConditionerId)
|
// return airConditioner ? airConditioner.name : ''
|
// }
|
|
// 取消按钮
|
function cancel() {
|
open.value = false
|
reset()
|
}
|
|
// 表单重置
|
function reset() {
|
form.value = {
|
id: null,
|
airConditionerId: null,
|
startTime: null,
|
offTime: null,
|
status: "0"
|
}
|
nextTick(() => {
|
if (formRef.value) {
|
formRef.value.resetFields()
|
}
|
})
|
}
|
|
/** 搜索按钮操作 */
|
function handleQuery() {
|
queryParams.value.pageNum = 1
|
getList()
|
}
|
|
/** 重置按钮操作 */
|
function resetQuery() {
|
queryFormRef.value.resetFields()
|
handleQuery()
|
}
|
|
// 多选框选中数据
|
function handleSelectionChange(selection) {
|
ids.value = selection.map(item => item.id)
|
single.value = selection.length !== 1
|
multiple.value = !selection.length
|
}
|
|
/** 新增按钮操作 */
|
function handleAdd() {
|
reset()
|
// 设置当前选中的空调ID
|
form.value.airConditionerId = currentAirConditionerId.value
|
open.value = true
|
title.value = "添加定时任务"
|
}
|
|
/** 修改按钮操作 */
|
function handleUpdate(row) {
|
reset()
|
const id = row.id || ids.value[0]
|
getSchedule(id).then(response => {
|
const data = response.data
|
|
// 处理时间格式,将HH:MM:SS格式转换为Date对象
|
if (data.startTime) {
|
const [hours, minutes, seconds] = data.startTime.split(':').map(Number)
|
const startTime = new Date()
|
startTime.setHours(hours, minutes, seconds)
|
data.startTime = startTime
|
}
|
|
if (data.offTime) {
|
const [hours, minutes, seconds] = data.offTime.split(':').map(Number)
|
const offTime = new Date()
|
offTime.setHours(hours, minutes, seconds)
|
data.offTime = offTime
|
}
|
|
form.value = data
|
open.value = true
|
title.value = "修改定时任务"
|
})
|
}
|
|
/** 提交按钮 */
|
function submitForm() {
|
formRef.value.validate(valid => {
|
if (valid) {
|
// 创建提交数据的副本
|
const submitData = { ...form.value }
|
|
// 处理时间格式,只保留时分秒
|
if (submitData.startTime) {
|
const startTime = new Date(submitData.startTime)
|
submitData.startTime = `${startTime.getHours().toString().padStart(2, '0')}:${startTime.getMinutes().toString().padStart(2, '0')}:${startTime.getSeconds().toString().padStart(2, '0')}`
|
}
|
|
if (submitData.offTime) {
|
const offTime = new Date(submitData.offTime)
|
submitData.offTime = `${offTime.getHours().toString().padStart(2, '0')}:${offTime.getMinutes().toString().padStart(2, '0')}:${offTime.getSeconds().toString().padStart(2, '0')}`
|
}
|
|
if (submitData.id != null) {
|
updateSchedule(submitData).then(response => {
|
ElMessage.success("修改成功")
|
open.value = false
|
getList()
|
})
|
} else {
|
addSchedule(submitData).then(response => {
|
ElMessage.success("新增成功")
|
open.value = false
|
getList()
|
})
|
}
|
}
|
})
|
}
|
|
/** 删除按钮操作 */
|
function handleDelete(row) {
|
const deleteIds = row.id || ids.value
|
ElMessageBox.confirm('是否确认删除定时任务编号为"' + deleteIds + '"的数据项?')
|
.then(() => {
|
return delSchedule(deleteIds)
|
})
|
.then(() => {
|
getList()
|
ElMessage.success("删除成功")
|
})
|
.catch(() => {})
|
}
|
|
/** 根据空调ID加载定时任务 */
|
function loadScheduleByAirConditioner(airConditioner) {
|
console.log('loadScheduleByAirConditioner', airConditioner)
|
if (airConditioner) {
|
currentAirConditionerName.value = airConditioner.name
|
// 设置当前选中的空调ID
|
currentAirConditionerId.value = airConditioner.id
|
queryParams.value.airConditionerId = airConditioner.controllerId
|
handleQuery()
|
}
|
}
|
|
onMounted(() => {
|
// 页面加载时先获取空调列表,但不自动加载定时任务列表
|
// 定时任务列表将通过父组件传递的空调ID加载
|
getAirConditioners()
|
})
|
|
// 暴露方法给父组件调用
|
defineExpose({
|
getList,
|
loadScheduleByAirConditioner
|
})
|
</script>
|