track-service.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {sqlHandle} from "../config/db.js";
  2. import {isValue, paramToUnder} from "../../util/index.js";
  3. import PageResult from "../config/page.js";
  4. import TrackEntity from "../entity/track-entity.js";
  5. const TrackService = {
  6. list: ({sourceId, name, orderByColumn, isAsc}) => {
  7. return new Promise(async (resolve, reject) => {
  8. try {
  9. let sql = 'SELECT * FROM ship_test_track'
  10. let where = ['del_flag = 0']
  11. if (isValue(sourceId)) {
  12. where.push(`source_id = "${sourceId}"`)
  13. } else {
  14. return reject('数据源ID不可为空')
  15. }
  16. if (isValue(name)) {
  17. where.push(`name LIKE "%${name}%"`)
  18. }
  19. if (where.length > 0) {
  20. sql += ' ' + `WHERE ${where.join(' AND ')}`
  21. }
  22. if (isValue(orderByColumn)) {
  23. sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
  24. } else {
  25. sql += ' ' + `ORDER BY sort DESC`
  26. }
  27. const result = await sqlHandle(sql)
  28. return resolve(result.map(v => new TrackEntity(v)))
  29. } catch (e) {
  30. return reject(e.sqlMessage)
  31. }
  32. })
  33. },
  34. page: ({sourceId, pageNum, pageSize, name, orderByColumn, isAsc}) => {
  35. return new Promise(async (resolve, reject) => {
  36. try {
  37. let sql = 'SELECT * FROM ship_test_track'
  38. let where = ['del_flag = 0']
  39. if (isValue(sourceId)) {
  40. where.push(`source_id = "${sourceId}"`)
  41. } else {
  42. return reject('数据源ID不可为空')
  43. }
  44. if (isValue(name)) {
  45. where.push(`name LIKE "%${name}%"`)
  46. }
  47. if (where.length > 0) {
  48. sql += ' ' + `WHERE ${where.join(' AND ')}`
  49. }
  50. if (isValue(orderByColumn)) {
  51. sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
  52. } else {
  53. sql += ' ' + `ORDER BY sort DESC`
  54. }
  55. const result = await PageResult(sql, pageNum, pageSize, TrackEntity)
  56. return resolve(result)
  57. } catch (e) {
  58. return reject(e.sqlMessage)
  59. }
  60. })
  61. },
  62. info: (id) => {
  63. return new Promise(async (resolve, reject) => {
  64. try {
  65. let sql = 'SELECT * FROM ship_test_track'
  66. let where = ['del_flag = 0']
  67. if (!isValue(id)) {
  68. return reject('id不可为空')
  69. } else {
  70. where.push(`id = ${id}`)
  71. }
  72. if (where.length > 0) {
  73. sql += ' ' + `WHERE ${where.join(' AND ')}`
  74. }
  75. const result = await sqlHandle(sql)
  76. return resolve(result.map(v => new TrackEntity(v))?.[0])
  77. } catch (e) {
  78. return reject(e.sqlMessage)
  79. }
  80. })
  81. },
  82. queryBy: ({name, sourceId}) => {
  83. return new Promise(async (resolve, reject) => {
  84. try {
  85. let sql = 'SELECT * FROM ship_test_track'
  86. let where = ['del_flag = 0']
  87. if (isValue(name)) {
  88. where.push(`name = "${name}"`)
  89. }
  90. if (isValue(sourceId)) {
  91. where.push(`source_id = "${sourceId}"`)
  92. }
  93. if (where.length > 0) {
  94. sql += ' ' + `WHERE ${where.join(' AND ')}`
  95. }
  96. const result = await sqlHandle(sql)
  97. return resolve(result.map(v => new TrackEntity(v)))
  98. } catch (e) {
  99. return reject(e.sqlMessage)
  100. }
  101. })
  102. },
  103. add: (form) => {
  104. return new Promise(async (resolve, reject) => {
  105. try {
  106. const trackEntity = new TrackEntity(form)
  107. const values = [
  108. ['source_id', trackEntity.sourceId],
  109. ['track_key', trackEntity.trackKey],
  110. ['track_point_key', trackEntity.trackPointKey],
  111. ['track_wkt_key', trackEntity.trackWktKey],
  112. ['track_speed_key', trackEntity.trackSpeedKey],
  113. ['track_course_key', trackEntity.trackCourseKey],
  114. ['track_head_key', trackEntity.trackHeadKey],
  115. ['history_track_key', trackEntity.historyTrackKey],
  116. ['history_track_point_key', trackEntity.historyTrackPointKey],
  117. ['history_track_wkt_key', trackEntity.historyTrackWktKey],
  118. ['history_track_speed_key', trackEntity.historyTrackSpeedKey],
  119. ['history_track_course_key', trackEntity.historyTrackCourseKey],
  120. ['history_track_head_key', trackEntity.historyTrackHeadKey],
  121. ]
  122. const sql = `INSERT INTO ship_test_track (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
  123. if (!isValue(trackEntity.sourceId)) {
  124. return reject('数据源ID不可为空')
  125. }
  126. const result = await sqlHandle(sql, values.map(v => v[1]))
  127. return resolve(result.insertId)
  128. } catch (e) {
  129. return reject(e.sqlMessage)
  130. }
  131. })
  132. },
  133. edit: (form) => {
  134. return new Promise(async (resolve, reject) => {
  135. try {
  136. const trackEntity = new TrackEntity(form)
  137. const values = [
  138. ['track_key', trackEntity.trackKey],
  139. ['track_point_key', trackEntity.trackPointKey],
  140. ['track_wkt_key', trackEntity.trackWktKey],
  141. ['track_speed_key', trackEntity.trackSpeedKey],
  142. ['track_course_key', trackEntity.trackCourseKey],
  143. ['track_head_key', trackEntity.trackHeadKey],
  144. ['history_track_key', trackEntity.historyTrackKey],
  145. ['history_track_point_key', trackEntity.historyTrackPointKey],
  146. ['history_track_wkt_key', trackEntity.historyTrackWktKey],
  147. ['history_track_speed_key', trackEntity.historyTrackSpeedKey],
  148. ['history_track_course_key', trackEntity.historyTrackCourseKey],
  149. ['history_track_head_key', trackEntity.historyTrackHeadKey],
  150. ]
  151. let sql = `UPDATE ship_test_track SET ${values.map(v => `${v[0]} = ?`).join(',')}`
  152. if (!isValue(trackEntity.id)) {
  153. return reject('id不可为空!')
  154. } else {
  155. sql += ' ' + `WHERE id = "${trackEntity.id}"`
  156. }
  157. const result = await sqlHandle(sql, values.map(v => v[1]))
  158. return resolve(trackEntity.id)
  159. } catch (e) {
  160. return reject(e.sqlMessage)
  161. }
  162. })
  163. },
  164. del: (ids) => {
  165. return new Promise(async (resolve, reject) => {
  166. try {
  167. let sql = 'UPDATE ship_test_track SET del_flag = "1"'
  168. if (ids?.length === 0) {
  169. return reject('ids不可为空!')
  170. } else {
  171. sql += ' ' + 'WHERE id IN (?)'
  172. }
  173. const result = await sqlHandle(sql, [ids])
  174. return resolve('删除成功!')
  175. } catch (e) {
  176. return reject(e.sqlMessage)
  177. }
  178. })
  179. }
  180. }
  181. export default TrackService