renkang
2 天以前 4a99240038013c7d962040e6f8eabd2d72095fd7
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<template>
  <div v-loading="loading" class="conditioner-tab">
    <el-form ref="configForm" :model="form" :rules="rules" label-width="150px" size="small" class="config-form">
      <section class="config-section">
        <div class="section-title">计费配置</div>
        <el-row :gutter="24" class="config-first-row">
          <el-col :span="12">
            <el-form-item label="欠费额度" prop="stopMoney" class="stop-money-item">
              <div class="el-input-group stop-money-group">
                <el-input-number
                  v-model="form.stopMoney"
                  :min="0"
                  :precision="2"
                  :step="10"
                  controls-position="right"
                  class="stop-money-input"
                />
                <div class="el-input-group__append">元</div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="计费开关" prop="isPwr">
              <el-switch v-model="form.isPwr" :active-value="1" :inactive-value="0" active-text="开启" inactive-text="关闭"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="18:00-09:00 不停机" prop="isRestStop">
              <el-switch v-model="form.isRestStop" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" class="remark-item">
          <el-input
            v-model="form.gsBz"
            type="textarea"
            :rows="2"
            maxlength="500"
            show-word-limit
            placeholder="请输入备注(选填)"
          />
        </el-form-item>
      </section>
 
      <section class="config-section">
        <div class="section-header">
          <span class="section-title required-title">关联内机</span>
          <el-button type="primary" size="small" icon="el-icon-plus" @click="openSelector">添加内机</el-button>
        </div>
        <el-form-item prop="conditioners" label-width="0" class="conditioners-form-item">
          <el-table :data="form.conditioners" stripe size="small" class="device-table" empty-text="暂未关联内机,请点击添加">
            <el-table-column label="设备" min-width="200" align="left" show-overflow-tooltip>
              <template slot-scope="{ row }">{{ deviceLabel(row) }}</template>
            </el-table-column>
            <el-table-column prop="platformDevId" label="平台设备ID" min-width="110" align="center"/>
            <el-table-column label="在线" min-width="80" align="center">
              <template slot-scope="{ row }">
                <span :class="row.online === '在线' ? 'green' : 'red'">{{ row.online || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="电费占比%" min-width="130" align="center">
              <template slot-scope="{ row }">
                <el-input-number v-model="row.devRatio" :min="1" :max="100" size="small" controls-position="right"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="80" align="center" fixed="right">
              <template slot-scope="{ $index }">
                <el-button type="text" class="red" @click="removeConditioner($index)">移除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </section>
    </el-form>
 
    <div class="footer-btns">
      <el-button type="primary" :loading="saving" v-permissions="['business:ywcustomerrecharge:bindDevice']" @click="save">保存配置</el-button>
    </div>
 
    <GlobalWindow title="选择空调内机" :visible.sync="selectorVisible" width="780px" @confirm="confirmSelect">
      <el-form inline @submit.native.prevent class="selector-form">
        <el-form-item label="关键字">
          <el-input v-model="selectorKeyword" placeholder="名称/编号" clearable @keypress.enter.native="searchDevices"/>
        </el-form-item>
        <el-button type="primary" @click="searchDevices">查询</el-button>
      </el-form>
      <el-table ref="devTable" v-loading="selectorLoading" :data="selectorList" stripe size="small" @selection-change="rows => selectedRows = rows">
        <el-table-column type="selection" width="45"/>
        <el-table-column label="设备" min-width="180" align="left" show-overflow-tooltip>
          <template slot-scope="{ row }">{{ deviceLabel(row) }}</template>
        </el-table-column>
        <el-table-column prop="platformDevId" label="平台设备ID" min-width="110" align="center"/>
        <el-table-column prop="online" label="在线" min-width="80" align="center"/>
      </el-table>
      <pagination @current-change="p => { selectorPagination.pageIndex = p; loadDevices() }" :pagination="selectorPagination"/>
    </GlobalWindow>
  </div>
</template>
 
<script>
import GlobalWindow from '@/components/common/GlobalWindow'
import Pagination from '@/components/common/Pagination'
import * as rechargeApi from '@/api/business/ywcustomerrecharge'
import * as conditionerApi from '@/api/business/ywconditioner'
 
export default {
  name: 'YwCustomerConditionerTab',
  components: { GlobalWindow, Pagination },
  props: {
    customerId: Number,
    active: Boolean
  },
  data () {
    return {
      loading: false,
      saving: false,
      form: {
        isPwr: null,
        isRestStop: null,
        stopMoney: undefined,
        gsBz: '',
        conditioners: []
      },
      rules: {
        stopMoney: [{
          validator: (rule, value, callback) => {
            if (value === null || value === undefined || value === '') {
              callback(new Error('请输入欠费额度'))
            } else if (Number(value) < 0) {
              callback(new Error('欠费额度不能小于0'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        isPwr: [{
          validator: (rule, value, callback) => {
            if (value === 0 || value === 1) callback()
            else callback(new Error('请选择计费开关'))
          },
          trigger: 'change'
        }],
        isRestStop: [{
          validator: (rule, value, callback) => {
            if (value === 0 || value === 1) callback()
            else callback(new Error('请选择是否停机'))
          },
          trigger: 'change'
        }],
        conditioners: [{
          validator: (rule, value, callback) => {
            if (value && value.length) callback()
            else callback(new Error('请至少关联一台内机'))
          },
          trigger: 'change'
        }]
      },
      selectorVisible: false,
      selectorLoading: false,
      selectorKeyword: '',
      selectorList: [],
      selectorPagination: { pageIndex: 1, pageSize: 10, total: 0 },
      selectedRows: []
    }
  },
  watch: {
    active (val) {
      if (val) this.loadConfig()
    },
    customerId () {
      if (this.active) this.loadConfig()
    }
  },
  mounted () {
    if (this.active) this.loadConfig()
  },
  methods: {
    deviceLabel (row) {
      const parts = [row.floorName, row.roomName, row.name].filter(Boolean)
      return parts.length ? parts.join('/') : (row.name || row.code || '-')
    },
    loadConfig () {
      if (!this.customerId) return
      this.loading = true
      Promise.all([
        rechargeApi.getGsConfig(this.customerId),
        rechargeApi.conditionerPage(this.customerId, { page: 1, capacity: 500, model: {} })
      ]).then(([gs, page]) => {
        if (gs) {
          this.form.isPwr = gs.isPwr != null ? gs.isPwr : null
          this.form.isRestStop = gs.isRestStop != null ? gs.isRestStop : null
          this.form.stopMoney = gs.stopMoney != null ? gs.stopMoney : undefined
          this.form.gsBz = gs.gsBz || ''
        } else {
          this.form.isPwr = null
          this.form.isRestStop = null
          this.form.stopMoney = undefined
          this.form.gsBz = ''
        }
        this.form.conditioners = (page.records || []).map(c => ({
          conditionerId: c.id,
          platformDevId: c.platformDevId,
          name: c.name,
          floorName: c.floorName,
          roomName: c.roomName,
          online: c.online,
          devRatio: c.devRatio != null ? c.devRatio : 100
        }))
        this.$nextTick(() => {
          if (this.$refs.configForm) {
            this.$refs.configForm.clearValidate()
          }
        })
      }).catch(e => this.$tip.apiFailed(e)).finally(() => { this.loading = false })
    },
    save () {
      this.$refs.configForm.validate(valid => {
        if (!valid) return
        this.saving = true
        rechargeApi.saveGsConfig({
          customerId: this.customerId,
          isPwr: this.form.isPwr,
          isRestStop: this.form.isRestStop,
          stopMoney: this.form.stopMoney,
          gsBz: this.form.gsBz,
          conditioners: this.form.conditioners.map(c => ({
            conditionerId: c.conditionerId,
            devRatio: c.devRatio
          }))
        }).then(() => {
          this.$tip.success('保存成功')
          this.loadConfig()
          this.$emit('success')
        }).catch(e => this.$tip.apiFailed(e)).finally(() => { this.saving = false })
      })
    },
    removeConditioner (index) {
      this.form.conditioners.splice(index, 1)
      this.$nextTick(() => {
        if (this.$refs.configForm) {
          this.$refs.configForm.validateField('conditioners')
        }
      })
    },
    openSelector () {
      this.selectorVisible = true
      this.selectorKeyword = ''
      this.selectedRows = []
      this.selectorPagination.pageIndex = 1
      this.loadDevices()
    },
    loadDevices () {
      this.selectorLoading = true
      conditionerApi.fetchList({
        page: this.selectorPagination.pageIndex,
        capacity: this.selectorPagination.pageSize,
        model: this.selectorKeyword ? { devKeyword: this.selectorKeyword } : {}
      }).then(data => {
        const boundIds = new Set(this.form.conditioners.map(c => c.conditionerId))
        this.selectorList = (data.records || []).filter(r => !boundIds.has(r.id))
        this.selectorPagination.total = data.total || 0
      }).catch(e => this.$tip.apiFailed(e)).finally(() => { this.selectorLoading = false })
    },
    searchDevices () {
      this.selectorPagination.pageIndex = 1
      this.loadDevices()
    },
    confirmSelect () {
      if (!this.selectedRows.length) {
        this.$tip.warning('请选择内机')
        return
      }
      this.selectedRows.forEach(r => {
        this.form.conditioners.push({
          conditionerId: r.id,
          platformDevId: r.platformDevId,
          name: r.name,
          floorName: r.floorName,
          roomName: r.roomName,
          online: r.online,
          devRatio: 100
        })
      })
      this.selectorVisible = false
      this.$nextTick(() => {
        if (this.$refs.configForm) {
          this.$refs.configForm.validateField('conditioners')
        }
      })
    }
  }
}
</script>
 
<style scoped>
.conditioner-tab {
  padding-top: 4px;
}
.config-section {
  margin-bottom: 16px;
  padding: 16px;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.section-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  line-height: 22px;
}
.section-header .section-title {
  margin-bottom: 0;
}
.config-section > .section-title {
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #ebeef5;
}
.config-form {
  margin-bottom: 0;
}
.config-first-row {
  margin-bottom: 4px;
  padding-bottom: 4px;
  border-bottom: 1px dashed #ebeef5;
}
.config-form ::v-deep .stop-money-item {
  margin-bottom: 8px;
}
.stop-money-group {
  display: inline-flex;
  width: 100%;
  max-width: 280px;
  vertical-align: middle;
}
.stop-money-group ::v-deep .stop-money-input {
  flex: 1;
  width: auto;
}
.stop-money-group ::v-deep .el-input-number {
  width: 100%;
}
.stop-money-group ::v-deep .el-input__inner {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.config-form ::v-deep .el-form-item {
  margin-bottom: 12px;
}
.config-form ::v-deep .remark-item {
  margin-bottom: 0;
}
.config-form ::v-deep .remark-item .el-textarea {
  max-width: 100%;
}
.config-form ::v-deep .el-switch {
  width: auto;
}
.required-title::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}
.conditioners-form-item {
  margin-bottom: 0;
}
.conditioners-form-item ::v-deep .el-form-item__content {
  margin-left: 0 !important;
}
.conditioners-form-item ::v-deep .el-form-item__error {
  padding-top: 4px;
}
.device-table {
  width: 100%;
}
.footer-btns {
  text-align: right;
  padding-top: 4px;
}
.selector-form {
  margin-bottom: 12px;
}
.green { color: #67c23a; }
.red { color: #f56c6c; }
</style>