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); } } }