diff --git a/app/controller/project.js b/app/controller/project.js index 58ad832..a57d20b 100644 --- a/app/controller/project.js +++ b/app/controller/project.js @@ -39,6 +39,24 @@ class ProjectController extends Controller { }; } } + async user_analysis() { + const ctx = this.ctx; + const param = qs.parse(this.ctx.query); + const date = param.date; + const id = param.id; + try { + const result = await ctx.service.project.user_analysis(id, date); + this.ctx.body = { + result, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } async analysis() { const ctx = this.ctx; const param = qs.parse(this.ctx.query); diff --git a/app/router.js b/app/router.js index cb6fa48..1ef805f 100644 --- a/app/router.js +++ b/app/router.js @@ -37,6 +37,7 @@ module.exports = app => { router.put('/contract', controller.contract.update); router.post('/contract/delete', controller.contract.remove); // 项目管理 + router.get('/project/analysis/user', controller.project.user_analysis); router.get('/project/analysis', controller.project.analysis); router.get('/project/list', controller.project.list); router.get('/project/:id', controller.project.info); diff --git a/app/service/contract.js b/app/service/contract.js index 3038dc1..b3bf6e7 100644 --- a/app/service/contract.js +++ b/app/service/contract.js @@ -6,10 +6,10 @@ const objToQuery = require('../util/common').objToQuery; class ContractService extends Service { async analysis(data) { - const where = objToQuery(data); + const where = objToQuery(data, 'contract'); const sql = ` - select * from contract - ${where !== '' ? `where ${where}` : ''} + select contract.*, user.name as manager_name from contract, user where contract.manager_id = user.id + ${where !== '' ? `and ${where}` : ''} `; console.log(sql); const result = await this.app.mysql.query(sql); diff --git a/app/service/project.js b/app/service/project.js index 93811a8..0ba15c7 100644 --- a/app/service/project.js +++ b/app/service/project.js @@ -5,6 +5,30 @@ const objExclude = require('../util/common').objExclude; const moment = require('moment'); class ProjectService extends Service { + async user_analysis(id, date) { + const sql = ` + select pj.*, user.name as manager_name from ( + select a.*,b.p_value, b.p_other_expenses,b.p_invoice from + ( + select + project.number, project.name, project.manager_id, project.create_time, contract.value as s_value, contract.other_expenses as s_other_expenses, contract.invoice as s_invoice + from project, contract where project.sale_contract_id = contract.id + ) a + left join + ( + select + project.number, project.name, project.manager_id, project.create_time, contract.value as p_value, contract.other_expenses as p_other_expenses, contract.invoice as p_invoice + from project, contract where project.purchase_contract_id = contract.id + ) b + on a.number = b.number + ) pj, user + where pj.manager_id = user.id + and pj.manager_id = ? + ${date !== undefined && date !== null ? ` and pj.create_time like '%${date}%'` : ''} + `; + const result = await this.app.mysql.query(sql, [ id ]); + return result; + } async analysis(date) { // const sql = ` // select @@ -20,20 +44,23 @@ class ProjectService extends Service { // where project.sale_contract_id = s_contract.id and project.purchase_contract_id = p_contract.id // `; const sql = ` - select a.*,b.p_value, b.p_other_expenses,b.p_invoice from - ( - select - project.number, project.name, project.create_time, contract.value as s_value, contract.other_expenses as s_other_expenses, contract.invoice as s_invoice - from project, contract where project.sale_contract_id = contract.id - ) a - left join - ( - select - project.number, project.name, project.create_time, contract.value as p_value, contract.other_expenses as p_other_expenses, contract.invoice as p_invoice - from project, contract where project.purchase_contract_id = contract.id - ) b - on a.number = b.number - ${date !== undefined && date !== null ? ` where a.create_time like '%${date}%'` : ''} + select pj.*, user.name as manager_name from ( + select a.*,b.p_value, b.p_other_expenses,b.p_invoice from + ( + select + project.number, project.name, project.manager_id, project.create_time, contract.value as s_value, contract.other_expenses as s_other_expenses, contract.invoice as s_invoice + from project, contract where project.sale_contract_id = contract.id + ) a + left join + ( + select + project.number, project.name, project.manager_id, project.create_time, contract.value as p_value, contract.other_expenses as p_other_expenses, contract.invoice as p_invoice + from project, contract where project.purchase_contract_id = contract.id + ) b + on a.number = b.number + ) pj, user + where pj.manager_id = user.id + ${date !== undefined && date !== null ? ` and pj.create_time like '%${date}%'` : ''} `; const result = await this.app.mysql.query(sql); return result; -- libgit2 0.21.0