params-service.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {sqlHandle} from "../config/db.js";
  2. import {isValue, paramToUnder} from "../../util/index.js";
  3. import PageResult from "../config/page.js";
  4. import ParamsEntity from "../entity/params-entity.js";
  5. const ParamsService = {
  6. list: ({sourceId, name, orderByColumn, isAsc}) => {
  7. return new Promise(async (resolve, reject) => {
  8. try {
  9. let sql = 'SELECT * FROM ship_test_params'
  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 ParamsEntity(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_params'
  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, ParamsEntity)
  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_params'
  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 ParamsEntity(v))?.[0])
  77. } catch (e) {
  78. return reject(e.sqlMessage)
  79. }
  80. })
  81. },
  82. queryBy: ({name}) => {
  83. return new Promise(async (resolve, reject) => {
  84. try {
  85. let sql = 'SELECT * FROM ship_test_params'
  86. let where = ['del_flag = 0']
  87. if (isValue(name)) {
  88. where.push(`name = "${name}"`)
  89. }
  90. if (where.length > 0) {
  91. sql += ' ' + `WHERE ${where.join(' AND ')}`
  92. }
  93. const result = await sqlHandle(sql)
  94. return resolve(result.map(v => new ParamsEntity(v)))
  95. } catch (e) {
  96. return reject(e.sqlMessage)
  97. }
  98. })
  99. },
  100. add: (form) => {
  101. return new Promise(async (resolve, reject) => {
  102. try {
  103. const paramsEntity = new ParamsEntity(form)
  104. const values = [
  105. ['source_id', paramsEntity.sourceId],
  106. ['name', paramsEntity.name],
  107. ['remark', paramsEntity.remark],
  108. ['sort', paramsEntity.sort],
  109. ['default_cql', paramsEntity.defaultCql],
  110. ]
  111. const sql = `INSERT INTO ship_test_params (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
  112. if (!isValue(paramsEntity.sourceId)) {
  113. return reject('数据源ID不可为空')
  114. }
  115. const sameValid = await ParamsService.queryBy({name: paramsEntity.name})
  116. if (sameValid.length > 0) {
  117. return reject('已存在相同名称!')
  118. }
  119. const result = await sqlHandle(sql, values.map(v => v[1]))
  120. return resolve(result.insertId)
  121. } catch (e) {
  122. return reject(e.sqlMessage)
  123. }
  124. })
  125. },
  126. edit: (form) => {
  127. return new Promise(async (resolve, reject) => {
  128. try {
  129. const paramsEntity = new ParamsEntity(form)
  130. const values = [
  131. ['name', paramsEntity.name],
  132. ['remark', paramsEntity.remark],
  133. ['sort', paramsEntity.sort],
  134. ['default_cql', paramsEntity.defaultCql],
  135. ]
  136. let sql = `UPDATE ship_test_params SET ${values.map(v => `${v[0]} = ?`).join(',')}`
  137. if (!isValue(paramsEntity.id)) {
  138. return reject('id不可为空!')
  139. } else {
  140. sql += ' ' + `WHERE id = "${paramsEntity.id}"`
  141. }
  142. const sameValid = await ParamsService.queryBy({name: paramsEntity.name})
  143. if (sameValid.filter(v => v.id !== paramsEntity.id).length > 0) {
  144. return reject('已存在相同名称!')
  145. }
  146. const result = await sqlHandle(sql, values.map(v => v[1]))
  147. return resolve(paramsEntity.id)
  148. } catch (e) {
  149. return reject(e.sqlMessage)
  150. }
  151. })
  152. },
  153. del: (ids) => {
  154. return new Promise(async (resolve, reject) => {
  155. try {
  156. let sql = 'UPDATE ship_test_params SET del_flag = "1"'
  157. if (ids?.length === 0) {
  158. return reject('ids不可为空!')
  159. } else {
  160. sql += ' ' + 'WHERE id IN (?)'
  161. }
  162. const result = await sqlHandle(sql, [ids])
  163. return resolve('删除成功!')
  164. } catch (e) {
  165. return reject(e.sqlMessage)
  166. }
  167. })
  168. }
  169. }
  170. export default ParamsService