123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- 'use strict';
- const Service = require('egg').Service;
- class VisitService extends Service {
- async health(query) {
- const { ctx } = this;
- query.health = { $ne: null };
- const resultList = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$health', count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 1, count: 1,
- },
- },
- ]);
- const resultCount = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $count: 'count' },
- ]);
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case '健康':
- resultArr.push({ label: '健康', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '一般':
- resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '较差':
- resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- default:
- console.log('老年人健康状况统计查询!');
- }
- });
- const health = ctx.health();
- health.forEach(item => {
- result.push({ label: item, value: 0 + '%' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async mind(query) {
- const { ctx } = this;
- query.$and = [{ mind: { $ne: null } }, { mind: { $ne: '健康' } }];
- const resultList = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$mind', count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 1, count: 1,
- },
- },
- ]);
- const resultCount = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $count: 'count' },
- ]);
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case '良好':
- resultArr.push({ label: '良好', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '一般':
- resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '较差':
- resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- default:
- console.log('老年人精神状态统计查询!');
- }
- });
- const mind = ctx.mind();
- mind.forEach(item => {
- result.push({ label: item, value: 0 + '%' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async security(query) {
- const { ctx } = this;
- query.security = { $ne: null };
- const resultList = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$security', count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 1, count: 1,
- },
- },
- ]);
- const resultCount = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $count: 'count' },
- ]);
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case '安全':
- resultArr.push({ label: '安全', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '一般':
- resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '较差':
- resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- default:
- console.log('老年人精神状态统计查询!');
- }
- });
- const security = ctx.security();
- security.forEach(item => {
- result.push({ label: item, value: 0 + '%' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async hygiene(query) {
- const { ctx } = this;
- query.$and = [{ hygiene: { $ne: null } }, { hygiene: { $ne: '健康' } }];
- const resultList = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$hygiene', count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 1, count: 1,
- },
- },
- ]);
- const resultCount = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $count: 'count' },
- ]);
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case '良好':
- resultArr.push({ label: '良好', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '一般':
- resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '较差':
- resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- default:
- console.log('老年人精神状态统计查询!');
- }
- });
- // mind可以与该类型共用
- const mind = ctx.mind();
- mind.forEach(item => {
- result.push({ label: item, value: 0 + '%' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async live(query) {
- const { ctx } = this;
- query.$and = [{ live: { $ne: null } }, { live: { $ne: '健康' } }];
- const resultList = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$live', count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 1, count: 1,
- },
- },
- ]);
- const resultCount = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $count: 'count' },
- ]);
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case '良好':
- resultArr.push({ label: '良好', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '一般':
- resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- case '较差':
- resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
- break;
- default:
- console.log('老年人精神状态统计查询!');
- }
- });
- // mind可以与该类型共用
- const mind = ctx.mind();
- mind.forEach(item => {
- result.push({ label: item, value: 0 + '%' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async selectUserBySex(query) {
- const { ctx } = this;
- query.sex = { $ne: null };
- const resultList = await ctx.model.SysUserModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$sex', count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 1, count: 1,
- },
- },
- ]);
- const resultCount = await ctx.model.SysUserModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $count: 'count' },
- ]);
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case '男':
- resultArr.push({ label: '男性巡防员', value: item.count, percent: Math.round(item.count / resultCount[0].count * 10000) / 100 + '' });
- break;
- case '女':
- resultArr.push({ label: '女性巡防员', value: item.count, percent: Math.round(item.count / resultCount[0].count * 10000) / 100 + '' });
- break;
- default:
- console.log('巡访员性别分布统计查询!');
- }
- });
- const userBySex = ctx.userBySex();
- userBySex.forEach(item => {
- result.push({ label: item, value: 0, percent: '0.00' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- itemA.percent = item.percent;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async selectAuthAndLook(query) {
- const { ctx } = this;
- const resultList = await ctx.model.SysUserModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $project: {
- _id: 1,
- politicalOutlook: '$politicalOutlook', // 政治面貌
- authentication: {
- // 认证状态
- $cond: {
- if: {
- $ifNull: [ '$file', false ],
- },
- then:
- {
- $cond:
- [{
- $ne: [{
- $ifNull: [ '$file', '' ],
- }, '' ],
- }, 0, 1 ],
- },
- else: 1,
- },
- },
- },
- },
- {
- $group: {
- _id: {
- authentication: '$authentication',
- politicalOutlook: '$politicalOutlook',
- },
- count: { $sum: 1 },
- },
- },
- {
- $group: {
- _id:
- {
- politicalOutlook: '$_id.politicalOutlook',
- authentication: '$_id.authentication',
- count: '$count',
- },
- },
- },
- {
- $match: {
- $or: [
- {
- '_id.authentication': 0,
- '_id.politicalOutlook': '群众',
- },
- {
- '_id.authentication': 0,
- '_id.politicalOutlook': '中共党员',
- },
- {
- '_id.authentication': 1,
- '_id.politicalOutlook': '群众',
- },
- {
- '_id.authentication': 1,
- '_id.politicalOutlook': '中共党员',
- },
- ],
- },
- },
- {
- $project: {
- _id: 0,
- politicalOutlook: '$_id.politicalOutlook',
- count: '$_id.count',
- status: { $cond: [{ $eq: [ '$_id.authentication', 0 ] }, '认证', '未认证' ] },
- label: { $concat: [{ $cond: [{ $eq: [ '$_id.authentication', 0 ] }, '认证', '未认证' ] }, '$_id.politicalOutlook' ] },
- },
- },
- ]);
- const resultArr = [];
- const result1 = []; // 党员
- const result2 = []; // 巡防员
- const result3 = []; // 党员
- const result4 = []; // 巡防员
- let result5 = {}; // 党员
- let result6 = {}; // 巡防员
- try {
- resultList.forEach(item => {
- switch (item.label) {
- case '认证中共党员':
- result1.push({ label: item.status, value: item.count });
- break;
- case '未认证中共党员':
- result1.push({ label: item.status, value: item.count });
- break;
- case '认证群众':
- result2.push({ label: item.status, value: item.count });
- break;
- case '未认证群众':
- result2.push({ label: item.status, value: item.count });
- break;
- default:
- console.log('巡访员性别分布统计查询!');
- }
- });
- // 党员
- const selectAuthAndLook1 = ctx.selectAuthAndLook();
- selectAuthAndLook1.forEach(item => {
- result3.push({ label: item, value: 0 });
- });
- result3.forEach(itemA => {
- result1.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- // 寻访员
- const selectAuthAndLook2 = ctx.selectAuthAndLook();
- selectAuthAndLook2.forEach(item => {
- result4.push({ label: item, value: 0 });
- });
- result4.forEach(itemA => {
- result2.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- result5 = { label: '党员', arr: result3 };
- result6 = { label: '群众', arr: result4 };
- resultArr.push(result5);
- resultArr.push(result6);
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return resultArr;
- }
- }
- async selectUserValue(query) {
- const { ctx } = this;
- const resultList = await ctx.model.ValueModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $group:
- {
- _id: '$userid',
- dept2: { $first: '$dept2' },
- dept3: { $first: '$dept3' },
- dept4: { $first: '$dept4' },
- dept5: { $first: '$dept5' },
- type0: { $sum: {
- $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
- } },
- type1: { $sum: {
- $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
- } },
- },
- },
- { $project: {
- _id: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1,
- infoValue: { $multiply: [ '$type0', 5 ] },
- visitValue: { $multiply: [ '$type1', 1 ] },
- totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
- } },
- { $sort: { totalValue: -1 } },
- {
- $limit: 10,
- },
- { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'user' } },
- { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
- { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 0,
- userName: '$user.userName',
- score: '$totalValue',
- address: '$dept4.name',
- } },
- ]);
- return resultList;
- }
- async selectByJob(query) {
- query.job = { $ne: null };
- const { ctx } = this;
- const allCount = await ctx.model.SysUserModel.find(ctx.alterDeptId(query)).count();
- const condition = [ '村', '社区', '民政' ];
- let knowJobCount = 0;
- let resultList;
- const resultArrCount = [];
- for (let i = 0; i < condition.length; i++) {
- resultList = await ctx.model.SysUserModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$job',
- count: {
- $sum: 1,
- },
- },
- },
- {
- $match: {
- _id: { $regex: condition[i] },
- },
- },
- ]);
- let resultCount = 0;
- resultList.forEach(item => {
- resultCount += item.count;
- });
- knowJobCount += resultCount;
- resultArrCount.push(resultCount);
- }
- const jobType = [ '村工作人员', '社区工作人员', '民政工作人员' ];
- const resultArr = [];
- for (let i = 0; i < resultArrCount.length; i++) {
- resultArr.push({ label: jobType[i], value: Math.round(resultArrCount[i] / allCount * 10000) / 100 + '%' });
- }
- resultArr.push({ label: '其他工作人员', value: (100 - Math.round(knowJobCount / allCount * 10000) / 100).toFixed(2) + '%' });
- return resultArr;
- }
- async selectDeptValue(query) {
- const { ctx } = this;
- const resultList = await ctx.model.ValueModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$type',
- count: {
- $sum: 1,
- },
- },
- },
- ]);
- const resultArr = [];
- let allValue = 0;
- resultList.forEach(item => {
- if (item._id === '0') { // 采集5积分
- allValue += item.count * 5;
- resultArr.push({ label: 'infoValue', value: item.count * 5 });
- } else { // 探访1积分
- allValue += item.count * 1;
- resultArr.push({ label: 'visitValue', value: item.count * 1 });
- }
- });
- resultArr.push({ label: 'totalValue', value: allValue });
- // 数组形式转为集合形式,与java返回结果对应
- const obj = {};
- resultArr.forEach(item => {
- const key = item.label;
- obj[key] = item.value;
- });
- return obj;
- }
- async visitWay(query) {
- const { ctx } = this;
- query.status = '3';
- const resultList = await ctx.model.InfoModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $project: {
- _id: 1,
- regularsInfo: 1,
- visitType1: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '上门巡访' ] }, 0 ] }, 1, 0 ] } },
- visitType2: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '电话问候' ] }, 0 ] }, 1, 0 ] } },
- visitType3: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '其它' ] }, 0 ] }, 1, 0 ] } },
- },
- },
- {
- $group: {
- _id: null,
- visitType1: { $sum: '$visitType1' },
- visitType2: { $sum: '$visitType2' },
- visitType3: { $sum: '$visitType3' },
- },
- },
- {
- $project: {
- _id: 0, visitType1: 1, visitType2: 1, visitType3: 1,
- },
- },
- ]);
- let arr = 0;
- // 数组内的value相加
- for (const i in resultList[0]) {
- arr += resultList[0][i];
- }
- const resultArr = [];
- const result = [];
- try {
- resultList.forEach(item => {
- if (item.visitType1) {
- resultArr.push({ label: '上门巡访', value: Math.round(item.visitType1 / arr * 10000) / 100 + '' });
- }
- if (item.visitType2) {
- resultArr.push({ label: '电话问候', value: Math.round(item.visitType2 / arr * 10000) / 100 + '' });
- }
- if (item.visitType3) {
- resultArr.push({ label: '其它', value: Math.round(item.visitType3 / arr * 10000) / 100 + '' });
- }
- });
- const visitWay = ctx.visitWay();
- visitWay.forEach(item => {
- result.push({ label: item, value: '0' });
- });
- result.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async visitnum(query) {
- const { ctx } = this;
- const resultList = await ctx.model.VisitModel.find(ctx.alterDeptId(query)).countDocuments();
- const resultArr = { visitnum: resultList };
- return resultArr;
- }
- }
- module.exports = VisitService;
|