Commit 55cbd78d011b2beba49c9e625c55cdfc9c0633a4
1 parent
9bd4c00e
Exists in
master
增加项目管理,修改list方法传值
Showing
11 changed files
with
190 additions
and
13 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,75 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const qs = require('qs'); | |
| 4 | +const Controller = require('egg').Controller; | |
| 5 | +const uuid = require('node-uuid'); | |
| 6 | + | |
| 7 | +class ProjectController extends Controller { | |
| 8 | + async info() { | |
| 9 | + const ctx = this.ctx; | |
| 10 | + const id = ctx.params.id; | |
| 11 | + try { | |
| 12 | + const result = await ctx.service.project.find(id); | |
| 13 | + this.ctx.body = { | |
| 14 | + result, | |
| 15 | + success: true, | |
| 16 | + }; | |
| 17 | + } catch (error) { | |
| 18 | + this.ctx.body = { | |
| 19 | + success: false, | |
| 20 | + error, | |
| 21 | + }; | |
| 22 | + } | |
| 23 | + } | |
| 24 | + async list() { | |
| 25 | + const param = qs.parse(this.ctx.query); | |
| 26 | + const ctx = this.ctx; | |
| 27 | + try { | |
| 28 | + const result = await ctx.service.project.list(param); | |
| 29 | + const total = await ctx.service.project.count(); | |
| 30 | + this.ctx.body = { | |
| 31 | + result, | |
| 32 | + total: total[0].total, | |
| 33 | + success: true, | |
| 34 | + }; | |
| 35 | + } catch (error) { | |
| 36 | + this.ctx.body = { | |
| 37 | + success: false, | |
| 38 | + error, | |
| 39 | + }; | |
| 40 | + } | |
| 41 | + } | |
| 42 | + async add() { | |
| 43 | + const ctx = this.ctx; | |
| 44 | + const id = uuid.v4(); | |
| 45 | + const data = ctx.request.body; | |
| 46 | + data.id = id; | |
| 47 | + try { | |
| 48 | + await ctx.service.project.add(data); | |
| 49 | + this.ctx.body = { success: true }; | |
| 50 | + } catch (error) { | |
| 51 | + this.ctx.body = { success: false, error }; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + async update() { | |
| 55 | + const ctx = this.ctx; | |
| 56 | + const data = ctx.request.body; | |
| 57 | + try { | |
| 58 | + await ctx.service.project.update(data); | |
| 59 | + this.ctx.body = { success: true }; | |
| 60 | + } catch (error) { | |
| 61 | + this.ctx.body = { success: false, error }; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + async remove() { | |
| 65 | + const ctx = this.ctx; | |
| 66 | + try { | |
| 67 | + await ctx.service.project.remove(ctx.request.body); | |
| 68 | + this.ctx.body = { success: true }; | |
| 69 | + } catch (error) { | |
| 70 | + this.ctx.body = { success: false, error }; | |
| 71 | + } | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | +module.exports = ProjectController; | ... | ... |
| ... | ... | @@ -0,0 +1,23 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const Controller = require('egg').Controller; | |
| 4 | + | |
| 5 | +class StatisticController extends Controller { | |
| 6 | + async count() { | |
| 7 | + const ctx = this.ctx; | |
| 8 | + try { | |
| 9 | + const result = await ctx.service.statistic.count(); | |
| 10 | + this.ctx.body = { | |
| 11 | + result, | |
| 12 | + success: true, | |
| 13 | + }; | |
| 14 | + } catch (error) { | |
| 15 | + this.ctx.body = { | |
| 16 | + success: false, | |
| 17 | + error, | |
| 18 | + }; | |
| 19 | + } | |
| 20 | + } | |
| 21 | +} | |
| 22 | + | |
| 23 | +module.exports = StatisticController; | ... | ... |
app/router.js
| ... | ... | @@ -6,6 +6,8 @@ |
| 6 | 6 | module.exports = app => { |
| 7 | 7 | const { router, controller } = app; |
| 8 | 8 | router.get('/', controller.home.index); |
| 9 | + // 首页统计 | |
| 10 | + router.get('/statistic/count', controller.statistic.count); | |
| 9 | 11 | // 用户管理 |
| 10 | 12 | router.get('/user/list', controller.user.list); |
| 11 | 13 | router.get('/user/:id', controller.user.info); |
| ... | ... | @@ -30,4 +32,10 @@ module.exports = app => { |
| 30 | 32 | router.post('/contract', controller.contract.add); |
| 31 | 33 | router.put('/contract', controller.contract.update); |
| 32 | 34 | router.post('/contract/delete', controller.contract.remove); |
| 35 | + // 项目管理 | |
| 36 | + router.get('/project/list', controller.project.list); | |
| 37 | + router.get('/project/:id', controller.project.info); | |
| 38 | + router.post('/project', controller.project.add); | |
| 39 | + router.put('/project', controller.project.update); | |
| 40 | + router.post('/project/delete', controller.project.remove); | |
| 33 | 41 | }; | ... | ... |
app/service/contract.js
| ... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 | const Service = require('egg').Service; |
| 4 | 4 | const objExclude = require('../util/common').objExclude; |
| 5 | 5 | const objToQuery = require('../util/common').objToQuery; |
| 6 | +const moment = require('moment'); | |
| 6 | 7 | |
| 7 | 8 | class ContractService extends Service { |
| 8 | 9 | async count() { |
| ... | ... | @@ -10,25 +11,29 @@ class ContractService extends Service { |
| 10 | 11 | return result; |
| 11 | 12 | } |
| 12 | 13 | async add(data) { |
| 13 | - const result = await this.app.mysql.insert('contract', data); | |
| 14 | + const value = data; | |
| 15 | + value.create_time = moment().format('YYYY-MM-DD HH:mm:ss'); | |
| 16 | + const result = await this.app.mysql.insert('contract', value); | |
| 14 | 17 | return result; |
| 15 | 18 | } |
| 16 | 19 | async update(data) { |
| 17 | - const result = await this.app.mysql.update('contract', data); | |
| 20 | + const value = data; | |
| 21 | + value.update_time = moment().format('YYYY-MM-DD HH:mm:ss'); | |
| 22 | + const result = await this.app.mysql.update('contract', value); | |
| 18 | 23 | return result; |
| 19 | 24 | } |
| 20 | 25 | async find(id) { |
| 21 | 26 | // const user = await this.app.mysql.get('contract', { id }); |
| 22 | 27 | const user = await this.app.mysql.query(` |
| 23 | - select id, customer_id, value, product, invoice, date_format(invoice_time, '%Y-%m-%d') as invoice_time, | |
| 24 | - method, date_format(method_time, '%Y-%m-%d') as method_time, other_expenses, remark, type | |
| 28 | + select id, customer_id, number, value, product, invoice, date_format(invoice_time, '%Y-%m-%d') as invoice_time, | |
| 29 | + method, date_format(method_time, '%Y-%m-%d') as method_time, other_expenses, remark, type, create_time, update_time | |
| 25 | 30 | from contract where id = ? |
| 26 | 31 | `, [ id ]); |
| 27 | - const reulst = user && user.length > 0 ? user[0] : {}; | |
| 28 | - return reulst; | |
| 32 | + const result = user && user.length > 0 ? user[0] : {}; | |
| 33 | + return result; | |
| 29 | 34 | } |
| 30 | 35 | async list(data) { |
| 31 | - const { current, pageSize } = data; | |
| 36 | + const { current = 1, pageSize = 10 } = data; | |
| 32 | 37 | const other = objExclude(data, [ 'current', 'pageSize' ]); |
| 33 | 38 | const limit = Number(pageSize); |
| 34 | 39 | const offset = Number((current - 1) * pageSize); |
| ... | ... | @@ -37,12 +42,11 @@ class ContractService extends Service { |
| 37 | 42 | const sql = ` |
| 38 | 43 | select contract.*, customer.name as customer_name |
| 39 | 44 | from contract left join customer on (contract.customer_id = customer.id) |
| 40 | - where ${where} | |
| 45 | + ${where !== '' ? `where ${where}` : ''} | |
| 41 | 46 | order by contract.type asc |
| 42 | 47 | limit ${offset},${limit} |
| 43 | 48 | `; |
| 44 | 49 | const list = await this.app.mysql.query(sql); |
| 45 | - console.log(sql); | |
| 46 | 50 | return list; |
| 47 | 51 | } |
| 48 | 52 | async remove(data) { | ... | ... |
app/service/customer.js
| ... | ... | @@ -21,7 +21,7 @@ class CustomerService extends Service { |
| 21 | 21 | return user; |
| 22 | 22 | } |
| 23 | 23 | async list(data) { |
| 24 | - const { current, pageSize } = data; | |
| 24 | + const { current = 1, pageSize = 10 } = data; | |
| 25 | 25 | const other = objExclude(data, [ 'current', 'pageSize' ]); |
| 26 | 26 | const limit = Number(pageSize); |
| 27 | 27 | const offset = Number((current - 1) * pageSize); | ... | ... |
app/service/dict.js
| ... | ... | @@ -21,7 +21,7 @@ class DictService extends Service { |
| 21 | 21 | return user; |
| 22 | 22 | } |
| 23 | 23 | async list(data) { |
| 24 | - const { current, pageSize } = data; | |
| 24 | + const { current = 1, pageSize = 10 } = data; | |
| 25 | 25 | const other = objExclude(data, [ 'current', 'pageSize' ]); |
| 26 | 26 | const limit = Number(pageSize); |
| 27 | 27 | const offset = Number((current - 1) * pageSize); | ... | ... |
| ... | ... | @@ -0,0 +1,37 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const Service = require('egg').Service; | |
| 4 | +const objExclude = require('../util/common').objExclude; | |
| 5 | + | |
| 6 | +class ProjectService extends Service { | |
| 7 | + async count() { | |
| 8 | + const result = await this.app.mysql.query('select count(*) as total from project'); | |
| 9 | + return result; | |
| 10 | + } | |
| 11 | + async add(data) { | |
| 12 | + const result = await this.app.mysql.insert('project', data); | |
| 13 | + return result; | |
| 14 | + } | |
| 15 | + async update(data) { | |
| 16 | + const result = await this.app.mysql.update('project', data); | |
| 17 | + return result; | |
| 18 | + } | |
| 19 | + async find(id) { | |
| 20 | + const user = await this.app.mysql.get('project', { id }); | |
| 21 | + return user; | |
| 22 | + } | |
| 23 | + async list(data) { | |
| 24 | + const { current = 1, pageSize = 10 } = data; | |
| 25 | + const other = objExclude(data, [ 'current', 'pageSize' ]); | |
| 26 | + const limit = Number(pageSize); | |
| 27 | + const offset = Number((current - 1) * pageSize); | |
| 28 | + const list = await this.app.mysql.select('project', { where: other, limit, offset, orders: [[ 'number', 'asc' ]] }); | |
| 29 | + return list; | |
| 30 | + } | |
| 31 | + async remove(data) { | |
| 32 | + const result = await this.app.mysql.delete('project', data); | |
| 33 | + return result; | |
| 34 | + } | |
| 35 | +} | |
| 36 | + | |
| 37 | +module.exports = ProjectService; | ... | ... |
| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const Service = require('egg').Service; | |
| 4 | + | |
| 5 | +class StatisticService extends Service { | |
| 6 | + async count() { | |
| 7 | + const result = await this.app.mysql.query(` | |
| 8 | + select sum(sale_contract_count) sale_contract_count, sum(purchase_contract_count) purchase_contract_count, sum(customer_count) customer_count, sum(supplier_count) supplier_count from ( | |
| 9 | + select count(*) as sale_contract_count, 0 as purchase_contract_count, 0 as customer_count, 0 as supplier_count from contract where type = 0 | |
| 10 | + union all | |
| 11 | + select 0 as sale_contract_count, count(*) as purchase_contract_count, 0 as customer_count, 0 as supplier_count from contract where type = 1 | |
| 12 | + union all | |
| 13 | + select 0 as sale_contract_count, 0 as purchase_contract_count, count(*) as customer_count, 0 as supplier_count from customer where type = 0 | |
| 14 | + union all | |
| 15 | + select 0 as sale_contract_count, 0 as purchase_contract_count, 0 as customer_count, count(*) as supplier_count from customer where type = 1 | |
| 16 | + ) t; | |
| 17 | + `); | |
| 18 | + return result && result.length > 0 ? result[0] : {}; | |
| 19 | + } | |
| 20 | +} | |
| 21 | + | |
| 22 | +module.exports = StatisticService; | ... | ... |
app/service/user.js
| ... | ... | @@ -21,7 +21,7 @@ class UserService extends Service { |
| 21 | 21 | return user; |
| 22 | 22 | } |
| 23 | 23 | async list(data) { |
| 24 | - const { current, pageSize } = data; | |
| 24 | + const { current = 1, pageSize = 10 } = data; | |
| 25 | 25 | const other = objExclude(data, [ 'current', 'pageSize' ]); |
| 26 | 26 | const limit = Number(pageSize); |
| 27 | 27 | const offset = Number((current - 1) * pageSize); | ... | ... |
app/util/common.js
| ... | ... | @@ -13,8 +13,15 @@ module.exports = { |
| 13 | 13 | objToQuery(obj, prefix) { |
| 14 | 14 | const arr = []; |
| 15 | 15 | Object.keys(obj).forEach(key => { |
| 16 | + arr.push(`${!prefix ? '' : `${prefix}.`}${key} like '%${obj[key]}%'`); | |
| 17 | + }); | |
| 18 | + return arr.join(' and '); | |
| 19 | + }, | |
| 20 | + objToSet(obj, prefix) { | |
| 21 | + const arr = []; | |
| 22 | + Object.keys(obj).forEach(key => { | |
| 16 | 23 | arr.push(`${!prefix ? '' : `${prefix}.`}${key} = ${obj[key]}`); |
| 17 | 24 | }); |
| 18 | - return arr.join('and'); | |
| 25 | + return arr.join(' , '); | |
| 19 | 26 | }, |
| 20 | 27 | }; | ... | ... |