1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 'use strict';
- const Service = require('../service/baseService');
- class FamilyService extends Service {
- tag() {
- return this.ctx.model.FamilyModel;
- }
- async bindList(data) {
- const page = parseInt(data.page);
- const rows = parseInt(data.rows);
- 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); // 社区
- }
- if (data.queryName) {
- where.oldInfo = { $regex: data.queryName };
- }
- const restltList = await this.ctx.model.VisitModel.aggregate([
- { $match: where },
- { $lookup: { from: 'family', localField: 'fid', foreignField: '_id', as: 'familys' } },
- { $unwind: { path: '$familys', preserveNullAndEmptyArrays: true } },
- { $match: { 'familys.openId': { $exists: true, $ne: [] } } },
- { $group:
- { _id: '$infoId',
- userid: { $first: '$userid' },
- oldInfo: { $first: '$oldInfo' },
- oldIdNumber: { $first: '$oldIdNumber' },
- fid: { $first: '$fid' },
- openId: { $first: '$familys.openId' },
- dept1: { $first: '$dept1' },
- dept2: { $first: '$dept2' },
- dept3: { $first: '$dept3' },
- dept4: { $first: '$dept4' },
- dept5: { $first: '$dept5' },
- },
- },
- {
- $skip: rows * (page - 1),
- },
- {
- $limit: rows,
- },
- { $lookup: { from: 'sys_user', localField: 'userid', foreignField: '_id', as: 'user' } },
- { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept1', foreignField: '_id', as: 'dept1' } },
- { $unwind: { path: '$dept1', 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,
- oldInfo: 1,
- oldIdNumber: 1,
- openId: '$openId',
- loginName: '$user.loginName',
- userName: '$user.userName',
- dept: { $concat: [ '$dept1.name', '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
- } },
- ]).allowDiskUse(true);
- const countResult = await this.ctx.model.VisitModel.aggregate([
- { $match: where },
- { $lookup: { from: 'family', localField: 'fid', foreignField: '_id', as: 'familys' } },
- { $unwind: { path: '$familys', preserveNullAndEmptyArrays: true } },
- { $match: { 'familys.openId': { $exists: true, $ne: [] } } },
- { $group: {
- _id: '$infoId',
- count: { $sum: 1 },
- } },
- {
- $count: 'count', // 总数
- },
- ]);
- let countT = 0;
- if (countResult[0]) {
- countT = countResult[0].count;
- }
- return { list: restltList, count: countT };
- }
- }
- module.exports = FamilyService;
|