| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\logic\CurrencyLogic;
- use Exception;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%temp_user_wallet}}".
- * @property int id ID
- * @property int store_id
- * @property int user_id
- * @property int saas_id
- * @property float money
- * @property float money_total
- * @property float frozen
- * @property float withdraw
- * @property string currency_code
- * @property int created_at
- * @property int updated_at
- */
- class TempUserWallet extends \yii\db\ActiveRecord
- {
- /**
- * 商城订单
- */
- const TYPE_ORDER = 'order';
- /**
- * 当面付
- */
- const TYPE_SCAN = 'scan';
- /**
- * 合伙人
- */
- const TYPE_PARTNER = 'partner';
- /**
- * 红包
- */
- const TYPE_RED_PACKET = 'red_packet';
- /**
- * 提现
- */
- const TYPE_CASH = 'cash';
- /**
- * 后台改动
- */
- const TYPE_OPERATOR_BACK = 'operator_back';
- const TYPE_NAME_LIST = array(
- self::TYPE_ORDER => '商城订单',
- self::TYPE_SCAN => '当面付',
- self::TYPE_RED_PACKET => '串码红包',
- self::TYPE_CASH => '提现',
- self::TYPE_OPERATOR_BACK => '后台充值',
- );
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%temp_user_wallet}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'user_id', 'saas_id'], 'integer'],
- [['money', 'money_total', 'frozen', 'withdraw'], 'number'],
- [['created_at', 'updated_at'], 'safe'],
- [['currency_code'], 'string', 'max' => 50],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => '店铺id',
- 'user_id' => '用户id',
- 'saas_id' => '联盟id',
- 'money' => '余额',
- 'money_total' => '余额总金额',
- 'frozen' => '冻结',
- 'withdraw' => '提现',
- 'currency_code' => '业务货币代码',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * 获取用户业务货币钱包
- */
- public static function getCurrencyWallet($store_id, $user_id, $currency_code)
- {
- $currency = Currency::findOne(['code' => $currency_code]);
- $wallet = self::find()->where(['user_id' => $user_id, 'store_id' => $store_id, 'currency_code' => $currency_code])->one();
- if (empty($wallet)) {
- $wallet = new self();
- $wallet->store_id = $store_id;
- $wallet->user_id = $user_id;
- if ($store_id == -1) {
- $wallet->saas_id = $user_id;
- } else {
- $wallet->saas_id = SaasUser::findSaasIdByUserId($user_id);
- }
- $wallet->currency_code = $currency_code;
- if (!$wallet->save()) {
- $msg = "店铺:{$store_id},业务:{$currency_code},用户:{$user_id} 钱包保存失败" . json_encode($wallet->getErrors());
- //debug_log([__METHOD__, __LINE__, $msg], "app_debug.log");
- }
- }
- $wallet->money = round($wallet->money, $currency->scale);
- $wallet->money_total = round($wallet->money_total, $currency->scale);
- $wallet->frozen = round($wallet->frozen, $currency->scale);
- $wallet->withdraw = round($wallet->withdraw, $currency->scale);
- return $wallet;
- }
- public static function getCurrencyWalletBalance($saas_id, $currency_code)
- {
- $currency = Currency::findOne(['code' => $currency_code]);
- $total = TempUserWallet::find()->where([
- 'saas_id' => $saas_id,
- 'currency_code' => $currency_code
- ])->select("SUM(`money`) as total")->scalar();
- return $total > 0 ? round($total, $currency->scale) : 0;
- }
- /**
- * 新增余额操作
- * @param $currency
- * @param $user_id
- * @param $store_id
- * @param $money
- * @param $desc
- * @param $type
- * @param string $source_table
- * @param int $source_id
- * @param bool $reduce_frozen
- * @return boolean
- */
- public static function addLog($currency, $store_id, $user_id, $money, $desc, $type, string $source_table = '', int $source_id = 0, bool $reduce_frozen = false)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} enable:{$currency['enable']} =============="], "app_debug.log");
- if ($currency['enable'] == 0) throw new Exception('货币' . $currency['code'] . '未开启');
- if ($money == 0 || empty($user_id)) {
- $trans->commit();
- return 1;
- }
- $user_wallet = self::getCurrencyWallet($store_id, $user_id, $currency['code']);
- if (($money + $user_wallet->money) < 0) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} 不足 =============="], "app_debug.log");
- throw new Exception($currency['name'] . '不足');
- }
- if ($money < 0 && $user_wallet->money <= abs($money)) {
- $money = -$user_wallet->money;
- }
- $before_money = $user_wallet->money ?? 0;
- $user_wallet->money += $money * 1;
- if ($money > 0) {
- $user_wallet->money_total += $money * 1;
- }
- $res = $user_wallet->save();
- if ($res === false) throw new Exception($user_wallet->getErrorMessage());
- $trans->commit();
- } catch (Exception $e) {
- $trans->rollBack();
- //debug_log([__METHOD__, __LINE__, "用户钱包保存失败:{$e->getMessage()}"], "app_wallet_debug.log");
- return false;
- }
- }
- }
|