source-service.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {sqlHandle} from "../config/db.js";
  2. import {isValue, paramToUnder} from "../../util/index.js";
  3. import PageResult from "../config/page.js";
  4. import SourceEntity from "../entity/source-entity.js";
  5. const SourceService = {
  6. list: ({name, orderByColumn, isAsc}) => {
  7. return new Promise(async (resolve, reject) => {
  8. try {
  9. let sql = 'SELECT * FROM ship_test_source'
  10. let where = ['del_flag = 0']
  11. if (isValue(name)) {
  12. where.push(`name LIKE "%${name}%"`)
  13. }
  14. if (where.length > 0) {
  15. sql += ' ' + `WHERE ${where.join(' AND ')}`
  16. }
  17. if (isValue(orderByColumn)) {
  18. sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
  19. } else {
  20. sql += ' ' + `ORDER BY sort DESC`
  21. }
  22. const result = await sqlHandle(sql)
  23. return resolve(result.map(v => new SourceEntity(v)))
  24. } catch (e) {
  25. return reject(e.sqlMessage)
  26. }
  27. })
  28. },
  29. page: ({pageNum, pageSize, name, orderByColumn, isAsc}) => {
  30. return new Promise(async (resolve, reject) => {
  31. try {
  32. let sql = 'SELECT * FROM ship_test_source'
  33. let where = ['del_flag = 0']
  34. if (isValue(name)) {
  35. where.push(`name LIKE "%${name}%"`)
  36. }
  37. if (where.length > 0) {
  38. sql += ' ' + `WHERE ${where.join(' AND ')}`
  39. }
  40. if (isValue(orderByColumn)) {
  41. sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
  42. } else {
  43. sql += ' ' + `ORDER BY sort DESC`
  44. }
  45. const result = await PageResult(sql, pageNum, pageSize, SourceEntity)
  46. return resolve(result)
  47. } catch (e) {
  48. return reject(e.sqlMessage)
  49. }
  50. })
  51. },
  52. info: (id) => {
  53. return new Promise(async (resolve, reject) => {
  54. try {
  55. let sql = 'SELECT * FROM ship_test_source'
  56. let where = ['del_flag = 0']
  57. if (!isValue(id)) {
  58. return reject('id不可为空')
  59. } else {
  60. where.push(`id = ${id}`)
  61. }
  62. if (where.length > 0) {
  63. sql += ' ' + `WHERE ${where.join(' AND ')}`
  64. }
  65. const result = await sqlHandle(sql)
  66. return resolve(result.map(v => new SourceEntity(v))?.[0])
  67. } catch (e) {
  68. return reject(e.sqlMessage)
  69. }
  70. })
  71. },
  72. queryBy: ({name}) => {
  73. return new Promise(async (resolve, reject) => {
  74. try {
  75. let sql = 'SELECT * FROM ship_test_source'
  76. let where = ['del_flag = 0']
  77. if (isValue(name)) {
  78. where.push(`name = "${name}"`)
  79. }
  80. if (where.length > 0) {
  81. sql += ' ' + `WHERE ${where.join(' AND ')}`
  82. }
  83. const result = await sqlHandle(sql)
  84. return resolve(result.map(v => new SourceEntity(v)))
  85. } catch (e) {
  86. return reject(e.sqlMessage)
  87. }
  88. })
  89. },
  90. add: (form) => {
  91. return new Promise(async (resolve, reject) => {
  92. try {
  93. const sourceEntity = new SourceEntity(form)
  94. const values = [
  95. ['name', sourceEntity.name],
  96. ['remark', sourceEntity.remark],
  97. ['sort', sourceEntity.sort],
  98. ['tile_url', sourceEntity.tileUrl],
  99. ['tile_name', sourceEntity.tileName],
  100. ['ws_url', sourceEntity.wsUrl],
  101. ['ws_data_flag', sourceEntity.wsDataFlag],
  102. ['history_ws_url', sourceEntity.historyWsUrl],
  103. ['history_ws_data_flag', sourceEntity.historyWsDataFlag],
  104. ]
  105. const sql = `INSERT INTO ship_test_source (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
  106. const sameValid = await SourceService.queryBy({name: sourceEntity.name})
  107. if (sameValid.length > 0) {
  108. return reject('已存在相同名称!')
  109. }
  110. const result = await sqlHandle(sql, values.map(v => v[1]))
  111. return resolve(result.insertId)
  112. } catch (e) {
  113. return reject(e.sqlMessage)
  114. }
  115. })
  116. },
  117. edit: (form) => {
  118. return new Promise(async (resolve, reject) => {
  119. try {
  120. const sourceEntity = new SourceEntity(form)
  121. const values = [
  122. ['name', sourceEntity.name],
  123. ['remark', sourceEntity.remark],
  124. ['sort', sourceEntity.sort],
  125. ['tile_url', sourceEntity.tileUrl],
  126. ['tile_name', sourceEntity.tileName],
  127. ['ws_url', sourceEntity.wsUrl],
  128. ['ws_data_flag', sourceEntity.wsDataFlag],
  129. ['history_ws_url', sourceEntity.historyWsUrl],
  130. ['history_ws_data_flag', sourceEntity.historyWsDataFlag],
  131. ]
  132. let sql = `UPDATE ship_test_source SET ${values.map(v => `${v[0]} = ?`).join(',')}`
  133. if (!isValue(sourceEntity.id)) {
  134. return reject('id不可为空!')
  135. } else {
  136. sql += ' ' + `WHERE id = "${sourceEntity.id}"`
  137. }
  138. const sameValid = await SourceService.queryBy({name: sourceEntity.name})
  139. if (sameValid.filter(v => v.id !== sourceEntity.id).length > 0) {
  140. return reject('已存在相同名称!')
  141. }
  142. const result = await sqlHandle(sql, values.map(v => v[1]))
  143. return resolve(sourceEntity.id)
  144. } catch (e) {
  145. return reject(e.sqlMessage)
  146. }
  147. })
  148. },
  149. del: (ids) => {
  150. return new Promise(async (resolve, reject) => {
  151. try {
  152. let sql = 'UPDATE ship_test_source SET del_flag = "1"'
  153. if (ids?.length === 0) {
  154. return reject('ids不可为空!')
  155. } else {
  156. sql += ' ' + 'WHERE id IN (?)'
  157. }
  158. const result = await sqlHandle(sql, [ids])
  159. return resolve('删除成功!')
  160. } catch (e) {
  161. return reject(e.sqlMessage)
  162. }
  163. })
  164. }
  165. }
  166. export default SourceService