familyService.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. class FamilyService extends Service {
  4. tag() {
  5. return this.ctx.model.FamilyModel;
  6. }
  7. async bindList(data) {
  8. const page = parseInt(data.page);
  9. const rows = parseInt(data.rows);
  10. const where = {};
  11. if (data.dept1) {
  12. where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
  13. }
  14. if (data.dept2) {
  15. where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
  16. }
  17. if (data.dept3) {
  18. where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
  19. }
  20. if (data.dept4) {
  21. where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
  22. }
  23. if (data.dept5) {
  24. where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
  25. }
  26. if (data.queryName) {
  27. where.oldInfo = { $regex: data.queryName };
  28. }
  29. const restltList = await this.ctx.model.VisitModel.aggregate([
  30. { $match: where },
  31. { $lookup: { from: 'family', localField: 'fid', foreignField: '_id', as: 'familys' } },
  32. { $unwind: { path: '$familys', preserveNullAndEmptyArrays: true } },
  33. { $match: { 'familys.openId': { $exists: true, $ne: [] } } },
  34. { $group:
  35. { _id: '$infoId',
  36. userid: { $first: '$userid' },
  37. oldInfo: { $first: '$oldInfo' },
  38. oldIdNumber: { $first: '$oldIdNumber' },
  39. fid: { $first: '$fid' },
  40. openId: { $first: '$familys.openId' },
  41. dept1: { $first: '$dept1' },
  42. dept2: { $first: '$dept2' },
  43. dept3: { $first: '$dept3' },
  44. dept4: { $first: '$dept4' },
  45. dept5: { $first: '$dept5' },
  46. },
  47. },
  48. {
  49. $skip: rows * (page - 1),
  50. },
  51. {
  52. $limit: rows,
  53. },
  54. { $lookup: { from: 'sys_user', localField: 'userid', foreignField: '_id', as: 'user' } },
  55. { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
  56. { $lookup: { from: 'sys_dept', localField: 'dept1', foreignField: '_id', as: 'dept1' } },
  57. { $unwind: { path: '$dept1', preserveNullAndEmptyArrays: true } },
  58. { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'dept2' } },
  59. { $unwind: { path: '$dept2', preserveNullAndEmptyArrays: true } },
  60. { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'dept3' } },
  61. { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
  62. { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
  63. { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
  64. { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5' } },
  65. { $unwind: { path: '$dept5', preserveNullAndEmptyArrays: true } },
  66. { $project: {
  67. _id: 1,
  68. oldInfo: 1,
  69. oldIdNumber: 1,
  70. openId: '$openId',
  71. loginName: '$user.loginName',
  72. userName: '$user.userName',
  73. dept: { $concat: [ '$dept1.name', '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
  74. } },
  75. ]).allowDiskUse(true);
  76. const countResult = await this.ctx.model.VisitModel.aggregate([
  77. { $match: where },
  78. { $lookup: { from: 'family', localField: 'fid', foreignField: '_id', as: 'familys' } },
  79. { $unwind: { path: '$familys', preserveNullAndEmptyArrays: true } },
  80. { $match: { 'familys.openId': { $exists: true, $ne: [] } } },
  81. { $group: {
  82. _id: '$infoId',
  83. count: { $sum: 1 },
  84. } },
  85. {
  86. $count: 'count', // 总数
  87. },
  88. ]);
  89. let countT = 0;
  90. if (countResult[0]) {
  91. countT = countResult[0].count;
  92. }
  93. return { list: restltList, count: countT };
  94. }
  95. }
  96. module.exports = FamilyService;