Sfoglia il codice sorgente

复消增加兑换商品

zxiansheng 4 anni fa
parent
commit
2eef8d6ee2

+ 106 - 1
common/helpers/user/Balance.php

@@ -21,10 +21,12 @@ use common\models\InvoiceFlow;
 use common\models\Period;
 use common\models\DeclarationLevel;
 use common\models\DecRole;
+use common\models\FlowExchangePoints;
 use common\models\UserPeriodPoints;
 use common\models\UserWallet;
 use common\models\UserBonus;
 use common\models\UserInfo;
+use common\models\UserPeriodExchangePoints;
 use yii\base\Exception;
 use yii\db\Expression;
 
@@ -36,6 +38,7 @@ class Balance {
 
     const BONUS_BALANCE_LOCK_KEY = 'Bonus';
     const RECONSUME_POINTS_BALANCE_LOCK_KEY = 'reconsumePoints';
+    const EXCHANGE_POINTS_BALANCE_LOCK_KEY = 'exchangePoints';
     const CF_BALANCE_LOCK_KEY = 'CF';
     const LX_BALANCE_LOCK_KEY = 'LX';
     const INVOICE_BALANCE_LOCK_KEY = 'Invoice';
@@ -151,6 +154,20 @@ class Balance {
         }
     }
 
