'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;