task.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { defineStore } from 'pinia'
  2. import { ref, computed } from 'vue'
  3. import type { Task, TimelineRecord } from '../types'
  4. import { getTaskList, getMyTasks, getTaskDetail, advanceTaskStep, getTaskLogs } from '../api/task'
  5. export const useTaskStore = defineStore('task', () => {
  6. // State
  7. const tasks = ref<Task[]>([])
  8. const currentTask = ref<Task | null>(null)
  9. const filterStatus = ref<string>('all')
  10. const loading = ref(false)
  11. const total = ref(0)
  12. // Getters
  13. const filteredTasks = computed(() => {
  14. if (filterStatus.value === 'all') return tasks.value
  15. return tasks.value.filter(t => t.status === filterStatus.value)
  16. })
  17. const pendingCount = computed(() => tasks.value.filter(t => t.status === 'pending').length)
  18. const confirmedCount = computed(() => tasks.value.filter(t => t.status === 'confirmed').length)
  19. const scheduledCount = computed(() => tasks.value.filter(t => t.status === 'scheduled').length)
  20. const todayCount = computed(() => tasks.value.filter(t => ['scheduled', 'departed', 'constructing'].includes(t.status)).length)
  21. const processingCount = computed(() => todayCount.value)
  22. const completedCount = computed(() => tasks.value.filter(t => t.status === 'completed').length)
  23. const rejectedCount = computed(() => tasks.value.filter(t => t.status === 'rejected').length)
  24. const emergencyCount = computed(() => tasks.value.filter(t => t.type === 'emergency').length)
  25. const departedCount = computed(() => tasks.value.filter(t => t.status === 'departed').length)
  26. const arrivedCount = computed(() => tasks.value.filter(t => t.status === 'arrived').length)
  27. const inspectingCount = computed(() => tasks.value.filter(t => t.status === 'inspecting').length)
  28. const tomorrowCount = ref(2)
  29. const historyCount = computed(() => completedCount.value)
  30. // Actions
  31. async function fetchTasks(pageNum = 1, pageSize = 10) {
  32. loading.value = true
  33. try {
  34. const res = await getTaskList(pageNum, pageSize)
  35. tasks.value = res.rows || []
  36. total.value = res.total || 0
  37. } finally {
  38. loading.value = false
  39. }
  40. }
  41. async function fetchMyTasks(planDate?: string) {
  42. loading.value = true
  43. try {
  44. const res = await getMyTasks(planDate)
  45. tasks.value = (res || []) as any
  46. total.value = tasks.value.length
  47. } finally {
  48. loading.value = false
  49. }
  50. }
  51. async function fetchTaskDetail(taskId: number) {
  52. loading.value = true
  53. try {
  54. const res = await getTaskDetail(taskId)
  55. // 转换日志为时间线
  56. if (res.logList) {
  57. res.timeline = res.logList.map((log: any) => ({
  58. time: log.createTime,
  59. action: log.action,
  60. operator: log.operatorName,
  61. role: log.operatorRole || 'dispatch',
  62. }))
  63. }
  64. currentTask.value = res
  65. } finally {
  66. loading.value = false
  67. }
  68. }
  69. function setFilter(status: string) {
  70. filterStatus.value = status
  71. }
  72. function setCurrentTask(taskId: string) {
  73. currentTask.value = tasks.value.find(t => String((t as any).id ?? (t as any).taskId) === String(taskId)) || null
  74. }
  75. async function advanceStep(taskId: number, step: number) {
  76. await advanceTaskStep(taskId, step)
  77. // 重新获取任务详情
  78. await fetchTaskDetail(taskId)
  79. }
  80. return {
  81. tasks,
  82. currentTask,
  83. filterStatus,
  84. loading,
  85. total,
  86. filteredTasks,
  87. pendingCount,
  88. confirmedCount,
  89. scheduledCount,
  90. todayCount,
  91. processingCount,
  92. completedCount,
  93. rejectedCount,
  94. emergencyCount,
  95. departedCount,
  96. arrivedCount,
  97. inspectingCount,
  98. tomorrowCount,
  99. historyCount,
  100. fetchTasks,
  101. fetchMyTasks,
  102. fetchTaskDetail,
  103. setFilter,
  104. setCurrentTask,
  105. advanceStep,
  106. }
  107. })