GetRecordDetail.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 } = 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. id
  17. } = req.query
  18. if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
  19. return res.json({
  20. ...BaseStdResponse.MISSING_PARAMETER
  21. })
  22. // 检查 session
  23. if (!await AccessControl.checkSession(uuid, session))
  24. return res.status(401).json({
  25. ...BaseStdResponse.ACCESS_DENIED
  26. })
  27. const baseWhere = `
  28. FROM
  29. lepao_record r
  30. LEFT JOIN
  31. lepao_account a
  32. ON
  33. r.lepao_account = a.student_num
  34. LEFT JOIN
  35. path_data p
  36. ON
  37. r.path_id = p.id
  38. WHERE
  39. (r.uuid = ? OR (a.create_user IS NOT NULL AND a.create_user = ?)) AND r.id = ?
  40. `
  41. let rows
  42. try {
  43. const sql = `
  44. SELECT
  45. r.time,
  46. r.result,
  47. r.state,
  48. r.lepao_account,
  49. r.path_id,
  50. r.path_data AS run_path_data,
  51. a.name,
  52. p.data,
  53. p.run_zone_name,
  54. p.run_zone_name AS path_run_zone_name,
  55. NULL AS path_distance
  56. ${baseWhere}
  57. `
  58. rows = await db.query(sql, [uuid, uuid, id])
  59. } catch (e) {
  60. if (!(e?.message || '').includes("Unknown column 'r.path_data'")) throw e
  61. const sql = `
  62. SELECT
  63. r.time,
  64. r.result,
  65. r.state,
  66. r.lepao_account,
  67. r.path_id,
  68. r.point_data AS run_path_data,
  69. a.name,
  70. p.data,
  71. p.run_zone_name,
  72. p.run_zone_name AS path_run_zone_name,
  73. NULL AS path_distance
  74. ${baseWhere}
  75. `
  76. rows = await db.query(sql, [uuid, uuid, id])
  77. }
  78. if (!rows)
  79. return res.json({
  80. ...BaseStdResponse.MISSING_FILE,
  81. msg: '获取记录数据失败!'
  82. })
  83. if(rows.length === 0)
  84. return res.json({
  85. ...BaseStdResponse.MISSING_FILE,
  86. msg: '记录不存在或已被删除!'
  87. })
  88. let data = rows[0]
  89. let pathLine = []
  90. const rawRunPath = data.run_path_data
  91. if (rawRunPath != null) {
  92. let arr = rawRunPath
  93. if (typeof rawRunPath === 'string') {
  94. try {
  95. arr = JSON.parse(rawRunPath)
  96. } catch {
  97. arr = null
  98. }
  99. }
  100. if (Array.isArray(arr)) {
  101. pathLine = arr.map((point) => [point.longitude ?? point.o, point.latitude ?? point.a])
  102. }
  103. }
  104. if (!pathLine.length) {
  105. const rawPath = data.data
  106. if (rawPath != null) {
  107. let arr = rawPath
  108. if (typeof rawPath === 'string') {
  109. try {
  110. arr = JSON.parse(rawPath)
  111. } catch {
  112. arr = null
  113. }
  114. }
  115. if (Array.isArray(arr)) {
  116. pathLine = arr.map((point) => [point.o, point.a])
  117. }
  118. }
  119. }
  120. data.path_polyline = pathLine
  121. delete data.data
  122. delete data.run_path_data
  123. data.jkes_record = enrichLepaoRecordRow({ result: data.result }).jkes_record
  124. res.json({
  125. ...BaseStdResponse.OK,
  126. data
  127. })
  128. }
  129. }
  130. module.exports.GetRecordDetail = GetRecordDetail