tRbacUserService.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class TRbacUserService extends Service {
  4. async index(cond, condStr = 'total') {
  5. const { ctx } = this;
  6. const maxStrArray = [ 'total', 'appTotal', 'iviTotal' ];
  7. let agg = [];
  8. if (maxStrArray.indexOf(condStr) !== -1) {
  9. agg = ctx.helper.getCommonAggMax({ ...cond, value: condStr });
  10. } else {
  11. agg = ctx.helper.getCommonAggSum({ ...cond, value: condStr });
  12. }
  13. return await ctx.model.Local.TRbacUserModel.aggregateFix(agg);
  14. }
  15. async sexAndAge(condStr = 'sexAndAgeTotal') {
  16. const { ctx } = this;
  17. const oneCond = { create_date: 1 };
  18. oneCond[condStr] = 1;
  19. const result = await ctx.model.Local.TRbacUserModel.findOne({}, oneCond)
  20. .sort({ create_date: -1 });
  21. if (result) {
  22. return { time: ctx.helper.formatTime1(result.create_date), list: result[condStr] };
  23. }
  24. return { time: '', list: [] };
  25. }
  26. async statistics({ timeRangData, initData, isForceUpdate }) {
  27. const { ctx } = this;
  28. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.TRbacUserModel,
  29. { ...initData });
  30. if (hasData && !isForceUpdate) {
  31. return;
  32. }
  33. initData.start_time = new Date();
  34. const total = await this.total({ create_date: { $lt: timeRangData.endTime } });
  35. ctx.logger.info('任务进行total');
  36. const newTotal = await this.total({ create_date:
  37. { $gte: timeRangData.startTime, $lt: timeRangData.endTime } });
  38. ctx.logger.info('任务进行newTotal');
  39. const sexAndAgeTotal = await this.sex2Age({ create_date: { $lt: timeRangData.endTime } });
  40. ctx.logger.info('任务进行sexAndAgeTotal');
  41. let appTotal = 0;
  42. if (timeRangData.startTime >= ctx.helper.timeMonthSeven) {
  43. appTotal = await this.total({
  44. create_date: { $lt: timeRangData.endTime }, user_orgin: 'APP',
  45. });
  46. ctx.logger.info('任务进行appTotal');
  47. }
  48. const appSexAndAgeTotal = await this.sex2Age({
  49. create_date: { $lt: timeRangData.endTime }, user_orgin: 'APP' });
  50. ctx.logger.info('任务进行appSexAndAgeTotal');
  51. const iviTotal = await this.total({
  52. create_date: { $lt: timeRangData.endTime }, user_orgin: 'IVI' });
  53. ctx.logger.info('任务进行iviTotal');
  54. const iviSexAndAgeTotal = await this.sex2Age({
  55. create_date: { $lt: timeRangData.endTime }, user_orgin: 'IVI' });
  56. ctx.logger.info('任务进行iviSexAndAgeTotal');
  57. let newAppTotal = 0;
  58. if (timeRangData.startTime >= ctx.helper.timeMonthSeven) {
  59. newAppTotal = await this.total({
  60. create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'APP',
  61. });
  62. ctx.logger.info('任务进行newAppTotal');
  63. }
  64. const newAppSexAndAgeTotal = await this.sex2Age({
  65. create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'APP' });
  66. ctx.logger.info('任务进行newAppSexAndAgeTotal');
  67. const newIviTotal = await this.total({
  68. create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'IVI' });
  69. ctx.logger.info('任务进行newIviTotal');
  70. const newIviSexAndAgeTotal = await this.sex2Age({
  71. create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'IVI' });
  72. ctx.logger.info('任务进行newIviSexAndAgeTotal');
  73. let activeAppTotal = 0;
  74. if (timeRangData.startTime >= ctx.helper.timeMonthSeven) {
  75. activeAppTotal = await this.activeAppTotal(timeRangData);
  76. ctx.logger.info('任务进行activeAppTotal');
  77. }
  78. const activeAppMonth = await this.activeAppTotal2({ startTime: ctx.helper.getMonthTop(timeRangData.startTime),
  79. endTime: timeRangData.endTime }, 3);
  80. ctx.logger.info('任务进行activeAppMonth');
  81. const activeAppYear = await this.activeAppTotal2({ startTime: ctx.helper.nowYear(timeRangData.startTime),
  82. endTime: timeRangData.endTime }, 30);
  83. ctx.logger.info('任务进行activeAppYear');
  84. const activeAppSexAndAgeTotal = await this.activeAppSex2Age(timeRangData);
  85. ctx.logger.info('任务进行activeAppSexAndAgeTotal');
  86. const activeIviTotal = await this.activeIviTotal(timeRangData);
  87. ctx.logger.info('任务进行activeIviTotal');
  88. const activeIviMonth = await this.activeIviTotal2({ startTime: ctx.helper.getMonthTop(timeRangData.startTime),
  89. endTime: timeRangData.endTime }, 3);
  90. ctx.logger.info('任务进行activeIviMonth');
  91. const activeIviYear = await this.activeIviTotal2({ startTime: ctx.helper.nowYear(timeRangData.startTime),
  92. endTime: timeRangData.endTime }, 30);
  93. ctx.logger.info('任务进行activeIviYear');
  94. const activeIviSexAndAgeTotal = await this.activeIviSex2Age(timeRangData);
  95. ctx.logger.info('任务进行activeIviSexAndAgeTotal');
  96. // 目前的所有性别年龄统计 和这下面的3个 算法-算车主都是算最终态
  97. const saledTotal = await this.total({ role_id: ctx.helper.saledRoleId });
  98. ctx.logger.info('任务进行saledTotal');
  99. const saledAppTotal = await this.total({ user_orgin: 'APP', role_id: ctx.helper.saledRoleId });
  100. ctx.logger.info('任务进行saledAppTotal');
  101. const saledIviTotal = await this.total({ user_orgin: 'IVI', role_id: ctx.helper.saledRoleId });
  102. ctx.logger.info('任务进行saledIviTotal');
  103. await ctx.service.statisticsService.save(ctx.model.Local.TRbacUserModel,
  104. { ...initData, total, newTotal, sexAndAgeTotal,
  105. appTotal, appSexAndAgeTotal,
  106. newAppTotal, newAppSexAndAgeTotal,
  107. activeAppTotal, activeAppMonth, activeAppYear, activeAppSexAndAgeTotal,
  108. iviTotal, iviSexAndAgeTotal,
  109. newIviTotal, newIviSexAndAgeTotal,
  110. activeIviTotal, activeIviMonth, activeIviYear, activeIviSexAndAgeTotal,
  111. saledTotal, saledAppTotal, saledIviTotal,
  112. }, isForceUpdate);
  113. }
  114. async total(cond) {
  115. const { ctx } = this;
  116. return await ctx.model.TRbacUserModel.find(cond).countDocuments();
  117. }
  118. async sex2Age(cond) {
  119. const { ctx } = this;
  120. const agg = [
  121. { $match: { ...cond, role_id: ctx.helper.saledRoleId } },
  122. ...ctx.helper.getAggAndSexMongo(),
  123. ];
  124. return await ctx.model.TRbacUserModel.aggregateFix(agg);
  125. }
  126. async activeAppTotal({ startTime, endTime }) {
  127. const { ctx } = this;
  128. const onlineAgg = [
  129. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  130. { $group: {
  131. _id: '$user_Id',
  132. } },
  133. ];
  134. const result1 = await ctx.model.AppOnlineUserModel.aggregateFix(onlineAgg);
  135. // const unionArray = result1.map(item => item._id);
  136. return result1.length;
  137. // return await ctx.model.TRbacUserModel.find({ user_id: { $in: unionArray }, user_orgin: 'APP' }).countDocuments();
  138. }
  139. async activeAppTotal2({ startTime, endTime }, count = 1) {
  140. const { ctx } = this;
  141. const onlineAgg = [
  142. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  143. { $group: {
  144. _id: { user_id: '$user_Id', login_date: { $dateToString:
  145. { date: { $toDate: '$login_time' }, format: '%Y-%m-%d',
  146. timezone: 'Asia/Shanghai', onNull: 0 } } },
  147. } },
  148. { $group: {
  149. _id: '$_id.user_id',
  150. login_count: { $sum: 1 },
  151. } },
  152. { $match: { login_count: { $gte: count } } },
  153. ];
  154. const result1 = await ctx.model.AppOnlineUserModel.aggregateFix(onlineAgg);
  155. // const unionArray = result1.map(item => item._id);
  156. return result1.length;
  157. // return await ctx.model.TRbacUserModel.find({ user_id: { $in: unionArray }, user_orgin: 'APP' }).countDocuments();
  158. }
  159. async activeAppSex2Age({ startTime, endTime }) {
  160. const { ctx } = this;
  161. const onlineAgg = [
  162. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  163. { $group: {
  164. _id: '$user_Id',
  165. } },
  166. ];
  167. const result1 = await ctx.model.AppOnlineUserModel.aggregateFix(onlineAgg);
  168. const unionArray = result1.map(item => item._id);
  169. const agg = [
  170. { $match: { user_id: { $in: unionArray }, user_orgin: 'APP' } },
  171. ...ctx.helper.getAggAndSexMongo(),
  172. ];
  173. return await ctx.model.TRbacUserModel.aggregateFix(agg);
  174. }
  175. async activeIviTotal({ startTime, endTime }) {
  176. const { ctx } = this;
  177. const agg = [
  178. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  179. { $group: {
  180. _id: '$user_id' },
  181. },
  182. ];
  183. const result = await ctx.model.IviOnlineUserModel.aggregateFix(agg);
  184. return await ctx.model.TRbacUserModel.find({ user_id: { $in: result.map(item => item._id) },
  185. user_orgin: 'IVI' }).countDocuments();
  186. }
  187. async activeIviTotal2({ startTime, endTime }, count = 1) {
  188. const { ctx } = this;
  189. const agg = [
  190. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  191. { $group: {
  192. _id: { user_id: '$user_id', login_date: { $dateToString:
  193. { date: { $toDate: '$login_time' }, format: '%Y-%m-%d',
  194. timezone: 'Asia/Shanghai', onNull: 0 } } },
  195. } },
  196. { $group: {
  197. _id: '$_id.user_id',
  198. login_count: { $sum: 1 },
  199. } },
  200. { $match: { login_count: { $gte: count } } },
  201. ];
  202. const result = await ctx.model.IviOnlineUserModel.aggregateFix(agg);
  203. return await ctx.model.TRbacUserModel.find({ user_id: { $in: result.map(item => item._id) },
  204. user_orgin: 'IVI' }).countDocuments();
  205. }
  206. async activeIviSex2Age({ startTime, endTime }) {
  207. const { ctx } = this;
  208. const onlineAgg = [
  209. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  210. { $group: {
  211. _id: '$user_id',
  212. } },
  213. ];
  214. const result = await ctx.model.IviOnlineUserModel.aggregateFix(onlineAgg);
  215. const unionArray = result.map(item => item._id);
  216. const agg = [
  217. { $match: { user_id: { $in: unionArray }, user_orgin: 'IVI' } },
  218. ...ctx.helper.getAggAndSexMongo(),
  219. ];
  220. return await ctx.model.TRbacUserModel.aggregateFix(agg);
  221. }
  222. }
  223. module.exports = TRbacUserService;