GetRecordDetail.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 AdminGetRecordDetail extends API {
  7. constructor() {
  8. super();
  9. this.setPath('/Admin/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. let permission = await AccessControl.getPermission(uuid)
  27. if (!permission.includes("admin") && !permission.includes("service"))
  28. return res.json({
  29. ...BaseStdResponse.PERMISSION_DENIED
  30. })
  31. const baseWhere = `
  32. FROM
  33. lepao_record r
  34. LEFT JOIN
  35. lepao_account a
  36. ON
  37. r.lepao_account = a.student_num
  38. LEFT JOIN
  39. path_data p
  40. ON
  41. r.path_id = p.id
  42. WHERE
  43. r.public_id = ?
  44. `
  45. let rows
  46. try {
  47. const sql = `
  48. SELECT
  49. r.id,
  50. r.public_id,
  51. r.time,
  52. r.result,
  53. r.state,
  54. r.run_mode,
  55. r.lepao_account,
  56. r.path_id,
  57. r.path_data AS run_path_data,
  58. a.name,
  59. p.data,
  60. p.run_zone_name,
  61. p.run_zone_name AS path_run_zone_name,
  62. NULL AS path_distance
  63. ${baseWhere}
  64. `
  65. rows = await db.query(sql, [public_id])
  66. } catch (e) {
  67. if (!(e?.message || '').includes("Unknown column 'r.path_data'")) throw e
  68. const sql = `
  69. SELECT
  70. r.id,
  71. r.public_id,
  72. r.time,
  73. r.result,
  74. r.state,
  75. r.run_mode,
  76. r.lepao_account,
  77. r.path_id,
  78. r.point_data AS run_path_data,
  79. a.name,
  80. p.data,
  81. p.run_zone_name,
  82. p.run_zone_name AS path_run_zone_name,
  83. NULL AS path_distance
  84. ${baseWhere}
  85. `
  86. rows = await db.query(sql, [public_id])
  87. }
  88. if (!rows)
  89. return res.json({
  90. ...BaseStdResponse.MISSING_FILE,
  91. msg: '获取记录数据失败!'
  92. })
  93. if(rows.length === 0)
  94. return res.json({
  95. ...BaseStdResponse.MISSING_FILE,
  96. msg: '记录不存在或已被删除!'
  97. })
  98. let data = rows[0]
  99. let pathLine = []
  100. const rawRunPath = data.run_path_data
  101. if (rawRunPath != null) {
  102. let arr = rawRunPath
  103. if (typeof rawRunPath === 'string') {
  104. try {
  105. arr = JSON.parse(rawRunPath)
  106. } catch {
  107. arr = null
  108. }
  109. }
  110. if (Array.isArray(arr)) {
  111. pathLine = arr.map((point) => [point.longitude ?? point.o, point.latitude ?? point.a])
  112. }
  113. }
  114. if (!pathLine.length) {
  115. const rawPath = data.data
  116. if (rawPath != null) {
  117. let arr = rawPath
  118. if (typeof rawPath === 'string') {
  119. try {
  120. arr = JSON.parse(rawPath)
  121. } catch {
  122. arr = null
  123. }
  124. }
  125. if (Array.isArray(arr)) {
  126. pathLine = arr.map((point) => [point.o, point.a])
  127. }
  128. }
  129. }
  130. data.path_polyline = pathLine
  131. delete data.data
  132. delete data.run_path_data
  133. const enriched = await enrichLepaoRecordRow({
  134. id: data.id,
  135. state: data.state,
  136. result: data.result
  137. })
  138. data.jkes_record = enriched.jkes_record
  139. if (enriched.lepao_schedule != null) {
  140. data.lepao_schedule = enriched.lepao_schedule
  141. }
  142. if (enriched.schedule != null) {
  143. data.schedule = enriched.schedule
  144. }
  145. res.json({
  146. ...BaseStdResponse.OK,
  147. data: stripInternalRecordId(data)
  148. })
  149. }
  150. }
  151. module.exports.AdminGetRecordDetail = AdminGetRecordDetail