individualOrDeptRank.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class IndividualOrDeptRankService extends Service {
  4. async visitdeptpage(query) {
  5. const { ctx } = this;
  6. let deptGroup = '';
  7. let params = {};
  8. if (query.time) {
  9. query.visitYM = query.time;
  10. delete query.time;
  11. }
  12. if (query.dept1) {
  13. deptGroup = '$dept2';
  14. params = { fid: query.dept1 };
  15. }
  16. if (query.dept2) {
  17. deptGroup = '$dept3';
  18. params = { fid: query.dept2 };
  19. }
  20. if (query.dept3) {
  21. deptGroup = '$dept4';
  22. params = { fid: query.dept3 };
  23. }
  24. if (query.dept4) {
  25. deptGroup = '$dept5';
  26. params = { fid: query.dept4 };
  27. }
  28. if (query.dept5) {
  29. deptGroup = '$dept5';
  30. params = { _id: query.dept5 };
  31. }
  32. const resultList = await ctx.model.VisitModel.aggregate([
  33. { $project: {
  34. _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1,
  35. visitYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$visitTime', 1000 * 60 * 60 * 8 ] } } },
  36. },
  37. },
  38. { $match: ctx.alterDeptId(query) },
  39. { $group: {
  40. _id: deptGroup,
  41. count: { $sum: 1 },
  42. },
  43. },
  44. { $lookup: { from: 'sys_dept', localField: '_id', foreignField: '_id', as: 'deptInfo' } },
  45. { $unwind: { path: '$deptInfo', preserveNullAndEmptyArrays: true } },
  46. { $project: {
  47. _id: 0, name: '$deptInfo.name', count: 1,
  48. },
  49. },
  50. { $sort: { count: -1 } },
  51. ]);
  52. const deptInfoByFid = await ctx.model.SysDeptModel.find(params);
  53. const resultArr = [];
  54. const allResultArr = [];
  55. let result = [];
  56. try {
  57. resultList.forEach(item =>
  58. resultArr.push({ name: item.name, num: item.count })
  59. );
  60. deptInfoByFid.forEach(item =>
  61. allResultArr.push({ name: item.name, num: 0 })
  62. );
  63. allResultArr.forEach(itemA => {
  64. resultArr.forEach(item => {
  65. if (item.name == itemA.name) {
  66. itemA.num = item.num;
  67. }
  68. });
  69. });
  70. result = allResultArr.sort(ctx.compareTo('num', 'DESC'));
  71. } catch (e) {
  72. ctx.logger.error('catch--------error', e);
  73. ctx.error(e);
  74. } finally {
  75. return result;
  76. }
  77. }
  78. async visitpersonpage(query) {
  79. const { ctx } = this;
  80. if (query.time) {
  81. query.visitYM = query.time;
  82. delete query.time;
  83. }
  84. const resultList = await ctx.model.VisitModel.aggregate([
  85. { $project: {
  86. _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, userid: 1,
  87. visitYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$visitTime', 1000 * 60 * 60 * 8 ] } } },
  88. },
  89. },
  90. { $match: ctx.alterDeptId(query) },
  91. { $group: {
  92. _id: '$userid',
  93. count: { $sum: 1 },
  94. },
  95. },
  96. { $sort: { count: -1 } },
  97. {
  98. $skip: 0,
  99. },
  100. {
  101. $limit: 50,
  102. },
  103. { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'userInfo' } },
  104. { $unwind: { path: '$userInfo', preserveNullAndEmptyArrays: true } },
  105. { $project: {
  106. _id: 1, name: '$userInfo.userName', count: 1, dept5: '$userInfo.dept5',
  107. },
  108. },
  109. { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5Info' } },
  110. { $unwind: { path: '$dept5Info', preserveNullAndEmptyArrays: true } },
  111. { $project: {
  112. _id: 0, name: 1, count: 1, dept5: '$dept5Info.name',
  113. },
  114. },
  115. ]);
  116. const resultArr = [];
  117. try {
  118. resultList.forEach(item =>
  119. resultArr.push({ name: item.name, num: item.count, address: item.dept5 })
  120. );
  121. } catch (e) {
  122. ctx.logger.error('catch--------error', e);
  123. ctx.error(e);
  124. } finally {
  125. return resultArr;
  126. }
  127. }
  128. async infodeptpage(query) {
  129. const { ctx } = this;
  130. let deptGroup = '';
  131. let params = {};
  132. query.status = '3';
  133. if (query.time) {
  134. query.infoYM = query.time;
  135. delete query.time;
  136. }
  137. if (query.dept1) {
  138. deptGroup = '$dept2';
  139. params = { fid: query.dept1 };
  140. }
  141. if (query.dept2) {
  142. deptGroup = '$dept3';
  143. params = { fid: query.dept2 };
  144. }
  145. if (query.dept3) {
  146. deptGroup = '$dept4';
  147. params = { fid: query.dept3 };
  148. }
  149. if (query.dept4) {
  150. deptGroup = '$dept5';
  151. params = { fid: query.dept4 };
  152. }
  153. if (query.dept5) {
  154. deptGroup = '$dept5';
  155. params = { _id: query.dept5 };
  156. }
  157. const resultList = await ctx.model.InfoModel.aggregate([
  158. { $project: {
  159. _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, status: 1,
  160. infoYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$time', 1000 * 60 * 60 * 8 ] } } },
  161. },
  162. },
  163. { $match: ctx.alterDeptId(query) },
  164. { $group: {
  165. _id: deptGroup,
  166. count: { $sum: 1 },
  167. },
  168. },
  169. { $lookup: { from: 'sys_dept', localField: '_id', foreignField: '_id', as: 'deptInfo' } },
  170. { $unwind: { path: '$deptInfo', preserveNullAndEmptyArrays: true } },
  171. { $project: {
  172. _id: 1, name: '$deptInfo.name', count: 1,
  173. },
  174. },
  175. { $sort: { count: -1 } },
  176. ]);
  177. const deptInfoByFid = await ctx.model.SysDeptModel.find(params);
  178. const resultArr = [];
  179. const allResultArr = [];
  180. let result = [];
  181. try {
  182. resultList.forEach(item =>
  183. resultArr.push({ name: item.name, num: item.count })
  184. );
  185. deptInfoByFid.forEach(item =>
  186. allResultArr.push({ name: item.name, num: 0 })
  187. );
  188. allResultArr.forEach(itemA => {
  189. resultArr.forEach(item => {
  190. if (item.name == itemA.name) {
  191. itemA.num = item.num;
  192. }
  193. });
  194. });
  195. result = allResultArr.sort(ctx.compareTo('num', 'DESC'));
  196. } catch (e) {
  197. ctx.logger.error('catch--------error', e);
  198. ctx.error(e);
  199. } finally {
  200. return result;
  201. }
  202. }
  203. async infopersonpage(query) {
  204. const { ctx } = this;
  205. query.status = '3';
  206. if (query.time) {
  207. query.infoYM = query.time;
  208. delete query.time;
  209. }
  210. const resultList = await ctx.model.InfoModel.aggregate([
  211. { $project: {
  212. _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, status: 1, userid: 1,
  213. infoYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$time', 1000 * 60 * 60 * 8 ] } } },
  214. },
  215. },
  216. { $match: ctx.alterDeptId(query) },
  217. { $group: {
  218. _id: '$userid',
  219. count: { $sum: 1 },
  220. },
  221. },
  222. { $sort: { count: -1 } },
  223. {
  224. $skip: 0,
  225. },
  226. {
  227. $limit: 50,
  228. },
  229. { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'userInfo' } },
  230. { $unwind: { path: '$userInfo', preserveNullAndEmptyArrays: true } },
  231. { $project: {
  232. _id: 1, name: '$userInfo.userName', count: 1, dept5: '$userInfo.dept5',
  233. },
  234. },
  235. { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5Info' } },
  236. { $unwind: { path: '$dept5Info', preserveNullAndEmptyArrays: true } },
  237. { $project: {
  238. _id: 0, name: 1, count: 1, dept5: '$dept5Info.name',
  239. },
  240. },
  241. ]);
  242. const resultArr = [];
  243. try {
  244. resultList.forEach(item =>
  245. resultArr.push({ name: item.name, num: item.count, address: item.dept5 })
  246. );
  247. } catch (e) {
  248. ctx.logger.error('catch--------error', e);
  249. ctx.error(e);
  250. } finally {
  251. return resultArr;
  252. }
  253. }
  254. async selectDeptByName(query) {
  255. const { ctx } = this;
  256. const deptName = query.deptName;
  257. let resultStr = '';
  258. const resultList = await ctx.model.SysDeptModel.find({ name: deptName });
  259. if (resultList.length > 0) {
  260. if (resultList[0].fid === '0') {
  261. resultStr = resultList[0]._id;
  262. } else {
  263. const resultList2 = await ctx.model.SysDeptModel.find({ _id: this.app.mongoose.Types.ObjectId(resultList[0].fid) });
  264. if (resultList2[0].fid === '0') {
  265. resultStr = resultList2[0]._id + '&' + resultList[0]._id;
  266. } else {
  267. const resultList3 = await ctx.model.SysDeptModel.find({ _id: this.app.mongoose.Types.ObjectId(resultList2[0].fid) });
  268. if (resultList3[0].fid === '0') {
  269. resultStr = resultList3[0]._id + '&' + resultList2[0]._id + '&' + resultList[0]._id;
  270. }
  271. }
  272. }
  273. }
  274. return resultStr;
  275. }
  276. }
  277. module.exports = IndividualOrDeptRankService;