statistic.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. console.log(dataParse);
  25. // 2023/7/25 更新用户行为积分
  26. const updateBean = await that.app.comoBean.instance({
  27. intergral: that.app.Sequelize.literal('intergral + ' + 1),
  28. update_ttime: that.app.szjcomo.date('Y-m-d H:i:s'),
  29. }, { where: { user_id: dataParse.user_id }, transaction });
  30. await that.app.comoBean.update(updateBean, that.app.model.Users, '用户积分更新失败,请稍候重试');
  31. // 2023/7/25 记录用户行为事件
  32. if (dataParse.type === 1) { // 商品浏览
  33. // 2023/7/25 查询用户浏览商品记录
  34. const result = await that.app.model.UsersProductBrowseLogs.findOne({
  35. where: {
  36. user_id: dataParse.user_id,
  37. product_id: dataParse.product_id,
  38. },
  39. });
  40. if (result) {
  41. // 2023/7/26 update 商品浏览
  42. const updateBrowse = await that.app.comoBean.instance({
  43. browse_count: that.app.Sequelize.literal('browse_count + ' + 1),
  44. product_name: dataParse.product_name,
  45. browse_params: dataParse.browse_params ? dataParse.browse_params : '{}',
  46. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  47. }, { where: { user_id: dataParse.user_id, product_id: dataParse.product_id }, transaction });
  48. await that.app.comoBean.update(updateBrowse, that.app.model.UsersProductBrowseLogs, '商品浏览记录更新失败,请稍候重试');
  49. } else {
  50. // 2023/7/26 create 商品浏览
  51. const createBrowse = await that.app.comoBean.instance({
  52. user_id: dataParse.user_id,
  53. product_id: dataParse.product_id,
  54. browse_count: 1,
  55. product_name: dataParse.product_name,
  56. browse_params: dataParse.browse_params ? dataParse.browse_params : '{}',
  57. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  58. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  59. }, { transaction });
  60. await that.app.comoBean.create(createBrowse, that.app.model.UsersProductBrowseLogs, '商品浏览记录添加失败,请稍候重试');
  61. }
  62. } else if (dataParse.type === 2) { // 动作记录
  63. // 2023/7/25 查询用户行为记录
  64. const result = await that.app.model.UsersEventLogs.findOne({
  65. where: {
  66. user_id: dataParse.user_id,
  67. event_name: dataParse.event_name,
  68. },
  69. });
  70. if (result) {
  71. // 2023/7/26 update 用户行为
  72. const updateBrowse = await that.app.comoBean.instance({
  73. event_count: that.app.Sequelize.literal('event_count + ' + 1),
  74. event_params: dataParse.event_params ? dataParse.event_params : '{}',
  75. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  76. }, { where: { user_id: dataParse.user_id, event_name: dataParse.event_name }, transaction });
  77. await that.app.comoBean.update(updateBrowse, that.app.model.UsersEventLogs, '用户行为记录更新失败,请稍候重试');
  78. } else {
  79. // 2023/7/26 create 用户行为
  80. const createBrowse = await that.app.comoBean.instance({
  81. user_id: dataParse.user_id,
  82. event_name: dataParse.event_name,
  83. event_count: 1,
  84. event_params: dataParse.event_params ? dataParse.event_params : '{}',
  85. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  86. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  87. }, { transaction });
  88. await that.app.comoBean.create(createBrowse, that.app.model.UsersEventLogs, '用户行为记录添加失败,请稍候重试');
  89. }
  90. }
  91. // 2023/7/26 事务提交
  92. transaction.commit();
  93. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {}, false));
  94. } catch (err) {
  95. console.log(err);
  96. if (transaction) {
  97. transaction.rollback();
  98. }
  99. // return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  100. }
  101. }
  102. /**
  103. * 用户行为统计记录
  104. * @return {Promise<*>}
  105. */
  106. async statisticLogsList() {
  107. const that = this;
  108. // const transaction = await that.app.model.transaction();
  109. try {
  110. const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse());
  111. console.log(dataParse);
  112. // 2023/7/26 查询
  113. // todo : 用户统计记录列表
  114. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {}, false));
  115. } catch (err) {
  116. console.log(err);
  117. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  118. }
  119. }
  120. /**
  121. * 活跃用户列表
  122. * @return {Promise<*>}
  123. */
  124. async activeUsers() {
  125. const that = this;
  126. // const transaction = await that.app.model.transaction();
  127. const seq = that.app.Sequelize;
  128. try {
  129. // const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse());
  130. const dataParse = await that.ctx.anyParse();
  131. console.log(dataParse);
  132. // 2023/7/26 查询
  133. const users = await that.app.model.Users.findAll({
  134. attributes: [ 'user_id', 'nickname', 'intergral', 'money', 'commission', 'headimgurl' ],
  135. order: [[ 'intergral', 'desc' ]],
  136. where: { intergral: { [seq.Op.gte]: 1 } },
  137. });
  138. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false));
  139. } catch (err) {
  140. console.log(err);
  141. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  142. }
  143. }
  144. };