123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- 'use strict';
- const Service = require('egg').Service;
- const mongoose = require('mongoose');
- class StatisticsService extends Service {
- // 1. 采集数据图
- async statistics(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const type = data.type;
- const userCond = this.getUserCond(matchStr);
- const getDeptManagerCond = this.getDeptManagerCond(matchStr);
- const infoCond = this.getInfoCond(matchStr);
- const abilityCond = this.getOldAbiltyCond(matchStr);
- const OldTypeCond = this.getOldTypeCond(matchStr);
- const facet = {};
- if (type != 'admin') {
- facet.userResult = userCond;
- if (type != 'user') {
- facet.infoResult = infoCond;
- facet.oldTypeResult = OldTypeCond;
- facet.abiltyResult = abilityCond;
- } else {
- if (data.deptLevel == 5) {
- userCond[3].$group.users = { $push: '$users.loginName' };
- }
- }
- } else {
- if (data.deptLevel == 5) {
- getDeptManagerCond[3].$group.users = { $push: '$users.loginName' };
- }
- facet.managerResult = getDeptManagerCond;
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- async user(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const type = data.type;
- const userCond = this.getUserCond(matchStr);
- const getDeptManagerCond = this.getDeptManagerCond(matchStr);
- const facet = {};
- if (type != 'admin') {
- facet.userResult = userCond;
- if (type != 'user') {
- } else {
- if (data.deptLevel == 5) {
- userCond[3].$group.users = { $push: '$users.loginName' };
- }
- }
- } else {
- if (data.deptLevel == 5) {
- getDeptManagerCond[3].$group.users = { $push: '$users.loginName' };
- }
- facet.managerResult = getDeptManagerCond;
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- async info(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const type = data.type;
- const infoCond = this.getInfoCond(matchStr);
- const facet = {};
- if (type != 'admin') {
- if (type != 'user') {
- facet.infoResult = infoCond;
- } else {
- if (data.deptLevel == 5) {
- }
- }
- } else {
- if (data.deptLevel == 5) {
- }
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- async oldType(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const type = data.type;
- const abilityCond = this.getOldAbiltyCond(matchStr);
- const OldTypeCond = this.getOldTypeCond(matchStr);
- const facet = {};
- if (type != 'admin') {
- if (type != 'user') {
- facet.oldTypeResult = OldTypeCond;
- facet.abiltyResult = abilityCond;
- } else {
- if (data.deptLevel == 5) {
- }
- }
- } else {
- if (data.deptLevel == 5) {
- }
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- // 地区管理员tree
- getDeptManagerCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup:
- {
- from: 'sys_user',
- let: { dept_id: '$_id' },
- pipeline: [
- { $match:
- { $expr:
- { $and:
- [
- { $eq: [ '$dept' + matchStr.level, '$$dept_id' ] },
- { $eq: [ '$role', mongoose.Types.ObjectId(this.app.config.defaultManagerRoleId) ] },
- ],
- },
- },
- },
- ],
- as: 'users',
- },
- },
- { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
- { $group:
- {
- _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- total: { $sum: { $cond: [ '$users', 1, 0 ] } },
- },
- },
- { $sort: { order: 1 } },
- ];
- }
- // 统计各地区采集员,认证情况
- getUserCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup:
- {
- from: 'sys_user',
- let: { dept_id: '$_id' },
- pipeline: [
- { $match:
- { $expr:
- { $and:
- [
- { $eq: [ '$dept' + matchStr.level, '$$dept_id' ] },
- { $eq: [ '$role', mongoose.Types.ObjectId(this.app.config.defaultUserRoleId) ] },
- ],
- },
- },
- },
- ],
- as: 'users',
- },
- },
- { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
- { $group:
- {
- _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- noIdentify: {
- $sum: {
- $cond: { if: { $ifNull: [ '$users', false ] }, then:
- { $cond:
- [{ $ne: [{ $ifNull: [ '$users.file', '' ] }, '' ] }, 0, 1 ],
- }, else: 1,
- },
- },
- },
- identify: {
- $sum: {
- $cond:
- [{ $ne: [{ $ifNull: [ '$users.file', '' ] }, '' ] }, 1, 0 ],
- },
- },
- total: { $sum: { $cond: [ '$users', 1, 0 ] } },
- },
- },
- { $sort: { order: 1 } },
- ];
- }
- // 统计各地区采集记录,审核情况
- getInfoCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'info', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'infos' } },
- { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- total: { $sum: {
- $cond: [ '$infos', 1, 0 ],
- } },
- status0: { $sum: {
- $cond: [{ $eq: [ '$infos.status', '0' ] }, 1, 0 ],
- } },
- status1: { $sum: {
- $cond: [{ $eq: [ '$infos.status', '1' ] }, 1, 0 ],
- } },
- status2: { $sum: {
- $cond: [{ $eq: [ '$infos.status', '2' ] }, 1, 0 ],
- } },
- status3: { $sum: {
- $cond: [{ $eq: [ '$infos.status', '3' ] }, 1, 0 ],
- } },
- status4: { $sum: {
- $cond: [{ $eq: [ '$infos.status', '4' ] }, 1, 0 ],
- } },
- },
- },
- { $sort: { order: 1 } },
- ];
- }
- // 统计各地区老人类别
- getOldTypeCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'info', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'infos' } },
- { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$infos.oldType', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$infos.status', preserveNullAndEmptyArrays: true } },
- // { $match: { 'infos.status': '3' } },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- oldType1: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '农村留守老年人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '农村留守老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType2: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '分散供养特困老年人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '分散供养特困老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType3: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '计划生育特殊家庭老人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '计划生育特殊家庭老人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType4: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '空巢老年人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '空巢老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType5: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '独居老年人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '独居老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType6: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '重残老人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '重残老人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType7: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '高龄老人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '高龄老人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType8: { $sum: {
- // $cond: [{ $eq: [ '$infos.oldType', '高龄老人' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '居家老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- },
- },
- { $sort: { order: 1 } },
- ];
- }
- // 统计各地区失能老人情况
- getOldAbiltyCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'info', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'infos' } },
- { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$infos.status', preserveNullAndEmptyArrays: true } },
- { $group:
- {
- _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- ability: {
- $sum: {
- // $cond: [{ $eq: [ '$infos.ability', '失能' ] }, 1, 0 ],
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.ability', '失能' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- },
- },
- },
- },
- {
- $sort: { order: 1 },
- },
- ];
- }
- // 2. 吉林省全县区74个审核成功统计图表 优化
- async dept3() {
- return await this.ctx.model.InfoModel.aggregate([
- { $match: { status: '3', dept1: this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082') } },
- { $group:
- { _id: '$dept3',
- dept2: { $first: '$dept2' },
- total: { $sum: 1 },
- },
- },
- { $lookup: { from: 'sys_dept', localField: '_id', foreignField: '_id', as: 'dept' } },
- { $unwind: { path: '$dept', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'fdept' } },
- { $unwind: { path: '$fdept', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1,
- dept2: 1,
- deptName: { $concat: [ '$fdept.name', '$dept.name' ] },
- total: '$total',
- } },
- { $sort: { total: -1 } },
- ]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- // // 2. 吉林省全县区74个审核成功统计图表
- // async dept3Statistics() {
- // return await this.ctx.model.SysDeptModel.aggregate([
- // { $match: { level: '3' } },
- // { $lookup: { from: 'info', localField: '_id', foreignField: 'dept3', as: 'infos' } },
- // { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
- // { $match: { 'infos.status': '3', 'infos.dept1': this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082') } },
- // { $group:
- // { _id: '$_id',
- // deptName: { $first: '$name' },
- // level: { $first: '$level' },
- // dept2: { $first: '$infos.dept2' },
- // total: { $sum: {
- // $cond: [ '$infos', 1, 0 ],
- // } },
- // },
- // },
- // { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'fdept' } },
- // { $unwind: { path: '$fdept', preserveNullAndEmptyArrays: true } },
- // { $project: {
- // _id: 1,
- // deptName: { $concat: [ '$fdept.name', '$deptName' ] },
- // level: '$level',
- // total: '$total',
- // } },
- // { $sort: { total: -1 } },
- // ]).allowDiskUse(true);// 允许使用磁盘缓存
- // }
- // 3. 地区积分统计
- async deptValues(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const type = data.type;
- const deptValuesCond = this.getDeptValuesCond(matchStr);
- const facet = {};
- if (type != 'user') {
- facet.deptValuesCond = deptValuesCond;
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- getDeptValuesCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'value', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'values' } },
- { $unwind: { path: '$values', preserveNullAndEmptyArrays: true } },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- total: { $sum: {
- $cond: [ '$values', 1, 0 ],
- } },
- type0: { $sum: {
- $cond: [{ $eq: [ '$values.type', '0' ] }, 1, 0 ],
- } },
- type1: { $sum: {
- $cond: [{ $eq: [ '$values.type', '1' ] }, 1, 0 ],
- } },
- },
- },
- { $project: {
- _id: 1,
- deptName: 1,
- deptCode: 1,
- level: 1,
- fid: 1,
- order: 1,
- total: 1,
- type0: 1,
- type1: 1,
- totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
- } },
- { $sort: { order: 1 } },
- ];
- }
- // 4. 个人积分统计-优化
- async userValues(data) {
- const where = {};
- if (data.dept1) {
- where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
- }
- if (data.dept2) {
- where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
- }
- if (data.dept3) {
- where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
- }
- if (data.dept4) {
- where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
- }
- if (data.dept5) {
- where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
- }
- const restltList = await this.ctx.model.ValueModel.aggregate([
- {
- $match: where,
- },
- { $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: 100,
- },
- { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'user' } },
- { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'dept2' } },
- { $unwind: { path: '$dept2', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'dept3' } },
- { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
- { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5' } },
- { $unwind: { path: '$dept5', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1,
- loginName: '$user.loginName',
- userName: '$user.userName',
- infoValue: 1,
- visitValue: 1,
- totalValue: 1,
- dept: { $concat: [ '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
- } },
- ]).allowDiskUse(true);// 允许使用磁盘缓存
- return restltList;
- }
- // // 个人积分统计-前100
- // async values(data) {
- // const where = {};
- // if (data.dept1) {
- // where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
- // }
- // if (data.dept2) {
- // where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
- // }
- // if (data.dept3) {
- // where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
- // }
- // if (data.dept4) {
- // where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
- // }
- // if (data.dept5) {
- // where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
- // }
- // const restltList = await this.ctx.model.ValueModel.aggregate([
- // {
- // $match: where,
- // },
- // { $group:
- // {
- // _id: '$userid',
- // type0: { $sum: {
- // $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
- // } },
- // type1: { $sum: {
- // $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
- // } },
- // },
- // },
- // { $project: {
- // _id: 1,
- // infoValue: { $multiply: [ '$type0', 5 ] },
- // visitValue: { $multiply: [ '$type1', 1 ] },
- // totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
- // } },
- // { $sort: { totalValue: -1 } },
- // {
- // $limit: 100,
- // },
- // ]).allowDiskUse(true);// 允许使用磁盘缓存
- // const newResult = [];
- // for (const item of restltList) {
- // const user = await this.ctx.model.SysUserModel.findOne({ _id: this.app.mongoose.Types.ObjectId(item._id) }).populate(this.ctx.getUserPop());
- // newResult.push({ _id: item._id,
- // totalValue: item.totalValue,
- // infoValue: item.infoValue,
- // visitValue: item.visitValue,
- // userName: user.userName,
- // loginName: user.loginName,
- // dept: user.dept2.name + user.dept3.name + user.dept4.name + user.dept5.name,
- // });
- // }
- // return newResult;
- // // return restltList;
- // }
- // 5. 探访类别分布图
- async visitDeptOldType(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const where = {};
- if (data.startTime && data.endTime) {
- where['visits.visitTime'] = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') };
- }
- const type = data.type;
- const visitOldTypeCond = this.visitOldTypeCond(matchStr, where);
- const visitDeptStatisticsCond = this.visitDeptStatisticsCond(matchStr, where);
- const facet = {};
- if (type != 'user') {
- facet.visitOldTypeCond = visitOldTypeCond;
- facet.visitDeptStatisticsCond = visitDeptStatisticsCond;
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- // 统计各地区老人类别
- visitOldTypeCond(matchStr, where) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'visit', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'visits' } },
- { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$visits.infoOldType', preserveNullAndEmptyArrays: true } },
- { $match: where },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- // total: { $sum: {
- // $cond: [ '$visits', 1, 0 ],
- // } },
- oldType1: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '农村留守老年人' ] }, 1, 0 ],
- } },
- oldType2: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '分散供养特困老年人' ] }, 1, 0 ],
- } },
- oldType3: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '计划生育特殊家庭老人' ] }, 1, 0 ],
- } },
- oldType4: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '空巢老年人' ] }, 1, 0 ],
- } },
- oldType5: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '独居老年人' ] }, 1, 0 ],
- } },
- oldType6: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '重残老人' ] }, 1, 0 ],
- } },
- oldType7: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '高龄老人' ] }, 1, 0 ],
- } },
- oldType8: { $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '居家老年人' ] }, 1, 0 ],
- } },
- ability: { // 原版-这里统计出错,先拆分,在sum,这样['居家','高龄'] 失能会计算2次.-CH-2023-8-15;
- // 修改后,能力情况为失能时,我给infoOldType添加了[失能老人]标签(一次性处理),且采集时业务逻辑也修改成这样。
- $sum: {
- $cond: [{ $eq: [ '$visits.infoOldType', '失能老人' ] }, 1, 0 ],
- },
- },
- },
- },
- { $sort: { order: 1 } },
- ];
- }
- // 统计各地区探访数量
- visitDeptStatisticsCond(matchStr, where) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'visit', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'visits' } },
- { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
- { $match: where },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- deptCode: { $first: '$code' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- order: { $first: '$order' },
- total: { $sum: {
- $cond: [ '$visits', 1, 0 ],
- } },
- },
- },
- { $sort: { total: -1 } },
- ];
- }
- // 6. 探访县区分布图
- async visitDept3Statistics(data) {
- const where = {};
- where['visits.dept1'] = this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082');// 吉林省
- if (data.startTime && data.endTime) {
- where['visits.visitTime'] = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') };
- }
- const matchStr = {};
- matchStr.level = '3';
- const visitDept3StatisticsCond = this.visitDept3StatisticsCond(where);
- const visitOldTypeCond = this.visitOldTypeCond(matchStr, where);
- const facet = {};
- facet.visitDept3StatisticsCond = visitDept3StatisticsCond;
- facet.visitOldTypeCond = visitOldTypeCond;
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- // 县区探访统计
- visitDept3StatisticsCond(where) {
- return [
- {
- $match: { level: '3' },
- },
- { $lookup: { from: 'visit', localField: '_id', foreignField: 'dept3', as: 'visits' } },
- { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
- {
- $match: where,
- },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- dept2: { $first: '$visits.dept2' },
- total: { $sum: {
- $cond: [ '$visits', 1, 0 ],
- } },
- },
- },
- { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'fdept' } },
- { $unwind: { path: '$fdept', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1,
- deptName: { $concat: [ '$fdept.name', '$deptName' ] },
- total: '$total',
- } },
- { $sort: { total: -1 } },
- ];
- }
- async urgencyStatistics(data) {
- const matchStr = {};
- if (data.deptFid && data.deptFid !== '0') {
- matchStr.fid = data.deptFid;
- }
- if (data.deptId) {
- matchStr._id = data.deptId;
- }
- matchStr.level = data.deptLevel + '';
- const type = data.type;
- const urgencyCond = this.getUrgencyCond(matchStr);
- const facet = {};
- if (type != 'admin') {
- if (type != 'user') {
- facet.urgencyResult = urgencyCond;
- } else {
- if (data.deptLevel == 5) {
- }
- }
- } else {
- if (data.deptLevel == 5) {
- }
- }
- return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- }
- getUrgencyCond(matchStr) {
- return [
- { $match: matchStr },
- { $lookup: { from: 'urgency_notice', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'urgencys' } },
- { $unwind: { path: '$urgencys', preserveNullAndEmptyArrays: true } },
- { $group:
- { _id: '$_id',
- deptName: { $first: '$name' },
- level: { $first: '$level' },
- fid: { $first: '$fid' },
- total: { $sum: {
- $cond: [ '$urgencys', 1, 0 ],
- } },
- status0: { $sum: {
- $cond: [{ $eq: [ '$urgencys.status', '0' ] }, 1, 0 ],
- } },
- status1: { $sum: {
- $cond: [{ $eq: [ '$urgencys.status', '1' ] }, 1, 0 ],
- } },
- },
- },
- { $sort: { order: 1 } },
- ];
- }
- }
- module.exports = StatisticsService;
|