yangfeng
2024-02-06 7edf66cafb871c5fb28911dfb02611ef5c9910b2
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
import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import type { Task, TasksGroupByChannel, TasksResponse } from '@/api/task'
import type { TaskListParams, TaskInfoParams } from '@/api'
import { getTaskList, getTaskInfo } from '@/api'
import { isNumber } from 'lodash-es'
import { getToken } from '@/common/utils/index'
 
export interface ChannelMoreBtnStatus {
  /** true 任务未加载完  false 所有任务已经加载完成*/
  [channel: number]: boolean
}
 
const token = getToken()
export const useTasksStore = defineStore('tasks', () => {
  const channels = ref<TasksGroupByChannel>({})
 
  const currentType = ref<1 | 2 | 3>(1)
 
  /**
   * 获取任务数据
   * @param type 1未完成2今天未完成3已完成
   * @param init 是否是首次获取,首次需要选中第一项任务
   */
  function getChannels(type: 1 | 2 | 3, init = false) {
    currentType.value = type
    const params: TaskListParams = {
      type,
      offset: 0,
      limit: 3,
      deviceID: localStorage.getItem('currentDeviceID') || ''
    }
 
    return getTaskList(params)
      .then((res) => {
        channels.value = res.data
      })
      .catch((err) => {
        console.error(err)
        channels.value = []
      })
      .finally(() => {
        if (init) {
          // 首次获取通道数据时自动选中第一个任务
          selectFirstTask(channels.value)
        }
      })
  }
 
  function selectFirstTask(channels: TasksGroupByChannel) {
    const firstNotEmptyChannel = Object.entries(channels).find((ele) => {
      const taskList = (ele[1] as TasksResponse)?.Tasks
 
      return !!taskList?.length
    })
 
    if (firstNotEmptyChannel && (token !== null || token !== '' || token !== 'undefined')) {
      const channelNumber = +firstNotEmptyChannel[0]
      // activeTask.value = channels[channelNumber].Tasks[0]
      console.log(channels[channelNumber].Tasks[0].Procedure.ID, '1111')
      const params: TaskInfoParams = {
        deviceID: localStorage.getItem('currentDeviceID') || '',
        procedureID: channels[channelNumber].Tasks[0].Procedure.ID
      }
      return getTaskInfo(params)
        .then((res) => {
          activeTask.value = res.data
        })
        .catch((err) => {
          console.error(err)
        })
        .finally(() => {})
    } else {
      // 如果没有任务就清空当前选中的任务
      activeTask.value = undefined
      if (channels[0]) {
        setActiveChannel(0)
      }
    }
  }
 
  /**
   * 数据加载完成后自动选中一个任务
   */
  function autoSelectTask(channel: number) {
    const currentChannelTaskList = channels.value[channel].Tasks
    if (currentChannelTaskList?.length && (token !== null || token !== '' || token !== 'undefined')) {
      // activeTask.value = currentChannelTaskList[0].Procedure.ID
      const params: TaskInfoParams = {
        deviceID: localStorage.getItem('currentDeviceID') || '',
        procedureID: currentChannelTaskList[0].Procedure.ID
      }
      return getTaskInfo(params)
        .then((res) => {
          activeTask.value = res.data
        })
        .catch((err) => {
          console.error(err)
        })
        .finally(() => {})
    } else {
      const firstNotEmptyChannel = Object.entries(channels.value).find((ele) => {
        const taskList = (ele[1] as TasksResponse)?.Tasks
 
        return !!taskList.length
      })
 
      if (firstNotEmptyChannel) {
        const channelNumber = +firstNotEmptyChannel[0]
        activeTask.value = channels.value[channelNumber].Tasks[0]
        setActiveChannel(channel)
      }
    }
  }
 
  /**
   * 刷新所有数据
   */
  function reloadChannel(channel: number) {
    if (token !== null || token !== '' || token !== 'undefined') {
      return getChannels(currentType.value).then(() => {
        autoSelectTask(channel)
      })
    } else {
      return
    }
  }
 
  function reloadAllData() {
    getChannels(currentType.value)
  }
 
  function moreChannelTasksBtn(channelNumber: number) {
    const taskLength = channels.value[channelNumber].Tasks?.length ?? 0
    const params: TaskListParams = {
      type: currentType.value,
      channel: channelNumber,
      offset: taskLength,
      limit: 10,
      deviceID: localStorage.getItem('currentDeviceID') || ''
    }
 
    if (token !== null || token !== '' || token !== 'undefined') {
      getTaskList(params)
        .then((res) => {
          const existTasks = channels.value![channelNumber].Tasks ?? []
          channels.value[channelNumber] = res.data[channelNumber] ?? {}
          channels.value[channelNumber].Tasks = channels.value[channelNumber].Tasks ?? []
          channels.value[channelNumber].Tasks = [...existTasks, ...channels.value[channelNumber].Tasks]
        })
        .catch((err) => {
          console.error(err)
        })
    }
  }
 
  function foldChannelTasksBtn(channelNumber: number) {
    const tasks = channels.value[channelNumber].Tasks ?? []
    channels.value[channelNumber].Tasks = tasks.slice(0, 3)
  }
 
  const moreBtnStatus = computed(() => {
    return Object.entries(channels.value).reduce((pre, currentValue) => {
      const channelNumber = +currentValue[0]
      const channelData = currentValue[1] as TasksResponse
      pre[channelNumber] = channelData.TaskCount > (channelData.Tasks?.length ?? 0)
      return pre
    }, {} as ChannelMoreBtnStatus)
  })
 
  /** 当前高亮的任务 */
  const activeTask = ref<Task>()
  function setActiveTask(task: Task | undefined) {
    activeTask.value = task
    if (isNumber(task?.Channel)) {
      setActiveChannel(task?.Channel as number)
    }
  }
 
  const activeChannel = ref<number>(0)
  function setActiveChannel(channelNumber: number) {
    activeChannel.value = channelNumber
  }
 
  return {
    channels,
    getChannels,
    moreBtnStatus,
    activeTask,
    reloadChannel,
    setActiveTask,
    moreChannelTasksBtn,
    foldChannelTasksBtn,
    reloadAllData,
    activeChannel,
    setActiveChannel
  }
})