|
|
@@ -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
|