quanyawei
2024-02-05 087691a24c27cebbe114ea1f82856280400a3138
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<template>
  <div class="main">
    <div>
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="选择组织:">
          <el-select
            v-model="formInline.organizationId"
            placeholder="选择组织"
          >
            <el-option
              v-for="(item,index) in orgData"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-radio-group
            v-model="time"
          >
            <el-radio-button label="今天" />
            <el-radio-button label="近1天" />
            <el-radio-button label="近3天" />
            <el-radio-button label="近5天" />
            <el-radio-button label="近7天" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="其他时间:">
          <el-date-picker
            v-model="selectTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            :clearable="false"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-date="[selectTime[0], selectTime[1]]"
            :picker-options="pickerOptions"
            @change="changeDay()"
          />
        </el-form-item>
        <el-form-item label="小时状态:">
          <el-select
            v-model="formInline.hourState"
            clearable
            placeholder="选择状态"
          >
            <el-option
              label="在线"
              value="1"
            />
            <el-option
              label="离线"
              value="0"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSearch"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <p>实时在线率:共<span class="greenClor">{{ macOnlineData.sum }}</span>台,在线<span class="greenClor">{{ macOnlineData.online }}</span>台,在线率 <span :class="Number(macOnlineData.onlineRate)<90 ?'redClor':'greenClor'">{{ macOnlineData.onlineRate }}%</span></p>
      <div>
        <el-table
          v-loading="loading"
          :data="tableData"
          max-height="700"
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="序号"
            width="60"
            align="center"
            label-class-name="itemSpan"
          />
          <el-table-column
            prop="mac"
            label="mac号"
            align="center"
            label-class-name="itemSpan"
          />
          <el-table-column
            prop="name"
            label="设备名称"
            align="center"
            label-class-name="itemSpan"
          >
            <template #default="scope">
              <el-button
                type="text"
                @click="handleDetail(scope.row)"
              >
                {{ scope.row.name }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="endTime"
            label="最近一次掉线时间"
            align="center"
            label-class-name="itemSpan"
          />
          <el-table-column
            prop="startTime"
            label="最近一次上线时间"
            align="center"
            label-class-name="itemSpan"
          />
          <el-table-column
            prop="num"
            label="掉线次数"
            width="90px"
            label-class-name="itemSpan"
            align="center"
          />
          <el-table-column
            prop="onlineRate"
            label-class-name="itemSpan"
            label="在线率"
            width="100px"
            align="center"
          >
            <template slot-scope="scope">
              <span :class=" scope.row.onlineRate<90 ?'redClor':'greenClor'">{{ scope.row.onlineRate }}%</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="onlineTime"
            label-class-name="itemSpan"
            label="当前在线时长"
            width="120px"
            align="center"
          />
          <el-table-column
            prop="hourState"
            label-class-name="itemSpan"
            label="小时状态"
            width="90px"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.hourState === '0'"
                style="color:red;font-size:15px"
                type="text"
              >
                离线
              </el-button>
              <el-button
                v-else
                style="color:#67c23a;font-size:15px"
                type="text"
              >
                在线
              </el-button>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="address"
            align="center"
            width="80px"
            label-class-name="itemSpan"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                style="font-size:15px"
                @click="handleDetail(scope.row)"
              >
                详情
              </el-button>
            </template>
          </el-table-column> -->
        </el-table>
      </div>
    </div>
    <detailBox
      v-if="handleShow"
      :visible.sync="handleShow"
      :mac-date="macDate"
    />
  </div>
</template>
 
<script>
import dayjs from 'dayjs'
import detailBox from '@/views/onlineRate/detailBox'
import _ from 'lodash'
export default {
  components: {
    detailBox
  },
  data () {
    return {
      formInline: {
        organizationId: '',
        hourState: '',
        state: '',
        startTime: '',
        endTime: ''
      },
      time: '今天',
      selectTime: [],
      macDate: {},
      macOnlineData: {},
      orgData: [],
      tableData: [],
      handleShow: false,
      loading: false,
      choiceDate: null,
 
      // 重点
 
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          // 把选择的第一个日期赋值给一个变量。
          this.choiceDate = minDate.getTime()
          // 如何你选择了两个日期了,就把那个变量置空
          if (maxDate) this.choiceDate = ''
        },
        disabledDate: time => {
          // 如何选择了一个日期
          if (this.choiceDate) {
            // 7天的时间戳
            const one = 6 * 24 * 3600 * 1000
            // 当前日期 - one = 6天之前
            const minTime = this.choiceDate - one
            // 当前日期 + one = 6天之后
            const maxTime = this.choiceDate + one
            return (
              time.getTime() < minTime ||
              time.getTime() > maxTime ||
              // 限制不能选择今天及以后
              time.getTime() > Date.now()
            )
          } else {
            // 如果没有选择日期,就要限制不能选择今天及以后
            return time.getTime() > Date.now()
          }
        }
      }
    }
  },
  computed: {
    onlineData () {
      return this.tableData.filter(item => item.state !== '0').length || 0
    },
    onlineRate () {
      return (this.onlineData / this.tableData.length) * 100 || 0
    }
  },
  watch: {
    time (newValue, oldValue) {
      let now = dayjs() // 获取当前时间
      if (newValue === '近1天') {
        this.$set(this.selectTime, 0, new Date(now.subtract(1, 'day').format('YYYY-MM-DD  HH:mm')))
        this.$set(this.selectTime, 1, new Date(now.format('YYYY-MM-DD  HH:mm')))
        this.formInline.endTime = now.subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        this.formInline.startTime = now.subtract(1, 'day').format('YYYY-MM-DD  HH:mm') // 获取昨天的零点
      } else if (newValue === '近3天') {
        this.$set(this.selectTime, 0, new Date(now.subtract(3, 'day').format('YYYY-MM-DD  HH:mm')))
        this.$set(this.selectTime, 1, new Date(now.format('YYYY-MM-DD  HH:mm')))
        this.formInline.endTime = now.subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        this.formInline.startTime = now.subtract(3, 'day').format('YYYY-MM-DD  HH:mm') // 获取前天的零点
      } else if (newValue === '近5天') {
        this.$set(this.selectTime, 0, new Date(now.subtract(5, 'day').format('YYYY-MM-DD  HH:mm')))
        this.$set(this.selectTime, 1, new Date(now.format('YYYY-MM-DD  HH:mm')))
        this.formInline.endTime = now.subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        this.formInline.startTime = now.subtract(5, 'day').format('YYYY-MM-DD  HH:mm') // 获取前天的零点
      } else if (newValue === '近7天') {
        this.$set(this.selectTime, 0, new Date(now.subtract(7, 'day').format('YYYY-MM-DD  HH:mm')))
        this.$set(this.selectTime, 1, new Date(now.format('YYYY-MM-DD  HH:mm')))
        this.formInline.endTime = now.subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        this.formInline.startTime = now.subtract(7, 'day').format('YYYY-MM-DD  HH:mm') // 获取前天的零点
      } else if (newValue === '今天') {
        this.$set(this.selectTime, 0, new Date(dayjs().startOf('day').format('YYYY-MM-DD  HH:mm')))
        this.$set(this.selectTime, 1, new Date(now.format('YYYY-MM-DD  HH:mm')))
        this.formInline.endTime = now.subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        console.log('000', dayjs().startOf('day').format('YYYY-MM-DD  HH:mm'))
        this.formInline.startTime = dayjs().startOf('day').format('YYYY-MM-DD  HH:mm')
      }
    }
  },
  mounted () {
    this.$set(this.selectTime, 0, new Date(dayjs().startOf('day').format('YYYY-MM-DD  HH:mm')))
    this.$set(this.selectTime, 1, new Date(dayjs().format('YYYY-MM-DD  HH:mm')))
  },
  created () {
    this.getOrganizationId()
  },
  methods: {
    changeDay () {
      console.log('this.selectTime', this.selectTime)
      if (this.selectTime !== null) {
        console.log(this.selectTime)
        this.formInline.startTime = this.selectTime[0]
 
        if (dayjs(this.selectTime[1]).format('YYYY-MM-DD HH') === dayjs().format('YYYY-MM-DD HH')) {
          this.formInline.endTime = dayjs(this.selectTime[1]).subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        } else {
          this.formInline.endTime = dayjs(this.selectTime[1]).startOf('day').format('YYYY-MM-DD  HH:mm')
        }
      }
      console.log('this.formInline12', this.formInline)
      this.time = ''
    },
    handleDetail (row) {
      this.handleShow = true
      this.macDate = _.cloneDeep(row)
      this.macDate.startTime = this.selectTime[0]
      this.macDate.endTime = this.selectTime[1]
      console.log(this.macDate)
    },
    handleSearch () {
      this.getOnlineData()
      console.log(' this.formInline', this.formInline)
      if (this.formInline.organizationId !== '') {
        if (this.time === '今天') {
          console.log('今天今天')
          this.formInline.startTime = dayjs().startOf('day').format('YYYY-MM-DD  HH:mm')
          this.formInline.endTime = dayjs().subtract(1, 'hour').format('YYYY-MM-DD  HH:mm')
        }
        this.loading = true
        this.$request({
          url: '/onlineRate/page',
          method: 'post',
          data: {
            ...this.formInline
          }
        })
          .then(res => {
            this.tableData = res.data || []
            this.loading = false
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.$message.warning('请选择组织或者时间')
      }
    },
    getOnlineData () {
      this.$request({
        url: '/onlineRate/state',
        method: 'get',
        params: {
          organizationId: this.formInline.organizationId
        }
      })
        .then(res => {
          this.macOnlineData = res.data
        })
        .catch(err => {
          console.log(err)
        })
    },
    getOrganizationId () {
      this.$request({
        url: '/organization/getOrganizationId',
        method: 'get'
      })
        .then(res => {
          this.orgData = res.data
        })
        .catch(err => {
          console.log(err)
        })
    },
  },
}
</script>
 
<style lang="scss" scoped>
.main{
  padding: 10px;
  font-size: 18px;
}
/deep/.el-form-item__label{
  font-size: 16px;
}
/deep/.itemSpan {
  font-size: 15px;
  color: rgba(16, 16, 16, 1);
}
 /deep/.cell{
  font-size: 15px;
}
.redClor{
  color: red;
}
.greenClor{
  color:#67c23a;
}
</style>>