123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import {sqlHandle} from "../config/db.js";
- import {isValue, paramToUnder} from "../../util/index.js";
- import PageResult from "../config/page.js";
- import TrackEntity from "../entity/track-entity.js";
- const TrackService = {
- list: ({sourceId, name, orderByColumn, isAsc}) => {
- return new Promise(async (resolve, reject) => {
- try {
- let sql = 'SELECT * FROM ship_test_track'
- let where = ['del_flag = 0']
- if (isValue(sourceId)) {
- where.push(`source_id = "${sourceId}"`)
- } else {
- return reject('数据源ID不可为空')
- }
- if (isValue(name)) {
- where.push(`name LIKE "%${name}%"`)
- }
- if (where.length > 0) {
- sql += ' ' + `WHERE ${where.join(' AND ')}`
- }
- if (isValue(orderByColumn)) {
- sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
- } else {
- sql += ' ' + `ORDER BY sort DESC`
- }
- const result = await sqlHandle(sql)
- return resolve(result.map(v => new TrackEntity(v)))
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- },
- page: ({sourceId, pageNum, pageSize, name, orderByColumn, isAsc}) => {
- return new Promise(async (resolve, reject) => {
- try {
- let sql = 'SELECT * FROM ship_test_track'
- let where = ['del_flag = 0']
- if (isValue(sourceId)) {
- where.push(`source_id = "${sourceId}"`)
- } else {
- return reject('数据源ID不可为空')
- }
- if (isValue(name)) {
- where.push(`name LIKE "%${name}%"`)
- }
- if (where.length > 0) {
- sql += ' ' + `WHERE ${where.join(' AND ')}`
- }
- if (isValue(orderByColumn)) {
- sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
- } else {
- sql += ' ' + `ORDER BY sort DESC`
- }
- const result = await PageResult(sql, pageNum, pageSize, TrackEntity)
- return resolve(result)
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- },
- info: (id) => {
- return new Promise(async (resolve, reject) => {
- try {
- let sql = 'SELECT * FROM ship_test_track'
- let where = ['del_flag = 0']
- if (!isValue(id)) {
- return reject('id不可为空')
- } else {
- where.push(`id = ${id}`)
- }
- if (where.length > 0) {
- sql += ' ' + `WHERE ${where.join(' AND ')}`
- }
- const result = await sqlHandle(sql)
- return resolve(result.map(v => new TrackEntity(v))?.[0])
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- },
- queryBy: ({name, sourceId}) => {
- return new Promise(async (resolve, reject) => {
- try {
- let sql = 'SELECT * FROM ship_test_track'
- let where = ['del_flag = 0']
- if (isValue(name)) {
- where.push(`name = "${name}"`)
- }
- if (isValue(sourceId)) {
- where.push(`source_id = "${sourceId}"`)
- }
- if (where.length > 0) {
- sql += ' ' + `WHERE ${where.join(' AND ')}`
- }
- const result = await sqlHandle(sql)
- return resolve(result.map(v => new TrackEntity(v)))
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- },
- add: (form) => {
- return new Promise(async (resolve, reject) => {
- try {
- const trackEntity = new TrackEntity(form)
- const values = [
- ['source_id', trackEntity.sourceId],
- ['track_key', trackEntity.trackKey],
- ['track_point_key', trackEntity.trackPointKey],
- ['track_wkt_key', trackEntity.trackWktKey],
- ['track_speed_key', trackEntity.trackSpeedKey],
- ['track_course_key', trackEntity.trackCourseKey],
- ['track_head_key', trackEntity.trackHeadKey],
- ['history_track_key', trackEntity.historyTrackKey],
- ['history_track_point_key', trackEntity.historyTrackPointKey],
- ['history_track_wkt_key', trackEntity.historyTrackWktKey],
- ['history_track_speed_key', trackEntity.historyTrackSpeedKey],
- ['history_track_course_key', trackEntity.historyTrackCourseKey],
- ['history_track_head_key', trackEntity.historyTrackHeadKey],
- ]
- const sql = `INSERT INTO ship_test_track (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
- if (!isValue(trackEntity.sourceId)) {
- return reject('数据源ID不可为空')
- }
- const result = await sqlHandle(sql, values.map(v => v[1]))
- return resolve(result.insertId)
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- },
- edit: (form) => {
- return new Promise(async (resolve, reject) => {
- try {
- const trackEntity = new TrackEntity(form)
- const values = [
- ['track_key', trackEntity.trackKey],
- ['track_point_key', trackEntity.trackPointKey],
- ['track_wkt_key', trackEntity.trackWktKey],
- ['track_speed_key', trackEntity.trackSpeedKey],
- ['track_course_key', trackEntity.trackCourseKey],
- ['track_head_key', trackEntity.trackHeadKey],
- ['history_track_key', trackEntity.historyTrackKey],
- ['history_track_point_key', trackEntity.historyTrackPointKey],
- ['history_track_wkt_key', trackEntity.historyTrackWktKey],
- ['history_track_speed_key', trackEntity.historyTrackSpeedKey],
- ['history_track_course_key', trackEntity.historyTrackCourseKey],
- ['history_track_head_key', trackEntity.historyTrackHeadKey],
- ]
- let sql = `UPDATE ship_test_track SET ${values.map(v => `${v[0]} = ?`).join(',')}`
- if (!isValue(trackEntity.id)) {
- return reject('id不可为空!')
- } else {
- sql += ' ' + `WHERE id = "${trackEntity.id}"`
- }
- const result = await sqlHandle(sql, values.map(v => v[1]))
- return resolve(trackEntity.id)
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- },
- del: (ids) => {
- return new Promise(async (resolve, reject) => {
- try {
- let sql = 'UPDATE ship_test_track SET del_flag = "1"'
- if (ids?.length === 0) {
- return reject('ids不可为空!')
- } else {
- sql += ' ' + 'WHERE id IN (?)'
- }
- const result = await sqlHandle(sql, [ids])
- return resolve('删除成功!')
- } catch (e) {
- return reject(e.sqlMessage)
- }
- })
- }
- }
- export default TrackService
|