gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!-- 单个条件配置组件 -->
<script setup lang="ts">
import type { RuleSceneApi } from '#/api/iot/rule/scene';
 
import { computed, ref } from 'vue';
 
import {
  getConditionTypeOptions,
  IoTDeviceStatusEnum,
  IotRuleSceneTriggerConditionParameterOperatorEnum,
  IotRuleSceneTriggerConditionTypeEnum,
} from '..\..\..\..\..\..\packages\constants\src';
 
import { useVModel } from '@vueuse/core';
import { Col, Form, Row, Select } from 'ant-design-vue';
 
import ValueInput from '../inputs/value-input.vue';
import DeviceSelector from '../selectors/device-selector.vue';
import OperatorSelector from '../selectors/operator-selector.vue';
import ProductSelector from '../selectors/product-selector.vue';
import PropertySelector from '../selectors/property-selector.vue';
import CurrentTimeConditionConfig from './current-time-condition-config.vue';
 
/** 单个条件配置组件 */
defineOptions({ name: 'ConditionConfig' });
 
const props = defineProps<{
  modelValue: RuleSceneApi.TriggerCondition;
  triggerType: number;
}>();
 
const emit = defineEmits<{
  (e: 'update:modelValue', value: RuleSceneApi.TriggerCondition): void;
}>();
 
/** 获取设备状态选项 */
const deviceStatusOptions = [
  {
    value: IoTDeviceStatusEnum.ONLINE.value,
    label: IoTDeviceStatusEnum.ONLINE.label,
  },
  {
    value: IoTDeviceStatusEnum.OFFLINE.value,
    label: IoTDeviceStatusEnum.OFFLINE.label,
  },
];
 
/** 获取状态操作符选项 */
const statusOperatorOptions = [
  {
    value: IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.value,
    label: IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.name,
  },
  {
    value: IotRuleSceneTriggerConditionParameterOperatorEnum.NOT_EQUALS.value,
    label: IotRuleSceneTriggerConditionParameterOperatorEnum.NOT_EQUALS.name,
  },
];
 
const condition = useVModel(props, 'modelValue', emit);
 
const propertyType = ref<string>('string'); // 属性类型
const propertyConfig = ref<any>(null); // 属性配置
const isDeviceCondition = computed(() => {
  return (
    condition.value.type ===
      IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS ||
    condition.value.type ===
      IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY
  );
}); // 计算属性:判断是否为设备相关条件
 
/**
 * 更新条件字段
 * @param field 字段名
 * @param value 字段值
 */
function updateConditionField(field: any, value: any) {
  Object.assign(condition.value, { [field]: value });
  emit('update:modelValue', condition.value);
}
 
/**
 * 更新整个条件对象
 * @param newCondition 新的条件对象
 */
function updateCondition(newCondition: RuleSceneApi.TriggerCondition) {
  condition.value = newCondition;
  emit('update:modelValue', condition.value);
}
 
/**
 * 处理条件类型变化事件
 * @param type 条件类型
 */
function handleConditionTypeChange(type: any) {
  // 根据条件类型清理字段
  const isCurrentTime =
    type === IotRuleSceneTriggerConditionTypeEnum.CURRENT_TIME;
  const isDeviceStatus =
    type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS;
 
  // 清理标识符字段(时间条件和设备状态条件都不需要)
  if (isCurrentTime || isDeviceStatus) {
    condition.value.identifier = undefined;
  }
 
  // 清理设备相关字段(仅时间条件需要)
  if (isCurrentTime) {
    condition.value.productId = undefined;
    condition.value.deviceId = undefined;
  }
 
  // 设置默认操作符
  condition.value.operator = isCurrentTime
    ? 'at_time'
    : IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.value;
 
  // 清空参数值
  condition.value.param = '';
}
 
/** 处理产品变化事件 */
function handleProductChange(_: any) {
  // 产品变化时清空设备和属性
  condition.value.deviceId = undefined;
  condition.value.identifier = '';
}
 
/** 处理设备变化事件 */
function handleDeviceChange(_: any) {
  // 设备变化时清空属性
  condition.value.identifier = '';
}
 
/**
 * 处理属性变化事件
 * @param propertyInfo.config - 属性配置
 * @param propertyInfo.type - 属性类型
 */
function handlePropertyChange(propertyInfo: { config: any; type: string }) {
  propertyType.value = propertyInfo.type;
  propertyConfig.value = propertyInfo.config;
 
  // 重置操作符和值
  condition.value.operator =
    IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.value;
  condition.value.param = '';
}
 
