params-dict-service.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {sqlHandle} from "../config/db.js";
  2. import {isValue, paramToUnder} from "../../util/index.js";
  3. import PageResult from "../config/page.js";
  4. import ParamsDictEntity from "../entity/params-dict-entity.js";
  5. const ParamsDictService = {
  6. list: ({paramsId, name, orderByColumn, isAsc}) => {
  7. return new Promise(async (resolve, reject) => {
  8. try {
  9. let sql = 'SELECT * FROM ship_test_params_dict'
  10. let where = ['del_flag = 0']
  11. if (isValue(paramsId)) {
  12. where.push(`params_id = "${paramsId}"`)
  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 ParamsDictEntity(v)))
  29. } catch (e) {
  30. return reject(e.sqlMessage)
  31. }
  32. })
  33. },
  34. page: ({paramsId, pageNum, pageSize, name, orderByColumn, isAsc}) => {
  35. return new Promise(async (resolve, reject) => {
  36. try {
  37. let sql = 'SELECT * FROM ship_test_params_dict'
  38. let where = ['del_flag = 0']
  39. if (isValue(paramsId)) {
  40. where.push(`params_id = "${paramsId}"`)
  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, ParamsDictEntity)
  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_dict'
  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 ParamsDictEntity(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_dict'
  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 ParamsDictEntity(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 paramsDictEntity = new ParamsDictEntity(form)
  104. const values = [
  105. ['params_id', paramsDictEntity.paramsId],
  106. ['name', paramsDictEntity.name],
  107. ['remark', paramsDictEntity.remark],
  108. ['sort', paramsDictEntity.sort],
  109. ['cql', paramsDictEntity.cql],
  110. ['active', paramsDictEntity.active],
  111. ]
  112. const sql = `INSERT INTO ship_test_params_dict (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
  113. if (!isValue(paramsDictEntity.paramsId)) {
  114. return reject('参数ID不可为空')
  115. }
  116. if (!isValue(paramsDictEntity.cql)) {
  117. return reject('cql不可为空')
  118. }
  119. const sameValid = await ParamsDictService.queryBy({name: paramsDictEntity.name})
  120. if (sameValid.length > 0) {
  121. return reject('已存在相同名称!')
  122. }
  123. const result = await sqlHandle(sql, values.map(v => v[1]))
  124. return resolve(result.insertId)
  125. } catch (e) {
  126. return reject(e.sqlMessage)
  127. }
  128. })
  129. },
  130. edit: (form) => {
  131. return new Promise(async (resolve, reject) => {
  132. try {
  133. const paramsDictEntity = new ParamsDictEntity(form)
  134. const values = [
  135. ['name', paramsDictEntity.name],
  136. ['remark', paramsDictEntity.remark],
  137. ['sort', paramsDictEntity.sort],
  138. ['cql', paramsDictEntity.cql],
  139. ]
  140. let sql = `UPDATE ship_test_params_dict SET ${values.map(v => `${v[0]} = ?`).join(',')}`
  141. if (!isValue(paramsDictEntity.id)) {
  142. return reject('id不可为空!')
  143. } else {
  144. sql += ' ' + `WHERE id = "${paramsDictEntity.id}"`
  145. }
  146. if (!isValue(paramsDictEntity.cql)) {
  147. return reject('cql不可为空')
  148. }
  149. const sameValid = await ParamsDictService.queryBy({name: paramsDictEntity.name})
  150. if (sameValid.filter(v => v.id !== paramsDictEntity.id).length > 0) {
  151. return reject('已存在相同名称!')
  152. }
  153. const result = await sqlHandle(sql, values.map(v => v[1]))
  154. return resolve(paramsDictEntity.id)
  155. } catch (e) {
  156. return reject(e.sqlMessage)
  157. }
  158. })
  159. },
  160. del: (ids) => {
  161. return new Promise(async (resolve, reject) => {
  162. try {
  163. let sql = 'UPDATE ship_test_params_dict SET del_flag = "1"'
  164. if (ids?.length === 0) {
  165. return reject('ids不可为空!')
  166. } else {
  167. sql += ' ' + 'WHERE id IN (?)'
  168. }
  169. const result = await sqlHandle(sql, [ids])
  170. return resolve('删除成功!')
  171. } catch (e) {
  172. return reject(e.sqlMessage)
  173. }
  174. })
  175. },
  176. enabled: (ids) => {
  177. return new Promise(async (resolve, reject) => {
  178. try {
  179. let sql = 'UPDATE ship_test_params_dict SET active = 0'
  180. if (ids?.length === 0) {
  181. return reject('ids不可为空!')
  182. } else {
  183. sql += ' ' + 'WHERE id IN (?)'
  184. }
  185. const result = await sqlHandle(sql, [ids])
  186. return resolve('启用成功!')
  187. } catch (e) {
  188. return reject(e.sqlMessage)
  189. }
  190. })
  191. },
  192. disabled: (ids) => {
  193. return new Promise(async (resolve, reject) => {
  194. try {
  195. let sql = 'UPDATE ship_test_params_dict SET active = 1'
  196. if (ids?.length === 0) {
  197. return reject('ids不可为空!')
  198. } else {
  199. sql += ' ' + 'WHERE id IN (?)'
  200. }
  201. const result = await sqlHandle(sql, [ids])
  202. return resolve('禁用成功!')
  203. } catch (e) {
  204. return reject(e.sqlMessage)
  205. }
  206. })
  207. }
  208. }
  209. export default ParamsDictService