+    /**
+     * 获取当前兑换积分余额
+     * @param $userId
+     * @return int|mixed
+     */
+    public static function getBalanceExchangePoints($userId) {
+        $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
+        if ($oneData) {
+            return $oneData['EXCHANGE_POINTS'];
+        } else {
+            return 0;
+        }
+    }
+
     /**
      * 获取当前车房养老奖余额
      * @param $userId
@@ -196,6 +213,9 @@ class Balance {
             case 'RECONSUME_POINTS':
                 $lockKey = self::RECONSUME_POINTS_BALANCE_LOCK_KEY . $userId;
                 break;
+            case 'EXCHANGE_POINTS':
+                $lockKey = self::EXCHANGE_POINTS_BALANCE_LOCK_KEY . $userId;
+                break;
             case 'CF':
                 $lockKey = self::CF_BALANCE_LOCK_KEY . $userId;
                 break;
@@ -431,7 +451,9 @@ class Balance {
                 'SORT' => $params['SORT'] ?? 0,
             ];
             unset($userInfo, $oneUserBonus);
-            if (strtolower($type) == 'reconsume_points' || strtolower($type) == 'cf' || strtolower($type) == 'lx') {
+            if (strtolower($type) == 'reconsume_points' || strtolower($type) == 'cf' || strtolower($type) == 'lx' 
+                || strtolower($type) == 'exchange_points'
+            ) {
                 unset($flowInsertData['CALC_ID']);
                 unset($flowInsertData['TRANSFER_SN']);
                 unset($flowInsertData['SORT']);
@@ -446,6 +468,14 @@ class Balance {
                     self::deductPeriodReconsumePoints($userId, abs($amount));
                 }
                 FlowReconsumePoints::insertOne($flowInsertData);
+            } elseif (strtolower($type) == 'exchange_points') {
+                //记录和扣除期数的积分
+                if( $amount > 0 ) {
+                    self::addPeriodExchangePoints($userId, $periodNum, $amount);
+                }else {
+                    self::deductPeriodExchangePoints($userId, abs($amount));
+                }
+                FlowExchangePoints::insertOne($flowInsertData);
             } elseif (strtolower($type) == 'cf') {
                 FlowCF::insertOne($flowInsertData);
             } elseif (strtolower($type) == 'lx') {
@@ -495,6 +525,42 @@ class Balance {
         return true;
     }
 
+    /**
+     * 添加对应期数的兑换积分
+     * @param $userId
+     * @param $periodNum
+     * @param $amount
+     * @throws \yii\db\Exception
+     * @return boolean
+     */
+    public static function addPeriodExchangePoints($userId, $periodNum, $amount) {
+        if($amount <= 0) return false;
+        $exists = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
+            'USER_ID' => $userId,
+            'PERIOD_NUM' => $periodNum,
+        ])->asArray()->exists();
+        if( $exists ) {
+            UserPeriodExchangePoints::updateAllCounters([
+                'EXCHANGE_POINTS' => $amount,
+                'REMAINDER_POINTS' => $amount,
+            ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
+                'USER_ID' => $userId,
+                'PERIOD_NUM' => $periodNum,
+            ]);
+        }else {
+            UserPeriodExchangePoints::insertOne([
+                'USER_ID' => $userId,
+                'PERIOD_NUM' => $periodNum,
+                'EXCHANGE_POINTS' => $amount,
+                'REMAINDER_POINTS' => $amount,
+                'EXPIRED' => 0,
+                'CREATED_AT' => Date::nowTime()
+            ]);
+        }
+
+        return true;
+    }
+
 
     /**
      * 减少
@@ -535,6 +601,45 @@ class Balance {
         return true;
     }
 
+    /**
+     * 减少
+     * @param $userId
+     * @param $amount
+     * @return bool
+     */
+    public static function deductPeriodExchangePoints($userId, $amount) {
+        if( $amount <= 0 ) return false;
+        $avalidList = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
+            'USER_ID' => $userId,
+            'EXPIRED'=>0
+        ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
+        if( !$avalidList ) return false;
+
+        foreach ($avalidList as $everyData) {
+            if( $amount <= 0 ) break;
+
+            $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
+            if( $amount >= $remainderPoints ) {
+                UserPeriodExchangePoints::updateAllCounters([
+                    'REMAINDER_POINTS' => (-1) * $remainderPoints
+                ], 'ID=:ID', ['ID'=>$everyData['ID']]);
+
+                $amount -= $remainderPoints;
+            }else {
+                UserPeriodExchangePoints::updateAllCounters([
+                    'REMAINDER_POINTS' => (-1) * $amount
+                ], 'ID=:ID', ['ID'=>$everyData['ID']]);
+
+                $amount = 0;
+            }
+
+            unset($everyData, $remainderPoints);
+        }
+        if( $amount > 0 ) return false;
+
+        return true;
+    }
+
     /**
      * 冻结用户余额
      * @param $userId

+ 4 - 0
common/models/BalanceAudit.php

@@ -38,6 +38,10 @@ class BalanceAudit extends \common\components\ActiveRecord
             'name' => 'reconsume_points',
             'label' => '复消积分',
         ],
+        'exchange_points' => [
+            'name' => 'exchange_points',
+            'label' => '兑换积分',
+        ],
         'cash' => [
             'name' => 'cash',
             'label' => '会员账户余额',

+ 82 - 0
common/models/FlowExchangePoints.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace common\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "{{%FLOW_RECONSUME_POINTS}}".
+ *
+ * @property string $ID
+ * @property string $USER_ID 会员ID
+ * @property string $LAST_DEC_LV 记录时会员级别
+ * @property string $LAST_EMP_LV 记录时会员聘级
+ * @property int $LAST_STATUS 记录时状态
+ * @property string $AMOUNT 金额
+ * @property string $TOTAL 当前总金额
+ * @property int $IS_INCR 是否增加
+ * @property string $REMARK 备注
+ * @property int $REMARK_IS_SHOW 备注是否显示
+ * @property string $DEAL_TYPE_ID 交易类型名称
+ * @property int $DEAL_TYPE_IS_PRESET 交易类型是否预置
+ * @property string $ADMIN_NAME 操作人名称
+ * @property int $PERIOD_NUM 所在期数
+ * @property int $CALC_MONTH 所在结算月
+ * @property string $P_MONTH 表分区的日期索引
+ * @property int $CREATED_AT 创建时间
+ * @property int $DELETED 是否删除
+ * @property int $DELETED_AT 删除时间
+ */
+class FlowExchangePoints extends \common\components\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return '{{%FLOW_EXCHANGE_POINTS}}';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['USER_ID', 'REMARK', 'PERIOD_NUM', 'CALC_MONTH', 'P_MONTH', 'CREATED_AT'], 'required'],
+            [['AMOUNT', 'TOTAL'], 'number'],
+            [['LAST_STATUS', 'IS_INCR', 'PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT', 'REMARK_IS_SHOW', 'DELETED', 'DELETED_AT', 'DEAL_TYPE_IS_PRESET'], 'integer'],
+            [['ID', 'USER_ID', 'ADMIN_NAME', 'LAST_DEC_LV', 'LAST_EMP_LV', 'DEAL_TYPE_ID'], 'string', 'max' => 32],
+            [['REMARK'], 'string', 'max' => 255],
+            [['ID'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'ID' => 'ID',
+            'USER_ID' => '会员ID',
+            'LAST_DEC_LV' => '记录时会员级别',
+            'LAST_EMP_LV' => '记录时会员聘级',
+            'LAST_STATUS' => '记录时状态',
+            'AMOUNT' => '金额',
+            'TOTAL' => '当前总金额',
+            'IS_INCR' => '是否增加',
+            'REMARK' => '备注',
+            'REMARK_IS_SHOW' => '备注是否显示',
+            'DEAL_TYPE_ID' => '交易类型ID',
+            'DEAL_TYPE_IS_PRESET' => '交易类型是否预置',
+            'ADMIN_NAME' => '操作人名称',
+            'PERIOD_NUM' => '所在期数',
+            'CALC_MONTH' => '所在结算月',
+            'P_MONTH' => '表分区的日期索引',
+            'CREATED_AT' => '创建时间',
+            'DELETED' => '是否删除',
+            'DELETED_AT' => '删除时间',
+        ];
+    }
+}

