Browse Source

1、待实现分佣根据订单状态提现;

Lawsun_M 10 months ago
parent
commit
045a38c999
1 changed files with 45 additions and 13 deletions
  1. 45 13
      app/controller/home/user.js

+ 45 - 13
app/controller/home/user.js

@@ -445,12 +445,19 @@ module.exports = class UserController extends shopController {
    */
   async userCommissionLog() {
     const that = this;
+    const seq = that.app.Sequelize;
     try {
       const data = await that.ctx.validate(that.userMoneyValidate, await that.ctx.getParse());
       const selectBean = await that.app.comoBean.instance(data, {
         // offset: (data.page - 1) * data.limit, limit: data.limit,
         where: { user_id: data.user_id },
         order: [ [ 'log_id', 'desc' ] ],
+        include: [ {
+          model: that.app.model.Orders,
+          where: { order_status: { [seq.Op.in]: [ 1, 2, 3, 4 ] } },
+          attributes: [ 'order_id', 'order_status', 'order_amount', ],
+          as: 'order'
+        } ],
         attributes: [ 'log_desc', 'create_time', 'commission', 'type', 'inviter_id', 'inviter_name', 'inviter_img' ],
       });
       // 2022/11/28 直接查询所有数据 不分页
@@ -845,7 +852,6 @@ module.exports = class UserController extends shopController {
 
   /**
    * 获取用户可提现金额(求和)
-   * todo 判断订单状态 确认收货的订单才能提现
    * @return {Promise<void>}
    */
   async getCouldCashCommission(user_id = -1) {
@@ -854,33 +860,59 @@ module.exports = class UserController extends shopController {
     }
     const that = this;
     const seq = that.app.Sequelize;
-    const currentDateTime = that.app.szjcomo.date('Y-m-d H:i:s');
-    const sevenDayTime = 7 * 24 * 60 * 60 * 1000;
-    const endTimeStamp = Date.parse(currentDateTime) - sevenDayTime;
-    const endDateTime = that.app.szjcomo.date('Y-m-d H:i:s', endTimeStamp / 1000);
-    // 2022/11/28 查询7天前的所得分佣总和 以及所有时间得提现总和 再求和 即为 可提现金额
+    // 2024/2/26  所有分佣金额
     const selectBean = await that.app.comoBean.instance({ user_id }, {
-      where: { user_id, create_time: { [seq.Op.lte]: endDateTime }, commission: { [seq.Op.gte]: 0 } },
+      where: { user_id, commission: { [seq.Op.gte]: 0 } },
+      include: [ {
+        model: that.app.model.Orders,
+        where: { order_status: { [seq.Op.in]: [ 1, 2, 3, 4 ] } },
+        attributes: [ 'order_id', 'order_status', 'order_amount', ],
+        as: 'order'
+      } ],
       attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'all_commission' ] ],
     });
-    const commissionResult = await that.service.base.select(selectBean, that.app.model.UsersCommissionLogs, '查询佣金失败,请稍候重试', false, false);
+    const allCommissionResult = await that.service.base.select(selectBean, that.app.model.UsersCommissionLogs, '查询佣金失败,请稍候重试', false, false);
+    // 2024/2/26  订单完成可提现金额
+    const selectBean3 = await that.app.comoBean.instance({ user_id }, {
+      where: { user_id, commission: { [seq.Op.gte]: 0 } },
+      include: [ {
+        model: that.app.model.Orders,
+        where: { order_status: { [seq.Op.in]: [ 3, 4 ] } },
+        attributes: [ 'order_id', 'order_status', 'order_amount', ],
+        as: 'order'
+      } ],
+      attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'order_done_commission' ] ],
+    });
+    const orderCommissionResult = await that.service.base.select(selectBean3, that.app.model.UsersCommissionLogs, '查询佣金失败,请稍候重试', false, false);
+    // 2024/2/26  已提现金额
     const selectBean2 = await that.app.comoBean.instance({ user_id }, {
       where: { user_id, commission: { [seq.Op.lte]: 0 }, type: { [seq.Op.ne]: -1 } }, // type-1提现失败类型
       attributes: [ 'user_id', [ seq.fn('sum', seq.col('commission')), 'all_cash' ] ],
     });
     const cashResult = await that.service.base.select(selectBean2, that.app.model.UsersCommissionLogs, '查询佣金失败,请稍候重试', false, false);
-    const commRes = JSON.parse(JSON.stringify(commissionResult));
+
+    const allCommRes = JSON.parse(JSON.stringify(allCommissionResult));
+    const orderCommRes = JSON.parse(JSON.stringify(orderCommissionResult));
     const cashRes = JSON.parse(JSON.stringify(cashResult));
-    commRes.all_commission_could_cash = new Decimal(commRes.all_commission ? commRes.all_commission : 0)
+    // 2024/2/26  待提现分佣金额
+    cashRes.all_commission_tobe_cash = new Decimal(allCommRes.all_commission ? allCommRes.all_commission : 0)
+      .add(new Decimal(cashRes.all_cash ? cashRes.all_cash : 0))
+      .toNumber();
+    // 2024/2/26  可提现分佣金额
+    cashRes.all_commission_could_cash = new Decimal(orderCommRes.order_done_commission ? orderCommRes.order_done_commission : 0)
+      .add(new Decimal(cashRes.all_cash ? cashRes.all_cash : 0))
+      .toNumber();
+    cashRes.all_commission = allCommRes.all_commission;
+    // 2024/2/26 待订单确认收货分佣
+    cashRes.all_commission_order_not_done = new Decimal(allCommRes.all_commission ? allCommRes.all_commission : 0)
       .add(new Decimal(cashRes.all_cash ? cashRes.all_cash : 0))
+      .sub(cashRes.all_commission_could_cash)
       .toNumber();
-    commRes.all_cash = cashRes.all_cash;
-    return commRes;
+    return cashRes;
   }
 
   /**
    * 用户佣金提现
-   * TODO 提现条件 分佣对应订单状态 确认收货 方可提现
    * @return {Promise<void>}
    */
   async userCashOut() {