GetRecordDetail.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const API = require("../../../lib/API")
  2. const db = require("../../../plugin/DataBase/db")
  3. const AccessControl = require("../../../lib/AccessControl")
  4. const { BaseStdResponse } = require("../../../BaseStdResponse")
  5. const { enrichLepaoRecordRow, stripInternalRecordId } = require("../../../plugin/jkes/formatRecordRow.js")
  6. class GetRecordDetail extends API {
  7. constructor() {
  8. super();
  9. this.setPath('/Lepao/GetRecordDetail')
  10. this.setMethod('GET')
  11. }
  12. async onRequest(req, res) {
  13. let {
  14. uuid,
  15. session,
  16. public_id
  17. } = req.query
  18. if ([uuid, session, public_id].some(value => value === '' || value === null || value === undefined))
  19. return res.json({
  20. ...BaseStdResponse.MISSING_PARAMETER
  21. })
  22. if (!await AccessControl.checkSession(uuid, session))
  23. return res.status(401).json({
  24. ...BaseStdResponse.ACCESS_DENIED
  25. })
  26. const baseWhere = `
  27. FROM
  28. lepao_record r
  29. LEFT JOIN
  30. lepao_account a
  31. ON
  32. r.lepao_account = a.student_num
  33. LEFT JOIN
  34. path_data p
  35. ON
  36. r.path_id = p.id
  37. WHERE
  38. (r.uuid = ? OR (a.create_user IS NOT NULL AND a.create_user = ?)) AND r.public_id = ?
  39. `
  40. let rows
  41. try {
  42. const sql = `
  43. SELECT
  44. r.id,
  45. r.public_id,
  46. r.time,
  47. r.result,
  48. r.state,
  49. r.run_mode,
  50. r.lepao_account,
  51. r.path_id,
  52. r.path_data AS run_path_data,
  53. a.name,
  54. p.data,
  55. p.run_zone_name,
  56. p.run_zone_name AS path_run_zone_name,
  57. NULL AS path_distance
  58. ${baseWhere}
  59. `
  60. rows = await db.query(sql, [uuid, uuid, public_id])
  61. } catch (e) {
  62. if (!(e?.message || '').includes("Unknown column 'r.path_data'")) throw e
  63. const sql = `
  64. SELECT
  65. r.id,
  66. r.public_id,
  67. r.time,
  68. r.result,
  69. r.state,
  70. r.run_mode,
  71. r.lepao_account,
  72. r.path_id,
  73. r.point_data AS run_path_data,
  74. a.name,
  75. p.data,
  76. p.run_zone_name,
  77. p.run_zone_name AS path_run_zone_name,
  78. NULL AS path_distance
  79. ${baseWhere}
  80. `
  81. rows = await db.query(sql, [uuid, uuid, public_id])
  82. }
  83. if (!rows)
  84. return res.json({
  85. ...BaseStdResponse.MISSING_FILE,
  86. msg: '获取记录数据失败!'
  87. })
  88. if(rows.length === 0)
  89. return res.json({
  90. ...BaseStdResponse.MISSING_FILE,
  91. msg: '记录不存在或已被删除!'
  92. })
  93. let data = rows[0]
  94. let pathLine = []
  95. const rawRunPath = data.run_path_data
  96. if (rawRunPath != null) {
  97. let arr = rawRunPath
  98. if (typeof rawRunPath === 'string') {
  99. try {
  100. arr = JSON.parse(rawRunPath)
  101. } catch {
  102. arr = null
  103. }
  104. }
  105. if (Array.isArray(arr)) {
  106. pathLine = arr.map((point) => [point.longitude ?? point.o, point.latitude ?? point.a])
  107. }
  108. }
  109. if (!pathLine.length) {
  110. const rawPath = data.data
  111. if (rawPath != null) {
  112. let arr = rawPath
  113. if (typeof rawPath === 'string') {
  114. try {
  115. arr = JSON.parse(rawPath)
  116. } catch {
  117. arr = null
  118. }
  119. }
  120. if (Array.isArray(arr)) {
  121. pathLine = arr.map((point) => [point.o, point.a])
  122. }
  123. }
  124. }
  125. data.path_polyline = pathLine
  126. delete data.data
  127. delete data.run_path_data
  128. const enriched = await enrichLepaoRecordRow({
  129. id: data.id,
  130. state: data.state,
  131. result: data.result
  132. })
  133. data.jkes_record = enriched.jkes_record
  134. if (enriched.lepao_schedule != null) {
  135. data.lepao_schedule = enriched.lepao_schedule
  136. }
  137. if (enriched.schedule != null) {
  138. data.schedule = enriched.schedule
  139. }
  140. res.json({
  141. ...BaseStdResponse.OK,
  142. data: stripInternalRecordId(data)
  143. })
  144. }
  145. }
  146. module.exports.GetRecordDetail = GetRecordDetail