inviter.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const BaseService = require('./base.js');
  3. // 邀请绑定服务类
  4. class InviterService extends BaseService {
  5. /**
  6. * 记录邀请用户关系绑定
  7. * @param user_id
  8. * @param inviter_id
  9. * @return {Promise<type[]>}
  10. */
  11. async addRelUserInviter(user_id, inviter_id) {
  12. const that = this;
  13. const createBean = await that.app.comoBean.instance({
  14. user_id,
  15. inviter_id,
  16. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  17. }, {});
  18. const result = await that.create(createBean, that.app.model.RelUserInviter, '记录邀请用户关系绑定失败');
  19. return result;
  20. }
  21. /**
  22. * 手动执行一次每日定时任务(完成订单分佣)
  23. * @return {Promise<void>}
  24. */
  25. async executeSchedule() {
  26. /* const that = this;
  27. // 2022/11/17 每日凌晨零点查询过去一周确认收货的订单
  28. that.logger.error('定时任务执行,时间为:%s', this.app.szjcomo.date('Y-m-d H:i:s'));
  29. const seq = that.app.Sequelize;
  30. const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s');
  31. // 2022/11/18 :当前测试调试时间 ,正式发布前时间需要调整
  32. const sevenDayTime = 7 * 24 * 60 * 60 * 1000;
  33. const endTimeStamp = Date.parse(currentDateTime);
  34. const startTimeStamp = endTimeStamp - sevenDayTime;
  35. const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000);
  36. const startDateTime = that.app.szjcomo.date('Y-m-d H:i:s', startTimeStamp / 1000);
  37. const opts = {
  38. where: {// 订单有邀请人id 且 没有分佣记录 且 订单状态是已收货状态( 1 待发货 2待收货 3待评价 4已完成)
  39. inviter_id: { [seq.Op.gte]: 0 },
  40. commission_log_id: -1,
  41. order_status: { [seq.Op.in]: [ 1, 2, 3, 4 ] },
  42. // query_time: { [seq.Op.between]: [ startDateTime, endDateTime ] }, 订单创建时间
  43. create_time: { [seq.Op.between]: [ startDateTime, endDateTime ] },
  44. },
  45. // /!*include: [
  46. // { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  47. // { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ] },
  48. // {
  49. // model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  50. // exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  51. // }, order: [[ 'rec_id', 'asc' ]],
  52. // },
  53. // ],*!/
  54. attributes: {
  55. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  56. }, order: [[ 'order_id', 'desc' ]],
  57. };
  58. const selectBean = await that.app.comoBean.instance({}, opts);
  59. const result = await that.app.comoBean.select(selectBean, that.app.model.Orders, true, true, '定时查询待分佣订单列表失败');
  60. if (result.count > 0) {
  61. result.rows.forEach(order => {
  62. // 2022/11/17 发放分佣并记录
  63. const dispatchResult = that.service.commission.dispatchCommission(order);
  64. let commission_log_id = -1;
  65. dispatchResult.then(log_id => {
  66. commission_log_id = log_id;
  67. if (commission_log_id >= 0) {
  68. // 2022/11/17 更新订单信息
  69. const updateBean = that.app.comoBean.instance({ commission_log_id }, {
  70. where: { order_id: order.order_id },
  71. });
  72. that.service.base.update(updateBean, that.app.model.Orders, '发放分佣后更新订单状态失败,请稍候重试');
  73. } else {
  74. that.logger.error('发放分佣记录失败,对应订单order_id: %s , 订单金额为:%s', order.order_id, order.order_amount);
  75. }
  76. });
  77. });
  78. }*/
  79. }
  80. }
  81. module.exports = InviterService;