/** 处理操作符变化事件 */
function handleOperatorChange() {
  // 重置值
  condition.value.param = '';
}
</script>
 
<template>
  <div class="gap-[16px] flex flex-col">
    <!-- 条件类型选择 -->
    <Row :gutter="16">
      <Col :span="8">
        <Form.Item label="条件类型" required>
          <Select
            :value="condition.type"
            @change="
              (value: any) => {
                updateConditionField('type', value);
                handleConditionTypeChange(value);
              }
            "
            placeholder="请选择条件类型"
            class="w-full"
          >
            <Select.Option
              v-for="option in getConditionTypeOptions()"
              :key="option.value"
              :value="option.value"
            >
              {{ option.label }}
            </Select.Option>
          </Select>
        </Form.Item>
      </Col>
    </Row>
 
    <!-- 产品设备选择 - 设备相关条件的公共部分 -->
    <Row v-if="isDeviceCondition" :gutter="16">
      <Col :span="12">
        <Form.Item label="产品" required>
          <ProductSelector
            :model-value="condition.productId"
            @update:model-value="
              (value) => updateConditionField('productId', value)
            "
            @change="handleProductChange"
          />
        </Form.Item>
      </Col>
      <Col :span="12">
        <Form.Item label="设备" required>
          <DeviceSelector
            :model-value="condition.deviceId"
            @update:model-value="
              (value) => updateConditionField('deviceId', value)
            "
            :product-id="condition.productId"
            @change="handleDeviceChange"
          />
        </Form.Item>
      </Col>
    </Row>
 
    <!-- 设备状态条件配置 -->
    <div
      v-if="
        condition.type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS
      "
      class="gap-[16px] flex flex-col"
    >
      <!-- 状态和操作符选择 -->
      <Row :gutter="16">
        <!-- 操作符选择 -->
        <Col :span="12">
          <Form.Item label="操作符" required>
            <Select
              :value="condition.operator"
              @change="(value: any) => updateConditionField('operator', value)"
              placeholder="请选择操作符"
              class="w-full"
            >
              <Select.Option
                v-for="option in statusOperatorOptions"
                :key="option.value"
                :value="option.value"
              >
                {{ option.label }}
              </Select.Option>
            </Select>
          </Form.Item>
        </Col>
 
        <!-- 状态选择 -->
        <Col :span="12">
          <Form.Item label="设备状态" required>
            <Select
              :value="condition.param"
              @change="(value: any) => updateConditionField('param', value)"
              placeholder="请选择设备状态"
              class="w-full"
            >
              <Select.Option
                v-for="option in deviceStatusOptions"
                :key="option.value"
                :value="option.value"
              >
                {{ option.label }}
              </Select.Option>
            </Select>
          </Form.Item>
        </Col>
      </Row>
    </div>
 
    <!-- 设备属性条件配置 -->
    <div
      v-else-if="
        condition.type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY
      "
      class="space-y-[16px]"
    >
      <!-- 属性配置 -->
      <Row :gutter="16">
        <!-- 属性/事件/服务选择 -->
        <Col :span="6">
          <Form.Item label="监控项" required>
            <PropertySelector
              :model-value="condition.identifier"
              @update:model-value="
                (value) => updateConditionField('identifier', value)
              "
              :trigger-type="triggerType"
              :product-id="condition.productId"
              :device-id="condition.deviceId"
              @change="handlePropertyChange"
            />
          </Form.Item>
        </Col>
 
        <!-- 操作符选择 -->
        <Col :span="6">
          <Form.Item label="操作符" required>
            <OperatorSelector
              :model-value="condition.operator"
              @update:model-value="
                (value) => updateConditionField('operator', value)
              "
              :property-type="propertyType"
              @change="handleOperatorChange"
            />
          </Form.Item>
        </Col>
 
        <!-- 值输入 -->
        <Col :span="12">
          <Form.Item label="比较值" required>
            <ValueInput
              :model-value="condition.param"
              @update:model-value="
                (value) => updateConditionField('param', value)
              "
              :property-type="propertyType"
              :operator="condition.operator"
              :property-config="propertyConfig"
            />
          </Form.Item>
        </Col>
      </Row>
    </div>
 
    <!-- 当前时间条件配置 -->
    <CurrentTimeConditionConfig
      v-else-if="
        condition.type === IotRuleSceneTriggerConditionTypeEnum.CURRENT_TIME
      "
      :model-value="condition"
      @update:model-value="updateCondition"
    />
  </div>
</template>
 
<style scoped>
:deep(.ant-form-item) {
  margin-bottom: 0;
}
</style>