diff --git a/app/controller/project.js b/app/controller/project.js new file mode 100644 index 0000000..3abe37f --- /dev/null +++ b/app/controller/project.js @@ -0,0 +1,75 @@ +'use strict'; + +const qs = require('qs'); +const Controller = require('egg').Controller; +const uuid = require('node-uuid'); + +class ProjectController extends Controller { + async info() { + const ctx = this.ctx; + const id = ctx.params.id; + try { + const result = await ctx.service.project.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.project.list(param); + const total = await ctx.service.project.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.project.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.project.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.project.remove(ctx.request.body); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } +} + +module.exports = ProjectController; diff --git a/app/controller/statistic.js b/app/controller/statistic.js new file mode 100644 index 0000000..b6884f0 --- /dev/null +++ b/app/controller/statistic.js @@ -0,0 +1,23 @@ +'use strict'; + +const Controller = require('egg').Controller; + +class StatisticController extends Controller { + async count() { + const ctx = this.ctx; + try { + const result = await ctx.service.statistic.count(); + this.ctx.body = { + result, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } +} + +module.exports = StatisticController; diff --git a/app/router.js b/app/router.js index 5505e21..aa1ad2b 100644 --- a/app/router.js +++ b/app/router.js @@ -6,6 +6,8 @@ module.exports = app => { const { router, controller } = app; router.get('/', controller.home.index); + // 首页统计 + router.get('/statistic/count', controller.statistic.count); // 用户管理 router.get('/user/list', controller.user.list); router.get('/user/:id', controller.user.info); @@ -30,4 +32,10 @@ module.exports = app => { router.post('/contract', controller.contract.add); router.put('/contract', controller.contract.update); router.post('/contract/delete', controller.contract.remove); + // 项目管理 + router.get('/project/list', controller.project.list); + router.get('/project/:id', controller.project.info); + router.post('/project', controller.project.add); + router.put('/project', controller.project.update); + router.post('/project/delete', controller.project.remove); }; diff --git a/app/service/contract.js b/app/service/contract.js index 5f0c895..7aca041 100644 --- a/app/service/contract.js +++ b/app/service/contract.js @@ -3,6 +3,7 @@ const Service = require('egg').Service; const objExclude = require('../util/common').objExclude; const objToQuery = require('../util/common').objToQuery; +const moment = require('moment'); class ContractService extends Service { async count() { @@ -10,25 +11,29 @@ class ContractService extends Service { return result; } async add(data) { - const result = await this.app.mysql.insert('contract', data); + const value = data; + value.create_time = moment().format('YYYY-MM-DD HH:mm:ss'); + const result = await this.app.mysql.insert('contract', value); return result; } async update(data) { - const result = await this.app.mysql.update('contract', data); + const value = data; + value.update_time = moment().format('YYYY-MM-DD HH:mm:ss'); + const result = await this.app.mysql.update('contract', value); 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 + select id, customer_id, number, 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, create_time, update_time from contract where id = ? `, [ id ]); - const reulst = user && user.length > 0 ? user[0] : {}; - return reulst; + const result = user && user.length > 0 ? user[0] : {}; + return result; } async list(data) { - const { current, pageSize } = data; + const { current = 1, pageSize = 10 } = data; const other = objExclude(data, [ 'current', 'pageSize' ]); const limit = Number(pageSize); const offset = Number((current - 1) * pageSize); @@ -37,12 +42,11 @@ class ContractService extends Service { const sql = ` select contract.*, customer.name as customer_name from contract left join customer on (contract.customer_id = customer.id) - where ${where} + ${where !== '' ? `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) { diff --git a/app/service/customer.js b/app/service/customer.js index 16d0d26..644a7f7 100644 --- a/app/service/customer.js +++ b/app/service/customer.js @@ -21,7 +21,7 @@ class CustomerService extends Service { return user; } async list(data) { - const { current, pageSize } = data; + const { current = 1, pageSize = 10 } = data; const other = objExclude(data, [ 'current', 'pageSize' ]); const limit = Number(pageSize); const offset = Number((current - 1) * pageSize); diff --git a/app/service/dict.js b/app/service/dict.js index 35dea29..a92a6aa 100644 --- a/app/service/dict.js +++ b/app/service/dict.js @@ -21,7 +21,7 @@ class DictService extends Service { return user; } async list(data) { - const { current, pageSize } = data; + const { current = 1, pageSize = 10 } = data; const other = objExclude(data, [ 'current', 'pageSize' ]); const limit = Number(pageSize); const offset = Number((current - 1) * pageSize); diff --git a/app/service/project.js b/app/service/project.js new file mode 100644 index 0000000..7cc2f6c --- /dev/null +++ b/app/service/project.js @@ -0,0 +1,37 @@ +'use strict'; + +const Service = require('egg').Service; +const objExclude = require('../util/common').objExclude; + +class ProjectService extends Service { + async count() { + const result = await this.app.mysql.query('select count(*) as total from project'); + return result; + } + async add(data) { + const result = await this.app.mysql.insert('project', data); + return result; + } + async update(data) { + const result = await this.app.mysql.update('project', data); + return result; + } + async find(id) { + const user = await this.app.mysql.get('project', { id }); + return user; + } + async list(data) { + const { current = 1, pageSize = 10 } = data; + const other = objExclude(data, [ 'current', 'pageSize' ]); + const limit = Number(pageSize); + const offset = Number((current - 1) * pageSize); + const list = await this.app.mysql.select('project', { where: other, limit, offset, orders: [[ 'number', 'asc' ]] }); + return list; + } + async remove(data) { + const result = await this.app.mysql.delete('project', data); + return result; + } +} + +module.exports = ProjectService; diff --git a/app/service/statistic.js b/app/service/statistic.js new file mode 100644 index 0000000..e412db3 --- /dev/null +++ b/app/service/statistic.js @@ -0,0 +1,22 @@ +'use strict'; + +const Service = require('egg').Service; + +class StatisticService extends Service { + async count() { + const result = await this.app.mysql.query(` + 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 ( + select count(*) as sale_contract_count, 0 as purchase_contract_count, 0 as customer_count, 0 as supplier_count from contract where type = 0 + union all + select 0 as sale_contract_count, count(*) as purchase_contract_count, 0 as customer_count, 0 as supplier_count from contract where type = 1 + union all + select 0 as sale_contract_count, 0 as purchase_contract_count, count(*) as customer_count, 0 as supplier_count from customer where type = 0 + union all + select 0 as sale_contract_count, 0 as purchase_contract_count, 0 as customer_count, count(*) as supplier_count from customer where type = 1 + ) t; + `); + return result && result.length > 0 ? result[0] : {}; + } +} + +module.exports = StatisticService; diff --git a/app/service/user.js b/app/service/user.js index 955c0fe..e2e89bd 100644 --- a/app/service/user.js +++ b/app/service/user.js @@ -21,7 +21,7 @@ class UserService extends Service { return user; } async list(data) { - const { current, pageSize } = data; + const { current = 1, pageSize = 10 } = data; const other = objExclude(data, [ 'current', 'pageSize' ]); const limit = Number(pageSize); const offset = Number((current - 1) * pageSize); diff --git a/app/util/common.js b/app/util/common.js index 51014d6..0e919f1 100644 --- a/app/util/common.js +++ b/app/util/common.js @@ -13,8 +13,15 @@ module.exports = { objToQuery(obj, prefix) { const arr = []; Object.keys(obj).forEach(key => { + arr.push(`${!prefix ? '' : `${prefix}.`}${key} like '%${obj[key]}%'`); + }); + return arr.join(' and '); + }, + objToSet(obj, prefix) { + const arr = []; + Object.keys(obj).forEach(key => { arr.push(`${!prefix ? '' : `${prefix}.`}${key} = ${obj[key]}`); }); - return arr.join('and'); + return arr.join(' , '); }, }; diff --git a/package.json b/package.json index a0535c1..6d37f8e 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "egg-cors": "^2.0.0", "egg-mysql": "^3.0.0", "egg-scripts": "^2.5.0", + "moment": "^2.22.1", "node-uuid": "^1.4.8" }, "devDependencies": { -- libgit2 0.21.0