123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace App\Jobs;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Config;
- use Illuminate\Support\Facades\Log;
- class GiftJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $msgid;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($msgid)
- {
- //
- $this->msgid = $msgid;
- Log::info(date('Y-m-d H:i:d').':'.$msgid);
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $result = $this->goaaa();
- /*if($result === false){
- //防止失败
- $key = 'kgelavarel_giftjob_'.$this->msgid;
- $limit_rs = $this->apiLimit(2,3600000,$key);//一小时最多另执行2次
- if($limit_rs === true){
- //echo 123;
- dispatch((new GiftJob($this->msgid))->delay(60));//加入队列
- }else{
- //echo 345;
- }
- }*/
- }
- private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
- {
- $app_name = Config::get('app.name');// laravel name
- $key = "{$app_name}:{$key}";
- $redis = Redis::connection();
- //
- //指定键值新增+1 并获取
- $count = $redis->incr($key);
- if ($count > $apiLimit) {
- return false;
- }
- //设置过期时间
- if ($count == 1) {
- $redis->pExpire($key, $apiLimitTime);
- }
- return true;
- }
- //主要调用
- public function goaaa(){
- //
- $data = DB::table('mt_user_match_audio_log')->where('id',$this->msgid)->first();
- DB::beginTransaction();
- $result = $this->shouyi($data->to_user_id,$data->money,'user_match_audio_log',$this->msgid);
- if($result !== true){
- DB::rollBack();
- return false;
- }
- DB::commit();
- return true;
- }
- //四大收益,视频,音频,私聊,礼物
- public function shouyi($user_id,$gold,$table,$table_id){
- //精确小数点
- bcscale(2);
- //枚举
- $table_enum = [
- 'user_match_audio_log' => [
- 'prefix'=>'audio',
- 'remark' =>'语音',
- ],
- 'user_match_typing_log' => [
- 'prefix'=>'typing',
- 'remark' =>'私聊',
- ],
- 'user_match_video_log' => [
- 'prefix'=>'video',
- 'remark' =>'视频',
- ],
- 'gift_user_typing' => [
- 'prefix'=>'gift',
- 'remark' =>'收礼',
- ],
- ];
- //变量前缀,钱包备注
- $prefix = $table_enum[$table]['prefix'];
- $remark = $table_enum[$table]['remark'];
- //收益者本人
- $userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$user_id)->first();
- //没有推荐人,自己等级最高
- if(empty($userinfo->intro_uid) || $userinfo->group_id == 3){
- return true;
- }
- //充值者上级
- $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','gender'])->where('id',$userinfo->intro_uid)->first();
- if(empty($intro_userinfo)){return true;}
- //配置
- $config_site = DB::table('mt_config')->whereIn('name',['shouyi_b2a_rate','shouyi_c2a_rate'])->pluck('name','value');
- //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
- if($intro_userinfo->group_id == 3){
- $shouyi_b2a_rate = $config_site['shouyi_b2a_rate'];//b2a 与 b02a相等
- $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
- $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
- if($result['status']===false)
- {
- //Db::rollback();
- return false;
- }
- return true;//结束了
- }
- //上级是二级邀请B1,本人C1,贡献给B和A(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
- if($intro_userinfo->group_id == 2 && $intro_userinfo->group_id > $userinfo->group_id){
- //充值者上上级,也就是A
- $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','gender'])->where('id',$intro_userinfo->intro_uid)->first();
- if(empty($intro_intro_userinfo)){return true;}
- if($intro_intro_userinfo->group_id == 3){
- $shouyi_c2a_rate = $config_site['shouyi_c2a_rate'];
- $jewelA = bcdiv(bcmul($gold,$shouyi_c2a_rate),100);
- $result = $this->lockChangeAccountRemain($intro_intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
- if($result['status']===false)
- {
- //Db::rollback();
- return false;
- }
- }
- return true;
- }
- return true;
- }
- /**
- *
- * @param floatval $number 金额(正数进账,负数出账)
- * @param $accountType 货币类型,money,score
- * @param $logtype 日志的类型
- * @param $remark 备注
- * @param $user_id 用户id
- * @param $table 来源表
- * @param $data_id 表id
- * @param $isAdmin 是否是管理员处理
- * @return array
- * @return array[status]
- * @return array[msg]
- * @return array[log_table]
- * @return array[log_id]
- */
- public function lockChangeAccountRemain($user_id,$accountType='gold',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
- {
- //初始化
- $result = array(
- 'status'=>false,
- 'msg'=>'',
- 'log_table' => '',
- 'log_id' => '',
- );
- //获取小数点
- $point = 1;
- if($accountType == 'agentjewel'){
- $point = 2;
- }
- bcscale($point);
- //检测0
- $number = floatval( $number );
- if( $number == 0 )
- {
- $result['status'] = true;
- $result['msg'] = '交易金额:0';
- return $result;
- }
- if(0 === bccomp($number, 0)){
- $result['status'] = true;
- $result['msg'] = '交易金额:0';
- return $result;
- }
- //查询余额
- $wallet = DB::table('mt_user_wallet')->where('user_id',$user_id)->lockForUpdate()->first();
- if(empty($wallet))
- {
- $result['msg'] = '不存在的用户';
- return $result;
- }
- //扣除金币单独方法
- //gold的增加,jewel的增、减
- //之前的余额
- $wallet_before = $wallet->$accountType;
- //正常方法
- if(bccomp(bcadd($wallet_before, $number), 0) === -1)
- {
- $result['msg'] = '余额不足!';
- return $result;
- }
- //钱币操作
- $data = array();
- $data['user_id'] = $user_id;
- $data['log_type'] = $logtype;
- $data['money_type'] = $accountType;
- $data['before'] = $wallet_before;
- $data['change_value'] = $number;
- $data['remain'] = bcadd($wallet_before, $number);
- $data['table'] = $table;
- $data['table_id'] = $table_id;
- $data['remark'] = $remark;
- $data['createtime'] = time();
- $data['updatetime'] = time();
- $log_table = 'mt_user_gold_log';
- if($accountType == 'agentjewel'){
- $log_table = 'mt_user_agentjewel_log';
- }
- $rs2_id = DB::table($log_table)->insertGetId($data);
- //修改金额
- $rs1 = DB::table('mt_user_wallet')->where('user_id',$user_id)->update([$accountType => $data['remain']]);
- //操作数据库失败
- if($rs1 === false || $rs2_id === false){
- $result['msg'] = '更新财务记录失败!';
- return $result;
- }
- //默认成功
- $result['status'] = true;
- $result['msg'] = '账户余额已更新!';
- $result['log_table'] = $log_table;
- $result['log_id'] = $rs2_id;
- return $result;
- }
- }
|