'use strict'; const Base = require('../base'); /** * 用户统计 */ module.exports = class StatisticController extends Base { get loginValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), pid: that.ctx.rules.name('商品分类父级ID') .default(4) .number(), }; } /** * 统计 * @return {Promise<*>} */ async statisticLogs() { const that = this; const transaction = await that.app.model.transaction(); try { // const dataParse = await that.ctx.postParse(); const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse()); // 2023/7/25 更新用户行为积分 const updateBean = await that.app.comoBean.instance({ intergral: that.app.Sequelize.literal('intergral + ' + 1), update_ttime: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { user_id: dataParse.user_id }, transaction }); await that.app.comoBean.update(updateBean, that.app.model.Users, '用户积分更新失败,请稍候重试'); // 2023/7/25 记录用户行为事件 if (dataParse.type === 1) { // 商品浏览 // 2023/7/25 查询用户浏览商品记录 const result = await that.app.model.UsersProductBrowseLogs.findOne({ where: { user_id: dataParse.user_id, product_id: dataParse.product_id, }, }); if (result) { // 2023/7/26 update 商品浏览 const updateBrowse = await that.app.comoBean.instance({ browse_count: that.app.Sequelize.literal('browse_count + ' + 1), product_name: dataParse.product_name, browse_params: dataParse.browse_params ? dataParse.browse_params : '{}', update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { user_id: dataParse.user_id, product_id: dataParse.product_id }, transaction }); await that.app.comoBean.update(updateBrowse, that.app.model.UsersProductBrowseLogs, '商品浏览记录更新失败,请稍候重试'); } else { // 2023/7/26 create 商品浏览 const createBrowse = await that.app.comoBean.instance({ user_id: dataParse.user_id, product_id: dataParse.product_id, pid: dataParse.pid, browse_count: 1, product_name: dataParse.product_name, browse_params: dataParse.browse_params ? dataParse.browse_params : '{}', update_time: that.app.szjcomo.date('Y-m-d H:i:s'), create_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { transaction }); await that.app.comoBean.create(createBrowse, that.app.model.UsersProductBrowseLogs, '商品浏览记录添加失败,请稍候重试'); } } else if (dataParse.type === 2) { // 动作记录 // 2023/7/25 查询用户行为记录 const result = await that.app.model.UsersEventLogs.findOne({ where: { user_id: dataParse.user_id, event_name: dataParse.event_name, }, }); if (result) { // 2023/7/26 update 用户行为 const updateBrowse = await that.app.comoBean.instance({ event_count: that.app.Sequelize.literal('event_count + ' + 1), event_params: dataParse.event_params ? dataParse.event_params : '{}', update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { user_id: dataParse.user_id, event_name: dataParse.event_name, pid: dataParse.pid }, transaction }); await that.app.comoBean.update(updateBrowse, that.app.model.UsersEventLogs, '用户行为记录更新失败,请稍候重试'); } else { // 2023/7/26 create 用户行为 const createBrowse = await that.app.comoBean.instance({ user_id: dataParse.user_id, pid: dataParse.pid, event_name: dataParse.event_name, event_count: 1, event_params: dataParse.event_params ? dataParse.event_params : '{}', update_time: that.app.szjcomo.date('Y-m-d H:i:s'), create_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { transaction }); await that.app.comoBean.create(createBrowse, that.app.model.UsersEventLogs, '用户行为记录添加失败,请稍候重试'); } } // 2023/7/26 事务提交 transaction.commit(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {}, false)); } catch (err) { console.log(err); if (transaction) { transaction.rollback(); } // return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * 用户行为统计记录 * @return {Promise<*>} */ async statisticLogsList() { const that = this; try { const dataParse = await that.ctx.anyParse(); // 2023/7/26 查询用户统计记录列表 const resultBrowse = await that.app.model.UsersProductBrowseLogs.findAll({ order: [[ 'update_time', 'desc' ], [ 'browse_count', 'desc' ]], where: { user_id: dataParse.user_id }, }); const resultEvent = await that.app.model.UsersEventLogs.findAll({ order: [[ 'update_time', 'desc' ], [ 'event_count', 'desc' ]], where: { user_id: dataParse.user_id }, }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { resultBrowse, resultEvent }, false)); } catch (err) { console.log(err); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * 活跃用户列表 * @return {Promise<*>} */ async activeUsers() { const that = this; // const transaction = await that.app.model.transaction(); const seq = that.app.Sequelize; try { // const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse()); const dataParse = await that.ctx.anyParse(); const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s'); const endTimeStamp = Date.parse(currentDateTime) - 15 * 24 * 60 * 60 * 1000; // const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000); const whereOpts = { intergral: { [seq.Op.gte]: 1 }, is_employee: false, login_time: { [seq.Op.gte]: endTimeStamp }, }; if (dataParse.is_office) { whereOpts.is_office = true; } if (dataParse.is_family) { whereOpts.is_family = true; } // 2023/7/26 查询 const users = await that.app.model.Users.findAll({ // attributes: [ 'user_id', 'nickname', 'intergral', 'money', 'commission', 'headimgurl' ], order: [[ 'login_time', 'desc' ], [ 'intergral', 'desc' ]], where: whereOpts, }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false)); } catch (err) { console.log(err); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * 我邀请的活跃用户列表 * @return {Promise<*>} */ async myActiveUsers() { const that = this; // const transaction = await that.app.model.transaction(); const seq = that.app.Sequelize; try { const dataParse = await that.ctx.validate(that.loginValidate, await that.ctx.anyParse()); // const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s'); // const endTimeStamp = Date.parse(currentDateTime) - 30 * 24 * 60 * 60 * 1000; // const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000); const selectBean = await that.app.comoBean.instance({}, { where: { inviter_id: dataParse.user_id }, // create_time: { [seq.Op.gte]: endDateTime }, order: [[ 'create_time', 'desc' ]], }); const myUsers = await that.service.base.select(selectBean, that.app.model.RelUserInviter, '查询邀请关联用户失败,请稍候重试', false, true); const userIds = []; for (const user of myUsers) { userIds.push(user.user_id); } const whereOpts = { user_id: { [seq.Op.in]: userIds }, intergral: { [seq.Op.gte]: 1 }, is_employee: false, }; if (dataParse.is_office) { whereOpts.is_office = true; } if (dataParse.is_family) { whereOpts.is_family = true; } const users = await that.app.model.Users.findAll({ where: whereOpts, order: [[ 'login_time', 'desc' ], [ 'intergral', 'desc' ]], }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', { users }, false)); } catch (err) { console.log(err); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };