riku
2024-11-13 ab70c6eb4a181b282af0eb200275cd8a4d2ab172
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
// pages/mService/cPracticaloperation/index.js
import scheduleservice from '../../../service/scheduleservice';
import $f from '../../../service/baserequest';
import moment from '../../../utils/moment.min';
 
Component({
  properties: {},
 
  data: {
    icon_calendar: $f.baseIconUrl + 'res/calendar.jpg',
    // 实操事务
    operations: [],
    alert: '',
  },
 
  lifetimes: {
    attached: function () {
      this.getOperations();
    },
  },
 
  methods: {
    // 获取用户实操事务
    getOperations() {
      const that = this;
      scheduleservice.getOperationRecords({
        success(res) {
          const operations = res.data;
          operations.forEach(o => {
            if (o.record) {
              o.state = o.record.prStateId;
              o.record.time = moment(o.record.prTime).format(
                'YYYY年MM月DD日 HH:mm:ss',
              );
            } else {
              o.state = o.operation.poStateRangeId.split(';')[0];
            }
          });
          that.setData({ operations });
          that.refreshAlertTxt();
        },
        fail(e) {
          console.log(e);
        },
      });
    },
 
    handleSwitchChange(e) {
      const that = this;
      const { index } = e.currentTarget.dataset;
      const s = this.data.operations[index];
      const newState = this.nextState(s.operation, s.state);
      scheduleservice.doOperations(
        { operationId: s.operation.poId, stateId: newState },
        {
          success(res) {
            that.setData({
              [`operations[${index}].state`]: res.data.prStateId,
              [`operations[${index}].record.time`]: moment(
                res.data.prTime,
              ).format('YYYY年MM月DD日 HH:mm:ss'),
            });
            that.refreshAlertTxt();
          },
          fail(e) {},
        },
      );
    },
 
    // 设备运行切换至下一个状态
    nextState(operation, state) {
      const stateRange = operation.poStateRangeId.split(';');
      let index = stateRange.indexOf(state);
      index++;
      if (index >= stateRange.length) {
        index = 0;
      }
      return stateRange[index];
    },
 
    refreshAlertTxt() {
      let alertRange;
      let maxState;
      this.data.operations.forEach(o => {
        if (alertRange == undefined) {
          alertRange = o.operation.poStateRemindRange.split(';');
        }
        if (maxState) {
          if (o.state > maxState) {
            maxState = o.state;
          }
        } else {
          maxState = o.state;
        }
      });
      if (alertRange && maxState) {
        this.setData({
          alert: alertRange[maxState],
        });
      }
    },
  },
});