longyvfengyun
2023-11-16 b537c7bccbcc20644902ad6f0e309bbe9a6eda24
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
import {ref} from "vue"
import {searchStatisticCreate, searchStatisticSum} from "@/views/moudle/carInfo/apis";
import getRecentDay from "@/util/getRecentDay";
 
/**
 * 最近几天在线车辆数
 * @return {{todayCarNum: Ref<UnwrapRef<number>>, getRecentDaysCarNum: (function(*): Promise<*[]>)}}
 */
export const recentDaysCarModule = ()=>{
  const todayCarNum = ref(0);
  /**
   * 查询最近几天车辆在线数
   * @param num 天数
   * @return {Promise<*[]>}
   */
  const getRecentDaysCarNum = async (num)=>{
    try {
      const res = await searchStatisticCreate();
      const rs = res.data;
      let data = [];
      if(rs.code === 1) {
        data = rs.data;
      }
      let recentDays = getRecentDay(num);
      for(let i=0; i<recentDays.length; i++) {
        let itemDay = recentDays[i];
        for(let j=0; j<data.length; j++) {
          let item = data[j];
          if(itemDay.date === item.date) {
            itemDay.value = item.num;
            break;
          }
        }
      }
      // 设置当天的在线数
      if(recentDays.length !== 0) {
        todayCarNum.value = recentDays[recentDays.length -1].value;
      }
      return recentDays;
    }catch (e) {
      console.log(e);
      return []
    }
  }
 
  return {
    todayCarNum,
    getRecentDaysCarNum
  }
 
}
 
/**
 * 车辆总数
 * @return {{getTotalCarNum: (function(): Promise<number>), totalCarNum: Ref<UnwrapRef<number>>}}
 */
export const totalCarNumModule = ()=>{
  const totalCarNum = ref(0);
 
  /**
   * 获取车辆的总数
   * @return {Promise<number>}
   */
  const getTotalCarNum = async ()=>{
    try {
      const res = await searchStatisticSum();
      const rs = res.data;
      let num = 0;
      if(rs.code === 1) {
        num = rs.data;
      }
      totalCarNum.value = num;
      return num;
    }catch (e) {
      console.log(e);
      return 0;
    }
  }
 
  return {
    totalCarNum,
    getTotalCarNum
  };
}