123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 'use strict';
- const Service = require('../service/baseService');
- const { sma4: sm4 } = require('../util/sm4');
- 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 = {};
- const whereInfo = {};
- where.openId = { $exists: true, $ne: [] };
- 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) {
- whereInfo.oldInfo = sm4.encrypt_ECB(data.queryName);
- }
- const restltList = await this.ctx.model.FamilyModel.aggregate([
- { $match: where },
- { $lookup: { from: 'info', localField: '_id', foreignField: 'fid', as: 'infos' } },
- { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_user', localField: 'userid', foreignField: '_id', as: 'user' } },
- { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1, openId: 1,
- oldInfo: '$infos.name', oldIdNumber: '$infos.idNumber',
- loginName: '$user.loginName', userName: '$user.userName',
- infoId: '$infos._id',
- } },
- { $match: whereInfo },
- { $lookup: { from: 'visit', localField: 'infoId', foreignField: 'infoId', as: 'visits' } },
- // { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1, openId: 1,
- oldInfo: 1, oldIdNumber: 1,
- loginName: 1, userName: 1,
- infoId: 1,
- dept1: { $first: '$visits.dept1' },
- dept2: { $first: '$visits.dept2' },
- dept3: { $first: '$visits.dept3' },
- dept4: { $first: '$visits.dept4' },
- dept5: { $first: '$visits.dept5' },
- } },
- { $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, openId: 1, oldInfo: 1, oldIdNumber: 1, loginName: 1, userName: 1, infoId: 1,
- dept: { $concat: [ '$dept1.name', '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
- } },
- {
- $skip: rows * (page - 1),
- },
- {
- $limit: rows,
- },
- ]);
- const countResult = await this.ctx.model.FamilyModel.aggregate([
- { $match: where },
- { $lookup: { from: 'info', localField: '_id', foreignField: 'fid', as: 'infos' } },
- { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1, oldInfo: '$infos.name', infoId: '$infos._id',
- } },
- { $match: whereInfo },
- // { $group: {
- // _id: 'infoId',
- // count: { $sum: 1 },
- // } },
- {
- $count: 'infoId', // 总数
- },
- ]);
- // 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].infoId;
- }
- return { list: restltList, count: countT };
- }
- }
- module.exports = FamilyService;
|