12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import {sqlHandle} from "../config/db.js";
- import {isValue, paramToUnder} from "../../util/index.js";
- import PageResult from "../config/page.js";
- 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}%"`)
- }
- 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 ')}`
- }
- 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}`)
- }
- 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
|