123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- 'use strict';
- const Service = require('egg').Service;
- class OtherService extends Service {
- async selectUserByAuth(query) {
- const { ctx, app } = this;
- query.role = app.mongoose.Types.ObjectId('5d0c41046a0a730a44fe06b5');
- const resultList = await ctx.model.SysUserModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $project: {
- _id: 1,
- authentication: {
- $cond: { if: { $ifNull: [ '$file', false ] }, then:
- { $cond:
- [{ $ne: [{ $ifNull: [ '$file', '' ] }, '' ] }, 0, 1 ],
- }, else: 1,
- },
- },
- } },
- {
- $group: {
- _id: '$authentication',
- count: { $sum: 1 },
- },
- },
- { $sort: { _id: 1 } },
- ]);
- const resultArr = [];
- try {
- resultList.forEach(item => {
- switch (item._id) {
- case 0:
- resultArr.push({ authentication: item._id.toString(), count: item.count });
- break;
- case 1:
- resultArr.push({ authentication: item._id.toString(), count: item.count });
- break;
- default:
- console.log('其他接口--采集员认证统计查询!');
- }
- });
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return resultArr;
- }
- }
- async fourquan(query) {
- const { ctx, app } = this;
- // 1.子女绑定个数(公众号输入老人身份证,点击查询老人信息就绑定了)
- const familyCount = await ctx.model.FamilyModel.find(ctx.alterDeptId(query)).countDocuments();
- const bindFamilyChildCount = await ctx.model.FamilyModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $project: {
- _id: 1,
- child: {
- $cond:
- [{ $ne: [{ $ifNull: [ '$openId', []] }, []] }, 0, 1 ],
- },
- },
- },
- { $match: { child: 0 } },
- ]);
- // 2.积分存量
- const resultList2 = await ctx.model.ValueModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$type',
- count: {
- $sum: 1,
- },
- },
- },
- ]);
- let allValue = 0;
- resultList2.forEach(item => {
- if (item._id === '0') { // 采集5积分
- allValue += item.count * 5;
- } else { // 探访1积分
- allValue += item.count * 1;
- }
- });
- // 3.巡访平均周期
- const resultList3 = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- {
- $group: {
- _id: '$infoId',
- mintime: {
- $min: '$visitTime',
- },
- maxtime: {
- $max: '$visitTime',
- },
- sum: {
- $sum: 1,
- },
- },
- },
- {
- $project: {
- avgtime: {
- $divide: [{
- $subtract: [{
- $toDouble: '$maxtime',
- }, {
- $toDouble: '$mintime',
- }],
- }, '$sum' ],
- },
- },
- },
- {
- $match: {
- avgtime: {
- $ne: 0,
- },
- },
- },
- {
- $group: {
- _id: null,
- allAvgTime: { $sum: '$avgtime' },
- count: { $sum: 1 },
- avgTime: { $avg: '$avgtime' },
- },
- },
- {
- $project: {
- _id: 0, allAvgTime: 1, count: 1, avgTime: 1,
- visitAvgDay: { $divide: [ '$avgTime', 1000 * 60 * 60 * 24 ] },
- visitAvgDayString: { $toString: { $divide: [ '$avgTime', 1000 * 60 * 60 * 24 ] } },
- },
- },
- {
- $project: {
- visitAvgDay: { // 自定义字段
- $substr: [ '$visitAvgDayString', 0, // substr,截取字符串
- { $cond: [ // 三元运算
- { $gte: [{ $indexOfCP: [ '$visitAvgDayString', '.' ] }, 0 ] }, // 是否存在小数点
- { $add: [{ $indexOfCP: [ '$visitAvgDayString', '.' ] }, 3 ] }, // 存在时,多向后取3位
- { $strLenCP: '$visitAvgDayString' }, // 不存在,取字符串的长度
- ] },
- ] },
- },
- },
- ]);
- let collection1 = {};
- if (resultList3.length > 0) {
- collection1 = { label: '巡访平均周期', value: resultList3[0].visitAvgDay + '天' };
- } else {
- collection1 = { label: '巡访平均周期', value: 0 + '天' };
- }
- const collection2 = { label: '子女绑定占比', value: Math.round(bindFamilyChildCount.length / familyCount * 100000) / 1000 };
- const collection3 = { label: '积分存量', value: allValue.toString() };
- const collection4 = { label: '兑换比例', value: 0 + '%' };
- const collectionResult = { data1: collection1, data2: collection2, data3: collection3, data4: collection4 };
- return collectionResult;
- }
- async infomonth(query) {
- const { ctx } = this;
- query.status = '3';
- // 当前年份【采集】数据(按月份组)
- const resultList1 = await ctx.model.InfoModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $project: {
- _id: 1,
- // 这里需要加8小时,数据库里面的时间是ISO类型
- infoMonth: { $dateToString: { format: '%m', date: { $add: [ '$time', 1000 * 60 * 60 * 8 ] } } },
- infoTypeOfYear: { $sum: {
- $cond: [{ $eq: [
- { $dateToString: { format: '%Y', date: { $add: [ '$time', 1000 * 60 * 60 * 8 ] } } },
- { $dateToString: { format: '%Y', date: { $add: [ new Date(), 1000 * 60 * 60 * 8 ] } } },
- ] }, 1, 0 ],
- } },
- },
- },
- { $match: { infoTypeOfYear: 1 } },
- {
- $group: {
- _id: '$infoMonth',
- count: { $sum: 1 },
- },
- },
- { $sort: { _id: 1 } },
- ]);
- // 当前年份【探访】数据(按月份组)
- delete query.status;
- const resultList2 = await ctx.model.VisitModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $project: {
- _id: 1,
- // 这里需要加8小时,数据库里面的时间是ISO类型
- visitMonth: { $dateToString: { format: '%m', date: { $add: [ '$visitTime', 1000 * 60 * 60 * 8 ] } } },
- visitTypeOfYear: { $sum: {
- $cond: [{ $eq: [
- { $dateToString: { format: '%Y', date: { $add: [ '$visitTime', 1000 * 60 * 60 * 8 ] } } },
- { $dateToString: { format: '%Y', date: { $add: [ new Date(), 1000 * 60 * 60 * 8 ] } } },
- ] }, 1, 0 ],
- } },
- },
- },
- { $match: { visitTypeOfYear: 1 } },
- {
- $group: {
- _id: '$visitMonth',
- count: { $sum: 1 },
- },
- },
- { $sort: { _id: 1 } },
- ]);
- const infoResultArr = [];
- const visitResultArr = [];
- let infoCollection;
- let visitCollection;
- const resultArr = [];
- const result1 = [];
- const result2 = [];
- try {
- // 采集数据
- resultList1.forEach(item => {
- infoResultArr.push({ label: item._id.toString(), value: item.count });
- });
- const monthNowArrInfo = ctx.monthNowArr();
- monthNowArrInfo.forEach(item => {
- result1.push({ label: item, value: 0 });
- });
- result1.forEach(itemA => {
- infoResultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- infoCollection = { label: '采集数据', data: result1 };
- resultArr.push(infoCollection);
- // 寻访数据
- resultList2.forEach(item => {
- visitResultArr.push({ label: item._id.toString(), value: item.count });
- });
- const monthNowArrVisit = ctx.monthNowArr();
- monthNowArrVisit.forEach(item => {
- result2.push({ label: item, value: 0 });
- });
- result2.forEach(itemA => {
- visitResultArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- visitCollection = { label: '巡访数据', data: result2 };
- resultArr.push(visitCollection);
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return resultArr;
- }
- }
- async selectSexAndLook(query) {
- const { ctx, app } = this;
- query.role = app.mongoose.Types.ObjectId('5d0c41046a0a730a44fe06b5');
- query.$and = [
- { $and: [{ politicalOutlook: { $ne: null } }, { politicalOutlook: { $ne: '' } }] },
- { $and: [{ sex: { $ne: null } }, { sex: { $ne: '' } }] },
- ];
- const resultList = await ctx.model.SysUserModel.aggregate([
- { $match: ctx.alterDeptId(query) },
- { $project: {
- _id: 1, sex: 1, politicalOutlook: 1,
- },
- },
- {
- $group: {
- _id: {
- sex: '$sex',
- politicalOutlook: '$politicalOutlook',
- },
- count: { $sum: 1 },
- },
- },
- {
- $project: {
- _id: 0,
- label: { $concat: [ '$_id.sex', '$_id.politicalOutlook' ] },
- sex: '$_id.sex', politicalOutlook: '$_id.politicalOutlook', count: 1,
- },
- },
- ]);
- const partyMemberArr = []; // 党员
- const thMassesArr = []; // 群众
- let partyMemberCollection = {};
- let thMassesCollection = {};
- const resultArr = [];
- const result1 = [];
- const result2 = [];
- try {
- resultList.forEach(item => {
- switch (item.label) {
- case '男中共党员':
- partyMemberArr.push({ label: item.sex, value: item.count.toString() });
- break;
- case '女中共党员':
- partyMemberArr.push({ label: item.sex, value: item.count.toString() });
- break;
- case '男群众':
- thMassesArr.push({ label: item.sex, value: item.count.toString() });
- break;
- case '女群众':
- thMassesArr.push({ label: item.sex, value: item.count.toString() });
- break;
- default:
- console.log('其他接口--采集员 性别,政治面貌查询出现脏数据,但不影响本接口,请查看user表的politicalOutlook字段!');
- }
- });
- // 党员
- const partyMemberSexArr = ctx.sex();
- partyMemberSexArr.forEach(item => {
- result1.push({ label: item, value: 0 });
- });
- result1.forEach(itemA => {
- partyMemberArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- partyMemberCollection = { label: '党员', arr: result1 };
- // 群众
- const thMassesSexArr = ctx.sex();
- thMassesSexArr.forEach(item => {
- result2.push({ label: item, value: 0 });
- });
- result2.forEach(itemA => {
- thMassesArr.forEach(item => {
- if (item.label == itemA.label) {
- itemA.value = item.value;
- }
- });
- });
- thMassesCollection = { label: '群众', arr: result2 };
- resultArr.push(partyMemberCollection);
- resultArr.push(thMassesCollection);
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return resultArr;
- }
- }
- async oldType(data) {
- const matchStr = {};
- matchStr.fid = data.deptFid;
- matchStr.level = data.deptLevel + '';
- const abilityCond = this.getOldAbiltyCond(matchStr);
- const OldTypeCond = this.getOldTypeCond(matchStr);
- const facet = {};
- facet.oldTypeResult = OldTypeCond;
- facet.abiltyResult = abilityCond;
- const result = await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
- const oldTypeResult = result[0].oldTypeResult;
- const abiltyResult = result[0].abiltyResult;
- for (let i = 0; i < abiltyResult.length; i++) {
- // 获取当前元素
- const abilityItem = abiltyResult[i];
- // 获取对应的oldTypeResult元素
- const oldTypeItem = oldTypeResult[i];
- // 将ability字段添加到oldTypeResult元素中,并命名为'oldType9'
- oldTypeItem.oldType9 = abilityItem.ability;
- }
- delete result[0].abiltyResult;
- return result[0].oldTypeResult;
- }
- // 统计各地区老人类别
- 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: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '农村留守老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType2: { $sum: {
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '分散供养特困老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType3: { $sum: {
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '计划生育特殊家庭老人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType4: { $sum: {
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '空巢老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType5: { $sum: {
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '独居老年人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType6: { $sum: {
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '重残老人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType7: { $sum: {
- $cond: [
- { $and:
- [
- { $eq: [ '$infos.oldType', '高龄老人' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- } },
- oldType8: { $sum: {
- $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: [
- { $and:
- [
- { $eq: [ '$infos.ability', '失能' ] },
- { $eq: [ '$infos.status', '3' ] },
- ],
- }, 1, 0 ],
- },
- },
- },
- },
- {
- $sort: { order: 1 },
- },
- ];
- }
- }
- module.exports = OtherService;
|