U
he wei
2025-04-24 368413ba07c0876e8b4eb8ba3484b27fa843d68f
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
<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { updateLockPosition } from '@/api/lockManager.js';
import useElement from "@/hooks/useElement.js";
 
const { $loading, $message, $confirm, $confirmPwdDo } = useElement();
 
const emit = defineEmits(['success', 'cancel']);
 
// 接收父组件传入的锁的数量
const props = defineProps({
  locationInfo: {
    type: Object,
    default: () => ({
      control: [],
      all: []
    })
  },
});
 
// // 初始化锁的配置数组
const lockConfigs = reactive([]);
 
// 所有机柜编号
// const allCabinets = ref(Array.from({ length: 20 }, (_, i) => ({idx: i + 1, doors: 0x00})));
const allCabinets = reactive({});
const cabIdxs = reactive({});
const doorIdxs = reactive({});
 
 
const lockCount = computed(() => {
  return props.locationInfo.control.length;
});
 
const otherLocks = computed(() => {
  let controls = props.locationInfo.control;
  let all = props.locationInfo.all;
  let res = [];
  for (let i = 0, len = all.length; i < len; i++) {
    let lockId = all[i].lockId;
    if (controls.every(v => v.lockId != lockId)) {
      res.push(all[i]);
    }
  }
  return res;
});
 
// 根据锁的数量初始化配置对象
function init() {
  let controls = props.locationInfo.control;
  // console.log('controls', controls, cabIdxs.value, '=============');
  for (let i = 0; i < 20; i++) {
    allCabinets[i] = {
      idx: i + 1,
      doors: 0x00
    };
  }
 
  for (let i = 0, len = lockCount.value; i < len; i++) {
    cabIdxs[i] = controls[i].screenFlag || undefined;
    doorIdxs[i] = controls[i].addressFlag || undefined;
  }
 
  let all = props.locationInfo.all;
  // 重新计算每个机柜的门状态 doors  使用all 来更新
  for (let i = 0, len = all.length; i < len; i++) {
    let idx = all[i].screenFlag - 1;
    if (idx < 0 || idx > 19) continue;
    allCabinets[idx].doors |= all[i].addressFlag;
  }
  console.log('config', allCabinets, '=============');
 
}
 
// 计算可用的机柜编号
const availableCabinets = computed(() => {
  // 计算可用的机柜  遍历所有的机柜,判断是否小于0x03
  return Object.values(allCabinets).filter(v=> v.doors < 0x03);
});
 
// 处理机柜选择事件
const handleCabinetSelect = (index) => {
  // 这里可以添加额外的逻辑,比如更新相关状态
  doorIdxs[index] = undefined;
  updateAllDoors();
};
 
const handleDoorSelect = (index) => {
  // console.log('door change', '=============');
  
  // allCabinets[index].doors |= doorIdxs[index];
  updateAllDoors();
}
 
// 更新全部的门状态
function updateAllDoors () {
  for (let i = 0; i < 20; i++) {
    allCabinets[i] = {
      idx: i + 1,
      doors: 0x00
    };
  }
  // otherLocks 
  let others = otherLocks.value;
  for (let i = 0, len = others.length; i < len; i++) {
    let idx = others[i].screenFlag - 1;
    if (idx < 0 || idx > 19) continue;
    allCabinets[idx].doors |= others[i].addressFlag;
  }
  // controls
  let controls = props.locationInfo.control;
  for (let i = 0, len = controls.length; i < len; i++) {
    let idx = cabIdxs[i];
    if (!idx) continue;
    allCabinets[idx-1].doors |= doorIdxs[i];
  }
  // console.log('allCabinets', allCabinets, '=============');
  
}
 
function cancel () {
  emit('cancel');
}
 
// 提交表单的方法
const submitForm = () => {
  console.log('提交的锁配置:', cabIdxs, doorIdxs);
  let params = [];
  Object.keys(cabIdxs).forEach((key) => {
    let value = cabIdxs[key];
    let door = doorIdxs[key];
    let lock = props.locationInfo.control[key];
    if (value && door) {
      params.push({
        lockId: lock.lockId,
        screenFlag: value,
        addressFlag: door,
        stationId: lock.stationId
      });
    }
  });
 
  let loading = $loading();
  updateLockPosition(params).then((res) => {
    let { code, data } = res;
    loading.close();
    if (code && data) {
      // console.log(data);
      $message.success("操作成功");
      emit('success');
    } else {
      $message.error("操作失败");
    }
  })
  .catch((err) => {
    loading.close();
    console.log(err);
  });
  
};
 
onMounted(() => {
  init();
 
});
</script>
 
<template>
  <!-- 遍历所有的锁 给每个锁选择 机柜序号 门序号 -->
  <!-- <el-form :model="lockConfigs"> -->
  <el-form class="form">
    <div class="scroller">
      <template v-for="(lock, index) in locationInfo.control" :key="index">
        <el-row :gutter="16">
          <el-col :span="24">
            <div class="info">锁具: {{ lock.lockName }}</div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机柜序号">
              <el-select
                v-model="cabIdxs[index]"
                placeholder="请选择机柜序号"
                clearable
                @change="handleCabinetSelect(index)"
              >
                <el-option
                  v-for="cabinet in availableCabinets"
                  :key="cabinet"
                  :label="cabinet.idx"
                  :value="cabinet.idx"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在柜门">
              <el-select
                v-model="doorIdxs[index]"
                :disabled="!cabIdxs[index]"
                clearable
                placeholder="请选择机柜序号"
                @change="handleDoorSelect(index)"
              >
                <el-option label="前门" :disabled="!cabIdxs[index] || (allCabinets[cabIdxs[index] - 1].doors & 0x01) > 0" :value="1"></el-option>
                <el-option label="后门" :disabled="!cabIdxs[index] || (allCabinets[cabIdxs[index] - 1].doors & 0x02) > 0" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </div>
    <div class="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submitForm">提交配置</el-button>
    </div>
  </el-form>
</template>
 
<style scoped lang="less">
// .form {
//   padding-right: 8px;
// }
.scroller {
  max-height: 400px;
  overflow-y: auto;
  overflow-x: hidden;
  margin-right: -8px;
  padding-right: 18px;
}
.info {
  font-size: 14px;
  color: #333;
  font-weight: bold;
  margin-bottom: 6px;
}
.footer {
  text-align: right;
}
</style>