|
@@ -0,0 +1,193 @@
|
|
|
+import {sqlHandle} from "../config/db.js";
|
|
|
+import {isValue, paramToUnder} from "../../util/index.js";
|
|
|
+import PageResult from "../config/page.js";
|
|
|
+import HoverEntity from "../entity/hover-entity.js";
|
|
|
+const HoverService = {
|
|
|
+ list: ({sourceId, name, orderByColumn, isAsc}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let sql = 'SELECT * FROM ship_test_hover'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (isValue(sourceId)) {
|
|
|
+ where.push(`source_id = "${sourceId}"`)
|
|
|
+ } else {
|
|
|
+ return reject('数据源ID不可为空')
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+ return resolve(result.map(v => {
|
|
|
+ const obj = new HoverEntity(v)
|
|
|
+ try {
|
|
|
+ obj.options = JSON.parse(obj.options)
|
|
|
+ } catch (e) {
|
|
|
+ }
|
|
|
+ return obj
|
|
|
+ }))
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ page: ({sourceId, pageNum, pageSize, name, orderByColumn, isAsc}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let sql = 'SELECT * FROM ship_test_hover'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (isValue(sourceId)) {
|
|
|
+ where.push(`source_id = "${sourceId}"`)
|
|
|
+ } else {
|
|
|
+ return reject('数据源ID不可为空')
|
|
|
+ }
|
|
|
+ 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, HoverEntity)
|
|
|
+ result.data.forEach(v => {
|
|
|
+ try {
|
|
|
+ v.options = JSON.parse(v.options)
|
|
|
+ } catch (e) {
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return resolve(result)
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ info: (id) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let sql = 'SELECT * FROM ship_test_hover'
|
|
|
+ let where = ['del_flag = 0']
|
|
|
+ if (!isValue(id)) {
|
|
|
+ return reject('id不可为空')
|
|
|
+ } else {
|
|
|
+ where.push(`id = ${id}`)
|
|
|
+ }
|
|
|
+ if (where.length > 0) {
|
|
|
+ sql += ' ' + `WHERE ${where.join(' AND ')}`
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql)
|
|
|
+ return resolve(result.map(v => {
|
|
|
+ const obj = new HoverEntity(v)
|
|
|
+ try {
|
|
|
+ obj.options = JSON.parse(obj.options)
|
|
|
+ } catch (e) {
|
|
|
+ }
|
|
|
+ return obj
|
|
|
+ })?.[0])
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ queryBy: ({name}) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let sql = 'SELECT * FROM ship_test_hover'
|
|
|
+ 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)
|
|
|
+ return resolve(result.map(v => {
|
|
|
+ const obj = new HoverEntity(v)
|
|
|
+ try {
|
|
|
+ obj.options = JSON.parse(obj.options)
|
|
|
+ } catch (e) {
|
|
|
+ }
|
|
|
+ return obj
|
|
|
+ }))
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ add: (form) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ const hoverEntity = new HoverEntity(form)
|
|
|
+ const values = [
|
|
|
+ ['source_id', hoverEntity.sourceId],
|
|
|
+ ['name', hoverEntity.name],
|
|
|
+ ['remark', hoverEntity.remark],
|
|
|
+ ['sort', hoverEntity.sort],
|
|
|
+ ['value', hoverEntity.value],
|
|
|
+ ['options', JSON.stringify(hoverEntity.options)],
|
|
|
+ ['history_value', hoverEntity.historyValue],
|
|
|
+ ]
|
|
|
+ const sql = `INSERT INTO ship_test_hover (${values.map(v => v[0]).join(',')}) VALUES (${values.map(v => '?').join(',')})`
|
|
|
+ if (!isValue(hoverEntity.sourceId)) {
|
|
|
+ return reject('数据源ID不可为空')
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql, values.map(v => v[1]))
|
|
|
+ return resolve(result.insertId)
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ edit: (form) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ const hoverEntity = new HoverEntity(form)
|
|
|
+ const values = [
|
|
|
+ ['name', hoverEntity.name],
|
|
|
+ ['remark', hoverEntity.remark],
|
|
|
+ ['sort', hoverEntity.sort],
|
|
|
+ ['value', hoverEntity.value],
|
|
|
+ ['options', JSON.stringify(hoverEntity.options)],
|
|
|
+ ['history_value', hoverEntity.historyValue],
|
|
|
+ ]
|
|
|
+ let sql = `UPDATE ship_test_hover SET ${values.map(v => `${v[0]} = ?`).join(',')}`
|
|
|
+ if (!isValue(hoverEntity.id)) {
|
|
|
+ return reject('id不可为空!')
|
|
|
+ } else {
|
|
|
+ sql += ' ' + `WHERE id = "${hoverEntity.id}"`
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql, values.map(v => v[1]))
|
|
|
+ return resolve(hoverEntity.id)
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ del: (ids) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let sql = 'UPDATE ship_test_hover SET del_flag = "1"'
|
|
|
+ if (ids?.length === 0) {
|
|
|
+ return reject('ids不可为空!')
|
|
|
+ } else {
|
|
|
+ sql += ' ' + 'WHERE id IN (?)'
|
|
|
+ }
|
|
|
+ const result = await sqlHandle(sql, [ids])
|
|
|
+ return resolve('删除成功!')
|
|
|
+ } catch (e) {
|
|
|
+ return reject(e.sqlMessage)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+export default HoverService
|