From 9bd4c00ed681c3334d79814dba334bff51bccbbd Mon Sep 17 00:00:00 2001 From: Aaron.Liu <427787340@qq.com> Date: Fri, 20 Apr 2018 18:32:10 +0800 Subject: [PATCH] 增加用户管理,合同管理,客户管理,数据字典接口 --- app/controller/contract.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/controller/customer.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/controller/dict.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/controller/user.js | 4 ++-- app/router.js | 19 +++++++++++++++++++ app/service/contract.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/service/customer.js | 37 +++++++++++++++++++++++++++++++++++++ app/service/dict.js | 37 +++++++++++++++++++++++++++++++++++++ app/service/user.js | 2 +- app/util/common.js | 7 +++++++ 10 files changed, 382 insertions(+), 3 deletions(-) create mode 100644 app/controller/contract.js create mode 100644 app/controller/customer.js create mode 100644 app/controller/dict.js create mode 100644 app/service/contract.js create mode 100644 app/service/customer.js create mode 100644 app/service/dict.js diff --git a/app/controller/contract.js b/app/controller/contract.js new file mode 100644 index 0000000..a6b380e --- /dev/null +++ b/app/controller/contract.js @@ -0,0 +1,75 @@ +'use strict'; + +const qs = require('qs'); +const Controller = require('egg').Controller; +const uuid = require('node-uuid'); + +class ContractController extends Controller { + async info() { + const ctx = this.ctx; + const id = ctx.params.id; + try { + const result = await ctx.service.contract.find(id); + this.ctx.body = { + result, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async list() { + const param = qs.parse(this.ctx.query); + const ctx = this.ctx; + try { + const result = await ctx.service.contract.list(param); + const total = await ctx.service.contract.count(); + this.ctx.body = { + result, + total: total[0].total, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async add() { + const ctx = this.ctx; + const id = uuid.v4(); + const data = ctx.request.body; + data.id = id; + try { + await ctx.service.contract.add(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async update() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + await ctx.service.contract.update(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async remove() { + const ctx = this.ctx; + try { + await ctx.service.contract.remove(ctx.request.body); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } +} + +module.exports = ContractController; diff --git a/app/controller/customer.js b/app/controller/customer.js new file mode 100644 index 0000000..50632e3 --- /dev/null +++ b/app/controller/customer.js @@ -0,0 +1,75 @@ +'use strict'; + +const qs = require('qs'); +const Controller = require('egg').Controller; +const uuid = require('node-uuid'); + +class CustomerController extends Controller { + async info() { + const ctx = this.ctx; + const id = ctx.params.id; + try { + const result = await ctx.service.customer.find(id); + this.ctx.body = { + result, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async list() { + const param = qs.parse(this.ctx.query); + const ctx = this.ctx; + try { + const result = await ctx.service.customer.list(param); + const total = await ctx.service.customer.count(); + this.ctx.body = { + result, + total: total[0].total, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async add() { + const ctx = this.ctx; + const id = uuid.v4(); + const data = ctx.request.body; + data.id = id; + try { + await ctx.service.customer.add(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async update() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + await ctx.service.customer.update(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async remove() { + const ctx = this.ctx; + try { + await ctx.service.customer.remove(ctx.request.body); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } +} + +module.exports = CustomerController; diff --git a/app/controller/dict.js b/app/controller/dict.js new file mode 100644 index 0000000..6351ed5 --- /dev/null +++ b/app/controller/dict.js @@ -0,0 +1,75 @@ +'use strict'; + +const qs = require('qs'); +const Controller = require('egg').Controller; +const uuid = require('node-uuid'); + +class DictController extends Controller { + async info() { + const ctx = this.ctx; + const id = ctx.params.id; + try { + const result = await ctx.service.dict.find(id); + this.ctx.body = { + result, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async list() { + const param = qs.parse(this.ctx.query); + const ctx = this.ctx; + try { + const result = await ctx.service.dict.list(param); + const total = await ctx.service.dict.count(); + this.ctx.body = { + result, + total: total[0].total, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async add() { + const ctx = this.ctx; + const id = uuid.v4(); + const data = ctx.request.body; + data.id = id; + try { + await ctx.service.dict.add(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async update() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + await ctx.service.dict.update(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async remove() { + const ctx = this.ctx; + try { + await ctx.service.dict.remove(ctx.request.body); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } +} + +module.exports = DictController; diff --git a/app/controller/user.js b/app/controller/user.js index 61764b7..22f11ce 100644 --- a/app/controller/user.js +++ b/app/controller/user.js @@ -7,9 +7,9 @@ const uuid = require('node-uuid'); class UserController extends Controller { async info() { const ctx = this.ctx; - const userId = ctx.params.id; + const id = ctx.params.id; try { - const result = await ctx.service.user.find(userId); + const result = await ctx.service.user.find(id); this.ctx.body = { result, success: true, diff --git a/app/router.js b/app/router.js index b617797..5505e21 100644 --- a/app/router.js +++ b/app/router.js @@ -6,9 +6,28 @@ module.exports = app => { const { router, controller } = app; router.get('/', controller.home.index); + // 用户管理 router.get('/user/list', controller.user.list); router.get('/user/:id', controller.user.info); router.post('/user', controller.user.add); router.put('/user', controller.user.update); router.post('/user/delete', controller.user.remove); + // 数据字典 + router.get('/dict/list', controller.dict.list); + router.get('/dict/:id', controller.dict.info); + router.post('/dict', controller.dict.add); + router.put('/dict', controller.dict.update); + router.post('/dict/delete', controller.dict.remove); + // 客户管理 + router.get('/customer/list', controller.customer.list); + router.get('/customer/:id', controller.customer.info); + router.post('/customer', controller.customer.add); + router.put('/customer', controller.customer.update); + router.post('/customer/delete', controller.customer.remove); + // 合同管理 + router.get('/contract/list', controller.contract.list); + router.get('/contract/:id', controller.contract.info); + router.post('/contract', controller.contract.add); + router.put('/contract', controller.contract.update); + router.post('/contract/delete', controller.contract.remove); }; diff --git a/app/service/contract.js b/app/service/contract.js new file mode 100644 index 0000000..5f0c895 --- /dev/null +++ b/app/service/contract.js @@ -0,0 +1,54 @@ +'use strict'; + +const Service = require('egg').Service; +const objExclude = require('../util/common').objExclude; +const objToQuery = require('../util/common').objToQuery; + +class ContractService extends Service { + async count() { + const result = await this.app.mysql.query('select count(*) as total from contract'); + return result; + } + async add(data) { + const result = await this.app.mysql.insert('contract', data); + return result; + } + async update(data) { + const result = await this.app.mysql.update('contract', data); + return result; + } + async find(id) { + // const user = await this.app.mysql.get('contract', { id }); + const user = await this.app.mysql.query(` + select id, customer_id, value, product, invoice, date_format(invoice_time, '%Y-%m-%d') as invoice_time, + method, date_format(method_time, '%Y-%m-%d') as method_time, other_expenses, remark, type + from contract where id = ? + `, [ id ]); + const reulst = user && user.length > 0 ? user[0] : {}; + return reulst; + } + async list(data) { + const { current, pageSize } = data; + const other = objExclude(data, [ 'current', 'pageSize' ]); + const limit = Number(pageSize); + const offset = Number((current - 1) * pageSize); + // const list = await this.app.mysql.select([ 'contract', 'customer' ], { columns: [ 'customer.name as cname', 'contract.type' ], where: other, limit, offset, orders: [[ 'type', 'asc' ]] }); + const where = objToQuery(other, 'contract'); + const sql = ` + select contract.*, customer.name as customer_name + from contract left join customer on (contract.customer_id = customer.id) + where ${where} + order by contract.type asc + limit ${offset},${limit} + `; + const list = await this.app.mysql.query(sql); + console.log(sql); + return list; + } + async remove(data) { + const result = await this.app.mysql.delete('contract', data); + return result; + } +} + +module.exports = ContractService; diff --git a/app/service/customer.js b/app/service/customer.js new file mode 100644 index 0000000..16d0d26 --- /dev/null +++ b/app/service/customer.js @@ -0,0 +1,37 @@ +'use strict'; + +const Service = require('egg').Service; +const objExclude = require('../util/common').objExclude; + +class CustomerService extends Service { + async count() { + const result = await this.app.mysql.query('select count(*) as total from customer'); + return result; + } + async add(data) { + const result = await this.app.mysql.insert('customer', data); + return result; + } + async update(data) { + const result = await this.app.mysql.update('customer', data); + return result; + } + async find(id) { + const user = await this.app.mysql.get('customer', { id }); + return user; + } + async list(data) { + const { current, pageSize } = data; + const other = objExclude(data, [ 'current', 'pageSize' ]); + const limit = Number(pageSize); + const offset = Number((current - 1) * pageSize); + const list = await this.app.mysql.select('customer', { where: other, limit, offset, orders: [[ 'type', 'asc' ]] }); + return list; + } + async remove(data) { + const result = await this.app.mysql.delete('customer', data); + return result; + } +} + +module.exports = CustomerService; diff --git a/app/service/dict.js b/app/service/dict.js new file mode 100644 index 0000000..35dea29 --- /dev/null +++ b/app/service/dict.js @@ -0,0 +1,37 @@ +'use strict'; + +const Service = require('egg').Service; +const objExclude = require('../util/common').objExclude; + +class DictService extends Service { + async count() { + const result = await this.app.mysql.query('select count(*) as total from dict'); + return result; + } + async add(data) { + const result = await this.app.mysql.insert('dict', data); + return result; + } + async update(data) { + const result = await this.app.mysql.update('dict', data); + return result; + } + async find(id) { + const user = await this.app.mysql.get('dict', { id }); + return user; + } + async list(data) { + const { current, pageSize } = data; + const other = objExclude(data, [ 'current', 'pageSize' ]); + const limit = Number(pageSize); + const offset = Number((current - 1) * pageSize); + const list = await this.app.mysql.select('dict', { where: other, limit, offset, orders: [[ 'type', 'asc' ], [ 'code', 'asc' ]] }); + return list; + } + async remove(data) { + const result = await this.app.mysql.delete('dict', data); + return result; + } +} + +module.exports = DictService; diff --git a/app/service/user.js b/app/service/user.js index 8205966..955c0fe 100644 --- a/app/service/user.js +++ b/app/service/user.js @@ -25,7 +25,7 @@ class UserService extends Service { const other = objExclude(data, [ 'current', 'pageSize' ]); const limit = Number(pageSize); const offset = Number((current - 1) * pageSize); - const list = await this.app.mysql.select('user', { columns: [ 'id', 'name', 'username', 'role' ], where: other, limit, offset }); + const list = await this.app.mysql.select('user', { columns: [ 'id', 'name', 'username', 'role' ], where: other, limit, offset, orders: [[ 'role', 'asc' ]] }); return list; } async remove(data) { diff --git a/app/util/common.js b/app/util/common.js index 5103930..51014d6 100644 --- a/app/util/common.js +++ b/app/util/common.js @@ -10,4 +10,11 @@ module.exports = { }); return result; }, + objToQuery(obj, prefix) { + const arr = []; + Object.keys(obj).forEach(key => { + arr.push(`${!prefix ? '' : `${prefix}.`}${key} = ${obj[key]}`); + }); + return arr.join('and'); + }, }; -- libgit2 0.21.0