'商城订单', 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; } } }