123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 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) => {
- 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 sort DESC`
- }
- const result = await sqlHandle(sql)
- resolve(result.map(v => new SourceEntity(v)))
- } catch (e) {
- console.log(e)
- reject(e.sqlMessage)
- }
- })
- },
- page: ({pageNum, pageSize, name, orderByColumn, isAsc}) => {
- 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 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 sort DESC`
- }
- const result = await PageResult(sql, pageNum, pageSize, SourceEntity)
- resolve(result)
- } catch (e) {
- reject(e.sqlMessage)
- }
- })
- },
- info: (id) => {
- return new Promise(async (resolve, reject) => {
- 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)
- }
- })
- },
- 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)
- }
- })
- },
- add: (form) => {
- return new Promise(async (resolve, reject) => {
- try {
- const sourceEntity = new SourceEntity(form)
- const values = [
- ['name', sourceEntity.name],
- ['remark', sourceEntity.remark],
- ['sort', sourceEntity.sort],
- ['tile_url', sourceEntity.tileUrl],
- ['tile_name', sourceEntity.tileName],
- ['ws_url', sourceEntity.wsUrl],
- ['ws_data_flag', sourceEntity.wsDataFlag],
- ]
- const sql = `INSERT INTO ship_test_source (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
- const sameValid = await SourceService.queryBy({name: sourceEntity.name})
- if (sameValid.length > 0) {
- reject('已存在相同名称!')
- return
- }
- const result = await sqlHandle(sql, values.map(v => v[1]))
- resolve(result.insertId)
- } catch (e) {
- reject(e.sqlMessage)
- }
- })
- },
- edit: (form) => {
- return new Promise(async (resolve, reject) => {
- try {
- const sourceEntity = new SourceEntity(form)
- const values = [
- ['name', sourceEntity.name],
- ['remark', sourceEntity.remark],
- ['sort', sourceEntity.sort],
- ['tile_url', sourceEntity.tileUrl],
- ['tile_name', sourceEntity.tileName],
- ['ws_url', sourceEntity.wsUrl],
- ['ws_data_flag', sourceEntity.wsDataFlag],
- ]
- let sql = `UPDATE ship_test_source SET ${values.map(v => `${v[0]} = ?`).join(',')}`
- 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 result = await sqlHandle(sql, values.map(v => v[1]))
- resolve(sourceEntity.id)
- } catch (e) {
- reject(e.sqlMessage)
- }
- })
- },
- 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)
- }
- })
- }
- }
- export default SourceService
|