statistic.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use strict';
  2. const Base = require('../base');
  3. /**
  4. * 用户统计
  5. */
  6. module.exports = class StatisticController extends Base {
  7. get loginValidate() {
  8. const that = this;
  9. return {
  10. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  11. .number(),
  12. };
  13. }
  14. /**
  15. * 统计
  16. * @return {Promise<*>}
  17. */
  18. async statisticLogs() {
  19. const that = this;
  20. const transaction = await that.app.model.transaction();
  21. try {
  22. // const dataParse = await that.ctx.postParse();
  23. const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse());
  24. // 2023/7/25 更新用户行为积分
  25. const updateBean = await that.app.comoBean.instance({
  26. intergral: that.app.Sequelize.literal('intergral + ' + 1),
  27. update_ttime: that.app.szjcomo.date('Y-m-d H:i:s'),
  28. }, { where: { user_id: dataParse.user_id }, transaction });
  29. await that.app.comoBean.update(updateBean, that.app.model.Users, '用户积分更新失败,请稍候重试');
  30. // 2023/7/25 记录用户行为事件
  31. if (dataParse.type === 1) { // 商品浏览
  32. // 2023/7/25 查询用户浏览商品记录
  33. const result = await that.app.model.UsersProductBrowseLogs.findOne({
  34. where: {
  35. user_id: dataParse.user_id,
  36. product_id: dataParse.product_id,
  37. },
  38. });
  39. if (result) {
  40. // 2023/7/26 update 商品浏览
  41. const updateBrowse = await that.app.comoBean.instance({
  42. browse_count: that.app.Sequelize.literal('browse_count + ' + 1),
  43. product_name: dataParse.product_name,
  44. browse_params: dataParse.browse_params ? dataParse.browse_params : '{}',
  45. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  46. }, { where: { user_id: dataParse.user_id, product_id: dataParse.product_id }, transaction });
  47. await that.app.comoBean.update(updateBrowse, that.app.model.UsersProductBrowseLogs, '商品浏览记录更新失败,请稍候重试');
  48. } else {
  49. // 2023/7/26 create 商品浏览
  50. const createBrowse = await that.app.comoBean.instance({
  51. user_id: dataParse.user_id,
  52. product_id: dataParse.product_id,
  53. browse_count: 1,
  54. product_name: dataParse.product_name,
  55. browse_params: dataParse.browse_params ? dataParse.browse_params : '{}',
  56. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  57. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  58. }, { transaction });
  59. await that.app.comoBean.create(createBrowse, that.app.model.UsersProductBrowseLogs, '商品浏览记录添加失败,请稍候重试');
  60. }
  61. } else if (dataParse.type === 2) { // 动作记录
  62. // 2023/7/25 查询用户行为记录
  63. const result = await that.app.model.UsersEventLogs.findOne({
  64. where: {
  65. user_id: dataParse.user_id,
  66. event_name: dataParse.event_name,
  67. },
  68. });
  69. if (result) {
  70. // 2023/7/26 update 用户行为
  71. const updateBrowse = await that.app.comoBean.instance({
  72. event_count: that.app.Sequelize.literal('event_count + ' + 1),
  73. event_params: dataParse.event_params ? dataParse.event_params : '{}',
  74. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  75. }, { where: { user_id: dataParse.user_id, event_name: dataParse.event_name }, transaction });
  76. await that.app.comoBean.update(updateBrowse, that.app.model.UsersEventLogs, '用户行为记录更新失败,请稍候重试');
  77. } else {
  78. // 2023/7/26 create 用户行为
  79. const createBrowse = await that.app.comoBean.instance({
  80. user_id: dataParse.user_id,
  81. event_name: dataParse.event_name,
  82. event_count: 1,
  83. event_params: dataParse.event_params ? dataParse.event_params : '{}',
  84. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  85. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  86. }, { transaction });
  87. await that.app.comoBean.create(createBrowse, that.app.model.UsersEventLogs, '用户行为记录添加失败,请稍候重试');
  88. }
  89. }
  90. // 2023/7/26 事务提交
  91. transaction.commit();
  92. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {}, false));
  93. } catch (err) {
  94. console.log(err);
  95. if (transaction) {
  96. transaction.rollback();
  97. }
  98. // return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  99. }
  100. }
  101. /**
  102. * 用户行为统计记录
  103. * @return {Promise<*>}
  104. */
  105. async statisticLogsList() {
  106. const that = this;
  107. try {
  108. const dataParse = await that.ctx.anyParse();
  109. // 2023/7/26 查询用户统计记录列表
  110. const resultBrowse = await that.app.model.UsersProductBrowseLogs.findAll({
  111. order: [[ 'update_time', 'desc' ], [ 'browse_count', 'desc' ]],
  112. where: { user_id: dataParse.user_id },
  113. });
  114. const resultEvent = await that.app.model.UsersEventLogs.findAll({
  115. order: [[ 'update_time', 'desc' ], [ 'event_count', 'desc' ]],
  116. where: { user_id: dataParse.user_id },
  117. });
  118. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { resultBrowse, resultEvent }, false));
  119. } catch (err) {
  120. console.log(err);
  121. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  122. }
  123. }
  124. /**
  125. * 活跃用户列表
  126. * @return {Promise<*>}
  127. */
  128. async activeUsers() {
  129. const that = this;
  130. // const transaction = await that.app.model.transaction();
  131. const seq = that.app.Sequelize;
  132. try {
  133. // const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse());
  134. // const dataParse = await that.ctx.anyParse();
  135. const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s');
  136. const endTimeStamp = Date.parse(currentDateTime) - 15 * 24 * 60 * 60 * 1000;
  137. const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000);
  138. // 2023/7/26 查询
  139. const users = await that.app.model.Users.findAll({
  140. // attributes: [ 'user_id', 'nickname', 'intergral', 'money', 'commission', 'headimgurl' ],
  141. order: [[ 'update_ttime', 'desc' ], [ 'intergral', 'desc' ]],
  142. where: { intergral: { [seq.Op.gte]: 1 }, is_employee: false, update_ttime: { [seq.Op.gte]: endDateTime } },
  143. });
  144. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false));
  145. } catch (err) {
  146. console.log(err);
  147. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  148. }
  149. }
  150. /**
  151. * 我邀请的活跃用户列表
  152. * @return {Promise<*>}
  153. */
  154. async myActiveUsers() {
  155. const that = this;
  156. // const transaction = await that.app.model.transaction();
  157. const seq = that.app.Sequelize;
  158. try {
  159. const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse());
  160. const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s');
  161. const endTimeStamp = Date.parse(currentDateTime) - 30 * 24 * 60 * 60 * 1000;
  162. const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000);
  163. const selectBean = await that.app.comoBean.instance({}, {
  164. where: { inviter_id: dataParse.user_id },
  165. create_time: { [seq.Op.gte]: endDateTime },
  166. });
  167. const myUsers = await that.service.base.select(selectBean, that.app.model.RelUserInviter, '查询邀请关联用户失败,请稍候重试', false, true);
  168. const userIds = [];
  169. for (const user of myUsers) {
  170. userIds.push(user.user_id);
  171. }
  172. const users = await that.app.model.Users.findAll({
  173. where: {
  174. user_id: { [seq.Op.in]: userIds },
  175. intergral: { [seq.Op.gte]: 1 },
  176. },
  177. order: [[ 'update_ttime', 'desc' ], [ 'intergral', 'desc' ]],
  178. });
  179. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false));
  180. } catch (err) {
  181. console.log(err);
  182. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  183. }
  184. }
  185. };