| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\jobs\AiCloudInventoryMemberJob;
- use app\logic\CurrencyLogic;
- use Exception;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%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 UserWallet extends \yii\db\ActiveRecord
- {
- /**
- * 商城订单
- */
- const TYPE_ORDER = 'order';
- /**
- * 当面付
- */
- const TYPE_SCAN = 'scan';
- /**
- * 合伙人
- */
- const TYPE_PARTNER = 'partner';
- const TYPE_STORE_UNION = 'store_union';
- const TYPE_UNIT_FOUNDER = 'unit_founder';
- /**
- * 红包
- */
- 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 '{{%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 = UserWallet::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,$new_store_id=0,$award_type="")
- {
- $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)) {
- throw new Exception("用户 $user_id 不存在或金额为0");
- }
- if($store_id != -1){
- $moblie = \Yii::$app->db->createCommand("
- SELECT binding
- FROM cyy_user
- WHERE store_id=:store_id
- AND id = :id
- ")
- ->bindValue(':store_id', $store_id)
- ->bindValue(':id', $user_id)
- ->queryScalar();
- $user_id_new = \Yii::$app->db->createCommand("
- SELECT id
- FROM cyy_saas_user
- WHERE mobile=:mobile
- ")
- ->bindValue(':mobile', $moblie)
- ->queryScalar();
- if(!empty($user_id_new)){
- $user_id = $user_id_new;
- }
- }
- $user_wallet = self::getCurrencyWallet(-1, $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'] . '不足');
- }
- $admin_info = \Yii::$app->db->createCommand("
- SELECT *
- FROM cyy_store
- WHERE id = :storeId
- ", [
- ':storeId' => $new_store_id
- ])->queryOne();
- $change_status = 0;
- $change_status_one = 0;
- /*if(!empty($award_type)){
- if(!empty($admin_info['award_type'])){
- $new_types = explode(',',$admin_info['award_type']);
- if(in_array($award_type,$new_types)){
- $saas_user = \Yii::$app->db->createCommand("
- SELECT *
- FROM cyy_saas_user
- WHERE id=:id
- ")
- ->bindValue(':id', $user_id)
- ->queryOne();
- if(!empty($saas_user)){
- if($money>$saas_user['total_amount']){
- $change_status = 1;
- }else{
- $change_status_one = 1;
- }
- }
- }
- }
- }*/
- if($change_status ==0){
- /*if($change_status_one == 1){
- \Yii::$app->db->createCommand("
- UPDATE cyy_saas_user
- SET total_amount = total_amount - :money
- WHERE id = :id
- ")
- ->bindValue(':id', $user_id)
- ->bindValue(':money', $money)
- ->execute();
- }*/
- $user_wallet = self::getCurrencyWallet(-1, $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;
- }
- if (!$user_wallet->save()) throw new Exception($user_wallet->getErrorMessage());
- //获取日志类
- $currency_logic = new CurrencyLogic($currency);
- $model = $currency_logic->getModel('wallet_log');
- if ($model === false) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} 用户钱包日志模型获取失败 =============="], "app_debug.log");
- throw new Exception("用户钱包日志模型获取失败");
- }
- $model->store_id = $store_id;
- $model->user_id = $user_id;
- $model->saas_id = $user_wallet->saas_id;
- $model->currency_code = $currency['code'];
- $model->money = $money;
- $model->before_money = $before_money;
- $model->type = $type;
- $model->source_table = $source_table;
- $model->source_id = $source_id;
- $model->desc = $desc;
- if (!$model->save()) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} model保存失败 =============="], "app_debug.log");
- throw new Exception($model->getErrorMessage());
- }
- $trans->commit();
- // 升级成为Ai云库存会员
- $aiCloudInventoryMemberJob = new AiCloudInventoryMemberJob(['saasId' => $user_wallet->saas_id,'storeId'=>$store_id]);
- if (!queue_push($aiCloudInventoryMemberJob)) { //推送失败再推送一次
- queue_push($aiCloudInventoryMemberJob);
- }
- return $model->id;
- }else{
- \Yii::$app->db->createCommand("
- UPDATE cyy_saas_user
- SET wait_amount = wait_amount + :money
- WHERE id = :id
- ")
- ->bindValue(':id', $user_id)
- ->bindValue(':money', $money)
- ->execute();
- $trans->commit();
- }
- } catch (Exception $e) {
- $trans->rollBack();
- //debug_log([__METHOD__, __LINE__, "用户钱包保存失败:{$e->getMessage()}"], "app_wallet_debug.log");
- return false;
- }
- }
- /**
- * 新增余额操作
- * @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 addLogNew($currency, $store_id, $user_id, $money, $desc, $type, string $source_table = '', int $source_id = 0)
- {
- $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)) {
- throw new Exception("用户 $user_id 不存在或金额为0");
- }
- $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;
- }
- if (!$user_wallet->save()) throw new Exception($user_wallet->getErrorMessage());
- //获取日志类
- $currency_logic = new CurrencyLogic($currency);
- $model = $currency_logic->getModel('wallet_log');
- if ($model === false) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} 用户钱包日志模型获取失败 =============="], "app_debug.log");
- throw new Exception("用户钱包日志模型获取失败");
- }
- $model->store_id = $store_id;
- $model->user_id = $user_id;
- $model->saas_id = $user_wallet->saas_id;
- $model->currency_code = $currency['code'];
- $model->money = $money;
- $model->before_money = $before_money;
- $model->type = $type;
- $model->source_table = $source_table;
- $model->source_id = $source_id;
- $model->desc = $desc;
- if (!$model->save()) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} model保存失败 =============="], "app_debug.log");
- throw new Exception($model->getErrorMessage());
- }
- $trans->commit();
- // 升级成为Ai云库存会员
- $aiCloudInventoryMemberJob = new AiCloudInventoryMemberJob(['saasId' => $user_wallet->saas_id,'storeId'=>$store_id]);
- if (!queue_push($aiCloudInventoryMemberJob)) { //推送失败再推送一次
- queue_push($aiCloudInventoryMemberJob);
- }
- return $model->id;
- } catch (Exception $e) {
- $trans->rollBack();
- //debug_log([__METHOD__, __LINE__, "用户钱包保存失败:{$e->getMessage()}"], "app_wallet_debug.log");
- return false;
- }
- }
- //用于拒绝提现
- public static function addLogRef($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)) {
- throw new Exception("用户 $user_id 不存在或金额为0");
- }
- $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;
- // }
- if (!$user_wallet->save()) throw new Exception($user_wallet->getErrorMessage());
- //获取日志类
- $currency_logic = new CurrencyLogic($currency);
- $model = $currency_logic->getModel('wallet_log');
- if ($model === false) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} 用户钱包日志模型获取失败 =============="], "app_debug.log");
- throw new Exception("用户钱包日志模型获取失败");
- }
- $model->store_id = $store_id;
- $model->user_id = $user_id;
- $model->saas_id = $user_wallet->saas_id;
- $model->currency_code = $currency['code'];
- $model->money = $money;
- $model->before_money = $before_money;
- $model->type = $type;
- $model->source_table = $source_table;
- $model->source_id = $source_id;
- $model->desc = $desc;
- if (!$model->save()) {
- // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} model保存失败 =============="], "app_debug.log");
- throw new Exception($model->getErrorMessage());
- }
- $trans->commit();
- return $model->id;
- } catch (Exception $e) {
- $trans->rollBack();
- //debug_log([__METHOD__, __LINE__, "用户钱包保存失败:{$e->getMessage()}"], "app_wallet_debug.log");
- return false;
- }
- }
- }
|