source-service.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. resolve(result.map(v => new SourceEntity(v)))
  24. } catch (e) {
  25. console.log(e)
  26. reject(e.sqlMessage)
  27. }
  28. })
  29. },
  30. page: ({pageNum, pageSize, name, orderByColumn, isAsc}) => {
  31. return new Promise(async (resolve, reject) => {
  32. try {
  33. let sql = 'SELECT * FROM ship_test_source'
  34. let where = ['del_flag = 0']
  35. if (isValue(name)) {
  36. where.push(`name LIKE "%${name}%"`)
  37. }
  38. if (where.length > 0) {
  39. sql += ' ' + `WHERE ${where.join(' AND ')}`
  40. }
  41. if (isValue(orderByColumn)) {
  42. sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
  43. } else {
  44. sql += ' ' + `ORDER BY sort DESC`
  45. }
  46. const result = await PageResult(sql, pageNum, pageSize, SourceEntity)
  47. resolve(result)
  48. } catch (e) {
  49. reject(e.sqlMessage)
  50. }
  51. })
  52. },
  53. info: (id) => {
  54. return new Promise(async (resolve, reject) => {
  55. try {
  56. let sql = 'SELECT * FROM ship_test_source'
  57. let where = ['del_flag = 0']
  58. if (!isValue(id)) {
  59. reject('id不可为空')
  60. return
  61. } else {
  62. where.push(`id = ${id}`)
  63. }
  64. if (where.length > 0) {
  65. sql += ' ' + `WHERE ${where.join(' AND ')}`
  66. }
  67. const result = await sqlHandle(sql)
  68. resolve(result.map(v => new SourceEntity(v))?.[0])
  69. } catch (e) {
  70. reject(e.sqlMessage)
  71. }
  72. })
  73. },
  74. queryBy: ({name}) => {
  75. return new Promise(async (resolve, reject) => {
  76. try {
  77. let sql = 'SELECT * FROM ship_test_source'
  78. let where = ['del_flag = 0']
  79. if (isValue(name)) {
  80. where.push(`name = "${name}"`)
  81. }
  82. if (where.length > 0) {
  83. sql += ' ' + `WHERE ${where.join(' AND ')}`
  84. }
  85. const result = await sqlHandle(sql)
  86. resolve(result.map(v => new SourceEntity(v)))
  87. } catch (e) {
  88. reject(e.sqlMessage)
  89. }
  90. })
  91. },
  92. add: (form) => {
  93. return new Promise(async (resolve, reject) => {
  94. try {
  95. const sourceEntity = new SourceEntity(form)
  96. const values = [
  97. ['name', sourceEntity.name],
  98. ['remark', sourceEntity.remark],
  99. ['sort', sourceEntity.sort],
  100. ['tile_url', sourceEntity.tileUrl],
  101. ['tile_name', sourceEntity.tileName],
  102. ['ws_url', sourceEntity.wsUrl],
  103. ['ws_data_flag', sourceEntity.wsDataFlag],
  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. reject('已存在相同名称!')
  109. return
  110. }
  111. const result = await sqlHandle(sql, values.map(v => v[1]))
  112. resolve(result.insertId)
  113. } catch (e) {
  114. reject(e.sqlMessage)
  115. }
  116. })
  117. },
  118. edit: (form) => {
  119. return new Promise(async (resolve, reject) => {
  120. try {
  121. const sourceEntity = new SourceEntity(form)
  122. const values = [
  123. ['name', sourceEntity.name],
  124. ['remark', sourceEntity.remark],
  125. ['sort', sourceEntity.sort],
  126. ['tile_url', sourceEntity.tileUrl],
  127. ['tile_name', sourceEntity.tileName],
  128. ['ws_url', sourceEntity.wsUrl],
  129. ['ws_data_flag', sourceEntity.wsDataFlag],
  130. ]
  131. let sql = `UPDATE ship_test_source SET ${values.map(v => `${v[0]} = ?`).join(',')}`
  132. if (!isValue(sourceEntity.id)) {
  133. reject('id不可为空!')
  134. return
  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. reject('已存在相同名称!')
  141. return
  142. }
  143. const result = await sqlHandle(sql, values.map(v => v[1]))
  144. resolve(sourceEntity.id)
  145. } catch (e) {
  146. reject(e.sqlMessage)
  147. }
  148. })
  149. },
  150. del: (ids) => {
  151. return new Promise(async (resolve, reject) => {
  152. try {
  153. let sql = 'UPDATE ship_test_source SET del_flag = "1"'
  154. if (ids?.length === 0) {
  155. reject('ids不可为空!')
  156. return
  157. } else {
  158. sql += ' ' + 'WHERE id IN (?)'
  159. }
  160. const result = await sqlHandle(sql, [ids])
  161. resolve('删除成功!')
  162. } catch (e) {
  163. reject(e.sqlMessage)
  164. }
  165. })
  166. }
  167. }
  168. export default SourceService