You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
5.5 KiB
163 lines
5.5 KiB
'use strict';
|
|
const fs = require('fs');
|
|
const moment = require('moment');
|
|
// 查询储备项目统计表
|
|
async function getReserveItemReport(ctx, next) {
|
|
const { type } = ctx.params;
|
|
let rslt = null;
|
|
try {
|
|
rslt = await ctx.fs.dc.models.ReserveItemReport.findAll({
|
|
order: [['year', 'DESC'], ['month', 'DESC']],
|
|
where: { type: type }
|
|
})
|
|
let newReportData = rslt.map(e => {
|
|
return {
|
|
id: e.id,
|
|
date: e.year + '-' + e.month,
|
|
path: e.path,
|
|
type: e.type,
|
|
name: e.name
|
|
}
|
|
})
|
|
ctx.status = 200
|
|
ctx.body = newReportData
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path:${ctx.path},error:${error}`)
|
|
ctx.status = 400;
|
|
ctx.body = { name: 'FindAllError', message: '获取失败' }
|
|
}
|
|
}
|
|
|
|
async function getSalersReport(ctx) {
|
|
try {
|
|
const { clickHouse } = ctx.app.fs
|
|
const { memberList, packageUserData } = ctx.app.fs.utils
|
|
const {
|
|
keywordTarget, keyword, limit, page, state,
|
|
hiredateStart, hiredateEnd, marital, native, workPlace,
|
|
orderBy, orderDirection, placeSearch, toExport
|
|
} = ctx.query
|
|
|
|
const userRes = await memberList({
|
|
keywordTarget, keyword, limit: '', page: '', state,
|
|
hiredateStart, hiredateEnd, marital, native, workPlace,
|
|
orderBy, orderDirection,
|
|
nowAttendanceTime: true
|
|
})
|
|
|
|
let { packageUser: members } = await packageUserData(userRes, {
|
|
state: true,
|
|
})
|
|
let mIds = members.map(m => m.pepUserId);
|
|
|
|
let innerSelectQuery = `where del=false and pep_user_id in [${mIds}]`
|
|
+ `${placeSearch ? `
|
|
and (sales.provinces LIKE '%${placeSearch}%' or sales.cities LIKE '%${placeSearch}%')
|
|
`: ''}`
|
|
|
|
const salersRes = await clickHouse.hr.query(`
|
|
SELECT * from sales_distribution as sales
|
|
${innerSelectQuery}
|
|
order by id desc
|
|
${!toExport && limit ? `LIMIT ${limit}` : ''}
|
|
${!toExport && limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
|
|
`).toPromise()
|
|
|
|
const countRes = await clickHouse.hr.query(`
|
|
SELECT
|
|
count(sales.pep_user_id) AS count from sales_distribution as sales
|
|
${innerSelectQuery}
|
|
`).toPromise()
|
|
let rslt = []
|
|
salersRes.map(d => {
|
|
let info = members.find(m => m.pepUserId == d.pep_user_id);
|
|
let item = {
|
|
name: info.userName,
|
|
userCode: info.userCode,
|
|
post: info.userPost,
|
|
department: info.departmrnt,
|
|
hireDate: info.hiredate,//入职时间
|
|
regularDate: info.regularDate,//转正时间
|
|
businessLines: d.business_lines,//业务线
|
|
...d
|
|
}
|
|
rslt.push(item);
|
|
})
|
|
if (toExport) {
|
|
await exportSalesDetail(ctx, rslt);//导出
|
|
} else {
|
|
ctx.status = 200;
|
|
ctx.body = {
|
|
count: countRes.length ? countRes[0].count : 0,
|
|
rows: rslt
|
|
};
|
|
}
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path:${ctx.path},error:${error}`)
|
|
ctx.status = 400;
|
|
ctx.body = { name: 'FindAllError', message: '获取销售人员分布明细表数据失败' }
|
|
}
|
|
}
|
|
|
|
async function exportSalesDetail(ctx, dataList) {
|
|
try {
|
|
let header = [{
|
|
title: "姓名",
|
|
key: 'name'
|
|
}, {
|
|
title: "部门名称",
|
|
key: 'department'
|
|
}, {
|
|
title: "销售区域(省/直辖市)",
|
|
key: 'provinces'
|
|
}, {
|
|
title: "销售区域(市)",
|
|
key: 'cities'
|
|
}, {
|
|
title: "业务线",
|
|
key: 'businessLines'
|
|
}, {
|
|
title: "岗位",
|
|
key: 'post'
|
|
}, {
|
|
title: "入职时间",
|
|
key: 'hireDate'
|
|
}, {
|
|
title: "转正时间",
|
|
key: 'regularDate'
|
|
}, {
|
|
title: "工龄",
|
|
key: 'workYears'
|
|
}];
|
|
const { utils: { simpleExcelDown } } = ctx.app.fs;
|
|
let exportData = []
|
|
for (let item of dataList) {
|
|
item.department = item.department.map(t => t.name).join('、') || '-';
|
|
// item.cities = item.cities || '-';
|
|
// item.businessLines = item.businessLines || '-';
|
|
// item.post = item.post || '-';
|
|
// item.regularDate = item.regularDate || '-';
|
|
item.workYears = item.hireDate ? String(moment(new Date()).diff(item.hireDate, 'years', true)).substring(0, 3) + '年' : '-'
|
|
//item.hireDate = item.hireDate || '-';
|
|
exportData.push(item)
|
|
}
|
|
const fileName = `销售人员分布明细表_${moment().format('YYYYMMDDHHmmss')}` + '.xlsx'
|
|
const filePath = await simpleExcelDown({ data: exportData, header, fileName: fileName })
|
|
const fileData = fs.readFileSync(filePath);
|
|
ctx.status = 200;
|
|
ctx.set('Content-Type', 'application/x-xls');
|
|
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
|
|
ctx.body = fileData;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getReserveItemReport,
|
|
getSalersReport
|
|
}
|