'use strict'; const Service = require('egg').Service; class InfoService extends Service { async fournums(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $project: { _id: 1, dept1: { $toString: '$dept1' }, age: { $subtract: [{ $year: new Date() }, { $toDouble: { $substr: [ '$birthday', 0, 4 ] } }], }, } }, { $group: { _id: { $concat: [ { $cond: [{ $lt: [ '$age', 60 ] }, '60以下', '' ] }, { $cond: [{ $and: [{ $gte: [ '$age', 60 ] }, { $lt: [ '$age', 70 ] }] }, '60-70岁', '' ] }, { $cond: [{ $and: [{ $gte: [ '$age', 70 ] }, { $lt: [ '$age', 80 ] }] }, '70-80岁', '' ] }, { $cond: [{ $and: [{ $gte: [ '$age', 80 ] }, { $lt: [ '$age', 85 ] }] }, '80-85岁', '' ] }, { $cond: [{ $gte: [ '$age', 85 ] }, '85岁以上', '' ] }, ], }, count: { $sum: 1 }, }, }, { $sort: { _id: 1 } }, ]); let allCount = 0; for (const index in resultList) { for (const i in resultList[index]) { if (i === 'count') { allCount += resultList[index][i]; } } } const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '60-70岁': resultArr.push({ label: item._id, value: item.count, percent: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; case '70-80岁': resultArr.push({ label: item._id, value: item.count, percent: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; case '80-85岁': resultArr.push({ label: item._id, value: item.count, percent: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; case '85岁以上': resultArr.push({ label: item._id, value: item.count, percent: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; default: console.log('老年信息统计--年龄分布查询失败!'); } }); const fournums = ctx.fournums(); fournums.forEach(item => { result.push({ label: item, value: 0, percent: 0 + '%' }); }); 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 sex(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $group: { _id: '$sex', count: { $sum: 1 }, }, }, { $sort: { _id: -1 } }, ]); let allCount = 0; for (const index in resultList) { for (const i in resultList[index]) { if (i === 'count') { allCount += resultList[index][i]; } } } const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '男': resultArr.push({ label: '老年人男性人数', value: item.count, percent: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; case '女': resultArr.push({ label: '老年人女性人数', value: item.count, percent: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; default: console.log('老年信息统计--性别分布查询失败!'); } }); const infoSex = ctx.infoSex(); infoSex.forEach(item => { result.push({ label: item, value: 0, percent: 0 + '%' }); }); 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 ability(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $project: { _id: 1, ability: 1, } }, { $group: { _id: '$ability', count: { $sum: 1 }, }, }, ]); let allCount = 0; let halfDisability = 0; for (const index in resultList) { for (const i in resultList[index]) { if (i === 'count') { allCount += resultList[index][i]; } if (resultList[index][i] === '半失能(部分自理)' || resultList[index][i] === '半失能(部分自理)') { halfDisability += resultList[index].count; } } } const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '完全自理': resultArr.push({ label: '完全自理', value: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; case '失能': resultArr.push({ label: '失能', value: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; } }); if (halfDisability > 0) { resultArr.push({ label: '半失能', value: Math.round(halfDisability / allCount * 10000) / 100 + '%' }); } // 保持能力评估类别完整性 const ability = ctx.ability(); ability.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 oldHealth(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $unwind: { path: '$health', preserveNullAndEmptyArrays: true } }, { $project: { _id: 1, health: 1, } }, { $group: { _id: '$health', count: { $sum: 1 }, }, }, ]); const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '健康': resultArr.push({ label: item._id, value: item.count }); break; case '残疾': resultArr.push({ label: item._id, value: item.count }); break; case '老年人慢性病': resultArr.push({ label: item._id, value: item.count }); break; case '患重特大疾病': resultArr.push({ label: item._id, value: item.count }); break; } }); // 保持能力评估类别完整性 const oldHealth = ctx.oldHealth(); oldHealth.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 oldType(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $unwind: { path: '$oldType', preserveNullAndEmptyArrays: true } }, { $project: { dept1: 1, _id: 1, oldType: 1, } }, { $group: { _id: '$oldType', count: { $sum: 1 }, }, }, ]); const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '失能老人': resultArr.push({ label: '失能', value: item.count }); break; case '居家老年人': resultArr.push({ label: '居家', value: item.count }); break; case '重残老人': resultArr.push({ label: '重残', value: item.count }); break; case '独居老年人': resultArr.push({ label: '独居', value: item.count }); break; case '计划生育特殊家庭老人': resultArr.push({ label: '失独', value: item.count }); break; case '分散供养特困老年人': resultArr.push({ label: '特困', value: item.count }); break; case '空巢老年人': resultArr.push({ label: '空巢', value: item.count }); break; case '高龄老人': resultArr.push({ label: '高龄', value: item.count }); break; case '农村留守老年人': resultArr.push({ label: '留守', value: item.count }); break; } }); const oldType = ctx.oldType(); oldType.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 visitFrequency(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $project: { dept1: 1, _id: 1, regularsInfo: 1, visitCountType1: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '"visitCount":"每天"' ] }, 0 ] }, 1, 0 ] } }, visitCountType2: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '"visitCount":"每周"' ] }, 0 ] }, 1, 0 ] } }, visitCountType3: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '"visitCount":"每月"' ] }, 0 ] }, 1, 0 ] } }, visitCountType4: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '"visitCount":"每季"' ] }, 0 ] }, 1, 0 ] } }, visitCountType5: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '"visitCount":"其它"' ] }, 0 ] }, 1, 0 ] } }, } }, { $group: { _id: null, visitCountType1: { $sum: '$visitCountType1' }, visitCountType2: { $sum: '$visitCountType2' }, visitCountType3: { $sum: '$visitCountType3' }, visitCountType4: { $sum: '$visitCountType4' }, visitCountType5: { $sum: '$visitCountType5' }, }, }, { $project: { _id: 0, visitCountType1: 1, visitCountType2: 1, visitCountType3: 1, visitCountType4: 1, visitCountType5: 1, }, }, ]); let allCount = 0; for (const i in resultList[0]) { allCount += resultList[0][i]; } const resultArr = []; const result = []; try { // 遍历集合 for (const key in resultList[0]) { if (key === 'visitCountType1') { resultArr.push({ label: '每天', value: resultList[0][key] }); } if (key === 'visitCountType2') { resultArr.push({ label: '每周', value: resultList[0][key] }); } if (key === 'visitCountType3') { resultArr.push({ label: '每月', value: resultList[0][key] }); } if (key === 'visitCountType4') { resultArr.push({ label: '每季', value: resultList[0][key] }); } if (key === 'visitCountType5') { resultArr.push({ label: '其他', value: resultList[0][key] }); } } const visitFrequency = ctx.visitFrequency(); visitFrequency.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 oldIncome(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $unwind: { path: '$sourceOfIncome', preserveNullAndEmptyArrays: true } }, { $project: { dept1: 1, _id: 1, sourceOfIncome: 1, } }, { $group: { _id: '$sourceOfIncome', count: { $sum: 1 }, }, }, ]); const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '财产性收入': resultArr.push({ label: item._id, value: item.count }); break; case '转移性收入': resultArr.push({ label: item._id, value: item.count }); break; case '家庭经营性收入': resultArr.push({ label: item._id, value: item.count }); break; case '工资性收入': resultArr.push({ label: item._id, value: item.count }); break; case '其它': resultArr.push({ label: item._id, value: item.count }); break; default: console.log('老年人收入来源统计查询出现脏数据!'); } }); const oldIncome = ctx.oldIncome(); oldIncome.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 income(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $project: { dept1: 1, _id: 1, income: 1, } }, { $group: { _id: '$income', count: { $sum: 1 }, }, }, ]); let allCount = 0; let disposableIncome = 0; for (const index in resultList) { for (const i in resultList[index]) { if (i === 'count') { allCount += resultList[index][i]; } if (resultList[index][i] === '5000元-10000元' || resultList[index][i] === '5000元~10000元') { console.log(); disposableIncome += resultList[index].count; } } } const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '5000元以下': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; case '10000元以上': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 + '%' }); break; } }); if (disposableIncome > 0) { resultArr.push({ label: '5000~10000元', value: Math.round(disposableIncome / allCount * 10000) / 100 + '%' }); } const income = ctx.income(); income.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 livingCondition(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $project: { _id: 1, livingCondition: 1, } }, { $group: { _id: '$livingCondition', count: { $sum: 1 }, }, }, ]); const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '特困供养人员': resultArr.push({ label: item._id, value: item.count }); break; case '一般家庭': resultArr.push({ label: item._id, value: item.count }); break; case '建档立卡贫困户': resultArr.push({ label: item._id, value: item.count }); break; case '低保': resultArr.push({ label: item._id, value: item.count }); break; default: console.log('老年人生活经济状况统计查询出现脏数据(livingCondition字段)!'); } }); const livingCondition = ctx.livingCondition(); livingCondition.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 oldDemand(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.aggregate([ { $match: ctx.alterDeptId(query) }, { $unwind: { path: '$demand', preserveNullAndEmptyArrays: true } }, { $project: { dept1: 1, _id: 1, demand: 1, } }, { $group: { _id: '$demand', count: { $sum: 1 }, }, }, ]); let allCount = 0; let spiritualConsolation = 0; // 精神慰藉 let safetyMonitoring = 0; // 安全监护 for (const index in resultList) { for (const i in resultList[index]) { if (i === 'count') { allCount += resultList[index][i]; } if (resultList[index][i] === '精神慰籍' || resultList[index][i] === '精神慰藉') { spiritualConsolation += resultList[index].count; } if (resultList[index][i] === '安全服务' || resultList[index][i] === '安全监护') { safetyMonitoring += resultList[index].count; } } } const resultArr = []; const result = []; try { resultList.forEach(item => { switch (item._id) { case '权益维护': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; case '文化娱乐': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; case '生产帮助': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; case '生活照料': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; case '紧急救助': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; case '医疗保健': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; case '其它': resultArr.push({ label: item._id, value: Math.round(item.count / allCount * 10000) / 100 }); break; } }); resultArr.push({ label: '精神慰藉', value: Math.round(spiritualConsolation / allCount * 10000) / 100 }); resultArr.push({ label: '安全监护', value: Math.round(safetyMonitoring / allCount * 10000) / 100 }); const oldDemand = ctx.oldDemand(); oldDemand.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 oldnum(query) { const { ctx } = this; query.status = '3'; const resultList = await ctx.model.InfoModel.find(ctx.alterDeptId(query)).countDocuments(); const resultArr = { oldnum: resultList }; return resultArr; } } module.exports = InfoService;