familyService.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. const { sma4: sm4 } = require('../util/sm4');
  4. class FamilyService extends Service {
  5. tag() {
  6. return this.ctx.model.FamilyModel;
  7. }
  8. async bindList(data) {
  9. const page = parseInt(data.page);
  10. const rows = parseInt(data.rows);
  11. const where = {};
  12. const whereInfo = {};
  13. where.openId = { $exists: true, $ne: [] };
  14. if (data.dept1) {
  15. where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
  16. }
  17. if (data.dept2) {
  18. where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
  19. }
  20. if (data.dept3) {
  21. where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
  22. }
  23. if (data.dept4) {
  24. where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
  25. }
  26. if (data.dept5) {
  27. where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
  28. }
  29. if (data.queryName) {
  30. whereInfo.oldInfo = sm4.encrypt_ECB(data.queryName);
  31. }
  32. const restltList = await this.ctx.model.FamilyModel.aggregate([
  33. { $match: where },
  34. { $lookup: { from: 'info', localField: '_id', foreignField: 'fid', as: 'infos' } },
  35. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  36. { $lookup: { from: 'sys_user', localField: 'userid', foreignField: '_id', as: 'user' } },
  37. { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
  38. { $project: {
  39. _id: 1, openId: 1,
  40. oldInfo: '$infos.name', oldIdNumber: '$infos.idNumber',
  41. loginName: '$user.loginName', userName: '$user.userName',
  42. infoId: '$infos._id',
  43. } },
  44. { $match: whereInfo },
  45. { $lookup: { from: 'visit', localField: 'infoId', foreignField: 'infoId', as: 'visits' } },
  46. // { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
  47. { $project: {
  48. _id: 1, openId: 1,
  49. oldInfo: 1, oldIdNumber: 1,
  50. loginName: 1, userName: 1,
  51. infoId: 1,
  52. dept1: { $first: '$visits.dept1' },
  53. dept2: { $first: '$visits.dept2' },
  54. dept3: { $first: '$visits.dept3' },
  55. dept4: { $first: '$visits.dept4' },
  56. dept5: { $first: '$visits.dept5' },
  57. } },
  58. { $lookup: { from: 'sys_dept', localField: 'dept1', foreignField: '_id', as: 'dept1' } },
  59. { $unwind: { path: '$dept1', preserveNullAndEmptyArrays: true } },
  60. { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'dept2' } },
  61. { $unwind: { path: '$dept2', preserveNullAndEmptyArrays: true } },
  62. { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'dept3' } },
  63. { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
  64. { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
  65. { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
  66. { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5' } },
  67. { $unwind: { path: '$dept5', preserveNullAndEmptyArrays: true } },
  68. { $project: {
  69. _id: 1, openId: 1, oldInfo: 1, oldIdNumber: 1, loginName: 1, userName: 1, infoId: 1,
  70. dept: { $concat: [ '$dept1.name', '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
  71. } },
  72. {
  73. $skip: rows * (page - 1),
  74. },
  75. {
  76. $limit: rows,
  77. },
  78. ]);
  79. const countResult = await this.ctx.model.FamilyModel.aggregate([
  80. { $match: where },
  81. { $lookup: { from: 'info', localField: '_id', foreignField: 'fid', as: 'infos' } },
  82. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  83. { $project: {
  84. _id: 1, oldInfo: '$infos.name', infoId: '$infos._id',
  85. } },
  86. { $match: whereInfo },
  87. // { $group: {
  88. // _id: 'infoId',
  89. // count: { $sum: 1 },
  90. // } },
  91. {
  92. $count: 'infoId', // 总数
  93. },
  94. ]);
  95. // const restltList = await this.ctx.model.VisitModel.aggregate([
  96. // { $match: where },
  97. // { $lookup: { from: 'family', localField: 'fid', foreignField: '_id', as: 'familys' } },
  98. // { $unwind: { path: '$familys', preserveNullAndEmptyArrays: true } },
  99. // { $match: { 'familys.openId': { $exists: true, $ne: [] } } },
  100. // { $group:
  101. // { _id: '$infoId',
  102. // userid: { $first: '$userid' },
  103. // oldInfo: { $first: '$oldInfo' },
  104. // oldIdNumber: { $first: '$oldIdNumber' },
  105. // fid: { $first: '$fid' },
  106. // openId: { $first: '$familys.openId' },
  107. // dept1: { $first: '$dept1' },
  108. // dept2: { $first: '$dept2' },
  109. // dept3: { $first: '$dept3' },
  110. // dept4: { $first: '$dept4' },
  111. // dept5: { $first: '$dept5' },
  112. // },
  113. // },
  114. // {
  115. // $skip: rows * (page - 1),
  116. // },
  117. // {
  118. // $limit: rows,
  119. // },
  120. // { $lookup: { from: 'sys_user', localField: 'userid', foreignField: '_id', as: 'user' } },
  121. // { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
  122. // { $lookup: { from: 'sys_dept', localField: 'dept1', foreignField: '_id', as: 'dept1' } },
  123. // { $unwind: { path: '$dept1', preserveNullAndEmptyArrays: true } },
  124. // { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'dept2' } },
  125. // { $unwind: { path: '$dept2', preserveNullAndEmptyArrays: true } },
  126. // { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'dept3' } },
  127. // { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
  128. // { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
  129. // { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
  130. // { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5' } },
  131. // { $unwind: { path: '$dept5', preserveNullAndEmptyArrays: true } },
  132. // { $project: {
  133. // _id: 1,
  134. // oldInfo: 1,
  135. // oldIdNumber: 1,
  136. // openId: '$openId',
  137. // loginName: '$user.loginName',
  138. // userName: '$user.userName',
  139. // dept: { $concat: [ '$dept1.name', '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
  140. // } },
  141. // ]).allowDiskUse(true);
  142. // const countResult = await this.ctx.model.VisitModel.aggregate([
  143. // { $match: where },
  144. // { $lookup: { from: 'family', localField: 'fid', foreignField: '_id', as: 'familys' } },
  145. // { $unwind: { path: '$familys', preserveNullAndEmptyArrays: true } },
  146. // { $match: { 'familys.openId': { $exists: true, $ne: [] } } },
  147. // { $group: {
  148. // _id: '$infoId',
  149. // count: { $sum: 1 },
  150. // } },
  151. // {
  152. // $count: 'count', // 总数
  153. // },
  154. // ]);
  155. let countT = 0;
  156. if (countResult[0]) {
  157. countT = countResult[0].infoId;
  158. }
  159. return { list: restltList, count: countT };
  160. }
  161. }
  162. module.exports = FamilyService;