|
@@ -1,42 +1,94 @@
|
|
|
import {sqlHandle} from "../config/db.js";
|
|
|
-import {isValue} from "../../util/index.js";
|
|
|
+import {isValue, paramToUnder} from "../../util/index.js";
|
|
|
import PageResult from "../config/page.js";
|
|
|
import SourceEntity from "../entity/source-entity.js";
|
|
|
const SourceService = {
|
|
|
- list: async ({name}) => {
|
|
|
- let sql = 'SELECT * FROM ship_test_source'
|
|
|
- let where = ['del_flag = 0']
|
|
|
- if (isValue(name)) {
|
|
|
- where.push(`name LIKE "%${name}%"`)
|
|
|
- }
|
|
|
- if (where.length > 0) {
|
|
|
- sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
- }
|
|
|
- const result = await sqlHandle(sql)
|
|
|
- return result.map(v => new SourceEntity(v))
|
|
|
+ list: ({name, orderByColumn, isAsc}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ let sql = 'SELECT * FROM ship_test_source'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (isValue(name)) {
|
|
|
+ where.push(`name LIKE "%${name}%"`)
|
|
|
+ }
|
|
|
+ if (where.length > 0) {
|
|
|
+ sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ }
|
|
|
+ if (isValue(orderByColumn)) {
|
|
|
+ sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
|
|
|
+ } else {
|
|
|
+ sql += ' ' + `ORDER BY update_time DESC`
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql)
|
|
|
+ resolve(result.map(v => new SourceEntity(v)))
|
|
|
+ })
|
|
|
},
|
|
|
- page: async ({pageNum, pageSize, name}) => {
|
|
|
- let sql = 'SELECT * FROM ship_test_source'
|
|
|
- let where = ['del_flag = 0']
|
|
|
- if (isValue(name)) {
|
|
|
- where.push(`name LIKE "%${name}%"`)
|
|
|
- }
|
|
|
- if (where.length > 0) {
|
|
|
- sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
- }
|
|
|
- const result = await PageResult(sql, pageNum, pageSize, SourceEntity)
|
|
|
- return result
|
|
|
+ page: ({pageNum, pageSize, name, orderByColumn, isAsc}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ let sql = 'SELECT * FROM ship_test_source'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (isValue(name)) {
|
|
|
+ where.push(`name LIKE "%${name}%"`)
|
|
|
+ }
|
|
|
+ if (where.length > 0) {
|
|
|
+ sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ }
|
|
|
+ if (isValue(orderByColumn)) {
|
|
|
+ sql += ' ' + `ORDER BY ${paramToUnder(orderByColumn)} ${isAsc ? 'ASC' : 'DESC'}`
|
|
|
+ } else {
|
|
|
+ sql += ' ' + `ORDER BY update_time DESC`
|
|
|
+ }
|
|
|
+ const result = await PageResult(sql, pageNum, pageSize, SourceEntity)
|
|
|
+ resolve(result)
|
|
|
+ })
|
|
|
},
|
|
|
- add: async (form) => {
|
|
|
- const sql = 'INSERT INTO ship_test_source (name, remark, sort) VALUES (?, ?, ?)'
|
|
|
- const sourceEntity = new SourceEntity(form)
|
|
|
- const values = [
|
|
|
- sourceEntity.name,
|
|
|
- sourceEntity.remark,
|
|
|
- sourceEntity.sort,
|
|
|
- ]
|
|
|
- const result = await sqlHandle(sql, values)
|
|
|
- return result
|
|
|
+ info: (id) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ let sql = 'SELECT * FROM ship_test_source'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (!isValue(id)) {
|
|
|
+ reject('id不可为空')
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ where.push(`id = ${id}`)
|
|
|
+ }
|
|
|
+ if (where.length > 0) {
|
|
|
+ sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql)
|
|
|
+ resolve(result.map(v => new SourceEntity(v))?.[0])
|
|
|
+ })
|
|
|
},
|
|
|
+ add: (form) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ const sql = 'INSERT INTO ship_test_source (name, remark, sort) VALUES (?, ?, ?)'
|
|
|
+ const sourceEntity = new SourceEntity(form)
|
|
|
+ const sameValid = await SourceService.queryBy({name: sourceEntity.name})
|
|
|
+ if (sameValid.length > 0) {
|
|
|
+ reject('已存在相同名称!')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const values = [
|
|
|
+ sourceEntity.name,
|
|
|
+ sourceEntity.remark,
|
|
|
+ sourceEntity.sort,
|
|
|
+ ]
|
|
|
+ const result = await sqlHandle(sql, values)
|
|
|
+ resolve(result)
|
|
|
+ })
|
|
|
+ },
|
|
|
+ queryBy: ({name}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ let sql = 'SELECT * FROM ship_test_source'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (isValue(name)) {
|
|
|
+ where.push(`name = "${name}"`)
|
|
|
+ }
|
|
|
+ if (where.length > 0) {
|
|
|
+ sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql)
|
|
|
+ resolve(result.map(v => new SourceEntity(v)))
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
export default SourceService
|