Browse Source

1、UIOT现金券发放及其核销使用;

Lawsun_M 3 months ago
parent
commit
4a046b9a02

+ 32 - 76
app/controller/home/user.js

@@ -214,7 +214,7 @@ module.exports = class UserController extends shopController {
         .required()
         .notEmpty()
         .number(),
-      diningCoinCode: that.ctx.rules.name('餐币二维码特征')
+      couponCode: that.ctx.rules.name('餐币二维码特征')
         .required()
         .notEmpty(),
     };
@@ -254,6 +254,8 @@ module.exports = class UserController extends shopController {
    */
   async loginHandle(user = {}) {
     const that = this;
+    // 2023/2/28 UIOT现金券发放
+    that.service.coupon.dispatchCoupon(user.user_id);
     const token = that.app.jwt.sign(user, that.app.config.jwt.secret, { expiresIn: '24h' });
     const result = { token, user };
     return result;
@@ -486,8 +488,8 @@ module.exports = class UserController extends shopController {
     }
   }
 
-  // 2023/2/28 用户餐饮币账户列表
-  async userDiningCoin() {
+  // 2023/2/28 用户卡券账户列表
+  async userCoupon() {
     const that = this;
     try {
       const data = await that.ctx.validate(that.userMoneyValidate, await that.ctx.getParse());
@@ -523,11 +525,11 @@ module.exports = class UserController extends shopController {
    */
   async getCouldTransferCoin(data) {
     const that = this;
-    if (!data.diningCoinCode) {
+    if (!data.couponCode) {
       throw new Error('参数错误');
     }
     const userInfo = await that.app.model.Users.findOne({
-      where: { openid: data.diningCoinCode },
+      where: { openid: data.couponCode },
       attributes: [ 'user_id' ],
     });
     if (!userInfo || userInfo.user_id < 0) {
@@ -632,7 +634,7 @@ module.exports = class UserController extends shopController {
   }
 
   /**
-   * 商家申请核销餐币
+   * 商家申请核销顾客卡券
    * @return {Promise<void>}
    */
   async coinTransfer() {
@@ -644,91 +646,45 @@ module.exports = class UserController extends shopController {
       // 2023/2/28 获取可核销餐币
       const intervalTime = Date.parse(that.app.szjcomo.date('Y-m-d H:i:s')) - data.time;
       if (intervalTime > 10 * 60 * 1000) {
-        throw new Error('顾客付款码已超时失效,请重新扫码!');
+        throw new Error('顾客卡券二维码已超时失效,请重新扫码!');
       }
       const transferParams = await that.getCouldTransferCoin(data);
       if (data.coinAmount > 0 && data.coinAmount <= transferParams.account) {
-        // 2023/2/28 发起核销收取餐币
+        // 2023/2/28 核销卡券的商家信息
         const partnerInfo = await that.app.model.PartnerInfo.findOne({
           where: { id: transferParams.partner_id },
         });
-        // 2023/4/12 核销餐币 包含通用电子餐币
-        const selectBean = await that.app.comoBean.instance({}, {
+
+        // 2024/9/13  被核销用户卡券信息
+        const result = await that.app.model.DinnerCoins.findOne({
           where: {
             user_id: transferParams.customer_id,
             // partner_id: transferParams.partner_id,
             partner_id: { [seq.Op.in]: [ transferParams.partner_id, 1 ] },
             expired: false,
-          },
-          order: [ [ 'partner_id', 'desc' ], [ 'ori_partner', 'desc' ] ],
-        });
-        // 2023/2/28 查询所有指定商家餐币
-        const result = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户失败,请稍候重试', false, true);
-        const customerAccounts = [];
-        let needAccount = 0;
-        for (const resultElement of result) {
-          needAccount += resultElement.account;
-          customerAccounts.push(JSON.parse(JSON.stringify(resultElement)));
-          if (needAccount >= data.coinAmount) {
-            break;
           }
-        }
-        let firstCoinAmount;
-        let secondCoinAmount;
-        let thirdCoinAmount;
-        let fourthCoinAmount;
-        let incomeCoinAmount = 0;
-        switch (customerAccounts.length) {
-          case 1:
-            firstCoinAmount = data.coinAmount;
-            secondCoinAmount = 0;
-            thirdCoinAmount = 0;
-            fourthCoinAmount = 0;
-            break;
-          case 2:
-            firstCoinAmount = customerAccounts[0].account;
-            secondCoinAmount = data.coinAmount - firstCoinAmount;
-            thirdCoinAmount = 0;
-            fourthCoinAmount = 0;
-            break;
-          case 3:
-            firstCoinAmount = customerAccounts[0].account;
-            secondCoinAmount = customerAccounts[1].account;
-            thirdCoinAmount = data.coinAmount - firstCoinAmount - secondCoinAmount;
-            fourthCoinAmount = 0;
-            break;
-          case 4:
-            firstCoinAmount = customerAccounts[0].account;
-            secondCoinAmount = customerAccounts[1].account;
-            thirdCoinAmount = customerAccounts[2].account;
-            fourthCoinAmount = data.coinAmount - firstCoinAmount - secondCoinAmount - thirdCoinAmount;
-            break;
-          default:
-            break;
-        }
-        // =========================================== 2023/3/1 第一个账户划账======================================
-        await that.doTransferCoins(that, transferParams, firstCoinAmount, partnerInfo, transaction, customerAccounts, 0);
-        incomeCoinAmount += firstCoinAmount * (customerAccounts[0].ori_partner ? partnerInfo.rate_from_partner : partnerInfo.rate_default);
-        // =============================== 2023/3/1 需要第二个账户来继续划账 ==========================================
-        if (customerAccounts.length > 1 && secondCoinAmount > 0) {
-          await that.doTransferCoins(that, transferParams, secondCoinAmount, partnerInfo, transaction, customerAccounts, 1);
-          incomeCoinAmount += secondCoinAmount * (customerAccounts[1].ori_partner ? partnerInfo.rate_from_partner : partnerInfo.rate_default);
-        }
-        // =============================== 2023/3/18 需要第三个账户来继续划账 ==========================================
-        if (customerAccounts.length > 2 && thirdCoinAmount > 0) {
-          await that.doTransferCoins(that, transferParams, thirdCoinAmount, partnerInfo, transaction, customerAccounts, 2);
-          incomeCoinAmount += thirdCoinAmount * (customerAccounts[2].ori_partner ? partnerInfo.rate_from_partner : partnerInfo.rate_default);
-        }
-        // =============================== 2023/3/18 需要第四个账户来继续划账 ==========================================
-        if (customerAccounts.length > 3 && fourthCoinAmount > 0) {
-          await that.doTransferCoins(that, transferParams, fourthCoinAmount, partnerInfo, transaction, customerAccounts, 3);
-          incomeCoinAmount += fourthCoinAmount * (customerAccounts[3].ori_partner ? partnerInfo.rate_from_partner : partnerInfo.rate_default);
+        });
+
+        if (result) {
+          // 2024/9/13 核销 更新顾客账户
+          const customerAccountBean = await that.app.comoBean.instance({
+            expired: true,
+            update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+            expired_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+          }, {
+            where: {
+              user_id: transferParams.customer_id,
+              partner_id: transferParams.partner_id,
+              expired: false,
+            }, transaction,
+          });
+          await that.service.base.update(customerAccountBean, that.app.model.DinnerCoins, '顾客餐饮币余额更新失败,请重试');
+        } else {
+          throw new Error('当前客户没有符合条件的卡券或者卡券已过期!');
         }
         await transaction.commit();
-        incomeCoinAmount = incomeCoinAmount.toFixed(2);
         return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {
-          incomeCoinAmount,
-          transferCoinAmount: data.coinAmount,
+          expired: true,
         }, false));
       }
       throw new Error('输入的核销金额有误,请稍后重试');

+ 0 - 2
app/controller/wechat/wechat.js

@@ -121,8 +121,6 @@ module.exports = class WechatController extends BaseController {
       const updateRes = await that.service.base.update(updateBean, that.app.model.Orders, '支付回调后更新订单状态失败,请稍候重试');
       try {
         if (updateRes) {
-          // 2023/2/28 餐饮币发放
-          that.service.diningCoin.orderDiningCoinDispatchAfterPay(attach.order_id);
           setTimeout(() => {
             // 2022/11/26 付款订单执行分佣
             that.service.commission.orderCommissionDispatchAfterPay(attach.order_id);

+ 1 - 1
app/routers/home/shop.js

@@ -80,7 +80,7 @@ module.exports = app => {
   // 获取用户餐币明细
   subRouter.get('/user/coinDetail', webLogin, shops.user.coinDetail);
   // 获取用户餐饮币账户
-  subRouter.get('/user/diningCoin', webLogin, shops.user.userDiningCoin);
+  subRouter.get('/user/coupon', webLogin, shops.user.userCoupon);
   // 获取新用户福利记录
   subRouter.get('/user/money/newUserBenefits', webLogin, shops.user.newUserBenefits);
   // 用户红包每日一抽

+ 99 - 0
app/service/coupon.js

@@ -0,0 +1,99 @@
+'use strict';
+
+const BaseService = require('./base.js');
+
+// 卡券变动服务类
+class CouponService extends BaseService {
+
+  /**
+   * 卡券变化记录
+   * @return {Promise<type[]>}
+   */
+  async addDiningCoinChangeLog(params = {}, transaction = null) {
+    const that = this;
+    let action_user = '';
+    //  -1提现失败,0提现成功;1现金券发放;2提现中;3
+    // eslint-disable-next-line default-case
+    switch (params.type) {
+      case 1:
+        action_user = '现金券发放';
+        break;
+    }
+    const createBean = await that.app.comoBean.instance({
+      user_id: params.user_id,
+      order_id: params.order_id,
+      type: params.type,
+      account: params.account,
+      out_batch_no: params.out_batch_no,
+      out_detail_no: params.out_detail_no,
+      partner_id: params.partner_id,
+      action_user,
+      log_desc: params.log_desc,
+      create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+      ori_partner: params.ori_partner,
+      customer_id: params.customer_id,
+      customer_name: params.customer_name,
+      customer_img: params.customer_img,
+    }, { transaction });
+    const result = await that.create(createBean, that.app.model.DinnerCoinLogs, '记录用户卡券变动失败');
+    return result;
+  }
+
+  /**
+   * 卡券发放到用户账户 并 记录
+   * @param user_id
+   */
+  async dispatchCoupon(user_id = -1) {
+    const that = this;
+    const transaction = await that.app.model.transaction();
+    try {
+      let order_id = -1;
+      let partner_id = 1;
+      let type = 1;
+      let account = 300;
+      let log_desc = '抽中UIOT现金券';
+
+      // 2023/2/28 查询用户卡券账户列表, 判断添加或更新卡券账户余额
+      const couponInfo = await that.app.model.DinnerCoins.findOne({
+        where: {
+          user_id,
+          // expired: false,
+        }, transaction, raw: true,
+      });
+      // 2024/9/13  没有UIOT现金券的用户自动发放
+      if (!couponInfo) {
+        // 2022/11/18 记录卡券发放记录
+        await that.addDiningCoinChangeLog({
+          user_id, order_id, partner_id, type, account, log_desc,
+        }, transaction);
+        // 2023/2/27 没有对应类型的coupon 则插入该类型的coupon账户
+        const data = {
+          user_id, account, ori_partner: false, partner_id,
+          create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
+          expired: false,
+          expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 999 * 24 * 60 * 60),
+        };
+        // 2024/9/13  查询卡券对应商家信息
+        const res = await that.app.model.PartnerInfo.findOne({
+          where: { id: 1 },// 2024/9/13  1.源森家具
+          transaction, raw: true,
+        });
+        data.partner_name = res.name;
+        data.partner_address = res.address;
+        data.partner_tel = res.tel_num;
+        data.partner_opening_time = res.opening_time;
+        const createBean = await that.app.comoBean.instance(data, { transaction });
+        await that.service.base.create(createBean, that.app.model.DinnerCoins, '卡券发放失败,请重试');
+      }
+      await transaction.commit();
+    } catch (e) {
+      if (transaction) await transaction.rollback();
+      throw new Error(e.toString());
+    }
+  }
+
+
+}
+
+
+module.exports = CouponService;

+ 0 - 202
app/service/diningCoin.js

@@ -1,202 +0,0 @@
-'use strict';
-
-const BaseService = require('./base.js');
-
-// 餐饮币变动服务类
-class DiningCoinService extends BaseService {
-
-  /**
-   * 餐饮币变化
-   * @return {Promise<type[]>}
-   */
-  async addDiningCoinChangeLog(params = {}, transaction = null) {
-    const that = this;
-    let action_user = '';
-    //  -1提现失败,0提现成功;1发放餐饮币;2提现中;3
-    // eslint-disable-next-line default-case
-    switch (params.type) {
-      case -1:
-      case 0:
-      case 2:
-        action_user = '店铺提现';
-        break;
-      case 1:
-        action_user = '购买餐币';
-        break;
-      case 3:
-        action_user = '餐饮消费';
-        break;
-      case 4:
-        action_user = '核销餐币';
-        break;
-    }
-    const createBean = await that.app.comoBean.instance({
-      user_id: params.user_id,
-      order_id: params.order_id,
-      type: params.type,
-      account: params.account,
-      out_batch_no: params.out_batch_no,
-      out_detail_no: params.out_detail_no,
-      partner_id: params.partner_id,
-      action_user,
-      log_desc: params.log_desc,
-      create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
-      ori_partner: params.ori_partner,
-      customer_id: params.customer_id,
-      customer_name: params.customer_name,
-      customer_img: params.customer_img,
-    }, { transaction });
-    const result = await that.create(createBean, that.app.model.DinnerCoinLogs, '记录用户餐饮币变动失败');
-    return result;
-  }
-
-  /**
-   * 餐饮币发放到用户账户 并 记录
-   * @param orderInfo
-   */
-  async dispatchDiningCoin(orderInfo = null) {
-    const that = this;
-    const transaction = await that.app.model.transaction();
-    try {
-      if (orderInfo && !orderInfo.dining_coin_send) {
-        // 2023/2/28  客源来源鉴别
-        let ori_partner_id = -1;
-        const inviterInfo = await that.app.model.Users.findOne({
-          where: { user_id: orderInfo.inviter_id },
-          attributes: [ 'partner_id' ],
-        });
-        if (inviterInfo) {
-          ori_partner_id = inviterInfo.partner_id;
-        }
-        // 2023/2/28 订单餐饮币发放状态
-        let dining_coin_send = false;
-        // 2023/2/27 订单商品匹配发放餐饮币
-        if (orderInfo.orders_products.length > 0) {
-          for (const ordersProduct of orderInfo.orders_products) {
-            if (ordersProduct.dining_partner_id > 0 && ordersProduct.dinning_coin_amount > 0) {
-              // 2022/11/18 记录餐饮币发放记录
-              await that.addDiningCoinChangeLog({
-                user_id: orderInfo.user_id,
-                order_id: orderInfo.order_id,
-                partner_id: ordersProduct.dining_partner_id,
-                type: 1,
-                account: ordersProduct.dinning_coin_amount * ordersProduct.product_count,
-                log_desc: '购酒获得餐饮币',
-              }, transaction);
-              // 2023/2/28 查询用户餐饮币账户列表 添加 或 更新账户余额
-              const info = await that.app.model.DinnerCoins.findOne({
-                where: {
-                  user_id: orderInfo.user_id,
-                  ori_partner: ori_partner_id === ordersProduct.dining_partner_id,
-                  partner_id: ordersProduct.dining_partner_id,
-                  expired: false,
-                },
-                transaction,
-                raw: true,
-              });
-              if (!info) {
-                // 2023/2/27 没有对应类型的餐饮币 则插入该类型的餐饮币账户
-                const data = {
-                  user_id: orderInfo.user_id,
-                  account: ordersProduct.dinning_coin_amount * ordersProduct.product_count,
-                  ori_partner: ori_partner_id === ordersProduct.dining_partner_id,
-                  partner_id: ordersProduct.dining_partner_id,
-                  create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
-                  expired: false,
-                  expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 90 * 24 * 60 * 60),
-                };
-                const res = await that.app.model.PartnerInfo.findOne({
-                  where: { id: ordersProduct.dining_partner_id },
-                  transaction,
-                  raw: true,
-                });
-                data.partner_name = res.name;
-                data.partner_address = res.address;
-                data.partner_tel = res.tel_num;
-                data.partner_opening_time = res.opening_time;
-                const createBean = await that.app.comoBean.instance(data, { transaction });
-                await that.service.base.create(createBean, that.app.model.DinnerCoins, '餐饮币发放失败,请重试');
-              } else {
-                const updateBean = await that.app.comoBean.instance({
-                  account: info.account + ordersProduct.dinning_coin_amount * ordersProduct.product_count,
-                  update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
-                  expired: false,
-                  expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 90 * 24 * 60 * 60),
-                }, {
-                  where: {
-                    user_id: orderInfo.user_id,
-                    ori_partner: ori_partner_id === ordersProduct.dining_partner_id,
-                    partner_id: ordersProduct.dining_partner_id,
-                  }, transaction,
-                });
-                await that.service.base.update(updateBean, that.app.model.DinnerCoins, '餐饮币余额更新失败,请重试');
-              }
-              dining_coin_send = true;
-              // 2023/3/28 更新餐店订单量+1
-              const updatePartnerBean = await that.app.comoBean.instance({
-                order_amount: that.app.Sequelize.literal('order_amount + ' + 1),
-                update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
-              }, { where: { id: ordersProduct.dining_partner_id }, transaction });
-              await that.update(updatePartnerBean, that.app.model.PartnerInfo, '合作餐店订单量更新失败,请稍候重试');
-            }
-          }
-          // 2023/2/28 更新订单餐饮币发放状态
-          if (dining_coin_send) {
-            const updateBean = that.app.comoBean.instance({ dining_coin_send }, {
-              where: { order_id: orderInfo.order_id }, transaction,
-            });
-            await that.service.base.update(updateBean, that.app.model.Orders, '更新订单餐饮币发放状态失败,请稍候重试');
-          }
-          await transaction.commit();
-        }
-      }
-    } catch (e) {
-      if (transaction) await transaction.rollback();
-      throw new Error(e.toString());
-    }
-  }
-
-  /**
-   * 付款后操作检查订单及餐饮币发放
-   * @return {Promise<void>}
-   */
-  async orderDiningCoinDispatchAfterPay(order_id = -1) {
-    const that = this;
-    // 2022/12/24 查询订单包含商品列表详情
-    const order = await that.selectOrderInfo(order_id);
-    if (!order) throw new Error('订单不存在');
-    await that.dispatchDiningCoin(order);
-  }
-
-  /**
-   * 查询订单详情
-   * @param order_id
-   * @return {Promise<null|*>}
-   */
-  async selectOrderInfo(order_id) {
-    const that = this;
-    const options = {
-      where: { order_id }, include: [
-        {
-          model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
-            exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
-          },
-        },
-      ], attributes: {
-        exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
-      },
-    };
-    const selectBean = await that.app.comoBean.instance({}, options);
-    try {
-      return await that.service.base.select(selectBean, that.app.model.Orders, '查询订单详情失败,请稍候重试', false, false);
-    } catch (e) {
-      that.logger.error('订单id:%s ;分佣查询订单详情失败', order_id);
-      return null;
-    }
-  }
-
-
-}
-
-
-module.exports = DiningCoinService;

+ 1 - 1
app/service/shop.js

@@ -79,7 +79,7 @@ class ShopService extends BaseService {
     });
     const res = await that.service.base.select(selectBean, that.app.model.DinnerCoins, '查询餐饮币账户余额失败,请稍候重试', false, false);
     const resultTemp = JSON.parse(JSON.stringify(result));
-    resultTemp.diningCoin = res.account;
+    resultTemp.coupon = res.account;
     return resultTemp;
   }