|
@@ -5,89 +5,154 @@ import SourceEntity from "../entity/source-entity.js";
|
|
|
const SourceService = {
|
|
|
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}%"`)
|
|
|
+ try {
|
|
|
+ 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)))
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- 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: ({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 ')}`
|
|
|
+ try {
|
|
|
+ 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)
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- 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)
|
|
|
})
|
|
|
},
|
|
|
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}`)
|
|
|
+ try {
|
|
|
+ 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])
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- if (where.length > 0) {
|
|
|
- sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ })
|
|
|
+ },
|
|
|
+ queryBy: ({name}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ 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)))
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- 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
|
|
|
+ try {
|
|
|
+ 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.insertId)
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- const values = [
|
|
|
- sourceEntity.name,
|
|
|
- sourceEntity.remark,
|
|
|
- sourceEntity.sort,
|
|
|
- ]
|
|
|
- const result = await sqlHandle(sql, values)
|
|
|
- resolve(result)
|
|
|
})
|
|
|
},
|
|
|
- queryBy: ({name}) => {
|
|
|
+ edit: (form) => {
|
|
|
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}"`)
|
|
|
+ try {
|
|
|
+ let sql = 'UPDATE ship_test_source SET name = ?, remark = ?, sort = ?'
|
|
|
+ const sourceEntity = new SourceEntity(form)
|
|
|
+ if (!isValue(sourceEntity.id)) {
|
|
|
+ reject('id不可为空!')
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ sql += ' ' + `WHERE id = "${sourceEntity.id}"`
|
|
|
+ }
|
|
|
+ const sameValid = await SourceService.queryBy({name: sourceEntity.name})
|
|
|
+ if (sameValid.filter(v => v.id !== sourceEntity.id).length > 0) {
|
|
|
+ reject('已存在相同名称!')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const values = [
|
|
|
+ sourceEntity.name,
|
|
|
+ sourceEntity.remark,
|
|
|
+ sourceEntity.sort,
|
|
|
+ ]
|
|
|
+ const result = await sqlHandle(sql, values)
|
|
|
+ resolve(sourceEntity.id)
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- if (where.length > 0) {
|
|
|
- sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ })
|
|
|
+ },
|
|
|
+ del: (ids) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let sql = 'UPDATE ship_test_source SET del_flag = "1"'
|
|
|
+ if (ids?.length === 0) {
|
|
|
+ reject('ids不可为空!')
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ sql += ' ' + 'WHERE id IN (?)'
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql, [ids])
|
|
|
+ resolve('删除成功!')
|
|
|
+ } catch (e) {
|
|
|
+ reject(e.sqlMessage)
|
|
|
}
|
|
|
- const result = await sqlHandle(sql)
|
|
|
- resolve(result.map(v => new SourceEntity(v)))
|
|
|
})
|
|
|
}
|
|
|
}
|