Riku
2024-08-12 7c3c82d429f86358142adceb080e8922f6a18aa0
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
import dayjs from 'dayjs';
import {
  fetchSubtask,
  fetchSubtaskSummary,
  fetchProblems,
} from '../../../services/inspection/fetchInspection';
import { newBehavior } from '../../../behaviors/picker/basePicker';
 
const PICKER_NAME = 'subtask';
const useSubtaskPicker = newBehavior(PICKER_NAME, []);
 
/**
 * 巡查监管任务详情
 */
export const useSubtask = Behavior({
  behaviors: [useSubtaskPicker],
  data: {
    subTask: {},
    problemList: [],
    problemLoading: false,
  },
  methods: {
    fetchSubtaskSummary() {
      const { scene, year, startTime, endTime } = this.data;
      const sceneId = scene.guid;
      return fetchSubtaskSummary({ sceneId, startTime, endTime }).then(res => {
        this.setData({ subtaskSummary: res });
        if (res.length > 0) {
          this.refreshSubtaskPicker();
        }
        this.refreshChart();
      });
    },
 
    fetchSubtask(subtaskId) {
      return fetchSubtask(subtaskId).then(res => {
        this.setData({ subTask: res });
      });
    },
 
    fetchProblems(subtaskId) {
      this.setData({ problemLoading: true });
      return fetchProblems(subtaskId, true)
        .then(res => {
          this.setData({ problemList: res });
        })
        .finally(() => this.setData({ problemLoading: false }));
    },
 
    /**
     * 刷新巡查任务选择器
     */
    refreshSubtaskPicker() {
      const { time, subtaskSummary } = this.data;
      const t1 = dayjs(time);
      let index = 0;
      let maxDay;
      const optionList = subtaskSummary.map((item, i) => {
        // 筛选与传入的时间相同月份中最新的一条巡查任务,作为默认展示的任务
        const t2 = dayjs(item.stPlanTime);
        if (t1.month() == t2.month()) {
          if (maxDay) {
            if (maxDay.isBefore(t2)) {
              maxDay = t2;
              index = i;
            }
          } else {
            maxDay = t2;
            index = i;
          }
        }
        return {
          label: t2.format('MM月DD日'),
          value: item.stGuid,
        };
      });
      this.setSelectOptions(PICKER_NAME, optionList, index);
      if (optionList.length > 0) {
        const subtaskId = optionList[index].value;
        this.fetchSubtask(subtaskId);
        this.fetchProblems(subtaskId);
      }
    },
 
    _confirm(e) {
      const { subtaskText, subtaskValue } = this.data;
      this.fetchSubtask(subtaskValue[0]);
      this.fetchProblems(subtaskValue[0]);
    },
  },
});