123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- namespace app\common\components;
- use app\modules\ucenter\models\User;
- use app\modules\ucenter\models\UserCoinLog;
- use app\modules\ucenter\models\UserPointLog;
- use app\modules\ucenter\models\UserMoneyLog;
- use app\modules\shopping\models\ShoppingCommissionLog;
- use app\modules\shopping\models\ShoppingStore;
- use app\modules\Doc\models\DocReal;
- use Yii;
- class Wallet
- {
- //金币变化
- public static function coinChange($user_id,$change_num,$bak,$change_type=1,$field='coin',$table_name='',$data_id=0)
- {
- $userInfo = User::findOne($user_id);
- $before_num = $userInfo[$field];
- $after_num = $change_type==1?$before_num + $change_num:(($before_num - $change_num)>0?($before_num - $change_num):0);
- $log = new UserCoinLog();
- $log->user_id = $userInfo['user_id'];
- $log->before_num = $before_num;
- $log->after_num = $after_num;
- $log->change_num = $change_num;
- $log->change_type = $change_type;
- $log->table_name = $table_name;
- $log->data_id = $data_id;
- $log->field = $field;
- $log->bak = $bak;
- $log->create_time = TIMESTAMP;
- if($log->save())
- {
- $updateNum = $change_type==1?$change_num:0-$change_num;
- User::findOne($user_id)->updateCounters([$field=>$updateNum]);
- //更新用户累计金币收入
- if($field=='coin_income'&&$change_type==1)
- {
- User::findOne($user_id)->updateCounters(['amount_coin_income'=>$updateNum]);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- //金币变化
- public static function pointChange($user_id,$change_num,$bak,$change_type=1,$field='point',$table_name='',$data_id=0)
- {
- if($change_num==0)return true;
- $userInfo = User::findOne($user_id);
- $before_num = intval($userInfo[$field]);
- $after_num = $change_type==1?$before_num + $change_num:(($before_num - $change_num)>0?($before_num - $change_num):0);
- $log = new UserPointLog();
- $log->user_id = $userInfo['user_id'];
- $log->before_num = $before_num;
- $log->after_num = $after_num;
- $log->change_num = $change_num;
- $log->table_name = $table_name;
- $log->data_id = $data_id;
- $log->change_type = $change_type;
- $log->bak = $bak;
- $log->create_time = TIMESTAMP;
- if($log->save())
- {
- $updateNum = $change_type==1?$change_num:0-$change_num;
- User::findOne($user_id)->updateCounters([$field=>$updateNum]);
- //更新用户累计积分
- if($field=='point'&&$change_type==1)
- {
- User::findOne($user_id)->updateCounters(['allpoint'=>$updateNum]);
- }
- return true;
- }
- else
- {
- file_put_contents(UPLOAD_PATH.'log.txt',$log->returnFirstError(),FILE_APPEND);
- }
- }
- //文档分销
- public static function docCommission($user_id,$doc,$paylog,$coin_num)
- {
- $leftCoin = $coin_num;
- $commissionconfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='commission'")->queryOne();
- $commissionConfig = string2array($commissionconfigResult['value']);
- if($commissionConfig['on'])
- {
- $user = User::findOne($user_id);
- if(!empty($user->referer_ids))
- {
- $paylog->agent_id = $user->referer_id;
- $paylog->agent_ids = $user->referer_ids;
- $paylog->save();
- $referer_ids = explode(",",trim($user->referer_ids,","));
- if(is_array($referer_ids))foreach(array_slice($referer_ids,0,Yii::$app->params['level']) as $k=>$referer_id)
- {
- $level = $k+1;
- $tempUser = User::findOne($referer_id);
- $vipSettings = [];
- $commissionRate = 0;
- if($tempUser->vipInfo())
- {
- $vipSettings = $tempUser->vipSettings();
- $commissionRate = $vipSettings?$vipSettings['doc_commission_level'.$level.'_rate']:$commissionConfig['doc_commission_level'.$level.'_rate'];
- }
- else
- {
- $commissionRate = $commissionConfig['doc_commission_level'.$level.'_rate'];
- }
- if($commissionRate>0)
- {
- $store = ShoppingStore::find()->where("user_id=$referer_id")->one();
- $commissionLog = new ShoppingCommissionLog();
- if(!empty($store))
- {
- $commissionLog->store_id = $store->store_id;
- }
- $commissionLog->user_id = $user_id;
- $commissionLog->order_id = 0;
- $commissionLog->goods_id = $paylog->log_id;
- $commissionLog->referer_id = $referer_id;
- $commissionLog->referer_level = $level;
- $commissionLog->commission = round(($commissionRate/100)*$coin_num,2);
- $commissionLog->type = 2;
- $commissionLog->bak = '分销文档';
- $commissionLog->create_time = TIMESTAMP;
- if($commissionLog->save())
- {
- Wallet::coinChange($referer_id,$commissionLog->commission,'分销返佣',1,'coin_income',DocReal::shortTableName(),$doc->id);
- $leftCoin -= $commissionLog->commission;
- //公众号消息推送
- MessageOne::commissionDoc($referer_id,$doc,$paylog->coin_num,$commissionLog->commission);
- }
- }
- }
- }
- }
- return $leftCoin;
- }
- //VIP分销
- public static function vipCommission($user_id,$userVipOrder)
- {
- $commissionconfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='commission'")->queryOne();
- $commissionConfig = string2array($commissionconfigResult['value']);
- if($commissionConfig['on'])
- {
- $user = User::findOne($user_id);
- if(!empty($user->referer_ids))
- {
- $referer_ids = explode(",",trim($user->referer_ids,","));
- if(is_array($referer_ids))foreach(array_slice($referer_ids,0,Yii::$app->params['level']) as $k=>$referer_id)
- {
- $level = $k+1;
- $tempUser = User::findOne($referer_id);
- $vipSettings = [];
- $commissionRate = 0;
- if($tempUser->vipInfo())
- {
- $vipSettings = $tempUser->vipSettings();
- $commissionRate = $vipSettings?$vipSettings['docvip_commission_level'.$level.'_rate']:$commissionConfig['docvip_commission_level'.$level.'_rate'];
- }
- else
- {
- $commissionRate = $commissionConfig['docvip_commission_level'.$level.'_rate'];
- }
- if($commissionRate>0)
- {
- $store = ShoppingStore::find()->where("user_id=$referer_id")->one();
- $commissionLog = new ShoppingCommissionLog();
- if(!empty($store))
- {
- $commissionLog->store_id = $store->store_id;
- }
- $commissionLog->user_id = $user_id;
- $commissionLog->order_id = $userVipOrder->order_id;
- $commissionLog->goods_id = $userVipOrder->vip_type;
- $commissionLog->referer_id = $referer_id;
- $commissionLog->referer_level = $level;
- $commissionLog->commission = round(($commissionRate/100)*$userVipOrder->money,2);
- $commissionLog->type = 1;
- $commissionLog->bak = '分销VIP';
- $commissionLog->create_time = TIMESTAMP;
- if($commissionLog->save())
- {
- Wallet::moneyChange($referer_id,$commissionLog->commission,'分销返佣',1);
- //公众号消息推送
- MessageOne::commissionVip($referer_id,$userVipOrder,$commissionLog->commission);
- }
- }
- }
- }
- }
- }
- //余额变化
- public static function moneyChange($user_id,$change_num,$bak,$change_type=1,$field='money',$status=1)
- {
- if($change_num==0)return true;
- $userInfo = User::findOne($user_id);
- $before_num = intval($userInfo[$field]);
- $after_num = $change_type==1?$before_num + $change_num:(($before_num - $change_num)>0?($before_num - $change_num):0);
- $log = new UserMoneyLog();
- $log->user_id = $userInfo['user_id'];
- $log->before_num = $before_num;
- $log->after_num = $after_num;
- $log->change_num = $change_num;
- $log->change_type = $change_type;
- $log->field = $field;
- $log->bak = $bak;
- $log->status = $status;
- $log->create_time = TIMESTAMP;
- if($log->save())
- {
- $updateNum = $change_type==1?$change_num:0-$change_num;
- User::findOne($user_id)->updateCounters([$field=>$updateNum]);
- //更新用户累计收益
- if($field=='money'&&$change_type==1&&$status==1)
- {
- User::findOne($user_id)->updateCounters(['amount_money_income'=>$updateNum]);
- }
- return true;
- }
- else
- {
- file_put_contents(UPLOAD_PATH.'log.txt',$log->returnFirstError(),FILE_APPEND);
- }
- }
- }
|