CzRger 1 year ago
parent
commit
c171c17dfe
3 changed files with 150 additions and 61 deletions
  1. 22 0
      ship-test/control/source-control.js
  2. 2 0
      ship-test/routers/index.js
  3. 126 61
      ship-test/service/source-service.js

+ 22 - 0
ship-test/control/source-control.js

@@ -45,5 +45,27 @@ const SourceControl = {
       res.send(new ApiResult().error(e))
     }
   },
+  edit: async (req, res) => {
+    try {
+      SourceService.edit(req.body).then(data => {
+        res.send(new ApiResult().success(data))
+      }).catch((e) => {
+        res.send(new ApiResult().error(null, e))
+      })
+    } catch (e) {
+      res.send(new ApiResult().error(e))
+    }
+  },
+  del: async (req, res) => {
+    try {
+      SourceService.del(req.params?.ids?.split(',')).then(data => {
+        res.send(new ApiResult().success(data))
+      }).catch((e) => {
+        res.send(new ApiResult().error(null, e))
+      })
+    } catch (e) {
+      res.send(new ApiResult().error(e))
+    }
+  },
 }
 export default SourceControl

+ 2 - 0
ship-test/routers/index.js

@@ -5,4 +5,6 @@ shipTestRouter.post('/source/list', SourceControl.list)
 shipTestRouter.post('/source/page', SourceControl.page)
 shipTestRouter.get('/source/info/:id', SourceControl.info)
 shipTestRouter.post('/source/add', SourceControl.add)
+shipTestRouter.put('/source/edit', SourceControl.edit)
+shipTestRouter.delete('/source/del/:ids', SourceControl.del)
 export default shipTestRouter

+ 126 - 61
ship-test/service/source-service.js

@@ -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)))
     })
   }
 }