statisticsService.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const mongoose = require('mongoose');
  4. class StatisticsService extends Service {
  5. // 1. 采集数据图
  6. async statistics(data) {
  7. const matchStr = {};
  8. if (data.deptFid && data.deptFid !== '0') {
  9. matchStr.fid = data.deptFid;
  10. }
  11. if (data.deptId) {
  12. matchStr._id = data.deptId;
  13. }
  14. matchStr.level = data.deptLevel + '';
  15. const type = data.type;
  16. const userCond = this.getUserCond(matchStr);
  17. const getDeptManagerCond = this.getDeptManagerCond(matchStr);
  18. const infoCond = this.getInfoCond(matchStr);
  19. const abilityCond = this.getOldAbiltyCond(matchStr);
  20. const OldTypeCond = this.getOldTypeCond(matchStr);
  21. const facet = {};
  22. if (type != 'admin') {
  23. facet.userResult = userCond;
  24. if (type != 'user') {
  25. facet.infoResult = infoCond;
  26. facet.oldTypeResult = OldTypeCond;
  27. facet.abiltyResult = abilityCond;
  28. } else {
  29. if (data.deptLevel == 5) {
  30. userCond[3].$group.users = { $push: '$users.loginName' };
  31. }
  32. }
  33. } else {
  34. if (data.deptLevel == 5) {
  35. getDeptManagerCond[3].$group.users = { $push: '$users.loginName' };
  36. }
  37. facet.managerResult = getDeptManagerCond;
  38. }
  39. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  40. }
  41. async user(data) {
  42. const matchStr = {};
  43. if (data.deptFid && data.deptFid !== '0') {
  44. matchStr.fid = data.deptFid;
  45. }
  46. if (data.deptId) {
  47. matchStr._id = data.deptId;
  48. }
  49. matchStr.level = data.deptLevel + '';
  50. const type = data.type;
  51. const userCond = this.getUserCond(matchStr);
  52. const getDeptManagerCond = this.getDeptManagerCond(matchStr);
  53. const facet = {};
  54. if (type != 'admin') {
  55. facet.userResult = userCond;
  56. if (type != 'user') {
  57. } else {
  58. if (data.deptLevel == 5) {
  59. userCond[3].$group.users = { $push: '$users.loginName' };
  60. }
  61. }
  62. } else {
  63. if (data.deptLevel == 5) {
  64. getDeptManagerCond[3].$group.users = { $push: '$users.loginName' };
  65. }
  66. facet.managerResult = getDeptManagerCond;
  67. }
  68. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  69. }
  70. async info(data) {
  71. const matchStr = {};
  72. if (data.deptFid && data.deptFid !== '0') {
  73. matchStr.fid = data.deptFid;
  74. }
  75. if (data.deptId) {
  76. matchStr._id = data.deptId;
  77. }
  78. matchStr.level = data.deptLevel + '';
  79. const type = data.type;
  80. const infoCond = this.getInfoCond(matchStr);
  81. const facet = {};
  82. if (type != 'admin') {
  83. if (type != 'user') {
  84. facet.infoResult = infoCond;
  85. } else {
  86. if (data.deptLevel == 5) {
  87. }
  88. }
  89. } else {
  90. if (data.deptLevel == 5) {
  91. }
  92. }
  93. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  94. }
  95. async oldType(data) {
  96. const matchStr = {};
  97. if (data.deptFid && data.deptFid !== '0') {
  98. matchStr.fid = data.deptFid;
  99. }
  100. if (data.deptId) {
  101. matchStr._id = data.deptId;
  102. }
  103. matchStr.level = data.deptLevel + '';
  104. const type = data.type;
  105. const abilityCond = this.getOldAbiltyCond(matchStr);
  106. const OldTypeCond = this.getOldTypeCond(matchStr);
  107. const facet = {};
  108. if (type != 'admin') {
  109. if (type != 'user') {
  110. facet.oldTypeResult = OldTypeCond;
  111. facet.abiltyResult = abilityCond;
  112. } else {
  113. if (data.deptLevel == 5) {
  114. }
  115. }
  116. } else {
  117. if (data.deptLevel == 5) {
  118. }
  119. }
  120. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  121. }
  122. // 地区管理员tree
  123. getDeptManagerCond(matchStr) {
  124. return [
  125. { $match: matchStr },
  126. { $lookup:
  127. {
  128. from: 'sys_user',
  129. let: { dept_id: '$_id' },
  130. pipeline: [
  131. { $match:
  132. { $expr:
  133. { $and:
  134. [
  135. { $eq: [ '$dept' + matchStr.level, '$$dept_id' ] },
  136. { $eq: [ '$role', mongoose.Types.ObjectId(this.app.config.defaultManagerRoleId) ] },
  137. ],
  138. },
  139. },
  140. },
  141. ],
  142. as: 'users',
  143. },
  144. },
  145. { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
  146. { $group:
  147. {
  148. _id: '$_id',
  149. deptName: { $first: '$name' },
  150. deptCode: { $first: '$code' },
  151. level: { $first: '$level' },
  152. fid: { $first: '$fid' },
  153. order: { $first: '$order' },
  154. total: { $sum: { $cond: [ '$users', 1, 0 ] } },
  155. },
  156. },
  157. { $sort: { order: 1 } },
  158. ];
  159. }
  160. // 统计各地区采集员,认证情况
  161. getUserCond(matchStr) {
  162. return [
  163. { $match: matchStr },
  164. { $lookup:
  165. {
  166. from: 'sys_user',
  167. let: { dept_id: '$_id' },
  168. pipeline: [
  169. { $match:
  170. { $expr:
  171. { $and:
  172. [
  173. { $eq: [ '$dept' + matchStr.level, '$$dept_id' ] },
  174. { $eq: [ '$role', mongoose.Types.ObjectId(this.app.config.defaultUserRoleId) ] },
  175. ],
  176. },
  177. },
  178. },
  179. ],
  180. as: 'users',
  181. },
  182. },
  183. { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
  184. { $group:
  185. {
  186. _id: '$_id',
  187. deptName: { $first: '$name' },
  188. deptCode: { $first: '$code' },
  189. level: { $first: '$level' },
  190. fid: { $first: '$fid' },
  191. order: { $first: '$order' },
  192. noIdentify: {
  193. $sum: {
  194. $cond: { if: { $ifNull: [ '$users', false ] }, then:
  195. { $cond:
  196. [{ $ne: [{ $ifNull: [ '$users.file', '' ] }, '' ] }, 0, 1 ],
  197. }, else: 1,
  198. },
  199. },
  200. },
  201. identify: {
  202. $sum: {
  203. $cond:
  204. [{ $ne: [{ $ifNull: [ '$users.file', '' ] }, '' ] }, 1, 0 ],
  205. },
  206. },
  207. total: { $sum: { $cond: [ '$users', 1, 0 ] } },
  208. },
  209. },
  210. { $sort: { order: 1 } },
  211. ];
  212. }
  213. // 统计各地区采集记录,审核情况
  214. getInfoCond(matchStr) {
  215. return [
  216. { $match: matchStr },
  217. { $lookup: { from: 'info', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'infos' } },
  218. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  219. { $group:
  220. { _id: '$_id',
  221. deptName: { $first: '$name' },
  222. deptCode: { $first: '$code' },
  223. level: { $first: '$level' },
  224. fid: { $first: '$fid' },
  225. order: { $first: '$order' },
  226. total: { $sum: {
  227. $cond: [ '$infos', 1, 0 ],
  228. } },
  229. status0: { $sum: {
  230. $cond: [{ $eq: [ '$infos.status', '0' ] }, 1, 0 ],
  231. } },
  232. status1: { $sum: {
  233. $cond: [{ $eq: [ '$infos.status', '1' ] }, 1, 0 ],
  234. } },
  235. status2: { $sum: {
  236. $cond: [{ $eq: [ '$infos.status', '2' ] }, 1, 0 ],
  237. } },
  238. status3: { $sum: {
  239. $cond: [{ $eq: [ '$infos.status', '3' ] }, 1, 0 ],
  240. } },
  241. status4: { $sum: {
  242. $cond: [{ $eq: [ '$infos.status', '4' ] }, 1, 0 ],
  243. } },
  244. },
  245. },
  246. { $sort: { order: 1 } },
  247. ];
  248. }
  249. // 统计各地区老人类别
  250. getOldTypeCond(matchStr) {
  251. return [
  252. { $match: matchStr },
  253. { $lookup: { from: 'info', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'infos' } },
  254. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  255. { $unwind: { path: '$infos.oldType', preserveNullAndEmptyArrays: true } },
  256. { $unwind: { path: '$infos.status', preserveNullAndEmptyArrays: true } },
  257. // { $match: { 'infos.status': '3' } },
  258. { $group:
  259. { _id: '$_id',
  260. deptName: { $first: '$name' },
  261. deptCode: { $first: '$code' },
  262. level: { $first: '$level' },
  263. fid: { $first: '$fid' },
  264. order: { $first: '$order' },
  265. oldType1: { $sum: {
  266. // $cond: [{ $eq: [ '$infos.oldType', '农村留守老年人' ] }, 1, 0 ],
  267. $cond: [
  268. { $and:
  269. [
  270. { $eq: [ '$infos.oldType', '农村留守老年人' ] },
  271. { $eq: [ '$infos.status', '3' ] },
  272. ],
  273. }, 1, 0 ],
  274. } },
  275. oldType2: { $sum: {
  276. // $cond: [{ $eq: [ '$infos.oldType', '分散供养特困老年人' ] }, 1, 0 ],
  277. $cond: [
  278. { $and:
  279. [
  280. { $eq: [ '$infos.oldType', '分散供养特困老年人' ] },
  281. { $eq: [ '$infos.status', '3' ] },
  282. ],
  283. }, 1, 0 ],
  284. } },
  285. oldType3: { $sum: {
  286. // $cond: [{ $eq: [ '$infos.oldType', '计划生育特殊家庭老人' ] }, 1, 0 ],
  287. $cond: [
  288. { $and:
  289. [
  290. { $eq: [ '$infos.oldType', '计划生育特殊家庭老人' ] },
  291. { $eq: [ '$infos.status', '3' ] },
  292. ],
  293. }, 1, 0 ],
  294. } },
  295. oldType4: { $sum: {
  296. // $cond: [{ $eq: [ '$infos.oldType', '空巢老年人' ] }, 1, 0 ],
  297. $cond: [
  298. { $and:
  299. [
  300. { $eq: [ '$infos.oldType', '空巢老年人' ] },
  301. { $eq: [ '$infos.status', '3' ] },
  302. ],
  303. }, 1, 0 ],
  304. } },
  305. oldType5: { $sum: {
  306. // $cond: [{ $eq: [ '$infos.oldType', '独居老年人' ] }, 1, 0 ],
  307. $cond: [
  308. { $and:
  309. [
  310. { $eq: [ '$infos.oldType', '独居老年人' ] },
  311. { $eq: [ '$infos.status', '3' ] },
  312. ],
  313. }, 1, 0 ],
  314. } },
  315. oldType6: { $sum: {
  316. // $cond: [{ $eq: [ '$infos.oldType', '重残老人' ] }, 1, 0 ],
  317. $cond: [
  318. { $and:
  319. [
  320. { $eq: [ '$infos.oldType', '重残老人' ] },
  321. { $eq: [ '$infos.status', '3' ] },
  322. ],
  323. }, 1, 0 ],
  324. } },
  325. oldType7: { $sum: {
  326. // $cond: [{ $eq: [ '$infos.oldType', '高龄老人' ] }, 1, 0 ],
  327. $cond: [
  328. { $and:
  329. [
  330. { $eq: [ '$infos.oldType', '高龄老人' ] },
  331. { $eq: [ '$infos.status', '3' ] },
  332. ],
  333. }, 1, 0 ],
  334. } },
  335. oldType8: { $sum: {
  336. // $cond: [{ $eq: [ '$infos.oldType', '高龄老人' ] }, 1, 0 ],
  337. $cond: [
  338. { $and:
  339. [
  340. { $eq: [ '$infos.oldType', '居家老年人' ] },
  341. { $eq: [ '$infos.status', '3' ] },
  342. ],
  343. }, 1, 0 ],
  344. } },
  345. },
  346. },
  347. { $sort: { order: 1 } },
  348. ];
  349. }
  350. // 统计各地区失能老人情况
  351. getOldAbiltyCond(matchStr) {
  352. return [
  353. { $match: matchStr },
  354. { $lookup: { from: 'info', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'infos' } },
  355. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  356. { $unwind: { path: '$infos.status', preserveNullAndEmptyArrays: true } },
  357. { $group:
  358. {
  359. _id: '$_id',
  360. deptName: { $first: '$name' },
  361. deptCode: { $first: '$code' },
  362. level: { $first: '$level' },
  363. fid: { $first: '$fid' },
  364. order: { $first: '$order' },
  365. ability: {
  366. $sum: {
  367. // $cond: [{ $eq: [ '$infos.ability', '失能' ] }, 1, 0 ],
  368. $cond: [
  369. { $and:
  370. [
  371. { $eq: [ '$infos.ability', '失能' ] },
  372. { $eq: [ '$infos.status', '3' ] },
  373. ],
  374. }, 1, 0 ],
  375. },
  376. },
  377. },
  378. },
  379. {
  380. $sort: { order: 1 },
  381. },
  382. ];
  383. }
  384. // 2. 吉林省全县区74个审核成功统计图表 优化
  385. async dept3() {
  386. return await this.ctx.model.InfoModel.aggregate([
  387. { $match: { status: '3', dept1: this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082') } },
  388. { $group:
  389. { _id: '$dept3',
  390. dept2: { $first: '$dept2' },
  391. total: { $sum: 1 },
  392. },
  393. },
  394. { $lookup: { from: 'sys_dept', localField: '_id', foreignField: '_id', as: 'dept' } },
  395. { $unwind: { path: '$dept', preserveNullAndEmptyArrays: true } },
  396. { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'fdept' } },
  397. { $unwind: { path: '$fdept', preserveNullAndEmptyArrays: true } },
  398. { $project: {
  399. _id: 1,
  400. dept2: 1,
  401. deptName: { $concat: [ '$fdept.name', '$dept.name' ] },
  402. total: '$total',
  403. } },
  404. { $sort: { total: -1 } },
  405. ]).allowDiskUse(true);// 允许使用磁盘缓存
  406. }
  407. // // 2. 吉林省全县区74个审核成功统计图表
  408. // async dept3Statistics() {
  409. // return await this.ctx.model.SysDeptModel.aggregate([
  410. // { $match: { level: '3' } },
  411. // { $lookup: { from: 'info', localField: '_id', foreignField: 'dept3', as: 'infos' } },
  412. // { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  413. // { $match: { 'infos.status': '3', 'infos.dept1': this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082') } },
  414. // { $group:
  415. // { _id: '$_id',
  416. // deptName: { $first: '$name' },
  417. // level: { $first: '$level' },
  418. // dept2: { $first: '$infos.dept2' },
  419. // total: { $sum: {
  420. // $cond: [ '$infos', 1, 0 ],
  421. // } },
  422. // },
  423. // },
  424. // { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'fdept' } },
  425. // { $unwind: { path: '$fdept', preserveNullAndEmptyArrays: true } },
  426. // { $project: {
  427. // _id: 1,
  428. // deptName: { $concat: [ '$fdept.name', '$deptName' ] },
  429. // level: '$level',
  430. // total: '$total',
  431. // } },
  432. // { $sort: { total: -1 } },
  433. // ]).allowDiskUse(true);// 允许使用磁盘缓存
  434. // }
  435. // 3. 地区积分统计
  436. async deptValues(data) {
  437. const matchStr = {};
  438. if (data.deptFid && data.deptFid !== '0') {
  439. matchStr.fid = data.deptFid;
  440. }
  441. if (data.deptId) {
  442. matchStr._id = data.deptId;
  443. }
  444. matchStr.level = data.deptLevel + '';
  445. const type = data.type;
  446. const deptValuesCond = this.getDeptValuesCond(matchStr);
  447. const facet = {};
  448. if (type != 'user') {
  449. facet.deptValuesCond = deptValuesCond;
  450. }
  451. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  452. }
  453. getDeptValuesCond(matchStr) {
  454. return [
  455. { $match: matchStr },
  456. { $lookup: { from: 'value', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'values' } },
  457. { $unwind: { path: '$values', preserveNullAndEmptyArrays: true } },
  458. { $group:
  459. { _id: '$_id',
  460. deptName: { $first: '$name' },
  461. deptCode: { $first: '$code' },
  462. level: { $first: '$level' },
  463. fid: { $first: '$fid' },
  464. order: { $first: '$order' },
  465. total: { $sum: {
  466. $cond: [ '$values', 1, 0 ],
  467. } },
  468. type0: { $sum: {
  469. $cond: [{ $eq: [ '$values.type', '0' ] }, 1, 0 ],
  470. } },
  471. type1: { $sum: {
  472. $cond: [{ $eq: [ '$values.type', '1' ] }, 1, 0 ],
  473. } },
  474. },
  475. },
  476. { $project: {
  477. _id: 1,
  478. deptName: 1,
  479. deptCode: 1,
  480. level: 1,
  481. fid: 1,
  482. order: 1,
  483. total: 1,
  484. type0: 1,
  485. type1: 1,
  486. totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
  487. } },
  488. { $sort: { order: 1 } },
  489. ];
  490. }
  491. // 4. 个人积分统计-优化
  492. async userValues(data) {
  493. const where = {};
  494. if (data.dept1) {
  495. where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
  496. }
  497. if (data.dept2) {
  498. where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
  499. }
  500. if (data.dept3) {
  501. where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
  502. }
  503. if (data.dept4) {
  504. where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
  505. }
  506. if (data.dept5) {
  507. where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
  508. }
  509. const restltList = await this.ctx.model.ValueModel.aggregate([
  510. {
  511. $match: where,
  512. },
  513. { $group:
  514. {
  515. _id: '$userid',
  516. dept2: { $first: '$dept2' },
  517. dept3: { $first: '$dept3' },
  518. dept4: { $first: '$dept4' },
  519. dept5: { $first: '$dept5' },
  520. type0: { $sum: {
  521. $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
  522. } },
  523. type1: { $sum: {
  524. $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
  525. } },
  526. },
  527. },
  528. { $project: {
  529. _id: 1,
  530. dept2: 1,
  531. dept3: 1,
  532. dept4: 1,
  533. dept5: 1,
  534. infoValue: { $multiply: [ '$type0', 5 ] },
  535. visitValue: { $multiply: [ '$type1', 1 ] },
  536. totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
  537. } },
  538. { $sort: { totalValue: -1 } },
  539. {
  540. $limit: 100,
  541. },
  542. { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'user' } },
  543. { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
  544. { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'dept2' } },
  545. { $unwind: { path: '$dept2', preserveNullAndEmptyArrays: true } },
  546. { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'dept3' } },
  547. { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
  548. { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
  549. { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
  550. { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5' } },
  551. { $unwind: { path: '$dept5', preserveNullAndEmptyArrays: true } },
  552. { $project: {
  553. _id: 1,
  554. loginName: '$user.loginName',
  555. userName: '$user.userName',
  556. infoValue: 1,
  557. visitValue: 1,
  558. totalValue: 1,
  559. dept: { $concat: [ '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
  560. } },
  561. ]).allowDiskUse(true);// 允许使用磁盘缓存
  562. return restltList;
  563. }
  564. // // 个人积分统计-前100
  565. // async values(data) {
  566. // const where = {};
  567. // if (data.dept1) {
  568. // where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
  569. // }
  570. // if (data.dept2) {
  571. // where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
  572. // }
  573. // if (data.dept3) {
  574. // where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
  575. // }
  576. // if (data.dept4) {
  577. // where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
  578. // }
  579. // if (data.dept5) {
  580. // where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
  581. // }
  582. // const restltList = await this.ctx.model.ValueModel.aggregate([
  583. // {
  584. // $match: where,
  585. // },
  586. // { $group:
  587. // {
  588. // _id: '$userid',
  589. // type0: { $sum: {
  590. // $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
  591. // } },
  592. // type1: { $sum: {
  593. // $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
  594. // } },
  595. // },
  596. // },
  597. // { $project: {
  598. // _id: 1,
  599. // infoValue: { $multiply: [ '$type0', 5 ] },
  600. // visitValue: { $multiply: [ '$type1', 1 ] },
  601. // totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
  602. // } },
  603. // { $sort: { totalValue: -1 } },
  604. // {
  605. // $limit: 100,
  606. // },
  607. // ]).allowDiskUse(true);// 允许使用磁盘缓存
  608. // const newResult = [];
  609. // for (const item of restltList) {
  610. // const user = await this.ctx.model.SysUserModel.findOne({ _id: this.app.mongoose.Types.ObjectId(item._id) }).populate(this.ctx.getUserPop());
  611. // newResult.push({ _id: item._id,
  612. // totalValue: item.totalValue,
  613. // infoValue: item.infoValue,
  614. // visitValue: item.visitValue,
  615. // userName: user.userName,
  616. // loginName: user.loginName,
  617. // dept: user.dept2.name + user.dept3.name + user.dept4.name + user.dept5.name,
  618. // });
  619. // }
  620. // return newResult;
  621. // // return restltList;
  622. // }
  623. // 5. 探访类别分布图
  624. async visitDeptOldType(data) {
  625. const matchStr = {};
  626. if (data.deptFid && data.deptFid !== '0') {
  627. matchStr.fid = data.deptFid;
  628. }
  629. if (data.deptId) {
  630. matchStr._id = data.deptId;
  631. }
  632. matchStr.level = data.deptLevel + '';
  633. const where = {};
  634. if (data.startTime && data.endTime) {
  635. where['visits.visitTime'] = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') };
  636. }
  637. const type = data.type;
  638. const visitOldTypeCond = this.visitOldTypeCond(matchStr, where);
  639. const visitDeptStatisticsCond = this.visitDeptStatisticsCond(matchStr, where);
  640. const facet = {};
  641. if (type != 'user') {
  642. facet.visitOldTypeCond = visitOldTypeCond;
  643. facet.visitDeptStatisticsCond = visitDeptStatisticsCond;
  644. }
  645. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  646. }
  647. // 统计各地区老人类别
  648. visitOldTypeCond(matchStr, where) {
  649. return [
  650. { $match: matchStr },
  651. { $lookup: { from: 'visit', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'visits' } },
  652. { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
  653. { $unwind: { path: '$visits.infoOldType', preserveNullAndEmptyArrays: true } },
  654. { $match: where },
  655. { $group:
  656. { _id: '$_id',
  657. deptName: { $first: '$name' },
  658. deptCode: { $first: '$code' },
  659. level: { $first: '$level' },
  660. fid: { $first: '$fid' },
  661. order: { $first: '$order' },
  662. // total: { $sum: {
  663. // $cond: [ '$visits', 1, 0 ],
  664. // } },
  665. oldType1: { $sum: {
  666. $cond: [{ $eq: [ '$visits.infoOldType', '农村留守老年人' ] }, 1, 0 ],
  667. } },
  668. oldType2: { $sum: {
  669. $cond: [{ $eq: [ '$visits.infoOldType', '分散供养特困老年人' ] }, 1, 0 ],
  670. } },
  671. oldType3: { $sum: {
  672. $cond: [{ $eq: [ '$visits.infoOldType', '计划生育特殊家庭老人' ] }, 1, 0 ],
  673. } },
  674. oldType4: { $sum: {
  675. $cond: [{ $eq: [ '$visits.infoOldType', '空巢老年人' ] }, 1, 0 ],
  676. } },
  677. oldType5: { $sum: {
  678. $cond: [{ $eq: [ '$visits.infoOldType', '独居老年人' ] }, 1, 0 ],
  679. } },
  680. oldType6: { $sum: {
  681. $cond: [{ $eq: [ '$visits.infoOldType', '重残老人' ] }, 1, 0 ],
  682. } },
  683. oldType7: { $sum: {
  684. $cond: [{ $eq: [ '$visits.infoOldType', '高龄老人' ] }, 1, 0 ],
  685. } },
  686. oldType8: { $sum: {
  687. $cond: [{ $eq: [ '$visits.infoOldType', '居家老年人' ] }, 1, 0 ],
  688. } },
  689. ability: { // 原版-这里统计出错,先拆分,在sum,这样['居家','高龄'] 失能会计算2次.-CH-2023-8-15;
  690. // 修改后,能力情况为失能时,我给infoOldType添加了[失能老人]标签(一次性处理),且采集时业务逻辑也修改成这样。
  691. $sum: {
  692. $cond: [{ $eq: [ '$visits.infoOldType', '失能老人' ] }, 1, 0 ],
  693. },
  694. },
  695. },
  696. },
  697. { $sort: { order: 1 } },
  698. ];
  699. }
  700. // 统计各地区探访数量
  701. visitDeptStatisticsCond(matchStr, where) {
  702. return [
  703. { $match: matchStr },
  704. { $lookup: { from: 'visit', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'visits' } },
  705. { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
  706. { $match: where },
  707. { $group:
  708. { _id: '$_id',
  709. deptName: { $first: '$name' },
  710. deptCode: { $first: '$code' },
  711. level: { $first: '$level' },
  712. fid: { $first: '$fid' },
  713. order: { $first: '$order' },
  714. total: { $sum: {
  715. $cond: [ '$visits', 1, 0 ],
  716. } },
  717. },
  718. },
  719. { $sort: { total: -1 } },
  720. ];
  721. }
  722. // 6. 探访县区分布图
  723. async visitDept3Statistics(data) {
  724. const where = {};
  725. where['visits.dept1'] = this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082');// 吉林省
  726. if (data.startTime && data.endTime) {
  727. where['visits.visitTime'] = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') };
  728. }
  729. const matchStr = {};
  730. matchStr.level = '3';
  731. const visitDept3StatisticsCond = this.visitDept3StatisticsCond(where);
  732. const visitOldTypeCond = this.visitOldTypeCond(matchStr, where);
  733. const facet = {};
  734. facet.visitDept3StatisticsCond = visitDept3StatisticsCond;
  735. facet.visitOldTypeCond = visitOldTypeCond;
  736. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  737. }
  738. // 县区探访统计
  739. visitDept3StatisticsCond(where) {
  740. return [
  741. {
  742. $match: { level: '3' },
  743. },
  744. { $lookup: { from: 'visit', localField: '_id', foreignField: 'dept3', as: 'visits' } },
  745. { $unwind: { path: '$visits', preserveNullAndEmptyArrays: true } },
  746. {
  747. $match: where,
  748. },
  749. { $group:
  750. { _id: '$_id',
  751. deptName: { $first: '$name' },
  752. dept2: { $first: '$visits.dept2' },
  753. total: { $sum: {
  754. $cond: [ '$visits', 1, 0 ],
  755. } },
  756. },
  757. },
  758. { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'fdept' } },
  759. { $unwind: { path: '$fdept', preserveNullAndEmptyArrays: true } },
  760. { $project: {
  761. _id: 1,
  762. deptName: { $concat: [ '$fdept.name', '$deptName' ] },
  763. total: '$total',
  764. } },
  765. { $sort: { total: -1 } },
  766. ];
  767. }
  768. async urgencyStatistics(data) {
  769. const matchStr = {};
  770. if (data.deptFid && data.deptFid !== '0') {
  771. matchStr.fid = data.deptFid;
  772. }
  773. if (data.deptId) {
  774. matchStr._id = data.deptId;
  775. }
  776. matchStr.level = data.deptLevel + '';
  777. const type = data.type;
  778. const urgencyCond = this.getUrgencyCond(matchStr);
  779. const facet = {};
  780. if (type != 'admin') {
  781. if (type != 'user') {
  782. facet.urgencyResult = urgencyCond;
  783. } else {
  784. if (data.deptLevel == 5) {
  785. }
  786. }
  787. } else {
  788. if (data.deptLevel == 5) {
  789. }
  790. }
  791. return await this.ctx.model.SysDeptModel.aggregate([{ $facet: facet }]).allowDiskUse(true);// 允许使用磁盘缓存
  792. }
  793. getUrgencyCond(matchStr) {
  794. return [
  795. { $match: matchStr },
  796. { $lookup: { from: 'urgency_notice', localField: '_id', foreignField: 'dept' + matchStr.level, as: 'urgencys' } },
  797. { $unwind: { path: '$urgencys', preserveNullAndEmptyArrays: true } },
  798. { $group:
  799. { _id: '$_id',
  800. deptName: { $first: '$name' },
  801. level: { $first: '$level' },
  802. fid: { $first: '$fid' },
  803. total: { $sum: {
  804. $cond: [ '$urgencys', 1, 0 ],
  805. } },
  806. status0: { $sum: {
  807. $cond: [{ $eq: [ '$urgencys.status', '0' ] }, 1, 0 ],
  808. } },
  809. status1: { $sum: {
  810. $cond: [{ $eq: [ '$urgencys.status', '1' ] }, 1, 0 ],
  811. } },
  812. },
  813. },
  814. { $sort: { order: 1 } },
  815. ];
  816. }
  817. }
  818. module.exports = StatisticsService;