visit.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class VisitService extends Service {
  4. async health(query) {
  5. const { ctx } = this;
  6. query.health = { $ne: null };
  7. const resultList = await ctx.model.VisitModel.aggregate([
  8. { $match: ctx.alterDeptId(query) },
  9. {
  10. $group: {
  11. _id: '$health', count: { $sum: 1 },
  12. },
  13. },
  14. {
  15. $project: {
  16. _id: 1, count: 1,
  17. },
  18. },
  19. ]);
  20. const resultCount = await ctx.model.VisitModel.aggregate([
  21. { $match: ctx.alterDeptId(query) },
  22. { $count: 'count' },
  23. ]);
  24. const resultArr = [];
  25. const result = [];
  26. try {
  27. resultList.forEach(item => {
  28. switch (item._id) {
  29. case '健康':
  30. resultArr.push({ label: '健康', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  31. break;
  32. case '一般':
  33. resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  34. break;
  35. case '较差':
  36. resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  37. break;
  38. default:
  39. console.log('老年人健康状况统计查询!');
  40. }
  41. });
  42. const health = ctx.health();
  43. health.forEach(item => {
  44. result.push({ label: item, value: 0 + '%' });
  45. });
  46. result.forEach(itemA => {
  47. resultArr.forEach(item => {
  48. if (item.label == itemA.label) {
  49. itemA.value = item.value;
  50. }
  51. });
  52. });
  53. } catch (e) {
  54. ctx.logger.error('catch--------error', e);
  55. ctx.error(e);
  56. } finally {
  57. return result;
  58. }
  59. }
  60. async mind(query) {
  61. const { ctx } = this;
  62. query.$and = [{ mind: { $ne: null } }, { mind: { $ne: '健康' } }];
  63. const resultList = await ctx.model.VisitModel.aggregate([
  64. { $match: ctx.alterDeptId(query) },
  65. {
  66. $group: {
  67. _id: '$mind', count: { $sum: 1 },
  68. },
  69. },
  70. {
  71. $project: {
  72. _id: 1, count: 1,
  73. },
  74. },
  75. ]);
  76. const resultCount = await ctx.model.VisitModel.aggregate([
  77. { $match: ctx.alterDeptId(query) },
  78. { $count: 'count' },
  79. ]);
  80. const resultArr = [];
  81. const result = [];
  82. try {
  83. resultList.forEach(item => {
  84. switch (item._id) {
  85. case '良好':
  86. resultArr.push({ label: '良好', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  87. break;
  88. case '一般':
  89. resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  90. break;
  91. case '较差':
  92. resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  93. break;
  94. default:
  95. console.log('老年人精神状态统计查询!');
  96. }
  97. });
  98. const mind = ctx.mind();
  99. mind.forEach(item => {
  100. result.push({ label: item, value: 0 + '%' });
  101. });
  102. result.forEach(itemA => {
  103. resultArr.forEach(item => {
  104. if (item.label == itemA.label) {
  105. itemA.value = item.value;
  106. }
  107. });
  108. });
  109. } catch (e) {
  110. ctx.logger.error('catch--------error', e);
  111. ctx.error(e);
  112. } finally {
  113. return result;
  114. }
  115. }
  116. async security(query) {
  117. const { ctx } = this;
  118. query.security = { $ne: null };
  119. const resultList = await ctx.model.VisitModel.aggregate([
  120. { $match: ctx.alterDeptId(query) },
  121. {
  122. $group: {
  123. _id: '$security', count: { $sum: 1 },
  124. },
  125. },
  126. {
  127. $project: {
  128. _id: 1, count: 1,
  129. },
  130. },
  131. ]);
  132. const resultCount = await ctx.model.VisitModel.aggregate([
  133. { $match: ctx.alterDeptId(query) },
  134. { $count: 'count' },
  135. ]);
  136. const resultArr = [];
  137. const result = [];
  138. try {
  139. resultList.forEach(item => {
  140. switch (item._id) {
  141. case '安全':
  142. resultArr.push({ label: '安全', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  143. break;
  144. case '一般':
  145. resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  146. break;
  147. case '较差':
  148. resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  149. break;
  150. default:
  151. console.log('老年人精神状态统计查询!');
  152. }
  153. });
  154. const security = ctx.security();
  155. security.forEach(item => {
  156. result.push({ label: item, value: 0 + '%' });
  157. });
  158. result.forEach(itemA => {
  159. resultArr.forEach(item => {
  160. if (item.label == itemA.label) {
  161. itemA.value = item.value;
  162. }
  163. });
  164. });
  165. } catch (e) {
  166. ctx.logger.error('catch--------error', e);
  167. ctx.error(e);
  168. } finally {
  169. return result;
  170. }
  171. }
  172. async hygiene(query) {
  173. const { ctx } = this;
  174. query.$and = [{ hygiene: { $ne: null } }, { hygiene: { $ne: '健康' } }];
  175. const resultList = await ctx.model.VisitModel.aggregate([
  176. { $match: ctx.alterDeptId(query) },
  177. {
  178. $group: {
  179. _id: '$hygiene', count: { $sum: 1 },
  180. },
  181. },
  182. {
  183. $project: {
  184. _id: 1, count: 1,
  185. },
  186. },
  187. ]);
  188. const resultCount = await ctx.model.VisitModel.aggregate([
  189. { $match: ctx.alterDeptId(query) },
  190. { $count: 'count' },
  191. ]);
  192. const resultArr = [];
  193. const result = [];
  194. try {
  195. resultList.forEach(item => {
  196. switch (item._id) {
  197. case '良好':
  198. resultArr.push({ label: '良好', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  199. break;
  200. case '一般':
  201. resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  202. break;
  203. case '较差':
  204. resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  205. break;
  206. default:
  207. console.log('老年人精神状态统计查询!');
  208. }
  209. });
  210. // mind可以与该类型共用
  211. const mind = ctx.mind();
  212. mind.forEach(item => {
  213. result.push({ label: item, value: 0 + '%' });
  214. });
  215. result.forEach(itemA => {
  216. resultArr.forEach(item => {
  217. if (item.label == itemA.label) {
  218. itemA.value = item.value;
  219. }
  220. });
  221. });
  222. } catch (e) {
  223. ctx.logger.error('catch--------error', e);
  224. ctx.error(e);
  225. } finally {
  226. return result;
  227. }
  228. }
  229. async live(query) {
  230. const { ctx } = this;
  231. query.$and = [{ live: { $ne: null } }, { live: { $ne: '健康' } }];
  232. const resultList = await ctx.model.VisitModel.aggregate([
  233. { $match: ctx.alterDeptId(query) },
  234. {
  235. $group: {
  236. _id: '$live', count: { $sum: 1 },
  237. },
  238. },
  239. {
  240. $project: {
  241. _id: 1, count: 1,
  242. },
  243. },
  244. ]);
  245. const resultCount = await ctx.model.VisitModel.aggregate([
  246. { $match: ctx.alterDeptId(query) },
  247. { $count: 'count' },
  248. ]);
  249. const resultArr = [];
  250. const result = [];
  251. try {
  252. resultList.forEach(item => {
  253. switch (item._id) {
  254. case '良好':
  255. resultArr.push({ label: '良好', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  256. break;
  257. case '一般':
  258. resultArr.push({ label: '一般', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  259. break;
  260. case '较差':
  261. resultArr.push({ label: '较差', value: Math.round(item.count / resultCount[0].count * 10000) / 100 + '%' });
  262. break;
  263. default:
  264. console.log('老年人精神状态统计查询!');
  265. }
  266. });
  267. // mind可以与该类型共用
  268. const mind = ctx.mind();
  269. mind.forEach(item => {
  270. result.push({ label: item, value: 0 + '%' });
  271. });
  272. result.forEach(itemA => {
  273. resultArr.forEach(item => {
  274. if (item.label == itemA.label) {
  275. itemA.value = item.value;
  276. }
  277. });
  278. });
  279. } catch (e) {
  280. ctx.logger.error('catch--------error', e);
  281. ctx.error(e);
  282. } finally {
  283. return result;
  284. }
  285. }
  286. async selectUserBySex(query) {
  287. const { ctx } = this;
  288. query.sex = { $ne: null };
  289. const resultList = await ctx.model.SysUserModel.aggregate([
  290. { $match: ctx.alterDeptId(query) },
  291. {
  292. $group: {
  293. _id: '$sex', count: { $sum: 1 },
  294. },
  295. },
  296. {
  297. $project: {
  298. _id: 1, count: 1,
  299. },
  300. },
  301. ]);
  302. const resultCount = await ctx.model.SysUserModel.aggregate([
  303. { $match: ctx.alterDeptId(query) },
  304. { $count: 'count' },
  305. ]);
  306. const resultArr = [];
  307. const result = [];
  308. try {
  309. resultList.forEach(item => {
  310. switch (item._id) {
  311. case '男':
  312. resultArr.push({ label: '男性巡防员', value: item.count, percent: Math.round(item.count / resultCount[0].count * 10000) / 100 + '' });
  313. break;
  314. case '女':
  315. resultArr.push({ label: '女性巡防员', value: item.count, percent: Math.round(item.count / resultCount[0].count * 10000) / 100 + '' });
  316. break;
  317. default:
  318. console.log('巡访员性别分布统计查询!');
  319. }
  320. });
  321. const userBySex = ctx.userBySex();
  322. userBySex.forEach(item => {
  323. result.push({ label: item, value: 0, percent: '0.00' });
  324. });
  325. result.forEach(itemA => {
  326. resultArr.forEach(item => {
  327. if (item.label == itemA.label) {
  328. itemA.value = item.value;
  329. itemA.percent = item.percent;
  330. }
  331. });
  332. });
  333. } catch (e) {
  334. ctx.logger.error('catch--------error', e);
  335. ctx.error(e);
  336. } finally {
  337. return result;
  338. }
  339. }
  340. async selectAuthAndLook(query) {
  341. const { ctx } = this;
  342. const resultList = await ctx.model.SysUserModel.aggregate([
  343. { $match: ctx.alterDeptId(query) },
  344. {
  345. $project: {
  346. _id: 1,
  347. politicalOutlook: '$politicalOutlook', // 政治面貌
  348. authentication: {
  349. // 认证状态
  350. $cond: {
  351. if: {
  352. $ifNull: [ '$file', false ],
  353. },
  354. then:
  355. {
  356. $cond:
  357. [{
  358. $ne: [{
  359. $ifNull: [ '$file', '' ],
  360. }, '' ],
  361. }, 0, 1 ],
  362. },
  363. else: 1,
  364. },
  365. },
  366. },
  367. },
  368. {
  369. $group: {
  370. _id: {
  371. authentication: '$authentication',
  372. politicalOutlook: '$politicalOutlook',
  373. },
  374. count: { $sum: 1 },
  375. },
  376. },
  377. {
  378. $group: {
  379. _id:
  380. {
  381. politicalOutlook: '$_id.politicalOutlook',
  382. authentication: '$_id.authentication',
  383. count: '$count',
  384. },
  385. },
  386. },
  387. {
  388. $match: {
  389. $or: [
  390. {
  391. '_id.authentication': 0,
  392. '_id.politicalOutlook': '群众',
  393. },
  394. {
  395. '_id.authentication': 0,
  396. '_id.politicalOutlook': '中共党员',
  397. },
  398. {
  399. '_id.authentication': 1,
  400. '_id.politicalOutlook': '群众',
  401. },
  402. {
  403. '_id.authentication': 1,
  404. '_id.politicalOutlook': '中共党员',
  405. },
  406. ],
  407. },
  408. },
  409. {
  410. $project: {
  411. _id: 0,
  412. politicalOutlook: '$_id.politicalOutlook',
  413. count: '$_id.count',
  414. status: { $cond: [{ $eq: [ '$_id.authentication', 0 ] }, '认证', '未认证' ] },
  415. label: { $concat: [{ $cond: [{ $eq: [ '$_id.authentication', 0 ] }, '认证', '未认证' ] }, '$_id.politicalOutlook' ] },
  416. },
  417. },
  418. ]);
  419. const resultArr = [];
  420. const result1 = []; // 党员
  421. const result2 = []; // 巡防员
  422. const result3 = []; // 党员
  423. const result4 = []; // 巡防员
  424. let result5 = {}; // 党员
  425. let result6 = {}; // 巡防员
  426. try {
  427. resultList.forEach(item => {
  428. switch (item.label) {
  429. case '认证中共党员':
  430. result1.push({ label: item.status, value: item.count });
  431. break;
  432. case '未认证中共党员':
  433. result1.push({ label: item.status, value: item.count });
  434. break;
  435. case '认证群众':
  436. result2.push({ label: item.status, value: item.count });
  437. break;
  438. case '未认证群众':
  439. result2.push({ label: item.status, value: item.count });
  440. break;
  441. default:
  442. console.log('巡访员性别分布统计查询!');
  443. }
  444. });
  445. // 党员
  446. const selectAuthAndLook1 = ctx.selectAuthAndLook();
  447. selectAuthAndLook1.forEach(item => {
  448. result3.push({ label: item, value: 0 });
  449. });
  450. result3.forEach(itemA => {
  451. result1.forEach(item => {
  452. if (item.label == itemA.label) {
  453. itemA.value = item.value;
  454. }
  455. });
  456. });
  457. // 寻访员
  458. const selectAuthAndLook2 = ctx.selectAuthAndLook();
  459. selectAuthAndLook2.forEach(item => {
  460. result4.push({ label: item, value: 0 });
  461. });
  462. result4.forEach(itemA => {
  463. result2.forEach(item => {
  464. if (item.label == itemA.label) {
  465. itemA.value = item.value;
  466. }
  467. });
  468. });
  469. result5 = { label: '党员', arr: result3 };
  470. result6 = { label: '群众', arr: result4 };
  471. resultArr.push(result5);
  472. resultArr.push(result6);
  473. } catch (e) {
  474. ctx.logger.error('catch--------error', e);
  475. ctx.error(e);
  476. } finally {
  477. return resultArr;
  478. }
  479. }
  480. async selectUserValue(query) {
  481. const { ctx } = this;
  482. const resultList = await ctx.model.ValueModel.aggregate([
  483. { $match: ctx.alterDeptId(query) },
  484. { $group:
  485. {
  486. _id: '$userid',
  487. dept2: { $first: '$dept2' },
  488. dept3: { $first: '$dept3' },
  489. dept4: { $first: '$dept4' },
  490. dept5: { $first: '$dept5' },
  491. type0: { $sum: {
  492. $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
  493. } },
  494. type1: { $sum: {
  495. $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
  496. } },
  497. },
  498. },
  499. { $project: {
  500. _id: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1,
  501. infoValue: { $multiply: [ '$type0', 5 ] },
  502. visitValue: { $multiply: [ '$type1', 1 ] },
  503. totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
  504. } },
  505. { $sort: { totalValue: -1 } },
  506. {
  507. $limit: 10,
  508. },
  509. { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'user' } },
  510. { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
  511. { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
  512. { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
  513. { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
  514. { $project: {
  515. _id: 0,
  516. userName: '$user.userName',
  517. score: '$totalValue',
  518. address: '$dept4.name',
  519. } },
  520. ]);
  521. return resultList;
  522. }
  523. async selectByJob(query) {
  524. query.job = { $ne: null };
  525. const { ctx } = this;
  526. const allCount = await ctx.model.SysUserModel.find(ctx.alterDeptId(query)).count();
  527. const condition = [ '村', '社区', '民政' ];
  528. let knowJobCount = 0;
  529. let resultList;
  530. const resultArrCount = [];
  531. for (let i = 0; i < condition.length; i++) {
  532. resultList = await ctx.model.SysUserModel.aggregate([
  533. { $match: ctx.alterDeptId(query) },
  534. {
  535. $group: {
  536. _id: '$job',
  537. count: {
  538. $sum: 1,
  539. },
  540. },
  541. },
  542. {
  543. $match: {
  544. _id: { $regex: condition[i] },
  545. },
  546. },
  547. ]);
  548. let resultCount = 0;
  549. resultList.forEach(item => {
  550. resultCount += item.count;
  551. });
  552. knowJobCount += resultCount;
  553. resultArrCount.push(resultCount);
  554. }
  555. const jobType = [ '村工作人员', '社区工作人员', '民政工作人员' ];
  556. const resultArr = [];
  557. for (let i = 0; i < resultArrCount.length; i++) {
  558. resultArr.push({ label: jobType[i], value: Math.round(resultArrCount[i] / allCount * 10000) / 100 + '%' });
  559. }
  560. resultArr.push({ label: '其他工作人员', value: (100 - Math.round(knowJobCount / allCount * 10000) / 100).toFixed(2) + '%' });
  561. return resultArr;
  562. }
  563. async selectDeptValue(query) {
  564. const { ctx } = this;
  565. const resultList = await ctx.model.ValueModel.aggregate([
  566. { $match: ctx.alterDeptId(query) },
  567. {
  568. $group: {
  569. _id: '$type',
  570. count: {
  571. $sum: 1,
  572. },
  573. },
  574. },
  575. ]);
  576. const resultArr = [];
  577. let allValue = 0;
  578. resultList.forEach(item => {
  579. if (item._id === '0') { // 采集5积分
  580. allValue += item.count * 5;
  581. resultArr.push({ label: 'infoValue', value: item.count * 5 });
  582. } else { // 探访1积分
  583. allValue += item.count * 1;
  584. resultArr.push({ label: 'visitValue', value: item.count * 1 });
  585. }
  586. });
  587. resultArr.push({ label: 'totalValue', value: allValue });
  588. // 数组形式转为集合形式,与java返回结果对应
  589. const obj = {};
  590. resultArr.forEach(item => {
  591. const key = item.label;
  592. obj[key] = item.value;
  593. });
  594. return obj;
  595. }
  596. async visitWay(query) {
  597. const { ctx } = this;
  598. query.status = '3';
  599. const resultList = await ctx.model.InfoModel.aggregate([
  600. { $match: ctx.alterDeptId(query) },
  601. {
  602. $project: {
  603. _id: 1,
  604. regularsInfo: 1,
  605. visitType1: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '上门巡访' ] }, 0 ] }, 1, 0 ] } },
  606. visitType2: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '电话问候' ] }, 0 ] }, 1, 0 ] } },
  607. visitType3: { $sum: { $cond: [{ $gt: [{ $indexOfCP: [ '$regularsInfo', '其它' ] }, 0 ] }, 1, 0 ] } },
  608. },
  609. },
  610. {
  611. $group: {
  612. _id: null,
  613. visitType1: { $sum: '$visitType1' },
  614. visitType2: { $sum: '$visitType2' },
  615. visitType3: { $sum: '$visitType3' },
  616. },
  617. },
  618. {
  619. $project: {
  620. _id: 0, visitType1: 1, visitType2: 1, visitType3: 1,
  621. },
  622. },
  623. ]);
  624. let arr = 0;
  625. // 数组内的value相加
  626. for (const i in resultList[0]) {
  627. arr += resultList[0][i];
  628. }
  629. const resultArr = [];
  630. const result = [];
  631. try {
  632. resultList.forEach(item => {
  633. if (item.visitType1) {
  634. resultArr.push({ label: '上门巡访', value: Math.round(item.visitType1 / arr * 10000) / 100 + '' });
  635. }
  636. if (item.visitType2) {
  637. resultArr.push({ label: '电话问候', value: Math.round(item.visitType2 / arr * 10000) / 100 + '' });
  638. }
  639. if (item.visitType3) {
  640. resultArr.push({ label: '其它', value: Math.round(item.visitType3 / arr * 10000) / 100 + '' });
  641. }
  642. });
  643. const visitWay = ctx.visitWay();
  644. visitWay.forEach(item => {
  645. result.push({ label: item, value: '0' });
  646. });
  647. result.forEach(itemA => {
  648. resultArr.forEach(item => {
  649. if (item.label == itemA.label) {
  650. itemA.value = item.value;
  651. }
  652. });
  653. });
  654. } catch (e) {
  655. ctx.logger.error('catch--------error', e);
  656. ctx.error(e);
  657. } finally {
  658. return result;
  659. }
  660. }
  661. async visitnum(query) {
  662. const { ctx } = this;
  663. const resultList = await ctx.model.VisitModel.find(ctx.alterDeptId(query)).countDocuments();
  664. const resultArr = { visitnum: resultList };
  665. return resultArr;
  666. }
  667. }
  668. module.exports = VisitService;