statistic.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. const whereOpts = {
  139. intergral: { [seq.Op.gte]: 1 },
  140. is_employee: false,
  141. login_time: { [seq.Op.gte]: endTimeStamp },
  142. };
  143. if (dataParse.is_office) {
  144. whereOpts.is_office = true;
  145. }
  146. if (dataParse.is_family) {
  147. whereOpts.is_family = true;
  148. }
  149. // 2023/7/26 查询
  150. const users = await that.app.model.Users.findAll({
  151. // attributes: [ 'user_id', 'nickname', 'intergral', 'money', 'commission', 'headimgurl' ],
  152. order: [[ 'login_time', 'desc' ], [ 'intergral', 'desc' ]],
  153. where: whereOpts,
  154. });
  155. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false));
  156. } catch (err) {
  157. console.log(err);
  158. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  159. }
  160. }
  161. /**
  162. * 我邀请的活跃用户列表
  163. * @return {Promise<*>}
  164. */
  165. async myActiveUsers() {
  166. const that = this;
  167. // const transaction = await that.app.model.transaction();
  168. const seq = that.app.Sequelize;
  169. try {
  170. const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse());
  171. // const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s');
  172. // const endTimeStamp = Date.parse(currentDateTime) - 30 * 24 * 60 * 60 * 1000;
  173. // const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000);
  174. const selectBean = await that.app.comoBean.instance({}, {
  175. where: { inviter_id: dataParse.user_id },
  176. // create_time: { [seq.Op.gte]: endDateTime },
  177. order: [[ 'create_time', 'desc' ]],
  178. });
  179. const myUsers = await that.service.base.select(selectBean, that.app.model.RelUserInviter, '查询邀请关联用户失败,请稍候重试', false, true);
  180. const userIds = [];
  181. for (const user of myUsers) {
  182. userIds.push(user.user_id);
  183. }
  184. const whereOpts = {
  185. user_id: { [seq.Op.in]: userIds },
  186. intergral: { [seq.Op.gte]: 1 },
  187. is_employee: false,
  188. };
  189. if (dataParse.is_office) {
  190. whereOpts.is_office = true;
  191. }
  192. if (dataParse.is_family) {
  193. whereOpts.is_family = true;
  194. }
  195. const users = await that.app.model.Users.findAll({
  196. where: whereOpts,
  197. order: [[ 'login_time', 'desc' ], [ 'intergral', 'desc' ]],
  198. });
  199. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false));
  200. } catch (err) {
  201. console.log(err);
  202. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  203. }
  204. }
  205. };