params-service.js 5.5 KB

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