+ 7 - 1
common/models/ShopGoods.php

@@ -35,8 +35,11 @@ class ShopGoods extends \common\components\ActiveRecord
             'name' => '余额购买',
         ],
         2 => [
-            'name' => '积分兑换',
+            'name' => '复消兑换',
         ],
+        3 => [
+            'name' => '积分兑换'
+        ]
     ];
     const GOODS_TYPE = [
         1 =>[
@@ -162,6 +165,9 @@ class ShopGoods extends \common\components\ActiveRecord
             'point'=>[
                 'name'=>'积分支付'
             ],
+            'exchange' => [
+                'name' => '积分兑换'
+            ]
         ];
     }
 

+ 59 - 0
common/models/UserPeriodExchangePoints.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace common\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "{{%USER_PERIOD_POINTS}}".
+ *
+ * @property string $ID
+ * @property string $USER_ID 会员ID
+ * @property int $PERIOD_NUM 报单期数
+ * @property string $EXCHANGE_POINTS 兑换积分
+ * @property string $REMAINDER_POINTS 剩余积分
+ * @property int EXPIRED 是否过期
+ * @property int EXPIRED_PERIOD 过期期数
+ * @property int EXPIRED_AT 过期时间
+ * @property int $CREATED_AT 创建时间
+ */
+class UserPeriodExchangePoints extends \common\components\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return '{{%USER_PERIOD_EXCHANGE_POINTS}}';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['USER_ID', 'PERIOD_NUM', 'CREATED_AT'], 'required'],
+            [['PERIOD_NUM', 'CREATED_AT', 'EXPIRED', 'EXPIRED_PERIOD', 'EXPIRED_AT'], 'integer'],
+            [['EXCHANGE_POINTS', 'REMAINDER_POINTS'], 'number'],
+            [['ID', 'USER_ID'], 'string', 'max' => 32],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'ID' => 'ID',
+            'USER_ID' => '会员ID',
+            'EXCHANGE_POINTS' => '兑换积分',
+            'REMAINDER_POINTS' => '剩余积分',
+            'EXPIRED' => '是否过期',
+            'EXPIRED_PERIOD' => '过期期数',
+            'EXPIRED_AT' => '过期时间',
+            'CREATED_AT' => '创建时间',
+        ];
+    }
+}

+ 8 - 2
common/models/forms/OrderForm.php

@@ -334,7 +334,11 @@ class OrderForm extends Model
                 if (Cash::getAvailableBalance($loginUserId) < $this->_payAmount) {
                     throw new Exception('余额不足,无法购买商品');
                 }
-            }else{
+            } else if ($this->payType =='exchange') {
+                if ($this->_payAmount > Balance::getBalanceExchangePoints($loginUserId)) {
+                    throw new Exception('兑换积分不足,无法购买商品');
+                }
+            } else{
                 if ($this->_payAmount > Balance::getBalanceReconsumePoints($loginUserId)) {
                     throw new Exception('复消积分不足,无法购买商品');
                 }
@@ -410,7 +414,9 @@ class OrderForm extends Model
         //扣除会员余额/积分
         if($this->payType=='cash') {
             Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($this->_payAmount), ['REMARK' => '会员复销余额支付']);
-        }else{
+        } else if ($this->payType=='exchange') {
+            Balance::changeUserBonus(\Yii::$app->user->id,'exchange_points', -abs($this->_payAmount),['DEAL_TYPE_ID' => DealType::RECONSUME_POINTS_EXCHANGE,'REMARK' => '会员复销积分兑换']);
+        } else{
             Balance::changeUserBonus(\Yii::$app->user->id,'reconsume_points', -abs($this->_payAmount),['DEAL_TYPE_ID' => DealType::RECONSUME_POINTS_EXCHANGE,'REMARK' => '会员复销积分兑换']);
         }
         return $orderModel;

+ 3 - 1
frontendApi/modules/v1/controllers/ShopController.php

@@ -79,10 +79,12 @@ class ShopController extends BaseController {
         }
         $userBalance = [
             'points' => 0,
-            'cash' => 0
+            'cash' => 0,
+            'exchange' => 0
         ];
         if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
             $userBalance['points'] = $userBonusResult['RECONSUME_POINTS'];
+            $userBalance['exchange'] = $userBonusResult['EXCHANGE_POINTS'];
         }
         if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
             $userBalance['cash'] = $userCashResult['CASH'];

+ 4 - 0
frontendEle/src/views/shop/order.vue

@@ -71,6 +71,10 @@
                         <div>账户余额</div>
                         <div>¥{{balance.cash}}元</div>
                     </div>
+                    <div class="sum_box">
+                        <div>兑换积分</div>
+                        <div>{{balance.exchange}}</div>
+                    </div>
                 </div>
             </div>
             <div>