statistic.js 8.6 KB

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