source-service.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {sqlHandle} from "../config/db.js";
  2. import {isValue} from "../../util/index.js";
  3. import PageResult from "../config/page.js";
  4. import SourceEntity from "../entity/source-entity.js";
  5. const SourceService = {
  6. list: async ({name}) => {
  7. let sql = 'SELECT * FROM ship_test_source'
  8. let where = ['del_flag = 0']
  9. if (isValue(name)) {
  10. where.push(`name LIKE "%${name}%"`)
  11. }
  12. if (where.length > 0) {
  13. sql += ' ' + `WHERE ${where.join(' AND ')}`
  14. }
  15. const result = await sqlHandle(sql)
  16. return result.map(v => new SourceEntity(v))
  17. },
  18. page: async ({pageNum, pageSize, name}) => {
  19. let sql = 'SELECT * FROM ship_test_source'
  20. let where = ['del_flag = 0']
  21. if (isValue(name)) {
  22. where.push(`name LIKE "%${name}%"`)
  23. }
  24. if (where.length > 0) {
  25. sql += ' ' + `WHERE ${where.join(' AND ')}`
  26. }
  27. const result = await PageResult(sql, pageNum, pageSize, SourceEntity)
  28. return result
  29. },
  30. add: async (form) => {
  31. const sql = 'INSERT INTO ship_test_source (name, remark, sort) VALUES (?, ?, ?)'
  32. const sourceEntity = new SourceEntity(form)
  33. const values = [
  34. sourceEntity.name,
  35. sourceEntity.remark,
  36. sourceEntity.sort,
  37. ]
  38. const result = await sqlHandle(sql, values)
  39. return result
  40. },
  41. }
  42. export default SourceService