useFlightState.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { Query } from '@/api/webApi'
  2. import { Ref } from 'vue'
  3. import { CommonData, CommonValue } from '~/common'
  4. export function useFlightState(
  5. name: string,
  6. tableData: Ref<CommonData[]>,
  7. finishedCount: Ref<number>
  8. ) {
  9. const warningRules = ref<CommonData[]>([])
  10. const getWarningRules = async () => {
  11. try {
  12. const {
  13. code,
  14. returnData: { listValues },
  15. message,
  16. } = await Query<CommonData>({
  17. id: DATACONTENT_ID.warningRules,
  18. dataContent: [],
  19. })
  20. if (Number(code) !== 0) {
  21. throw new Error(message || '失败')
  22. }
  23. warningRules.value = listValues
  24. } catch (error) {
  25. console.error(error)
  26. }
  27. }
  28. watch(tableData, data => {
  29. finishedCount.value = 0
  30. const now = Date.now()
  31. data.forEach(row => {
  32. if (
  33. name.includes('DepartureAirport') &&
  34. typeof row['planDepartureTime'] === 'string' &&
  35. row['planDepartureTime']
  36. ) {
  37. const departureTime = new Date(
  38. row.planDepartureTime.replace('T', ' ')
  39. ).getTime()
  40. // 判断已起飞
  41. if (now >= departureTime) {
  42. row.hasTakenOff = 'Y'
  43. if (
  44. name.includes('International') &&
  45. row['enterPark'] &&
  46. row['receiveSure1']
  47. ) {
  48. finishedCount.value++
  49. }
  50. if (!name.includes('International') && row['receiveSure']) {
  51. finishedCount.value++
  52. }
  53. } else {
  54. row.hasTakenOff = 'N'
  55. // 未起飞的匹配预警报警策略
  56. row.warningState = getWarningState(row)
  57. }
  58. }
  59. if (
  60. name.includes('International') &&
  61. !(['CA', 'ZH', 'CZ', 'O3', '5X', 'FX', 'DO'] as CommonValue[]).includes(
  62. row.IATACode
  63. )
  64. ) {
  65. row.groundService = 'Y'
  66. }
  67. if (name === 'ArrivalAirport') {
  68. if (
  69. (['ZH', 'CA', 'KY', 'SC'] as CommonValue[]).includes(row.IATACode)
  70. ) {
  71. row.groundServiceSZ = 'Y'
  72. } else {
  73. row.groundService = 'Y'
  74. }
  75. }
  76. if (
  77. name.includes('ArrivalAirport') &&
  78. typeof row.planLandingTime === 'string'
  79. ) {
  80. const landingTime = new Date(
  81. row.planLandingTime.replace('T', ' ')
  82. ).getTime()
  83. // 判断已降落
  84. if (now >= landingTime) {
  85. row.hasLanded = 'Y'
  86. if (row['unLoad'] && row['tally']) {
  87. finishedCount.value++
  88. }
  89. } else {
  90. row.hasLanded = 'N'
  91. }
  92. }
  93. })
  94. })
  95. const getWarningState = (flightData: CommonData) => {
  96. let warningState = ''
  97. if (
  98. typeof flightData.planDepartureTime !== 'string' ||
  99. typeof flightData.loadPlaneTime !== 'string'
  100. ) {
  101. return warningState
  102. }
  103. const now = Date.now()
  104. const departureTime = new Date(
  105. flightData.planDepartureTime.replace('T', ' ')
  106. ).getTime()
  107. const loadTime = new Date(
  108. flightData.loadPlaneTime.replace('T', ' ')
  109. ).getTime()
  110. const duration = departureTime - loadTime
  111. warningRules.value.some(
  112. ({
  113. startDate,
  114. endDate,
  115. IATACode,
  116. flightNO,
  117. warningDuration,
  118. alarmDuration,
  119. }) => {
  120. let startTime = 0,
  121. endTime = 0
  122. if (typeof startDate === 'string') {
  123. startTime = new Date(startDate.replace('T', ' ')).getTime()
  124. }
  125. if (typeof endDate === 'string') {
  126. endTime = new Date(endDate.replace('T', ' ')).getTime()
  127. }
  128. // 排除不在生效期内或是航司、航班号不匹配的策略
  129. if ((startTime && startTime > now) || (endTime && endTime < now)) {
  130. return false
  131. }
  132. if (IATACode && IATACode !== flightData.IATACode) {
  133. return false
  134. }
  135. if (flightNO && flightNO !== flightData.flightNO) {
  136. return false
  137. }
  138. if (typeof alarmDuration === 'number' && duration < alarmDuration) {
  139. warningState = 'alarm'
  140. return true
  141. }
  142. if (typeof warningDuration === 'number' && duration < warningDuration) {
  143. warningState = 'warning'
  144. return true
  145. }
  146. return false
  147. }
  148. )
  149. return warningState
  150. }
  151. return {
  152. warningRules,
  153. getWarningRules,
  154. }
  155. }